]>
git.ipfire.org Git - thirdparty/openssl.git/blob - util/perl/OpenSSL/Util/Pod.pm
1 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 # Licensed under the Apache License 2.0 (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 man section number
57 to be used with the given .pod file.
59 =item B<debug =E<gt> 0|1>
61 If set to 1, extra debug text will be printed on STDERR
71 =item B<extract_pod_info> returns a hash table with the following
76 =item B<section =E<gt> N>
78 The man section number this .pod file belongs to. Often the same as
81 =item B<names =E<gt> [ "name", ... ]>
83 All the names extracted from the NAME section.
85 =item B<contents =E<gt> "...">
87 The whole contents of the .pod file.
95 sub extract_pod_info
{
97 my $defaults_ref = shift || {};
98 my %defaults = ( debug
=> 0, section
=> 0, %$defaults_ref );
100 my $filename = undef;
103 # If not a file handle, then it's assume to be a file path (a string)
104 if (ref $input eq "") {
106 open $fh, $input or die "Trying to read $filename: $!\n";
107 print STDERR
"DEBUG: Reading $input\n" if $defaults{debug
};
110 if (ref $input eq "GLOB") {
112 $contents = <$input>;
114 die "Unknown input type";
117 my @invisible_names = ();
118 my %podinfo = ( section
=> $defaults{section
});
120 # Regexp to split a text into paragraphs found at
121 # https://www.perlmonks.org/?node_id=584367
122 # Most of all, \G (continue at last match end) and /g (anchor
123 # this match for \G) are significant
124 foreach (map { /\G((?:(?!\n\n).)*\n+|.+\z)/sg } $contents) {
125 # Remove as many line endings as possible from the end of the paragraph
128 print STDERR
"DEBUG: Paragraph:\n$_\n"
131 # Stop reading when we have reached past the NAME section.
133 && defined $podinfo{lastsect
}
134 && $podinfo{lastsect
} eq "NAME");
136 # Collect the section name
137 if (m
|^=head1\s
*(.*)|) {
138 $podinfo{lastsect
} = $1;
139 $podinfo{lastsect
} =~ s/\s+$//;
140 print STDERR
"DEBUG: Found new pod section $1\n"
142 print STDERR
"DEBUG: Clearing pod section text\n"
144 $podinfo{lastsecttext
} = "";
147 # Add invisible names
148 if (m
|^=for\s
+openssl\s
+names
:\s
*(.*)|s
) {
150 my @tmp = map { map { s/\s+//g; $_ } split(/,/, $_) } $x;
152 "DEBUG: Found invisible names: ", join(', ', @tmp), "\n"
154 push @invisible_names, @tmp;
157 next if (m
|^=| || m
|^\s
*$|);
159 # Collect the section text
160 print STDERR
"DEBUG: accumulating pod section text \"$_\"\n"
162 $podinfo{lastsecttext
} .= " " if $podinfo{lastsecttext
};
163 $podinfo{lastsecttext
} .= $_;
169 print STDERR
"DEBUG: Done reading $filename\n" if $defaults{debug
};
172 $podinfo{lastsecttext
} =~ s
|\s
+-\s
+.*$||s
;
175 map { s/^\s+//g; # Trim prefix blanks
176 s/\s+$//g; # Trim suffix blanks
177 s
|/|-|g
; # Treat slash as dash
179 split(m
|,|, $podinfo{lastsecttext
});
182 "DEBUG: Collected names are: ",
183 join(', ', @names, @invisible_names), "\n"
186 return ( section
=> $podinfo{section
},
187 names
=> [ @names, @invisible_names ],
188 contents
=> $contents,
189 filename
=> $filename );