DocSet/Source/POD.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 222 CC 23 SCAR 32.1
CC
23
Cov
0%
podify_items
72.0
extract_pod
30.0
retrieve_meta_data
20.0
CRAP 24.9
1 package DocSet::Source::POD;
2  
3 use strict;
4 use warnings;
5  
6 use DocSet::Util;
7 use DocSet::RunTime;
8  
9 use vars qw(@ISA);
10 require DocSet::Doc;
11 @ISA = qw(DocSet::Doc);
12  
13 use constant HEAD_MAX_LEVEL => 4;
14 use constant MAX_DESC_LENGTH => 500;
15  
16 # META: we are presenting too early, or this code should be moved to
17 # POD2HTML specific module
18 require Pod::POM::View::HTML;
19 my $mode = 'Pod::POM::View::HTML';
20  
21 sub retrieve_meta_data {
22 my ($self) = @_;
23  
24 $self->parse_pod;
25  
26 #print Pod::POM::View::HTML->print($pom);
27  
28 my $meta = {
29 title => 'No Title',
30 abstract => '',
31 };
32  
33 my $pom = $self->{parsed_tree};
34 my @sections = $pom->head1();
35  
36
37 if (@sections) {
38  
39 # extract the title from the NAME section and remove it from content
40 if ($sections[0]->title =~ /NAME/) {
41 # don't present on purpose ->present($mode); there should
42 # be no markup in NAME a problem with
43 # <TITLE><CODE>....</CODE><TITLE> and alike
44 $meta->{title} = (shift @sections)->content();
45 $meta->{title} =~ s/^\s*|\s*$//sg;
46 }
47  
48 # stitle is the same in docs
49 $meta->{stitle} = $meta->{title};
50  
51 # locate the DESCRIPTION section (should be in the first three
52 # sections)
53 for (0..2) {
54 next unless defined $sections[$_]
55 && $sections[$_]->title =~ /DESCRIPTION/i;
56  
57 my $abstract = $sections[$_]->content->present($mode);
58  
59 # cannot do this now, as it might cut some markup in the middle: <i>1 2</i>
60 # # we are interested only in the first paragraph, or if its
61 # # too big first MAX_DESC_LENGTH chars.
62 # my $index = index $abstract, " ", MAX_DESC_LENGTH;
63 # # cut only if index didn't return '-1' which is when the the
64 # # space wasn't found starting from location MAX_DESC_LENGTH
65 # unless ($index == -1) {
66 # $abstract = substr $abstract, 0, $index+1;
67 # $abstract .= " ...&nbsp;<i>(continued)</i>";
68 # }
69 #
70 # # temp workaround, but can only split on paras
71 $abstract =~ s|<p>(.*?)</p>.*|$1|s;
72  
73 $meta->{abstract} = $abstract;
74 last;
75 }
76 }
77  
78 $meta->{link} = $self->{rel_dst_path};
79  
80 # put all the meta data under the same attribute
81 $self->{meta} = $meta;
82  
83 # build the toc datastructure
84 my @toc = ();
85 my $level = 1;
86 for my $node (@sections) {
87 push @toc, $self->render_toc_level($node, $level);
88 }
89 $self->{toc} = \@toc;
90  
91 }
92  
93 sub render_toc_level {
94 my ($self, $node, $level) = @_;
95 my $title = $node->title;
96 my $link = "$title"; # must stringify to get the raw string
97 $link =~ s/^\s*|\s*$//g; # strip leading and closing spaces
98 $link =~ s/\W/_/g; # META: put into a sub? see Doc::Common::pod_pom_html_anchor
99 # prepand '#' for internal links
100 my $toc_link = "toc_$link"; # self referring toc entry
101 $link = "#$link";
102  
103 my %toc_entry = (
104 title => $title->present($mode), # run the formatting if any
105 link => $link,
106 toc_link => $toc_link,
107 );
108  
109 my @sub = ();
110 $level++;
111 if ($level <= HEAD_MAX_LEVEL) {
112 # if there are deeper than =head4 levels we don't go down (spec is 1-4)
113 my $method = "head$level";
114 for my $sub_node ($node->$method()) {
115 push @sub, $self->render_toc_level($sub_node, $level);
116 }
117 }
118 $toc_entry{subs} = \@sub if @sub;
119  
120 return \%toc_entry;
121 }
122  
123  
124  
125 sub parse_pod {
126 my ($self) = @_;
127
128 # already parsed
129 return if exists $self->{parsed_tree} && $self->{parsed_tree};
130  
131 # print ${ $self->{content} };
132  
133 use Pod::POM;
134 my %options;
135 my $parser = Pod::POM->new(\%options);
136 my $pom = $parser->parse_text(${ $self->{content} })
137 or die $parser->error();
138  
139 $self->{parsed_tree} = $pom;
140  
141 # examine any warnings raised
142 if (my @warnings = $parser->warnings()) {
143 print "\n", '-' x 40, "\n";
144 print "File: $self->{src_path}\n";
145 warn "$_\n" for @warnings;
146 }
147 }
148  
149 sub src_filter {
150 my ($self) = @_;
151  
152 $self->extract_pod;
153  
154 $self->head2page_breaks() if $self->{docset}->options('slides_mode');
155  
156 $self->podify_items() if $self->{docset}->options('podify_items');
157 }
158  
159 sub extract_pod {
160 my ($self) = @_;
161  
162 my @pod = ();
163 my $in_pod = 0;
164 for (split /\n{2,}/, ${ $self->{content} }) {
165 unless ($in_pod) {
166 s/^[\s\n]*//ms; # skip empty lines in preamble
167 $in_pod = /^=/s;
168 }
169 next unless $in_pod;
170 $in_pod = 0 if /^=cut/;
171 push @pod, $_;
172 }
173  
174 # handle empty files
175 unless (@pod) {
176 push @pod, "=head1 NAME", "=head1 Not documented", "=cut";
177 }
178  
179 my $content = join "\n\n", @pod;
180 $self->{content} = \$content;
181 }
182  
183 sub podify_items {
184 my ($self) = @_;
185
186 # tmp storage
187 my @paras = ();
188 my $items = 0;
189 my $second = 0;
190  
191 # we want the source in paragraphs
192 my @content = split /\n\n/, ${ $self->{content} };
193  
194 foreach (@content) {
195 # is it an item?
196 if (/^(\*|\d+)\s+((\*|\d+)\s+)?/) {
197 $items++;
198 if ($2) {
199 $second++;
200 s/^(\*|\d+)\s+//; # strip the first level shortcut
201 s/^(\*|\d+)\s+/=item $1\n\n/; # do the second
202 s/^/=over 4\n\n/ if $second == 1; # start 2nd level
203 } else {
204 # first time insert the =over pod tag
205 s/^(\*|\d+)\s+/=item $1\n\n/; # start 1st level
206 s/^/=over 4\n\n/ if $items == 1;
207 s/^/=back\n\n/ if $second; # complete 2nd level
208 $second = 0; # end 2nd level section
209 }
210 push @paras, split /\n\n/, $_;
211 } else {
212 # complete the =over =item =back tag
213 $second=0, push @paras, "=back" if $second; # if 2nd level is not closed
214 push @paras, "=back" if $items;
215 push @paras, $_;
216 # not a tag item
217 $items = 0;
218 }
219 }
220  
221 my $content = join "\n\n", @paras;
222 $self->{content} = \$content;
223  
224 }
225  
226  
227 # add a page break for =headX in slides mode
228 sub head2page_breaks {
229 my ($self) = @_;
230
231 # we want the source in paragraphs
232 my @content = split /\n\n/, ${ $self->{content} };
233  
234 my $count = 0;
235 my @paras = ();
236 foreach (@content) {
237 # add a page break starting from the third head (since the
238 # first is removed anyway, and we don't want to start a new
239 # page on the very first page)
240 if (/^=head/) {
241 $count++;
242 if ($count > 2) {
243 push @paras, qq{=for html <?page-break>};
244 }
245 }
246 push @paras, $_;
247 }
248  
249 my $content = join "\n\n", @paras;
250 $self->{content} = \$content;
251  
252 }
253  
254 1;
255 __END__