| 1 | package Perl::APIReference::Generator; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp qw/croak/; | ||
| 7 | use Pod::Eventual::Simple; | ||
| 8 | use Perl::APIReference; | ||
| 9 | |||
| 10 | our $VERSION = '0.01'; | ||
| 11 | |||
| 12 | sub parse { | ||
| 13 | shift if @_ and defined $_[0] and $_[0] eq __PACKAGE__; | ||
| 14 | my %args = @_; | ||
| 15 | my $perl_version = $args{perl_version}; | ||
| 16 | croak("Bad perl version '$perl_version'") | ||
| 17 | if not defined $perl_version or $perl_version !~ /^5/; | ||
| 18 | my $file = $args{file}; | ||
| 19 | croak("Bad input file") | ||
| 20 | if not defined $file or not -e $file; | ||
| 21 | |||
| 22 | my $output = Pod::Eventual::Simple->read_file($file); | ||
| 23 | croak("Failed to parse POD") | ||
| 24 | if not defined $output; | ||
| 25 | |||
| 26 | my $entries = _get_entries($output); | ||
| 27 | |||
| 28 | return Perl::APIReference->_new_from_parse( | ||
| 29 | perl_version => $perl_version, | ||
| 30 | index => $entries, | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _get_entries { | ||
| 35 | my $output = shift; | ||
| 36 | my $in_entry = 0; | ||
| 37 | my $entry = {}; | ||
| 38 | my $entries = {}; | ||
| 39 | |||
| 40 | # TODO add "section" | ||
| 41 | # TODO robustify | ||
| 42 | foreach my $node (@$output) { | ||
| 43 | my $type = $node->{type}; | ||
| 44 | next if $type eq 'nonpod'; | ||
| 45 | my $command = $node->{command}; | ||
| 46 | |||
| 47 | if ($in_entry) { | ||
| 48 | if ($type eq 'command') { | ||
| 49 | if ($command eq 'item') { | ||
| 50 | _finish_entry($entry, $entries); | ||
| 51 | $in_entry = _start_entry($entry, $node); | ||
| 52 | } | ||
| 53 | elsif ($command eq 'back') { | ||
| 54 | $in_entry = 0; | ||
| 55 | _finish_entry($entry, $entries); | ||
| 56 | } | ||
| 57 | # Not handling the "else" case here basically just strips out | ||
| 58 | # any other POD primitives that are nested. This is NOT how a | ||
| 59 | # parser should work, but since this is somewhat human guided | ||
| 60 | # one-off tooling, I validated that the output is approximately | ||
| 61 | # what it should be, so laziness wins out for now. | ||
| 62 | #else {use Data::Dumper; warn Dumper $node;} | ||
| 63 | } | ||
| 64 | elsif ($type eq 'text' or $type eq 'blank') { | ||
| 65 | $entry->{text} .= $node->{content}; | ||
| 66 | } | ||
| 67 | |||
| 68 | } # end if in entry | ||
| 69 | else { | ||
| 70 | if ($type eq 'command' | ||
| 71 | and $command eq 'item' | ||
| 72 | and $node->{content} !~ /^L/ # stript out the section index for 5.34+ | ||
| 73 | ) | ||
| 74 | { | ||
| 75 | $in_entry = _start_entry($entry, $node); | ||
| 76 | } | ||
| 77 | } # end if not in entry | ||
| 78 | } | ||
| 79 | _finish_entry($entry, $entries); | ||
| 80 | return $entries; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | sub _finish_entry { | ||
| 85 | my $entry = shift; | ||
| 86 | my $entries = shift; | ||
| 87 | if (not defined $entry->{name} or not defined $entry->{text}) { | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | $entry->{text} =~ s/\s+$//; | ||
| 91 | $entry->{text} =~ s/^\s+//; | ||
| 92 | # TODO fix collisions! | ||
| 93 | $entries->{$entry->{name}} = {%$entry}; | ||
| 94 | %$entry = (); | ||
| 95 | return(); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _start_entry { | ||
| 99 | my $entry = shift; | ||
| 100 | my $node = shift; | ||
| 101 | my $name = $node->{content}; | ||
| 102 | $name =~ s/\s+$//; | ||
| 103 | $name =~ /([^\n]+)/ or return; | ||
| 104 | $name = $1; | ||
| 105 | # drop POD formatting from the name (5.34+) | ||
| 106 | $name =~ s/^\s*[CIB]<//; | ||
| 107 | $name =~ s/>$//; | ||
| 108 | %$entry = ( | ||
| 109 | name => $name, | ||
| 110 | text => '', | ||
| 111 | ); | ||
| 112 | return(1); | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | __END__ |