Markdent/Role/HTMLStream.pm

filter: Statementstmt 57.3% 132 / 230 Branchbran 19.2% 5 / 26 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 49.1% 30 / 61 Podpod 0.0% 0 / 46 totaltotal 45.3% 169 / 373 CC 16 SCAR 13.3
CC
16
Cov
53%
start_table_cell
20.0
start_table
12.0
image
12.0
CRAP 3.8
« Markdent/Role/AnyParser.pm ↑ Summary Markdent/Dialect/GitHub/BlockParser.pm »
1 package Markdent::Role::HTMLStream;
2  
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our $VERSION = '0.41';
8  
9 34 ▶use HTML::Entities qw( encode_entities );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Markdent::CheckedOutput;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Markdent::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 34 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 34 with 'Markdent::Role::EventsAsMethods';
17  
18 34 requires qw( start_document end_document );
19  
20 34 has _output => (
21 is => 'ro',
22 isa => t('OutputStream'),
23 required => 1,
24 init_arg => 'output',
25 );
26  
27 34 has _encodable_entities => (
28 is => 'ro',
29 isa => t('Str'),
30 default => q{<>&"\x00-\x09\x11\x12\x14-\x1f},
31 init_arg => 'encodable_entities',
32 );
33  
34 5 ▶override BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
35 5 my $self = shift;
36  
37 5 my $args = super();
38  
39 5 my $output = $args->{output};
40  
41 # This will blow up soon if there's no output.
42 5 ▶ return $args unless $output;
Branch: 1 branch
BranchTrueFalseTotal
unless $output 0 5 5
43  
44 # If the user supplied a non IO::Handle object we won't wrap it.
45 5 ▶ return $args if blessed $output && !$output->isa('IO::Handle');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($output) and not $output->isa("IO::Handle") 0 5 5
Condition66%2/3
builtin::blessed($output) and not $output->isa("IO::Handle")
!ll&&!rl&&r
410
MC/DC0%0/2
builtin::blessed($output) and not $output->isa("IO::Handle")
builtin::blessed($output) not $output->isa("IO::Handle")
Truth table66%2/3
A and not(B)
A= builtin::blessed($output)B= not $output->isa("IO::Handle")
ABresult
0X0
100
111
46  
47 5 $args->{output} = Markdent::CheckedOutput->new($output);
48  
49 5 return $args;
50 34 };
51  
52 {
53 34 my $validator = validation_for(
54 params => [
55 level => { type => t('HeaderLevel') },
56 ],
57 named_to_list => 1,
58 );
59  
60 3 ▶ sub start_header {
Subroutine start_header: called CC=1 SCAR=0.0
Pod undocumented
61 3 my $self = shift;
62 3 my ($level) = $validator->(@_);
63  
64 3 $self->_stream_start_tag( 'h' . $level );
65 }
66 }
67  
68 {
69 34 my $validator = validation_for(
70 params => [
71 level => { type => t('HeaderLevel') },
72 ],
73 named_to_list => 1,
74 );
75  
76 3 ▶ sub end_header {
Subroutine end_header: called CC=1 SCAR=0.0
Pod undocumented
77 3 my $self = shift;
78 3 my ($level) = $validator->(@_);
79  
80 3 $self->_stream_end_tag( 'h' . $level );
81 }
82 }
83  
84 34 ▶sub start_blockquote {
Subroutine start_blockquote: not called CC=1 SCAR=2.6
Pod undocumented
85 0 my $self = shift;
86  
87 0 $self->_stream_start_tag('blockquote');
88 }
89  
90 0 ▶sub end_blockquote {
Subroutine end_blockquote: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my $self = shift;
92  
93 0 $self->_stream_end_tag('blockquote');
94 }
95  
96 3 ▶sub start_unordered_list {
Subroutine start_unordered_list: called CC=1 SCAR=0.0
Pod undocumented
97 3 my $self = shift;
98  
99 3 $self->_stream_start_tag('ul');
100 }
101  
102 3 ▶sub end_unordered_list {
Subroutine end_unordered_list: called CC=1 SCAR=0.0
Pod undocumented
103 3 my $self = shift;
104  
105 3 $self->_stream_end_tag('ul');
106 }
107  
108 0 ▶sub start_ordered_list {
Subroutine start_ordered_list: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self = shift;
110  
111 0 $self->_stream_start_tag('ol');
112 }
113  
114 0 ▶sub end_ordered_list {
Subroutine end_ordered_list: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $self = shift;
116  
117 0 $self->_stream_end_tag('ol');
118 }
119  
120 6 ▶sub start_list_item {
Subroutine start_list_item: called CC=1 SCAR=0.0
Pod undocumented
121 6 my $self = shift;
122  
123 6 $self->_stream_start_tag('li');
124 }
125  
126 6 ▶sub end_list_item {
Subroutine end_list_item: called CC=1 SCAR=0.0
Pod undocumented
127 6 my $self = shift;
128  
129 6 $self->_stream_end_tag('li');
130 }
131  
132 {
133 34 my $validator = validation_for(
134 params => [
135 code => { type => t('Str') },
136 language => {
137 type => t('Str'),
138 optional => 1,
139 },
140 ],
141 named_to_list => 1,
142 );
143  
144 0 ▶ sub code_block {
Subroutine code_block: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my $self = shift;
146 0 my ( $code, $language ) = $validator->(@_);
147  
148 0 $self->_stream_start_tag('pre');
149  
150 0 ▶ my %class = $language ? ( class => 'language-' . $language ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$language ? : 0 0 0
151 0 $self->_stream_start_tag( 'code', \%class );
152  
153 0 $self->_stream_text($code);
154  
155 0 $self->_stream_end_tag('code');
156 0 $self->_stream_end_tag('pre');
157 }
158 }
159  
160 {
161 34 my $validator = validation_for(
162 params => [
163 text => { type => t('Str') },
164 ],
165 named_to_list => 1,
166 );
167  
168 0 ▶ sub preformatted {
Subroutine preformatted: not called CC=1 SCAR=6.9
Pod undocumented
169 0 my $self = shift;
170 0 my ($text) = $validator->(@_);
171  
172 0 $self->_stream_start_tag('pre');
173 0 $self->_stream_start_tag('code');
174 0 $self->_stream_text($text);
175 0 $self->_stream_end_tag('code');
176 0 $self->_stream_end_tag('pre');
177 }
178 }
179  
180 34 ▶sub start_paragraph {
Subroutine start_paragraph: called CC=1 SCAR=0.0
Pod undocumented
181 9 my $self = shift;
182  
183 9 $self->_stream_start_tag('p');
184 }
185  
186 7 ▶sub end_paragraph {
Subroutine end_paragraph: called CC=1 SCAR=0.0
Pod undocumented
187 7 my $self = shift;
188  
189 7 $self->_stream_end_tag('p');
190 }
191  
192 {
193 34 my $validator = validation_for(
194 params => [
195 caption => {
196 type => t('Str'),
197 optional => 1,
198 },
199 ],
200 named_to_list => 1,
201 );
202  
203 0 ▶ sub start_table {
Subroutine start_table: not called CC=3 SCAR=24.8
Pod undocumented
204 0 my $self = shift;
205 0 my ($caption) = $validator->(@_);
206  
207 0 $self->_stream_start_tag('table');
208  
209 0 ▶ if ( defined $caption && length $caption ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $caption and length $caption) 0 0 0
Condition0%0/3
defined $caption and length $caption
!ll&&!rl&&r
000
MC/DC0%0/2
defined $caption and length $caption
defined $caption length $caption
Truth table0%0/3
A and B
A= defined $captionB= length $caption
ABresult
0X0
100
111
210 0 $self->_stream_start_tag('caption');
211 0 $self->_stream_text($caption);
212 0 $self->_stream_end_tag('caption');
213 }
214 }
215 }
216  
217 34 ▶sub end_table {
Subroutine end_table: not called CC=1 SCAR=2.6
Pod undocumented
218 0 my $self = shift;
219  
220 0 $self->_stream_end_tag('table');
221 }
222  
223 0 ▶sub start_table_header {
Subroutine start_table_header: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my $self = shift;
225  
226 0 $self->_stream_start_tag('thead');
227 }
228  
229 0 ▶sub end_table_header {
Subroutine end_table_header: not called CC=1 SCAR=6.9
Pod undocumented
230 0 my $self = shift;
231  
232 0 $self->_stream_end_tag('thead');
233 }
234  
235 0 ▶sub start_table_body {
Subroutine start_table_body: not called CC=1 SCAR=6.9
Pod undocumented
236 0 my $self = shift;
237  
238 0 $self->_stream_start_tag('tbody');
239 }
240  
241 0 ▶sub end_table_body {
Subroutine end_table_body: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my $self = shift;
243  
244 0 $self->_stream_end_tag('tbody');
245 }
246  
247 0 ▶sub start_table_row {
Subroutine start_table_row: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my $self = shift;
249  
250 0 $self->_stream_start_tag('tr');
251 }
252  
253 0 ▶sub end_table_row {
Subroutine end_table_row: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my $self = shift;
255  
256 0 $self->_stream_end_tag('tr');
257 }
258  
259 {
260 34 my $validator = validation_for(
261 params => [
262 alignment => {
263 type => t('TableCellAlignment'),
264 optional => 1,
265 },
266 colspan => { type => t('PositiveInt') },
267 is_header_cell => { type => t('Bool') },
268 ],
269 named_to_list => 1,
270 );
271  
272 0 ▶ sub start_table_cell {
Subroutine start_table_cell: not called CC=4 SCAR=30.0
Pod undocumented
273 0 my $self = shift;
274 0 my ( $alignment, $colspan, $is_header ) = $validator->(@_);
275  
276 0 ▶ my $tag = $is_header ? 'th' : 'td';
Branch: 1 branch
BranchTrueFalseTotal
$is_header ? : 0 0 0
277  
278 0 my %attr;
279 0 ▶ $attr{style} = "text-align: $alignment"
Branch: 1 branch
BranchTrueFalseTotal
if $alignment 0 0 0
280 if $alignment;
281  
282 0 ▶ $attr{colspan} = $colspan
Branch: 1 branch
BranchTrueFalseTotal
if $colspan != 1 0 0 0
283 if $colspan != 1;
284  
285 0 $self->_stream_start_tag( $tag, \%attr );
286 }
287 }
288  
289 {
290 34 my $validator = validation_for(
291 params => [
292 is_header_cell => { type => t('Bool') },
293 ],
294 named_to_list => 1,
295 );
296  
297 0 ▶ sub end_table_cell {
Subroutine end_table_cell: not called CC=2 SCAR=17.9
Pod undocumented
298 0 my $self = shift;
299 0 my ($is_header) = $validator->(@_);
300  
301 0 ▶ $self->_stream_end_tag( $is_header ? 'th' : 'td' );
Branch: 1 branch
BranchTrueFalseTotal
$is_header ? : 0 0 0
302 }
303 }
304  
305 34 ▶sub start_emphasis {
Subroutine start_emphasis: called CC=1 SCAR=0.0
Pod undocumented
306 3 my $self = shift;
307  
308 3 $self->_stream_start_tag('em');
309 }
310  
311 3 ▶sub end_emphasis {
Subroutine end_emphasis: called CC=1 SCAR=0.0
Pod undocumented
312 3 my $self = shift;
313  
314 3 $self->_stream_end_tag('em');
315 }
316  
317 3 ▶sub start_strong {
Subroutine start_strong: called CC=1 SCAR=0.0
Pod undocumented
318 3 my $self = shift;
319  
320 3 $self->_stream_start_tag('strong');
321 }
322  
323 3 ▶sub end_strong {
Subroutine end_strong: called CC=1 SCAR=0.0
Pod undocumented
324 3 my $self = shift;
325  
326 3 $self->_stream_end_tag('strong');
327 }
328  
329 0 ▶sub start_strikethrough {
Subroutine start_strikethrough: not called CC=1 SCAR=6.9
Pod undocumented
330 0 my $self = shift;
331  
332 0 $self->_stream_start_tag('del');
333 }
334  
335 0 ▶sub end_strikethrough {
Subroutine end_strikethrough: not called CC=1 SCAR=6.9
Pod undocumented
336 0 my $self = shift;
337  
338 0 $self->_stream_end_tag('del');
339 }
340  
341 0 ▶sub start_code {
Subroutine start_code: not called CC=1 SCAR=6.9
Pod undocumented
342 0 my $self = shift;
343  
344 0 $self->_stream_start_tag('code');
345 }
346  
347 0 ▶sub end_code {
Subroutine end_code: not called CC=1 SCAR=6.9
Pod undocumented
348 0 my $self = shift;
349  
350 0 $self->_stream_end_tag('code');
351 }
352  
353 {
354 34 my $validator = validation_for(
355 params => [
356 uri => { type => t('Str') },
357 ],
358 named_to_list => 1,
359 );
360  
361 0 ▶ sub auto_link {
Subroutine auto_link: not called CC=1 SCAR=6.9
Pod undocumented
362 0 my $self = shift;
363 0 my ($uri) = $validator->(@_);
364  
365 0 $self->_stream_start_tag( 'a', { href => $uri } );
366 0 $self->_stream_text($uri);
367 0 $self->_stream_end_tag('a');
368 }
369 }
370  
371 {
372 34 my $validator = validation_for(
373 params => {
374 uri => { type => t('Str') },
375 title => {
376 type => t('Str'),
377 optional => 1,
378 },
379 id => {
380 type => t('Str'),
381 optional => 1,
382 },
383 is_implicit_id => {
384 type => t('Bool'),
385 optional => 1,
386 },
387 }
388 );
389  
390 0 ▶ sub start_link {
Subroutine start_link: not called CC=2 SCAR=17.9
Pod undocumented
391 0 my $self = shift;
392 0 my %p = $validator->(@_);
393  
394 0 delete @p{ grep { !defined $p{$_} } keys %p };
395  
396 $self->_stream_start_tag(
397 'a', {
398 href => $p{uri},
399 0 ▶ exists $p{title} ? ( title => $p{title} ) : (),
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'title'} ? : 0 0 0
400 },
401 );
402 }
403 }
404  
405 34 ▶sub end_link {
Subroutine end_link: not called CC=1 SCAR=2.6
Pod undocumented
406 0 my $self = shift;
407  
408 0 $self->_stream_end_tag('a');
409 }
410  
411 0 ▶sub line_break {
Subroutine line_break: not called CC=1 SCAR=6.9
Pod undocumented
412 0 my $self = shift;
413  
414 0 $self->_stream_start_tag('br');
415 }
416  
417 {
418 34 my $validator = validation_for(
419 params => [
420 text => { type => t('Str') },
421 ],
422 named_to_list => 1,
423 );
424  
425 29 ▶ sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
426 29 my $self = shift;
427 29 my ($text) = $validator->(@_);
428  
429 29 $self->_stream_text($text);
430 }
431 }
432  
433 {
434 34 my $validator = validation_for(
435 params => [
436 tag => { type => t('Str') },
437 attributes => { type => t('HashRef') },
438 ],
439 named_to_list => 1,
440 );
441  
442 1 ▶ sub start_html_tag {
Subroutine start_html_tag: called CC=1 SCAR=0.0
Pod undocumented
443 1 my $self = shift;
444 1 my ( $tag, $attributes ) = $validator->(@_);
445  
446 1 $self->_stream_start_tag( $tag, $attributes );
447 }
448 }
449  
450 {
451 34 my $validator = validation_for(
452 params => [
453 text => { type => t('Str') },
454 ],
455 named_to_list => 1,
456 );
457  
458 0 ▶ sub html_comment_block {
Subroutine html_comment_block: not called CC=1 SCAR=6.9
Pod undocumented
459 0 my $self = shift;
460 0 my ($text) = $validator->(@_);
461  
462 0 $self->_stream_raw( '<!--' . $text . '-->' . "\n" );
463 }
464 }
465  
466 {
467 34 my $validator = validation_for(
468 params => [
469 text => { type => t('Str') },
470 ],
471 named_to_list => 1,
472 );
473  
474 0 ▶ sub html_comment {
Subroutine html_comment: not called CC=1 SCAR=6.9
Pod undocumented
475 0 my $self = shift;
476 0 my ($text) = $validator->(@_);
477  
478 0 $self->_stream_raw( '<!--' . $text . '-->' );
479 }
480 }
481  
482 {
483 34 my $validator = validation_for(
484 params => [
485 tag => { type => t('Str') },
486 attributes => { type => t('HashRef') },
487 ],
488 named_to_list => 1,
489 );
490  
491 0 ▶ sub html_tag {
Subroutine html_tag: not called CC=1 SCAR=6.9
Pod undocumented
492 0 my $self = shift;
493 0 my ( $tag, $attributes ) = $validator->(@_);
494  
495 0 $self->_stream_start_tag( $tag, $attributes );
496 }
497 }
498  
499 {
500 34 my $validator = validation_for(
501 params => [
502 tag => { type => t('Str') },
503 ],
504 named_to_list => 1,
505 );
506  
507 1 ▶ sub end_html_tag {
Subroutine end_html_tag: called CC=1 SCAR=0.0
Pod undocumented
508 1 my $self = shift;
509 1 my ($tag) = $validator->(@_);
510  
511 1 $self->_stream_end_tag($tag);
512 }
513 }
514  
515 {
516 34 my $validator = validation_for(
517 params => [
518 entity => { type => t('Str') },
519 ],
520 named_to_list => 1,
521 );
522  
523 0 ▶ sub html_entity {
Subroutine html_entity: not called CC=1 SCAR=6.9
Pod undocumented
524 0 my $self = shift;
525 0 my ($entity) = $validator->(@_);
526  
527 0 $self->_stream_raw( '&' . $entity . ';' );
528 }
529 }
530  
531 {
532 34 my $validator = validation_for(
533 params => [
534 html => { type => t('Str') },
535 ],
536 named_to_list => 1,
537 );
538  
539 0 ▶ sub html_block {
Subroutine html_block: not called CC=1 SCAR=6.9
Pod undocumented
540 0 my $self = shift;
541 0 my ($html) = $validator->(@_);
542  
543 0 $self->_output->print($html);
544 }
545 }
546  
547 {
548 34 my $validator = validation_for(
549 params => {
550 alt_text => { type => t('Str') },
551 uri => {
552 type => t('Str'),
553 optional => 1,
554 },
555 title => {
556 type => t('Str'),
557 optional => 1,
558 },
559 id => {
560 type => t('Str'),
561 optional => 1,
562 },
563 is_implicit_id => {
564 type => t('Bool'),
565 optional => 1,
566 },
567 },
568 );
569  
570 0 ▶ sub image {
Subroutine image: not called CC=3 SCAR=24.8
Pod undocumented
571 0 my $self = shift;
572 0 my %p = $validator->(@_);
573  
574 0 delete @p{ grep { !defined $p{$_} } keys %p };
575  
576 $self->_stream_start_tag(
577 'img', {
578 src => $p{uri},
579 ( exists $p{alt_text} ? ( alt => $p{alt_text} ) : () ),
580 0 ▶ ( exists $p{title} ? ( title => $p{title} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
exists $p{'alt_text'} ? : 0 0 0
exists $p{'title'} ? : 0 0 0
581 },
582 );
583 }
584 }
585  
586 34 ▶sub horizontal_rule {
Subroutine horizontal_rule: not called CC=1 SCAR=2.6
Pod undocumented
587 0 my $self = shift;
588  
589 0 $self->_stream_start_tag('hr');
590 }
591  
592 32 ▶sub _stream_start_tag {
Subroutine _stream_start_tag: called CC=2 SCAR=6.9
593 32 my $self = shift;
594 32 my $tag = shift;
595 32 my $attr = shift;
596  
597 $self->_output->print(
598 '<'
599 . $tag
600 . (
601 32 ▶ keys %{$attr}
Branch: 1 branch
BranchTrueFalseTotal
keys %{$attr;} ? : 1 31 32
602 ? q{ } . $self->_attributes($attr)
603 : q{}
604 )
605 . '>'
606 );
607 }
608  
609 30 ▶sub _stream_end_tag {
Subroutine _stream_end_tag: called CC=1 SCAR=0.0
610 30 my $self = shift;
611 30 my $tag = shift;
612  
613 30 $self->_output->print( '</' . $tag . '>' );
614 }
615  
616 30 ▶sub _stream_text {
Subroutine _stream_text: called CC=1 SCAR=0.0
617 30 my $self = shift;
618  
619 30 $self->_output->print(
620 encode_entities(
621 shift,
622 $self->_encodable_entities,
623 )
624 );
625 }
626  
627 1 ▶sub _stream_raw {
Subroutine _stream_raw: called CC=1 SCAR=0.0
628 1 my $self = shift;
629  
630 1 $self->_output->print(shift);
631 }
632  
633 1 ▶sub _attributes {
Subroutine _attributes: called CC=1 SCAR=0.0
634 1 my $self = shift;
635 1 my $attr = shift;
636  
637 return join q{ },
638 2 map { $self->_attribute( $_, $attr->{$_} ) }
639 1 sort { $a cmp $b } keys %{$attr};
640 }
641  
642 2 ▶sub _attribute {
Subroutine _attribute: called CC=2 SCAR=7.0
643 2 my $self = shift;
644 2 my $key = shift;
645 2 my $value = shift;
646  
647 2 ▶ return $key unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
648  
649 2 return join '=', $key,
650 q{"} . encode_entities(
651 $value,
652 $self->_encodable_entities,
653 ) . q{"};
654 }
655  
656 34 1;
657  
658 # ABSTRACT: A role for handlers which generate HTML
659  
660 __END__
« Markdent/Role/AnyParser.pm ↑ Summary Markdent/Dialect/GitHub/BlockParser.pm »