1 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 # Licensed under the OpenSSL license (the "License"). You may not use
4 # this file except in compliance with the License. You can obtain a copy
5 # in the file LICENSE in the source distribution or at
6 # https://www.openssl.org/source/license.html
8 package OpenSSL::Util::Pod;
14 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17 @EXPORT = qw(extract_pod_info);
22 OpenSSL::Util::Pod - utilities to manipulate .pod files
26 use OpenSSL::Util::Pod;
28 my %podinfo = extract_pod_info("foo.pod");
30 # or if the file is already opened... Note that this consumes the
31 # remainder of the file.
33 my %podinfo = extract_pod_info(\*STDIN);
39 =item B<extract_pod_info "FILENAME", HASHREF>
41 =item B<extract_pod_info "FILENAME">
43 =item B<extract_pod_info GLOB, HASHREF>
45 =item B<extract_pod_info GLOB>
47 Extracts information from a .pod file, given a STRING (file name) or a
48 GLOB (a file handle). The result is given back as a hash table.
50 The additional hash is for extra parameters:
54 =item B<section =E<gt> N>
56 The value MUST be a number, and will be the default man section number
57 to be used with the given .pod file. This number can be altered if
58 the .pod file has a line like this:
60 =for comment openssl_manual_section: 4
62 =item B<debug =E<gt> 0|1>
64 If set to 1, extra debug text will be printed on STDERR
74 =item B<extract_pod_info> returns a hash table with the following
79 =item B<section =E<gt> N>
81 The man section number this .pod file belongs to. Often the same as
84 =item B<names =E<gt> [ "name", ... ]>
86 All the names extracted from the NAME section.
94 sub extract_pod_info {
96 my $defaults_ref = shift || {};
97 my %defaults = ( debug => 0, section => 0, %$defaults_ref );
101 # If not a file handle, then it's assume to be a file path (a string)
102 unless (ref $input eq "GLOB") {
104 open $fh, $input or die "Trying to read $filename: $!\n";
105 print STDERR "DEBUG: Reading $input\n" if $defaults{debug};
109 my %podinfo = ( section => $defaults{section});
112 if (m|^=for\s+comment\s+openssl_manual_section:\s*([0-9])\s*$|) {
113 print STDERR "DEBUG: Found man section number $1\n"
115 $podinfo{section} = $1;
118 # Stop reading when we have reached past the NAME section.
120 && defined $podinfo{lastsect}
121 && $podinfo{lastsect} eq "NAME");
123 # Collect the section name
124 if (m|^=head1\s*(.*)|) {
125 $podinfo{lastsect} = $1;
126 $podinfo{lastsect} =~ s/\s+$//;
127 print STDERR "DEBUG: Found new pod section $1\n"
129 print STDERR "DEBUG: Clearing pod section text\n"
131 $podinfo{lastsecttext} = "";
134 next if (m|^=| || m|^\s*$|);
136 # Collect the section text
137 print STDERR "DEBUG: accumulating pod section text \"$_\"\n"
139 $podinfo{lastsecttext} .= " " if $podinfo{lastsecttext};
140 $podinfo{lastsecttext} .= $_;
146 print STDERR "DEBUG: Done reading $filename\n" if $defaults{debug};
149 $podinfo{lastsecttext} =~ s| - .*$||;
153 split(m|,|, $podinfo{lastsecttext});
155 return ( section => $podinfo{section}, names => [ @names ] );