File Coverage

lib/Pod/Definitions.pm
Criterion Covered Total %
statement 151 159 94.9
branch 15 18 83.3
condition n/a
subroutine 25 26 96.1
pod 6 7 85.7
total 197 210 93.8


line stmt bran cond sub pod time code
1             package Pod::Definitions;
2              
3             our $VERSION = '0.02';
4              
5 1     1   1744 use strict;
  1         7  
  1         30  
6 1     1   4 use warnings;
  1         1  
  1         24  
7              
8 1     1   18 use v5.20;
  1         5  
9              
10 1     1   6 use feature 'signatures';
  1         1  
  1         191  
11 1     1   6 no warnings 'experimental::signatures';
  1         2  
  1         49  
12              
13 1     1   374 use Pod::Headings;
  1         3  
  1         42  
14 1     1   435 use Pod::Definitions::Heuristic;
  1         2  
  1         432  
15              
16             #
17             #
18             #
19 2     2 1 772 sub new ($class, @args) {
  2         5  
  2         2  
  2         3  
20 2         4 my $self = {@args};
21 2         4 bless $self, $class;
22              
23 2         5 return $self;
24             }
25              
26             #
27             # Accessors
28             #
29              
30 2     2 1 1352 sub file ($self) { return $self->{file}; } # Local path to file
  2         3  
  2         2  
  2         10  
31 8     8 1 12 sub manpage ($self) { return $self->{manpage}; } # Full name of manpage ('Mojo::Path')
  8         10  
  8         8  
  8         23  
32 2     2 1 5 sub module ($self) { return $self->{module}; } # Module leaf name ('Path')
  2         3  
  2         3  
  2         7  
33 3     3 1 4 sub sections ($self) { return $self->{sections}; } # Hash (key=toplevel section) of arrays
  3         4  
  3         4  
  3         17  
34             # of section names
35             #
36             #
37             #
38              
39 6     6 0 6 sub convert_to_href_text ($human_text) {
  6         6  
  6         7  
40 6         23 $human_text =~ s/(\s|\(|=|\[)/-/g;
41 6         12 $human_text =~ s/([^a-zA-Z0-9_\-*:])//g;
42 6         34 return $human_text;
43             }
44              
45 6     6   7 sub _save_definition ($self, $parser, $attrs, $head1, $text) {
  6         7  
  6         7  
  6         7  
  6         6  
  6         8  
  6         7  
46 6         19 my $cooked_heading = Pod::Definitions::Heuristic->new(text => $text);
47 6         9 push @{$self->{sections}{$head1}}, {raw => $text,
  6         18  
48             cooked => $cooked_heading->clean,
49             link => $self->manpage(),
50             link_fragment => convert_to_href_text($text),
51             };
52             }
53              
54 2     2   3 sub _save_file_manpage ($self, $text) {
  2         3  
  2         2  
  2         3  
55 2 50       7 $self->{manpage} = $text unless defined $self->{manpage};
56             }
57              
58 2     2   3 sub _save_file_module_leaf ($self, $text) {
  2         37  
  2         6  
  2         2  
59 2         6 $self->{module} = $text;
60             }
61              
62 2     2   3 sub _save_module_name ($self, $parser, $elem, $attrs, $text) {
  2         2  
  2         14  
  2         4  
  2         10  
  2         5  
  2         1  
63 2         9 $text =~ m/^\s*(?\S+)/;
64 1     1   482 my $module_name = $+{module_name};
  1         494  
  1         270  
  2         20  
65 2         9 $self->_save_file_manpage($module_name);
66             # "Mojo::Log" → index under last component: "Log"
67 2         12 $self->_save_file_module_leaf( (split /::/, $module_name)[-1] );
68             }
69              
70 2     2   3 sub _save_version ($self, $parser, $elem, $attrs, $text) {
  2         3  
  2         4  
  2         12  
  2         6  
  2         3  
  2         2  
71 2         8 $self->{version} = $text;
72             }
73              
74 2     2   6 sub _save_see_also ($self, $parser, $elem, $attrs, $text) {
  2         2  
  2         4  
  2         3  
  2         2  
  2         5  
  2         2  
75 2         2 push @{$self->{see_also}}, $text;
  2         7  
76             }
77              
78 2     2 1 946 sub parse_file ($self, $file, $podname = undef) {
  2         4  
  2         3  
  2         4  
  2         2  
79              
80 2         1 my $save_next;
81              
82 2         7 $self->{file} = $file;
83 2 50       6 $self->_save_file_manpage($podname) if defined $podname;
84              
85 15         15 return Pod::Headings->new(
86 15     15   18 head1 => sub ($parser, $elem, $attrs, $plaintext) {
  15         19  
  15         16  
  15         16  
  15         24  
87             # "Archive::Zip Methods" -> "Methods":
88 15 100       89 $plaintext =~ s/^($self->{manpage}\s+)//i if defined $self->{manpage};
89             # Change headings starting in all-uppercase to inital caps
90             # only. Note, "READING CPAN.pm" -> "Reading cpan.pm"
91             # (there is only so much we can do without A.I.)
92 1 50   1   644 if ($plaintext =~ /^[ \p{Uppercase}]{2,}/) {
  1         14  
  1         17  
  15         60  
93 15         105 $plaintext =~ s/^(.)(.*)/\u$1\L$2/;
94             }
95 15         31 $parser->{_save_head1} = $plaintext;
96 15         20 undef $parser->{_save_head2};
97 15         21 $parser->{_save_first_para} = 1;
98              
99 15 100       86 if (lc($plaintext) eq 'name') {
    100          
    100          
100 2         7 $save_next = \&_save_module_name;
101             } elsif (lc($plaintext) eq 'version') {
102 2         4 $save_next = \&_save_version;
103             } elsif (lc($plaintext) eq 'see also') {
104 2         3 $save_next = \&_save_see_also;
105             } else {
106 9         10 undef $save_next;
107             }
108              
109 15         39 1;
110             },
111 6     6   5 head2 => sub ($parser, $elem, $attrs, $plaintext) {
  6         7  
  6         7  
  6         8  
  6         7  
  6         8  
112             # print " $elem: $parser->{_save_head1}: $plaintext\n";
113 6         9 $parser->{_save_head2} = $plaintext;
114 6         9 $parser->{_save_first_para} = 1;
115              
116 6         15 $self->_save_definition ( $parser, $attrs, $parser->{_save_head1}, $plaintext );
117              
118 6         13 1;
119             },
120 0     0   0 head3 => sub ($parser, $elem, $attrs, $plaintext) {
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
121             # print " $elem: $parser->{_save_head1} / $parser->{_save_head2}: $plaintext\n";
122 0         0 $self->_save_definition ( $parser, $attrs, $parser->{_save_head2}, $plaintext );
123 0         0 1;
124             },
125 23     23   23 Para => sub ($parser, $elem, $attrs, $plaintext) {
  23         31  
  23         25  
  23         24  
  23         24  
  23         25  
126 23 100       44 if ($parser->{_save_first_para}) {
127             # print " .... text: $plaintext\n";
128 18 100       38 $self->$save_next($parser, $elem, $attrs, $plaintext) if defined $save_next;
129 18         23 undef $save_next;
130             }
131 23         34 $parser->{_save_first_para} = 0;
132 23         36 1;
133             },
134 2         30 L => 1, # Return 0 to drop the plaintext passed to the containing element
135             # Possible extension: In 'See Also' sections, accumulate the
136             # actual links from Pod::Simple in the same form with (raw, cooked, link)
137             )->parse_file($file);
138             }
139              
140             1;
141              
142             __END__