Man/DOM.pm

filter: Statementstmt 92.2% 178 / 193 Branchbran 83.3% 5 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 88.7% 71 / 80 Podpod 66.6% 8 / 12 totaltotal 89.4% 263 / 294 CC 5 SCAR 2.0
CC
5
Cov
92%
append_chunk
3.0
meta
2.0
_make
2.0
CRAP 1.2
« Man.pm ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011-2022 -- leonerd@leonerd.org.uk
5  
6 package Parse::Man::DOM 0.03;
7  
8 7 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use base qw( Parse::Man );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 C<Parse::Man::DOM> - parse nroff-formatted manpages and return a DOM tree
16  
17 =head1 SYNOPSIS
18  
19 use Parse::Man::DOM;
20  
21 my $parser = Parse::Man::DOM->new;
22  
23 my $document = $parser->from_file( "my_manpage.1" );
24  
25 print "The manpage name is", $document->meta( "name" ), "\n";
26  
27 =head1 DESCRIPTION
28  
29 This subclass of L<Parse::Man> returns an object tree representing the parsed
30 content of the input file. The returned result will be an object of the
31 C<Parse::Man::DOM::Document> class, which itself will contain other objects
32 nested within it.
33  
34 =cut
35  
36 sub parse
37 17 ▶{
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
38 17 my $self = shift;
39  
40 17 local $self->{current_document} = $self->_make( document => );
41  
42 17 $self->SUPER::parse( @_ );
43  
44 17 return $self->{current_document};
45 }
46  
47 sub para_TH
48 2 ▶{
Subroutine para_TH: called CC=1 SCAR=0.0
Pod documented
49 2 my $self = shift;
50 2 my ( $name, $section ) = @_;
51  
52 2 $self->{current_document}->add_meta( $self->_make( metadata => name => $name ) );
53 2 $self->{current_document}->add_meta( $self->_make( metadata => section => $section ) );
54 }
55  
56 sub para_SH
57 2 ▶{
Subroutine para_SH: called CC=1 SCAR=0.0
Pod documented
58 2 my $self = shift;
59 2 my ( $text ) = @_;
60 2 $self->{current_document}->append_para( $self->_make( heading => 1 => $text ) );
61 }
62  
63 sub para_SS
64 0 ▶{
Subroutine para_SS: not called CC=1 SCAR=6.9
Pod documented
65 0 my $self = shift;
66 0 my ( $text ) = @_;
67 0 $self->{current_document}->append_para( $self->_make( heading => 2 => $text ) );
68 }
69  
70 sub para_P
71 16 ▶{
Subroutine para_P: called CC=1 SCAR=0.0
Pod documented
72 16 my $self = shift;
73 16 my ( $opts ) = @_;
74 16 $self->{current_document}->append_para( $self->_make( para_plain => $opts, $self->_make( chunklist => ) ) );
75 }
76  
77 sub para_TP
78 1 ▶{
Subroutine para_TP: called CC=1 SCAR=0.0
Pod documented
79 1 my $self = shift;
80 1 my ( $opts ) = @_;
81 1 $self->{current_document}->append_para( $self->_make( para_term => $opts, $self->_make( chunklist => ), $self->_make( chunklist => ) ) );
82 }
83  
84 sub para_IP
85 2 ▶{
Subroutine para_IP: called CC=1 SCAR=0.0
Pod documented
86 2 my $self = shift;
87 2 my ( $opts ) = @_;
88 2 $self->{current_document}->append_para( $self->_make( para_indent => $opts, $self->_make( chunklist => ) ) );
89 }
90  
91 sub para_EX
92 0 ▶{
Subroutine para_EX: not called CC=1 SCAR=6.9
Pod documented
93 0 my $self = shift;
94 0 my ( $opts ) = @_;
95 0 $self->{current_document}->append_para( $self->_make( para_example => $opts, $self->_make( chunklist => ) ) );
96 }
97  
98 sub chunk
99 27 ▶{
Subroutine chunk: called CC=1 SCAR=0.0
Pod documented
100 27 my $self = shift;
101 27 my ( $text, %opts ) = @_;
102 27 $self->{current_document}->append_chunk( $self->_make( chunk => $text => $opts{font}, $opts{size} ) );
103 }
104  
105 sub join_para
106 5 ▶{
Subroutine join_para: called CC=1 SCAR=0.0
Pod undocumented
107 5 my $self = shift;
108 5 $self->{current_document}->append_chunk( $self->_make( linebreak => ) );
109 }
110  
111 sub entity_br
112 0 ▶{
Subroutine entity_br: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $self = shift;
114 0 $self->{current_document}->append_chunk( $self->_make( break => ) );
115 }
116  
117 sub entity_sp
118 1 ▶{
Subroutine entity_sp: called CC=1 SCAR=0.0
Pod undocumented
119 1 my $self = shift;
120 1 $self->{current_document}->append_chunk( $self->_make( space => ) );
121 }
122  
123 sub _make
124 95 ▶{
Subroutine _make: called CC=2 SCAR=7.0
125 95 my $self = shift;
126 95 my $type = shift;
127 95 ▶ my $code = $self->can( "${type}_class" ) or die "Unable to make a ${type}";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can("${type}_class") or die "Unable to make a $type" 95 0 95
128 95 return $code->()->new( @_ );
129 }
130  
131 7 ▶use constant document_class => "Parse::Man::DOM::Document";
Subroutine BEGIN: called CC=1 SCAR=0.0
132 7 ▶use constant metadata_class => "Parse::Man::DOM::Metadata";
Subroutine BEGIN: called CC=1 SCAR=0.0
133 7 ▶use constant heading_class => "Parse::Man::DOM::Heading";
Subroutine BEGIN: called CC=1 SCAR=0.0
134 7 ▶use constant para_plain_class => "Parse::Man::DOM::Para::Plain";
Subroutine BEGIN: called CC=1 SCAR=0.0
135 7 ▶use constant para_term_class => "Parse::Man::DOM::Para::Term";
Subroutine BEGIN: called CC=1 SCAR=0.0
136 7 ▶use constant para_indent_class => "Parse::Man::DOM::Para::Indent";
Subroutine BEGIN: called CC=1 SCAR=0.0
137 7 ▶use constant para_example_class => "Parse::Man::DOM::Para::Example";
Subroutine BEGIN: called CC=1 SCAR=0.0
138 7 ▶use constant chunklist_class => "Parse::Man::DOM::Chunklist";
Subroutine BEGIN: called CC=1 SCAR=0.0
139 7 ▶use constant chunk_class => "Parse::Man::DOM::Chunk";
Subroutine BEGIN: called CC=1 SCAR=0.0
140 7 ▶use constant space_class => "Parse::Man::DOM::Space";
Subroutine BEGIN: called CC=1 SCAR=0.0
141 7 ▶use constant break_class => "Parse::Man::DOM::Break";
Subroutine BEGIN: called CC=1 SCAR=0.0
142 7 ▶use constant linebreak_class => "Parse::Man::DOM::Linebreak";
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 package Parse::Man::DOM::Document;
145  
146 =head1 Parse::Man::DOM::Document
147  
148 Represents the document as a whole.
149  
150 =cut
151  
152 sub new
153 17 ▶{
Subroutine new: called CC=1 SCAR=0.0
154 17 my $class = shift;
155 17 return bless { meta => {}, paras => [] }, $class;
156 }
157  
158 =head2 meta
159  
160 $meta = $document->meta( $key )
161  
162 Returns a C<Parse::Man::DOM::Metadata> object for the named item of metadata.
163  
164 =over 4
165  
166 =item * name
167  
168 The page name given to the C<.TH> directive.
169  
170 =item * section
171  
172 The section number given to the C<.TH> directive.
173  
174 =back
175  
176 =cut
177  
178 sub meta
179 3 ▶{
Subroutine meta: called CC=2 SCAR=7.1
180 3 my $self = shift;
181 3 my ( $name ) = @_;
182 3 ▶ return $self->{meta}{$name} || die "No meta defined for $name";
Condition50%1/2
$self->{'meta'}{$name} || die("No meta defined for $name")
l!l
30
MC/DC0%0/1
$self->{'meta'}{$name} || die("No meta defined for $name")
$self->{'meta'}{$name}
Truth table50%1/2
A
A= $self->{'meta'}{$name}
Aresult
11
00
183 }
184  
185 sub add_meta
186 4 ▶{
Subroutine add_meta: called CC=1 SCAR=0.0
187 4 my $self = shift;
188 4 my ( $meta ) = @_;
189 4 $self->{meta}{ $meta->name } = $meta;
190 }
191  
192 =head2 paras
193  
194 @paras = $document->paras
195  
196 Returns a list of C<Parse::Man::DOM::Heading> or C<Parse::Man::DOM::Para> or
197 subclass objects, containing the actual page content.
198  
199 =cut
200  
201 sub paras
202 16 ▶{
Subroutine paras: called CC=1 SCAR=0.0
203 16 my $self = shift;
204 16 return @{ $self->{paras} };
205 }
206  
207 sub append_para
208 21 ▶{
Subroutine append_para: called CC=1 SCAR=0.0
209 21 my $self = shift;
210 21 push @{ $self->{paras} }, @_;
211 }
212  
213 33 ▶sub last_para { shift->{paras}[-1] }
Subroutine last_para: called CC=1 SCAR=0.0
214  
215 33 ▶sub append_chunk { shift->last_para->append_chunk( @_ ) }
Subroutine append_chunk: called CC=1 SCAR=0.0
216  
217 package Parse::Man::DOM::Metadata;
218  
219 =head1 Parse::Man::DOM::Metadata
220  
221 Represents a single item of metadata about the page.
222  
223 =cut
224  
225 sub new
226 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
227 4 my $class = shift;
228 4 return bless [ $_[0] => $_[1] ], $class;
229 }
230  
231 =head2 name
232  
233 $name = $metadata->name
234  
235 The string name of the metadata
236  
237 =head2 value
238  
239 $value = $metadata->value
240  
241 The string value of the metadata
242  
243 =cut
244  
245 4 ▶sub name { shift->[0] }
Subroutine name: called CC=1 SCAR=0.0
246 2 ▶sub value { shift->[1] }
Subroutine value: called CC=1 SCAR=0.0
247  
248 package Parse::Man::DOM::Heading;
249 7 ▶use constant type => "heading";
Subroutine BEGIN: called CC=1 SCAR=0.0
250  
251 =head1 Parse::Man::DOM::Heading
252  
253 Represents the contents of a C<.SH> or C<.SS> heading
254  
255 =cut
256  
257 sub new
258 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
259 2 my $class = shift;
260 2 return bless [ $_[0] => $_[1] ], $class;
261 }
262  
263 =head2 level
264  
265 $level = $heading->level
266  
267 The heading level number; 1 for C<.SH>, 2 for C<.SS>
268  
269 =head2 text
270  
271 $text = $heading->text
272  
273 The plain text string of the heading title
274  
275 =cut
276  
277 1 ▶sub level { shift->[0] }
Subroutine level: called CC=1 SCAR=0.0
278 1 ▶sub text { shift->[1] }
Subroutine text: called CC=1 SCAR=0.0
279  
280 package Parse::Man::DOM::Para;
281  
282 =head1 Parse::Man::DOM::Para
283  
284 Represents a paragraph of formatted text content. Will be one of the following
285 subclasses.
286  
287 =cut
288  
289 =head2 filling
290  
291 $filling = $para->filling
292  
293 Returns true if filling (C<.fi>) is in effect, or false if no-filling (C<.nf>)
294 is in effect.
295  
296 =head2 body
297  
298 $chunklist = $para->body
299  
300 Returns a C<Parse::Man::DOM::Chunklist> to represent the actual content of the
301 paragraph.
302  
303 =head2 indent
304  
305 $indent = $para->indent
306  
307 Returns the indentation size in column count, if defined.
308  
309 =cut
310  
311 4 ▶sub filling { shift->{filling} }
Subroutine filling: called CC=1 SCAR=0.0
312 41 ▶sub body { shift->{body} }
Subroutine body: called CC=1 SCAR=0.0
313 1 ▶sub indent { shift->{indent} }
Subroutine indent: called CC=1 SCAR=0.0
314  
315 package Parse::Man::DOM::Para::Plain;
316 7 ▶use base qw( Parse::Man::DOM::Para );
Subroutine BEGIN: called CC=1 SCAR=0.0
317 7 ▶use constant type => "plain";
Subroutine BEGIN: called CC=1 SCAR=0.0
318  
319 =head1 Parse::Man::DOM::Para::Plain
320  
321 Represent a plain (C<.P> or C<.PP>) paragraph.
322  
323 =head2 type
324  
325 $type = $para->type
326  
327 Returns C<"plain">.
328  
329 =cut
330  
331 sub new
332 18 ▶{
Subroutine new: called CC=1 SCAR=0.0
333 18 my $class = shift;
334 18 my ( $opts, $body ) = @_;
335 18 return bless { (map { $_ => $opts->{$_} } qw( filling indent )), body => $body }, $class;
336 }
337  
338 27 ▶sub append_chunk { shift->body->append_chunk( @_ ) }
Subroutine append_chunk: called CC=1 SCAR=0.0
339  
340 package Parse::Man::DOM::Para::Term;
341 7 ▶use base qw( Parse::Man::DOM::Para );
Subroutine BEGIN: called CC=1 SCAR=0.0
342 7 ▶use constant type => "term";
Subroutine BEGIN: called CC=1 SCAR=0.0
343  
344 =head1 Parse::Man::DOM::Para::Term
345  
346 Represents a term paragraph (C<.TP>).
347  
348 =head2 type
349  
350 $type = $para->type
351  
352 Returns C<"term">.
353  
354 =cut
355  
356 sub new
357 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
358 1 my $class = shift;
359 1 my ( $opts, $term, $definition ) = @_;
360  
361 1 return bless { indent => $opts->{indent}, term => $term, definition => $definition }, $class;
362 }
363  
364 =head2 term
365  
366 $chunklist = $para->term
367  
368 Returns a C<Parse::Man::DOM::Chunklist> for the defined term name.
369  
370 =head2 definition
371  
372 $chunklist = $para->definition
373  
374 Returns a C<Parse::Man::DOM::Chunklist> for the defined term definition.
375  
376 =cut
377  
378 2 ▶sub term { shift->{term} }
Subroutine term: called CC=1 SCAR=0.0
379 6 ▶sub definition { shift->{definition} }
Subroutine definition: called CC=1 SCAR=0.0
380  
381 sub append_chunk
382 6 ▶{
Subroutine append_chunk: called CC=3 SCAR=11.0
383 6 my $self = shift;
384 6 my ( $chunk ) = @_;
385  
386 6 ▶ if( $chunk->isa( "Parse::Man::DOM::Linebreak" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk->isa('Parse::Man::DOM::Linebreak')) { } 2 4 6
elsif (not $self->{'term_done'}) { } 1 3 4
387 2 $self->{term_done} = 1;
388 }
389 elsif( !$self->{term_done} ) {
390 1 $self->term->append_chunk( $chunk );
391 }
392 else {
393 3 $self->definition->append_chunk( $chunk );
394 }
395 }
396  
397 package Parse::Man::DOM::Para::Indent;
398 7 ▶use base qw( Parse::Man::DOM::Para::Plain );
Subroutine BEGIN: called CC=1 SCAR=0.0
399 7 ▶use constant type => "indent";
Subroutine BEGIN: called CC=1 SCAR=0.0
400  
401 =head1 Parse::Man::DOM::Para::Indent
402  
403 Represents an indented paragraph (C<.IP>).
404  
405 =head2 type
406  
407 $type = $para->type
408  
409 Returns C<"indent">.
410  
411 =cut
412  
413 sub new
414 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
415 2 my $class = shift;
416 2 my ( $opts, $body ) = @_;
417 2 my $self = $class->SUPER::new( @_ );
418 2 $self->{marker} = $opts->{marker};
419 2 return $self;
420 }
421  
422 =head2 marker
423  
424 $marker = $para->marker
425  
426 Returns the indentation marker text, if defined.
427  
428 =cut
429  
430 1 ▶sub marker { shift->{marker} }
Subroutine marker: called CC=1 SCAR=0.0
431  
432 package Parse::Man::DOM::Para::Example;
433 7 ▶use base qw( Parse::Man::DOM::Para::Plain );
Subroutine BEGIN: called CC=1 SCAR=0.0
434 7 ▶use constant type => "example";
Subroutine BEGIN: called CC=1 SCAR=0.0
435  
436 =head1 Parse::Man::DOM::Para::Example
437  
438 Represents an example paragraph (C<.EX> / C<.EE>).
439  
440 =head2 type
441  
442 $type = $para->type
443  
444 Returns C<"example">.
445  
446 =cut
447  
448 package Parse::Man::DOM::Chunklist;
449  
450 =head1 Parse::Man::DOM::Chunklist
451  
452 Contains a list of C<Parse::Man::DOM::Chunk> objects to represent paragraph
453 content.
454  
455 =cut
456  
457 sub new
458 20 ▶{
Subroutine new: called CC=1 SCAR=0.0
459 20 my $class = shift;
460 20 return bless { chunks => [ @_ ] }, $class;
461 }
462  
463 =head2 chunks
464  
465 @chunks = $chunklist->chunks
466  
467 Returns a list of C<Parse::Man::DOM::Chunk> objects.
468  
469 =cut
470  
471 18 ▶sub chunks { @{ shift->{chunks} } }
Subroutine chunks: called CC=1 SCAR=0.0
472  
473 sub append_chunk
474 31 ▶{
Subroutine append_chunk: called CC=1 SCAR=0.0
475 31 my $self = shift;
476 31 push @{ $self->{chunks} }, @_;
477 }
478  
479 package Parse::Man::DOM::Chunk;
480  
481 =head1 Parse::Man::DOM::Chunk
482  
483 Represents a chunk of text with a particular format applied.
484  
485 =cut
486  
487 sub new
488 27 ▶{
Subroutine new: called CC=1 SCAR=0.0
489 27 my $class = shift;
490 27 return bless [ @_ ], $class;
491 }
492  
493 0 ▶sub is_linebreak { 0 }
Subroutine is_linebreak: not called CC=1 SCAR=6.9
494 0 ▶sub is_space { 0 }
Subroutine is_space: not called CC=1 SCAR=6.9
495 0 ▶sub is_break { 0 }
Subroutine is_break: not called CC=1 SCAR=6.9
496  
497 =head2 text
498  
499 $text = $chunk->text
500  
501 The plain string value of the text for this chunk.
502  
503 =head2 font
504  
505 $font = $chunk->font
506  
507 The font name in effect for this chunk. One of C<"R">, C<"B">, C<"I"> or
508 C<"SM">.
509  
510 =head2 size
511  
512 $size = $chunk->size
513  
514 The size of this chunk, relative to the paragraph base of 0.
515  
516 =cut
517  
518 23 ▶sub text { shift->[0] }
Subroutine text: called CC=1 SCAR=0.0
519 12 ▶sub font { shift->[1] }
Subroutine font: called CC=1 SCAR=0.0
520 0 ▶sub size { shift->[2] }
Subroutine size: not called CC=1 SCAR=6.9
521  
522 package Parse::Man::DOM::Linebreak;
523 7 ▶use base qw( Parse::Man::DOM::Chunk );
Subroutine BEGIN: called CC=1 SCAR=0.0
524  
525 sub new
526 5 ▶{
Subroutine new: called CC=1 SCAR=0.0
527 5 my $class = shift;
528 5 return bless [], $class;
529 }
530  
531 3 ▶sub is_linebreak { 1 }
Subroutine is_linebreak: called CC=1 SCAR=0.0
532  
533 package Parse::Man::DOM::Space;
534 7 ▶use base qw( Parse::Man::DOM::Chunk );
Subroutine BEGIN: called CC=1 SCAR=0.0
535  
536 sub new
537 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
538 1 my $class = shift;
539 1 return bless [], $class;
540 }
541  
542 1 ▶sub is_space { 1 }
Subroutine is_space: called CC=1 SCAR=0.0
543  
544 package Parse::Man::DOM::Break;
545 7 ▶use base qw( Parse::Man::DOM::Chunk );
Subroutine BEGIN: called CC=1 SCAR=0.0
546  
547 sub new
548 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
549 0 my $class = shift;
550 0 return bless [], $class;
551 }
552  
553 0 ▶sub is_break { 1 }
Subroutine is_break: not called CC=1 SCAR=6.9
554  
555 =head1 AUTHOR
556  
557 Paul Evans <leonerd@leonerd.org.uk>
558  
559 =cut
560  
561 7 0x55AA;
« Man.pm ↑ Summary