Pod.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  2. #
  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
  7. package OpenSSL::Util::Pod;
  8. use strict;
  9. use warnings;
  10. use Exporter;
  11. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  12. $VERSION = "0.1";
  13. @ISA = qw(Exporter);
  14. @EXPORT = qw(extract_pod_info);
  15. @EXPORT_OK = qw();
  16. =head1 NAME
  17. OpenSSL::Util::Pod - utilities to manipulate .pod files
  18. =head1 SYNOPSIS
  19. use OpenSSL::Util::Pod;
  20. my %podinfo = extract_pod_info("foo.pod");
  21. # or if the file is already opened... Note that this consumes the
  22. # remainder of the file.
  23. my %podinfo = extract_pod_info(\*STDIN);
  24. =head1 DESCRIPTION
  25. =over
  26. =item B<extract_pod_info "FILENAME", HASHREF>
  27. =item B<extract_pod_info "FILENAME">
  28. =item B<extract_pod_info GLOB, HASHREF>
  29. =item B<extract_pod_info GLOB>
  30. Extracts information from a .pod file, given a STRING (file name) or a
  31. GLOB (a file handle). The result is given back as a hash table.
  32. The additional hash is for extra parameters:
  33. =over
  34. =item B<section =E<gt> N>
  35. The value MUST be a number, and will be the man section number
  36. to be used with the given .pod file.
  37. =item B<debug =E<gt> 0|1>
  38. If set to 1, extra debug text will be printed on STDERR
  39. =back
  40. =back
  41. =head1 RETURN VALUES
  42. =over
  43. =item B<extract_pod_info> returns a hash table with the following
  44. items:
  45. =over
  46. =item B<section =E<gt> N>
  47. The man section number this .pod file belongs to. Often the same as
  48. was given as input.
  49. =item B<names =E<gt> [ "name", ... ]>
  50. All the names extracted from the NAME section.
  51. =back
  52. =back
  53. =cut
  54. sub extract_pod_info {
  55. my $input = shift;
  56. my $defaults_ref = shift || {};
  57. my %defaults = ( debug => 0, section => 0, %$defaults_ref );
  58. my $fh = undef;
  59. my $filename = undef;
  60. # If not a file handle, then it's assume to be a file path (a string)
  61. unless (ref $input eq "GLOB") {
  62. $filename = $input;
  63. open $fh, $input or die "Trying to read $filename: $!\n";
  64. print STDERR "DEBUG: Reading $input\n" if $defaults{debug};
  65. $input = $fh;
  66. }
  67. my %podinfo = ( section => $defaults{section});
  68. while(<$input>) {
  69. s|\R$||;
  70. # Stop reading when we have reached past the NAME section.
  71. last if (m|^=head1|
  72. && defined $podinfo{lastsect}
  73. && $podinfo{lastsect} eq "NAME");
  74. # Collect the section name
  75. if (m|^=head1\s*(.*)|) {
  76. $podinfo{lastsect} = $1;
  77. $podinfo{lastsect} =~ s/\s+$//;
  78. print STDERR "DEBUG: Found new pod section $1\n"
  79. if $defaults{debug};
  80. print STDERR "DEBUG: Clearing pod section text\n"
  81. if $defaults{debug};
  82. $podinfo{lastsecttext} = "";
  83. }
  84. next if (m|^=| || m|^\s*$|);
  85. # Collect the section text
  86. print STDERR "DEBUG: accumulating pod section text \"$_\"\n"
  87. if $defaults{debug};
  88. $podinfo{lastsecttext} .= " " if $podinfo{lastsecttext};
  89. $podinfo{lastsecttext} .= $_;
  90. }
  91. if (defined $fh) {
  92. close $fh;
  93. print STDERR "DEBUG: Done reading $filename\n" if $defaults{debug};
  94. }
  95. $podinfo{lastsecttext} =~ s| - .*$||;
  96. my @names =
  97. map { s|\s+||g; s|/|-|g; $_ }
  98. split(m|,|, $podinfo{lastsecttext});
  99. return ( section => $podinfo{section}, names => [ @names ] );
  100. }
  101. 1;