Pod/Handler.pm

filter: Statementstmt 79.6% 168 / 211 Branchbran 73.5% 25 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 42 / 54 Podpod 0.0% 0 / 43 totaltotal 68.0% 236 / 347 CC 19 SCAR 8.3
CC
19
Cov
78%
text
6.0
end_table
6.0
image
5.1
CRAP 2.3
« Pod/script.pm ↑ Summary Pod.pm »
1 package Markdown::Pod::Handler;
2 # ABSTRACT: Parser module to convert from markdown to POD
3  
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 our $VERSION = '0.007';
8  
9 32 ▶use Markdent::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use MooseX::Params::Validate qw( validated_list validated_hash );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 32 with 'Markdent::Role::EventsAsMethods';
18  
19 32 has encoding => (
20 is => 'ro',
21 isa => t('Str'),
22 default => q{},
23 );
24  
25 32 has _output => (
26 is => 'ro',
27 isa => t('OutputStream'),
28 required => 1,
29 init_arg => 'output',
30 );
31  
32 # Default width for horizontal rule
33 #
34 32 our $HORIZONTAL_RULE_WIDTH = 80;
35  
36 32 my $link_buf;
37 my $code_buf;
38 32 my $tble_buf;
39 32 my @tble = ( [] );
40 32 my @blockquotes;
41 my @list_type;
42  
43 use constant {
44 32 STACK_LINK => 1,
45 STACK_CODE => 2,
46 STACK_TBLE => 3,
47 STACK_STRONG => 4,
48 STACK_EMPHASIS => 5,
49 32 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 32 my @style_stack;
52  
53 32 my $list_depth = 0;
54  
55 sub _stream {
56 7754 ▶ my ( $self, @params ) = @_;
Subroutine _stream: called CC=1 SCAR=0.0
57 7754 print { $self->_output } @params;
58 }
59  
60 sub start_document {
61 31 ▶ my $self = shift;
Subroutine start_document: called CC=2 SCAR=7.2
Pod undocumented
62  
63 31 ▶ $self->_stream( '=encoding ' . $self->encoding . "\n\n" ) if $self->encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 31 0 31
64 }
65  
66 ▶sub end_document { }
Subroutine end_document: called CC=1 SCAR=0.0
Pod undocumented
67  
68 sub text {
69 4094 ▶ my $self = shift;
Subroutine text: called CC=6 SCAR=18.0
Pod undocumented
70 4094 my ($text) = validated_list( \@_, text => { type => 'Str' } );
71  
72 4094 ▶ if (@style_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@style_stack) { } 1503 2591 4094
73 # This allows the end_link() handler to know that *some* text was inside
74 # it. So if one has [`text`](http://example.org/), the end_code()
75 # handler will output the code to the stream before the end_link()
76 # finishes.
77 1503 ▶ $link_buf->{text} = '' if grep { $_ == STACK_LINK } @style_stack;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == 1;} @style_stack 601 902 1503
78  
79 1503 ▶ if ( $style_stack[-1] == STACK_LINK ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($style_stack[-1] == 1) { } 598 905 1503
elsif ($style_stack[-1] == 2) { } 728 177 905
elsif ($style_stack[-1] == 3) { } 0 177 177
80 598 $link_buf->{text} = $text;
81 }
82 elsif ( $style_stack[-1] == STACK_CODE ) {
83 728 $code_buf->{text} = $text;
84 }
85 elsif ( $style_stack[-1] == STACK_TBLE ) {
86 0 $tble_buf->{text} = $text;
87 }
88 else {
89 # another kind of style that does not require storing state
90 177 $self->_stream($text);
91 }
92 }
93 else {
94 2591 $self->_stream($text);
95 }
96 }
97  
98 sub start_header {
99 253 ▶ my $self = shift;
Subroutine start_header: called CC=1 SCAR=0.0
Pod undocumented
100 253 my ($level) = validated_list( \@_, level => { type => 'HeaderLevel' }, );
101  
102 253 $self->_stream("\n=head$level ");
103 }
104  
105 sub end_header {
106 253 ▶ my $self = shift;
Subroutine end_header: called CC=1 SCAR=0.0
Pod undocumented
107 253 my ($level) = validated_list( \@_, level => { type => 'HeaderLevel' }, );
108  
109 253 $self->_stream("\n");
110 }
111  
112 sub start_paragraph {
113 830 ▶ my $self = shift;
Subroutine start_paragraph: called CC=1 SCAR=0.0
Pod undocumented
114 }
115  
116 sub end_paragraph {
117 830 ▶ my $self = shift;
Subroutine end_paragraph: called CC=1 SCAR=0.0
Pod undocumented
118  
119 830 $self->_stream("\n");
120 }
121  
122 sub start_link {
123 601 ▶ my $self = shift;
Subroutine start_link: called CC=1 SCAR=0.0
Pod undocumented
124 601 my %p = validated_hash(
125 \@_,
126 uri => { type => 'Str' },
127 title => { type => 'Str', optional => 1 },
128 id => { type => 'Str', optional => 1 },
129 is_implicit_id => { type => 'Bool', optional => 1 },
130 );
131  
132 601 delete @p{ grep { !defined $p{$_} } keys %p };
133  
134 601 push @style_stack, STACK_LINK;
135 601 $link_buf->{uri} = $p{uri};
136 601 $self->_stream('L<');
137 }
138  
139 sub end_link {
140 601 ▶ my $self = shift;
Subroutine end_link: called CC=3 SCAR=12.3
Pod undocumented
141  
142 601 ▶ if ( $link_buf && exists $link_buf->{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_buf and exists $link_buf->{'text'}) { } 601 0 601
Condition33%1/3
$link_buf and exists $link_buf->{"text"}
!ll&&!rl&&r
00601
MC/DC0%0/2
$link_buf and exists $link_buf->{"text"}
$link_buf exists $link_buf->{"text"}
Truth table33%1/3
A and B
A= $link_bufB= exists $link_buf->{"text"}
ABresult
0X0
100
111
143 601 $self->_stream("$link_buf->{text}|$link_buf->{uri}>");
144 }
145 else {
146 0 $self->_stream("$link_buf->{uri}>");
147 }
148  
149 601 pop @style_stack;
150 601 $link_buf = undef;
151 }
152  
153 sub start_strong {
154 13 ▶ my $self = shift;
Subroutine start_strong: called CC=1 SCAR=0.0
Pod undocumented
155  
156 13 push @style_stack, STACK_STRONG;
157 13 $self->_stream('B<');
158 }
159  
160 sub end_strong {
161 13 ▶ my $self = shift;
Subroutine end_strong: called CC=1 SCAR=0.0
Pod undocumented
162  
163 13 pop @style_stack;
164 13 $self->_stream('>');
165 }
166  
167 sub start_emphasis {
168 166 ▶ my $self = shift;
Subroutine start_emphasis: called CC=1 SCAR=0.0
Pod undocumented
169  
170 166 push @style_stack, STACK_EMPHASIS;
171 166 $self->_stream('I<');
172 }
173  
174 sub end_emphasis {
175 166 ▶ my $self = shift;
Subroutine end_emphasis: called CC=1 SCAR=0.0
Pod undocumented
176  
177 166 pop @style_stack;
178 166 $self->_stream('>');
179 }
180  
181 sub preformatted {
182 257 ▶ my $self = shift;
Subroutine preformatted: called CC=1 SCAR=0.0
Pod undocumented
183 257 my ($text) = validated_list( \@_, text => { type => 'Str' }, );
184  
185 257 chomp $text;
186 257 $text =~ s/^/ /gsm;
187 257 $self->_stream( $text, "\n\n" );
188 }
189  
190 sub start_blockquote {
191 1 ▶ my $self = shift;
Subroutine start_blockquote: called CC=1 SCAR=0.0
Pod undocumented
192  
193 1 $self->_stream("=over 2\n\n");
194 }
195  
196 sub end_blockquote {
197 1 ▶ my $self = shift;
Subroutine end_blockquote: called CC=1 SCAR=0.0
Pod undocumented
198  
199 1 $self->_stream("=back\n\n");
200 }
201  
202 sub start_unordered_list {
203 86 ▶ my $self = shift;
Subroutine start_unordered_list: called CC=2 SCAR=6.9
Pod undocumented
204  
205 86 ▶ $self->_stream("\n") if $list_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $list_depth 3 83 86
206 86 $list_depth++;
207 86 $self->_stream("=over\n\n");
208 }
209  
210 sub end_unordered_list {
211 86 ▶ my $self = shift;
Subroutine end_unordered_list: called CC=1 SCAR=0.0
Pod undocumented
212  
213 86 $list_depth--;
214 86 $self->_stream("=back\n\n");
215 }
216  
217 sub start_ordered_list {
218 3 ▶ my $self = shift;
Subroutine start_ordered_list: called CC=2 SCAR=7.0
Pod undocumented
219  
220 3 ▶ $self->_stream("\n") if $list_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $list_depth 0 3 3
221 3 $list_depth++;
222 3 $self->_stream("=over\n\n");
223 }
224  
225 sub end_ordered_list {
226 3 ▶ my $self = shift;
Subroutine end_ordered_list: called CC=1 SCAR=0.0
Pod undocumented
227  
228 3 $list_depth--;
229 3 $self->_stream("=back\n\n");
230 }
231  
232 sub start_list_item {
233 388 ▶ my $self = shift;
Subroutine start_list_item: called CC=1 SCAR=0.0
Pod undocumented
234 388 my %p = validated_hash( \@_, bullet => { type => 'Str' }, );
235  
236 388 $self->_stream("=item $p{bullet}\n\n");
237 }
238  
239 sub end_list_item {
240 388 ▶ my $self = shift;
Subroutine end_list_item: called CC=1 SCAR=0.0
Pod undocumented
241  
242 388 $self->_stream("\n\n");
243 }
244  
245 sub start_code {
246 728 ▶ my $self = shift;
Subroutine start_code: called CC=1 SCAR=0.0
Pod undocumented
247 # Start buffering this snippet
248 728 push @style_stack, STACK_CODE;
249 728 $code_buf = {};
250 }
251  
252 sub end_code {
253 728 ▶ my $self = shift;
Subroutine end_code: called CC=3 SCAR=11.1
Pod undocumented
254 728 my $text = $code_buf->{'text'};
255 728 ▶ if ( $text =~ /\n/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\n/m) { } 0 728 728
256 # Multi-line. Probably code block
257 #
258 0 $text =~ s/^(.*)$/ $1/mg;
259 0 $self->_stream($text);
260 }
261 else {
262 # Single line
263 #
264 728 ▶ if ( $text =~ /[<>]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /[<>]/) { } 17 711 728
265 # this is so that extra angle brackets are not used unless necessary
266 17 my @all_angle = $text =~ /(<+|>+)/g;
267 17 my @all_angle_len = map { length $_ } @all_angle;
268 17 my $longest = List::Util::max @all_angle_len;
269  
270 17 my $start_angle = "<" x ( $longest + 2 );
271 17 my $end_angle = ">" x ( $longest + 2 );
272 17 $self->_stream("C$start_angle $text $end_angle");
273 }
274 else {
275 711 $self->_stream("C<$text>");
276 }
277 }
278 728 pop @style_stack;
279 728 $code_buf = undef;
280 }
281  
282 sub code_block {
283 0 ▶ my $self = shift;
Subroutine code_block: not called CC=1 SCAR=6.9
Pod undocumented
284 0 my ($code) = validated_list(
285 \@_,
286 code => { type => 'Str' },
287 language => { type => 'Str', optional => 1 }
288 );
289 0 $code =~ s/^(.*)$/ $1/mg;
290 0 $self->_stream("\n$code\n");
291 }
292  
293 sub image {
294 74 ▶ my $self = shift;
Subroutine image: called CC=5 SCAR=16.3
Pod undocumented
295 74 my %p = validated_hash(
296 \@_,
297 alt_text => { type => 'Str' },
298 uri => { type => 'Str', optional => 1 },
299 title => { type => 'Str', optional => 1 },
300 id => { type => 'Str', optional => 1 },
301 is_implicit_id => { type => 'Bool', optional => 1 },
302 );
303  
304 74 delete @p{ grep { !defined $p{$_} } keys %p };
305  
306 74 ▶ my $alt_text = exists $p{alt_text} ? qq|alt="$p{alt_text}"| : q{};
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'alt_text'} ? : 74 0 74
307  
308 74 ▶ my $attr = exists $p{title} ? $p{title} : q{};
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'title'} ? : 33 41 74
309 74 my $attr_text = q{};
310 74 ▶ while ( $attr =~ s/(\S+)="(.*?)"// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ s/(\S+)="(.*?)"//) 0 74 74
311 0 $attr_text .= qq{ $1="$2"};
312 }
313 74 ▶ while ( $attr =~ /(\S+)=(\S+)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /(\S+)=(\S+)/g) 33 74 107
314 33 $attr_text .= qq{ $1="$2"};
315 }
316  
317 74 $self->_stream(qq|=for html <img src="$p{uri}" $alt_text$attr_text />|);
318 }
319  
320 sub start_html_tag {
321 7 ▶ my $self = shift;
Subroutine start_html_tag: called CC=1 SCAR=0.0
Pod undocumented
322 7 my ( $tag, $attributes ) = validated_list(
323 \@_,
324 tag => { type => 'Str' },
325 attributes => { type => 'HashRef' },
326 );
327 }
328  
329 sub end_html_tag {
330 5 ▶ my $self = shift;
Subroutine end_html_tag: called CC=1 SCAR=0.0
Pod undocumented
331 5 my ( $tag, $attributes ) = validated_list( \@_, tag => { type => 'Str' }, );
332 }
333  
334 sub html_tag {
335 38 ▶ my $self = shift;
Subroutine html_tag: called CC=2 SCAR=6.9
Pod undocumented
336 38 my ( $tag, $attributes ) = validated_list(
337 \@_,
338 tag => { type => 'Str' },
339 attributes => { type => 'HashRef' },
340 );
341  
342 38 my $attributes_str = q{};
343 $attributes_str = join q{ },
344 38 map { qq|$_="$attributes->{$_}"| } sort keys %$attributes;
345 38 ▶ if ( $tag =~ /^br$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^br$/i) { } 37 1 38
346 37 $self->_stream(qq|<$tag $attributes_str />\n|);
347 }
348 else {
349 1 $self->_stream(qq|<$tag $attributes_str />|);
350 }
351 }
352  
353 sub html_block {
354 2 ▶ my $self = shift;
Subroutine html_block: called CC=1 SCAR=0.0
Pod undocumented
355 2 my ($html) = validated_list( \@_, html => { type => 'Str' }, );
356  
357 2 chomp $html;
358 2 $self->_output()->print(
359 <<"END_HTML"
360  
361 =begin html
362  
363 $html
364  
365 =end html
366  
367 END_HTML
368 );
369 }
370  
371 sub line_break {
372 2 ▶ my $self = shift;
Subroutine line_break: called CC=1 SCAR=0.0
Pod undocumented
373 2 $self->_stream("\n\n");
374 }
375  
376 sub html_entity {
377 1 ▶ my $self = shift;
Subroutine html_entity: called CC=1 SCAR=0.0
Pod undocumented
378 1 my ($entity) = validated_list( \@_, entity => { type => 'Str' } );
379  
380 1 $self->_stream("E<$entity>");
381 }
382  
383 # Added A.Speer
384 sub horizontal_rule {
385 0 ▶ my $self = shift;
Subroutine horizontal_rule: not called CC=1 SCAR=6.9
Pod undocumented
386 0 $self->_stream( ( '=' x $HORIZONTAL_RULE_WIDTH ) . "\n" );
387 }
388  
389 sub auto_link {
390 0 ▶ my $self = shift;
Subroutine auto_link: not called CC=1 SCAR=6.9
Pod undocumented
391 0 my ($uri) = validated_list( \@_, uri => { type => 'Str' } );
392 0 $self->_stream("L<$uri>");
393 }
394  
395 sub html_comment_block {
396 0 ▶ my $self = shift;
Subroutine html_comment_block: not called CC=1 SCAR=6.9
Pod undocumented
397 # Stub
398 }
399  
400 sub start_table {
401 0 ▶ my $self = shift;
Subroutine start_table: not called CC=1 SCAR=6.9
Pod undocumented
402 # Stub
403 }
404  
405 sub start_table_body {
406 0 ▶ my $self = shift;
Subroutine start_table_body: not called CC=1 SCAR=6.9
Pod undocumented
407 # Stub
408 }
409  
410 sub start_table_row {
411 0 ▶ my $self = shift;
Subroutine start_table_row: not called CC=1 SCAR=6.9
Pod undocumented
412 # Stub
413 }
414  
415 sub start_table_cell {
416 0 ▶ my $self = shift;
Subroutine start_table_cell: not called CC=1 SCAR=6.9
Pod undocumented
417 0 push @style_stack, STACK_TBLE;
418 0 $tble_buf = {};
419 }
420  
421 sub end_table {
422 0 ▶ my $self = shift;
Subroutine end_table: not called CC=2 SCAR=17.9
Pod undocumented
423 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Text::Table::Tiny; 1 } } or die "unable to load Text::Table::Tiny - please make sure it is installed !" 0 0 0
424 0 require Text::Table::Tiny;
425 0 1;
426 }
427 || die('unable to load Text::Table::Tiny - please make sure it is installed !');
428 0 my $table =
429 Text::Table::Tiny::table( rows => \@tble, separate_rows => 0, header_row => 0 );
430 # Indent so table appears as POD code. Open to other suggestions
431 0 $table =~ s/^(.*)/ $1/mg;
432 0 $table .= "\n";
433 # Safety in case parser skips end-cell - which it seems to do sometimes
434 0 pop @style_stack;
435 0 $tble_buf = undef;
436 0 $self->_stream($table);
437 }
438  
439 sub end_table_body {
440 0 ▶ my $self = shift;
Subroutine end_table_body: not called CC=1 SCAR=6.9
Pod undocumented
441 # Safety
442 0 pop @style_stack;
443 0 $tble_buf = undef;
444 }
445  
446 sub end_table_row {
447 0 ▶ my $self = shift;
Subroutine end_table_row: not called CC=1 SCAR=6.9
Pod undocumented
448 0 push @tble, [];
449 # Safety
450 0 pop @style_stack;
451 0 $tble_buf = undef;
452 }
453  
454 sub end_table_cell {
455 0 ▶ my $self = shift;
Subroutine end_table_cell: not called CC=1 SCAR=6.9
Pod undocumented
456 0 push @{ $tble[$#tble] }, $tble_buf->{'text'};
457 # Stop buffering table text
458 0 pop @style_stack;
459 0 $tble_buf = undef;
460 }
461  
462 32 __PACKAGE__->meta->make_immutable;
463 32 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
464 32 1;
465  
466 __END__
467  
468 =pod
469  
470 =encoding UTF-8
471  
472 =head1 NAME
473  
474 Markdown::Pod::Handler - Parser module to convert from markdown to POD
475  
476 =head1 VERSION
477  
478 version 0.007
479  
480 =head1 SYNOPSIS
481  
482 my $handler = Markdown::Pod::Handler->new(
483 encoding => $encoding,
484 output => $fh,
485 );
486
487 my $parser = Markdent::Parser->new(
488 dialect => $dialect,
489 handler => $handler,
490 );
491  
492 =head1 DESCRIPTION
493  
494 This module is a handler of L<Markdent> Markdown parser.
495 It converts Markdown to POD.
496  
497 =head1 ATTRIBUTES
498  
499 =head2 markdown
500  
501 markdown text
502  
503 =head2 encoding
504  
505 encoding to use
506  
507 =head1 METHODS
508  
509 =head2 new
510  
511 create Markdown::Pod::Handler object
512  
513 =head2 markdown_to_pod
514  
515 convert markdown text to POD text
516  
517 =for Pod::Coverage STACK_CODE
518 STACK_EMPHASIS
519 STACK_LINK
520 STACK_STRONG
521 STACK_TBLE
522 auto_link
523 code_block
524 end_blockquote
525 end_code
526 end_document
527 end_emphasis
528 end_header
529 end_html_tag
530 end_link
531 end_list_item
532 end_ordered_list
533 end_paragraph
534 end_strong
535 end_table
536 end_table_body
537 end_table_cell
538 end_table_row
539 end_unordered_list
540 horizontal_rule
541 html_block
542 html_comment_block
543 html_entity
544 html_tag
545 image
546 line_break
547 preformatted
548 start_blockquote
549 start_code
550 start_document
551 start_emphasis
552 start_header
553 start_html_tag
554 start_link
555 start_list_item
556 start_ordered_list
557 start_paragraph
558 start_strong
559 start_table
560 start_table_body
561 start_table_cell
562 start_table_row
563 start_unordered_list
564 text
565  
566 =head1 SEE ALSO
567  
568 =over
569  
570 =item *
571  
572 L<Markdent>
573  
574 =item *
575  
576 L<Pod::Markdown>
577  
578 =item *
579  
580 L<Text::MultiMarkdown>, L<Text::Markdown>
581  
582 =back
583  
584 =head1 AUTHOR
585  
586 김도형 - Keedi Kim <keedi@cpan.org>
587  
588 =head1 COPYRIGHT AND LICENSE
589  
590 This software is copyright (c) 2018 by Keedi Kim.
591  
592 This is free software; you can redistribute it and/or modify it under
593 the same terms as the Perl 5 programming language system itself.
594  
595 =cut
« Pod/script.pm ↑ Summary Pod.pm »