lib/Pod/Markdown.pm

filter: Statementstmt 98.1% 420 / 428 Branchbran 92.8% 130 / 140 Conditioncond 78.2% 36 / 46 MC/DCmcdc 57.6% 15 / 26 Subroutinesub 98.0% 99 / 101 Podpod 18.6% 11 / 59 totaltotal 88.8% 711 / 800 CC 95 SCAR 13.3
CC
95
Cov
95%
_prepare_fragment_formats
10.0
end_L
9.3
format_perldoc_url
9.0
CRAP 3.8
↑ Summary script/pod2markdown »
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of Pod-Markdown
4 #
5 # This software is copyright (c) 2011 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 29 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 package Pod::Markdown;
15 # git description: v3.300-3-gb01c18d
16  
17 29 our $AUTHORITY = 'cpan:RWSTAUNER';
18 # ABSTRACT: Convert POD to Markdown
19 29 $Pod::Markdown::VERSION = '3.400';
20 29 ▶use Pod::Simple 3.27 (); # detected_encoding and keep_encoding bug fix
Subroutine BEGIN: called CC=1 SCAR=0.0
21 29 ▶use parent qw(Pod::Simple::Methody);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 29 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 29 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 29 our %URL_PREFIXES = (
26 sco => 'http://search.cpan.org/perldoc?',
27 metacpan => 'https://metacpan.org/pod/',
28 man => 'http://man.he.net/man',
29 );
30 29 $URL_PREFIXES{perldoc} = $URL_PREFIXES{metacpan};
31  
32 29 our $LOCAL_MODULE_RE = qr/^(Local::|\w*?_\w*)/;
33  
34 ## no critic
35 #{
36 29 our $HAS_HTML_ENTITIES;
37  
38 # Stolen from Pod::Simple::XHTML 3.28. {{{
39  
40 29 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
41 29 $HAS_HTML_ENTITIES = eval "require HTML::Entities; 1";
42 }
43  
44 29 my %entities = (
45 q{>} => 'gt',
46 q{<} => 'lt',
47 q{'} => '#39',
48 q{"} => 'quot',
49 q{&} => 'amp',
50 );
51  
52 238 ▶ sub encode_entities {
Subroutine encode_entities: called CC=4 SCAR=13.9
Pod undocumented
53 238 my $self = shift;
54 238 my $ents = $self->html_encode_chars;
55 238 ▶ return HTML::Entities::encode_entities( $_[0], $ents ) if $HAS_HTML_ENTITIES;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::Markdown::HAS_HTML_ENTITIES 189 49 238
56 49 ▶ if (defined $ents) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ents) { } 48 1 49
57 48 $ents =~ s,(?<!\\)([]/]),\\$1,g;
58 48 $ents =~ s,(?<!\\)\\\z,\\\\,;
59 } else {
60 1 $ents = join '', keys %entities;
61 }
62 49 my $str = $_[0];
63 49 ▶ $str =~ s/([$ents])/'&' . ($entities{$1} || sprintf '#x%X', ord $1) . ';'/ge;
Condition66%2/3
$entities{$1} || sprintf('#x%X', ord $1)
l!l&&r!l&&!r
12110
MC/DC0%0/2
$entities{$1} || sprintf('#x%X', ord $1)
$entities{$1} sprintf('#x%X', ord $1)
Truth table66%2/3
A || B
A= $entities{$1}B= sprintf('#x%X', ord $1)
ABresult
1X1
011
000
64 49 return $str;
65 }
66  
67 # }}}
68  
69 # Add a few very common ones for consistency and readability
70 # (in case HTML::Entities isn't available).
71 29 %entities = (
72 # Pod::Markdown has always required 5.8 so unicode_to_native will be available.
73 chr(utf8::unicode_to_native(0xA0)) => 'nbsp',
74 chr(utf8::unicode_to_native(0xA9)) => 'copy',
75 %entities
76 );
77  
78 12 ▶ sub __entity_encode_ord_he {
Subroutine __entity_encode_ord_he: called CC=2 SCAR=8.1
79 12 my $chr = chr $_[0];
80 # Skip the encode_entities() logic and go straight for the substitution
81 # since we already have the char we know we want replaced.
82 # Both the hash and the function are documented as exportable (so should be reliable).
83 12 ▶ return $HTML::Entities::char2entity{ $chr } || HTML::Entities::num_entity( $chr );
Condition33%1/3
$HTML::Entities::char2entity{$chr} || HTML::Entities::num_entity($chr)
l!l&&r!l&&!r
1200
MC/DC0%0/2
$HTML::Entities::char2entity{$chr} || HTML::Entities::num_entity($chr)
$HTML::Entities::char2entity{$chr} HTML::Entities::num_entity($chr)
Truth table33%1/3
A || B
A= $HTML::Entities::char2entity{$chr}B= HTML::Entities::num_entity($chr)
ABresult
1X1
011
000
84 }
85 7 ▶ sub __entity_encode_ord_basic {
Subroutine __entity_encode_ord_basic: called CC=2 SCAR=9.2
86 7 ▶ return '&' . ($entities{chr $_[0]} || sprintf '#x%X', $_[0]) . ';';
Condition33%1/3
$entities{chr $_[0]} || sprintf('#x%X', $_[0])
l!l&&r!l&&!r
070
MC/DC0%0/2
$entities{chr $_[0]} || sprintf('#x%X', $_[0])
$entities{chr $_[0]} sprintf('#x%X', $_[0])
Truth table33%1/3
A || B
A= $entities{chr $_[0]}B= sprintf('#x%X', $_[0])
ABresult
1X1
011
000
87 }
88  
89 # From HTML::Entities 3.69
90 29 my $DEFAULT_ENTITY_CHARS = '^\n\r\t !\#\$%\(-;=?-~';
91  
92 #}
93 ## use critic
94  
95 # Use hash for simple "exists" check in `new` (much more accurate than `->can`).
96 29 my %attributes = map { ($_ => 1) }
97 qw(
98 html_encode_chars
99 match_encoding
100 output_encoding
101 local_module_re
102 local_module_url_prefix
103 man_url_prefix
104 perldoc_url_prefix
105 perldoc_fragment_format
106 markdown_fragment_format
107 include_meta_tags
108 escape_url
109 );
110  
111  
112 316 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
113 316 my $class = shift;
114 316 my %args = @_;
115  
116 316 my $self = $class->SUPER::new();
117 316 $self->preserve_whitespace(1);
118 316 $self->nbsp_for_S(1);
119 316 $self->accept_targets(qw( markdown html ));
120 316 $self->escape_url(1);
121  
122 # Default to the global, but allow it to be overwritten in args.
123 316 $self->local_module_re($LOCAL_MODULE_RE);
124  
125 316 for my $type ( qw( perldoc man ) ){
126 632 my $attr = $type . '_url_prefix';
127 # Initialize to the alias.
128 632 $self->$attr($type);
129 }
130  
131 316 ▶ while( my ($attr, $val) = each %args ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 924 316 1240
132 # NOTE: Checking exists on a private var means we don't allow Pod::Simple
133 # attributes to be set this way. It's not very consistent, but I think
134 # I'm ok with that for now since there probably aren't many Pod::Simple attributes
135 # being changed besides `output_*` which feel like API rather than attributes.
136 # We'll see.
137 # This is currently backward-compatible as we previously just put the attribute
138 # into the private stash so anything unknown was silently ignored.
139 # We could open this up to `$self->can($attr)` in the future if that seems better
140 # but it tricked me when I was testing a misspelled attribute name
141 # which also happened to be a Pod::Simple method.
142  
143 924 ▶ exists $attributes{ $attr } or
Branch: 1 branch
BranchTrueFalseTotal
exists $attributes{$attr} or warn("Unknown argument to $class->new(): '${attr}'"), next 922 2 924
144 # Provide a more descriptive message than "Can't locate object method".
145 warn("Unknown argument to ${class}->new(): '$attr'"), next;
146  
147 # Call setter.
148 922 $self->$attr($val);
149 }
150  
151 # TODO: call from the setters.
152 316 $self->_prepare_fragment_formats;
153  
154 316 ▶ if(defined $self->local_module_url_prefix && $self->local_module_url_prefix eq '' && !$self->escape_url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->local_module_url_prefix and $self->local_module_url_prefix eq "" and not $self->escape_url) 1 315 316
Condition100%6/6
defined $self->local_module_url_prefix and $self->local_module_url_prefix eq "" and not $self->escape_url
!ll&&!rl&&r
31411
defined $self->local_module_url_prefix and $self->local_module_url_prefix eq ""
!ll&&!rl&&r
30772
MC/DC100%3/3
defined $self->local_module_url_prefix and $self->local_module_url_prefix eq "" and not $self->escape_url
defined $self->local_module_url_prefix $self->local_module_url_prefix eq "" not $self->escape_url
Truth table100%4/4
A and B and not(C)
A= defined $self->local_module_url_prefixB= $self->local_module_url_prefix eq ""C= not $self->escape_url
ABCresult
0XX0
10X0
1100
1111
155 1 warn("turning escape_url with an empty local_module_url_prefix is not recommended as relative URLs could be confused for IPv6 addresses");
156 }
157  
158 316 return $self;
159 }
160  
161 29 for my $type ( qw( local_module perldoc man ) ){
162 87 my $attr = $type . '_url_prefix';
163 29 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
164 1369 ▶ *$attr = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
165 1369 my $self = shift;
166 1369 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 875 494 1369
167 875 ▶ $self->{$attr} = $URL_PREFIXES{ $_[0] } || $_[0];
Condition100%3/3
$Pod::Markdown::URL_PREFIXES{$_[0]} || $_[0]
l!l&&r!l&&!r
6392342
MC/DC100%2/2
$Pod::Markdown::URL_PREFIXES{$_[0]} || $_[0]
$Pod::Markdown::URL_PREFIXES{$_[0]} $_[0]
Truth table100%3/3
A || B
A= $Pod::Markdown::URL_PREFIXES{$_[0]}B= $_[0]
ABresult
1X1
011
000
168 }
169 else {
170 494 return $self->{$attr};
171 }
172 }
173 87 }
174  
175 ## Attribute accessors ##
176  
177  
178 310 ▶sub html_encode_chars {
Subroutine html_encode_chars: called CC=5 SCAR=16.5
Pod documented
179 310 my $self = shift;
180 310 my $stash = $self->_private;
181  
182 # Setter.
183 310 ▶ if( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 72 238 310
184 # If false ('', 0, undef), disable.
185 72 ▶ if( !$_[0] ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]) { } 0 72 72
186 0 delete $stash->{html_encode_chars};
187 0 $stash->{encode_amp} = 1;
188 0 $stash->{encode_lt} = 1;
189 }
190 else {
191 # Special case boolean '1' to mean "all".
192 # If we have HTML::Entities, undef will use the default.
193 # Without it, we need to specify so that we use the same list (for consistency).
194 72 ▶ $stash->{html_encode_chars} = $_[0] eq '1' ? ($HAS_HTML_ENTITIES ? undef : $DEFAULT_ENTITY_CHARS) : $_[0];
Branch: 2 branches
BranchTrueFalseTotal
$_[0] eq '1' ? : 24 48 72
$Pod::Markdown::HAS_HTML_ENTITIES ? : 22 2 24
195  
196 # If [char] doesn't get encoded, we need to do it ourselves.
197 72 $stash->{encode_amp} = ($self->encode_entities('&') eq '&');
198 72 $stash->{encode_lt} = ($self->encode_entities('<') eq '<');
199 }
200 72 return;
201 }
202  
203 # Getter.
204 238 return $stash->{html_encode_chars};
205 }
206  
207  
208 # I prefer ro-accessors (immutability!) but it can be confusing
209 # to not support the same API as other Pod::Simple classes.
210  
211 # NOTE: Pod::Simple::_accessorize is not a documented public API.
212 # Skip any that have already been defined.
213 29 __PACKAGE__->_accessorize(grep { !__PACKAGE__->can($_) } keys %attributes);
214  
215 316 ▶sub _prepare_fragment_formats {
Subroutine _prepare_fragment_formats: called CC=10 SCAR=23.0
216 316 my ($self) = @_;
217  
218 316 foreach my $attr ( keys %attributes ){
219 3476 ▶ next unless $attr =~ /^(\w+)_fragment_format/;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /^(\w+)_fragment_format/ 2844 632 3476
220 632 my $type = $1;
221 632 my $format = $self->$attr;
222  
223 # If one was provided.
224 632 ▶ if( $format ){
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 510 122 632
225 # If the attribute is a coderef just use it.
226 510 ▶ next if ref($format) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "CODE" 464 46 510
227 }
228 # Else determine a default.
229 else {
230 122 ▶ if( $type eq 'perldoc' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'perldoc') { } 60 62 122
231 # Choose a default that matches the destination url.
232 60 my $target = $self->perldoc_url_prefix;
233 60 foreach my $alias ( qw( metacpan sco ) ){
234 120 ▶ if( $target eq $URL_PREFIXES{ $alias } ){
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq $Pod::Markdown::URL_PREFIXES{$alias}) 51 69 120
235 51 $format = $alias;
236 }
237 }
238 # This seems like a reasonable fallback.
239 60 ▶ $format ||= 'pod_simple_xhtml';
Condition100%2/2
$format ||= "pod_simple_xhtml"
l!l
519
MC/DC100%1/1
$format ||= "pod_simple_xhtml"
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
240 }
241 else {
242 62 $format = $type;
243 }
244 }
245  
246 # The short name should become a method name with the prefix prepended.
247 168 my $prefix = 'format_fragment_';
248 168 $format =~ s/^$prefix//;
249 168 ▶ die "Unknown fragment format '$format'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($prefix . $format) 0 168 168
250 unless $self->can($prefix . $format);
251  
252 # Save it.
253 168 $self->$attr($format);
254 }
255  
256 316 return;
257 }
258  
259 ## Backward compatible API ##
260  
261 # For backward compatibility (previously based on Pod::Parser):
262 # While Pod::Simple provides a parse_from_file() method
263 # it's primarily for Pod::Parser compatibility.
264 # When called without an output handle it will print to STDOUT
265 # but the old Pod::Markdown never printed to a handle
266 # so we don't want to start now.
267 10 ▶sub parse_from_file {
Subroutine parse_from_file: called CC=1 SCAR=0.0
Pod documented
268 10 my ($self, $file) = @_;
269  
270 # TODO: Check that all dependent cpan modules use the Pod::Simple API
271 # then add a deprecation warning here to avoid confusion.
272  
273 10 $self->output_string(\($self->{_as_markdown_}));
274 10 $self->parse_file($file);
275 }
276  
277 # Likewise, though Pod::Simple doesn't define this method at all.
278 9 ▶sub parse_from_filehandle { shift->parse_from_file(@_) }
Subroutine parse_from_filehandle: called CC=1 SCAR=0.0
Pod undocumented
279  
280  
281 ## Document state ##
282  
283 10167 ▶sub _private {
Subroutine _private: called CC=2 SCAR=6.9
284 10167 my ($self) = @_;
285 $self->{_Pod_Markdown_} ||= {
286 10167 ▶ indent => 0,
Condition100%2/2
$self->{"_Pod_Markdown_"} ||= {"indent", 0, "stacks", [], "states", [{}], "link", [], "encode_amp", 1, "encode_lt", 1}
l!l
9864303
MC/DC100%1/1
$self->{"_Pod_Markdown_"} ||= {"indent", 0, "stacks", [], "states", [{}], "link", [], "encode_amp", 1, "encode_lt", 1}
$self->{"_Pod_Markdown_"}
Truth table100%2/2
A
A= $self->{"_Pod_Markdown_"}
Aresult
11
00
287 stacks => [],
288 states => [{}],
289 link => [],
290 encode_amp => 1,
291 encode_lt => 1,
292 };
293 }
294  
295 67 ▶sub _increase_indent {
Subroutine _increase_indent: called CC=2 SCAR=7.6
296 67 ▶ ++$_[0]->_private->{indent} >= 1
Branch: 1 branch
BranchTrueFalseTotal
++$_[0]->_private->{"indent"} >= 1 or die "Invalid state: indent < 0" 67 0 67
297 or die 'Invalid state: indent < 0';
298 }
299 67 ▶sub _decrease_indent {
Subroutine _decrease_indent: called CC=2 SCAR=7.6
300 67 ▶ --$_[0]->_private->{indent} >= 0
Branch: 1 branch
BranchTrueFalseTotal
--$_[0]->_private->{"indent"} >= 0 or die "Invalid state: indent < 0" 67 0 67
301 or die 'Invalid state: indent < 0';
302 }
303  
304 1041 ▶sub _new_stack {
Subroutine _new_stack: called CC=1 SCAR=0.0
305 1041 push @{ $_[0]->_private->{stacks} }, [];
306 1041 push @{ $_[0]->_private->{states} }, {};
307 }
308  
309 18 ▶sub _last_string {
Subroutine _last_string: called CC=1 SCAR=0.0
310 18 $_[0]->_private->{stacks}->[-1][-1];
311 }
312  
313 739 ▶sub _pop_stack_text {
Subroutine _pop_stack_text: called CC=1 SCAR=0.0
314 739 $_[0]->_private->{last_state} = pop @{ $_[0]->_private->{states} };
315 739 join '', @{ pop @{ $_[0]->_private->{stacks} } };
316 }
317  
318 454 ▶sub _stack_state {
Subroutine _stack_state: called CC=1 SCAR=0.0
319 454 $_[0]->_private->{states}->[-1];
320 }
321  
322 1400 ▶sub _save {
Subroutine _save: called CC=1 SCAR=0.0
323 1400 my ($self, $text) = @_;
324 1400 push @{ $self->_private->{stacks}->[-1] }, $text;
325 # return $text; # DEBUG
326 }
327  
328 517 ▶sub _save_line {
Subroutine _save_line: called CC=1 SCAR=0.0
329 517 my ($self, $text) = @_;
330  
331 517 $text = $self->_process_escapes($text);
332  
333 517 $self->_save($text . $/);
334 }
335  
336 # For paragraphs, etc.
337 452 ▶sub _save_block {
Subroutine _save_block: called CC=1 SCAR=0.0
338 452 my ($self, $text) = @_;
339  
340 452 $self->_stack_state->{blocks}++;
341  
342 452 $self->_save_line($self->_indent($text) . $/);
343 }
344  
345 ## Formatting ##
346  
347 344 ▶sub _chomp_all {
Subroutine _chomp_all: called CC=2 SCAR=6.9
348 344 my ($self, $text) = @_;
349 344 ▶ 1 while chomp $text;
Branch: 1 branch
BranchTrueFalseTotal
chomp $text and do { '???' } 673 344 1017
350 344 return $text;
351 }
352  
353 495 ▶sub _indent {
Subroutine _indent: called CC=2 SCAR=6.9
354 495 my ($self, $text) = @_;
355 495 my $level = $self->_private->{indent};
356  
357 495 ▶ if( $level ){
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 34 461 495
358 34 my $indent = ' ' x ($level * 4);
359  
360 # Capture text on the line so that we don't indent blank lines (/^\x20{4}$/).
361 34 $text =~ s/^(.+)/$indent$1/mg;
362 }
363  
364 495 return $text;
365 }
366  
367 # as_markdown() exists solely for backward compatibility
368 # and requires having called parse_from_file() to be useful.
369  
370  
371 10 ▶sub as_markdown {
Subroutine as_markdown: called CC=3 SCAR=11.0
Pod undocumented
372 10 my ($parser, %args) = @_;
373 10 my @header;
374 # Don't add meta tags again if we've already done it.
375 10 ▶ if( $args{with_meta} && !$parser->include_meta_tags ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"with_meta"} and not $parser->include_meta_tags) 3 7 10
Condition100%3/3
$args{"with_meta"} and not $parser->include_meta_tags
!ll&&!rl&&r
433
MC/DC100%2/2
$args{"with_meta"} and not $parser->include_meta_tags
$args{"with_meta"} not $parser->include_meta_tags
Truth table100%3/3
A and not(B)
A= $args{"with_meta"}B= not $parser->include_meta_tags
ABresult
0X0
100
111
376 3 @header = $parser->_build_markdown_head;
377 }
378 10 return join("\n" x 2, @header, $parser->{_as_markdown_});
379 }
380  
381 9 ▶sub _build_markdown_head {
Subroutine _build_markdown_head: called CC=1 SCAR=0.0
382 9 my $parser = shift;
383 9 my $data = $parser->_private;
384 return join "\n",
385 12 map { qq![[meta \l$_="$data->{$_}"]]! }
386 9 grep { defined $data->{$_} }
387 qw( Title Author );
388 }
389  
390 ## Escaping ##
391  
392 # http://daringfireball.net/projects/markdown/syntax#backslash
393 # Markdown provides backslash escapes for the following characters:
394 #
395 # \ backslash
396 # ` backtick
397 # * asterisk
398 # _ underscore
399 # {} curly braces
400 # [] square brackets
401 # () parentheses
402 # # hash mark
403 # + plus sign
404 # - minus sign (hyphen)
405 # . dot
406 # ! exclamation mark
407  
408 # However some of those only need to be escaped in certain places:
409 # * Backslashes *do* need to be escaped or they may be swallowed by markdown.
410 # * Word-surrounding characters (/[`*_]/) *do* need to be escaped mid-word
411 # because the markdown spec explicitly allows mid-word em*pha*sis.
412 # * I don't actually see anything that curly braces are used for.
413 # * Escaping square brackets is enough to avoid accidentally
414 # creating links and images (so we don't need to escape plain parentheses
415 # or exclamation points as that would generate a lot of unnecesary noise).
416 # Parentheses will be escaped in urls (&end_L) to avoid premature termination.
417 # * We don't need a backslash for every hash mark or every hyphen found mid-word,
418 # just the ones that start a line (likewise for plus and dot).
419 # (Those will all be handled by _escape_paragraph_markdown).
420  
421  
422 # Backslash escape markdown characters to avoid having them interpreted.
423 483 ▶sub _escape_inline_markdown {
Subroutine _escape_inline_markdown: called CC=1 SCAR=0.0
424 483 local $_ = $_[1];
425  
426 # s/([\\`*_{}\[\]()#+-.!])/\\$1/g; # See comments above.
427 483 s/([\\`*_\[\]])/\\$1/g;
428  
429 483 return $_;
430 }
431  
432 # Escape markdown characters that would be interpreted
433 # at the start of a line.
434 354 ▶sub _escape_paragraph_markdown {
Subroutine _escape_paragraph_markdown: called CC=1 SCAR=0.0
435 354 local $_ = $_[1];
436  
437 # Escape headings, horizontal rules, (unordered) lists, and blockquotes.
438 354 s/^([-+#>])/\\$1/mg;
439  
440 # Markdown doesn't support backslash escapes for equal signs
441 # even though they can be used to underline a header.
442 # So use html to escape them to avoid having them interpreted.
443 354 s/^([=])/sprintf '&#x%x;', ord($1)/mge;
444  
445 # Escape the dots that would wrongfully create numbered lists.
446 354 s/^( (?:>\s+)? \d+ ) (\.\x20)/$1\\$2/xgm;
447  
448 354 return $_;
449 }
450  
451  
452 # Additionally Markdown allows inline html so we need to escape things that look like it.
453 # While _some_ Markdown processors handle backslash-escaped html,
454 # [Daring Fireball](http://daringfireball.net/projects/markdown/syntax) states distinctly:
455 # > In HTML, there are two characters that demand special treatment: < and &...
456 # > If you want to use them as literal characters, you must escape them as entities, e.g. &lt;, and &amp;.
457  
458 # It goes on to say:
459 # > Markdown allows you to use these characters naturally,
460 # > taking care of all the necessary escaping for you.
461 # > If you use an ampersand as part of an HTML entity,
462 # > it remains unchanged; otherwise it will be translated into &amp;.
463 # > Similarly, because Markdown supports inline HTML,
464 # > if you use angle brackets as delimiters for HTML tags, Markdown will treat them as such.
465  
466 # In order to only encode the occurrences that require it (something that
467 # could be interpreted as an entity) we escape them all so that we can do the
468 # suffix test later after the string is complete (since we don't know what
469 # strings might come after this one).
470  
471 my %_escape =
472 map {
473 29 my ($k, $v) = split /:/;
474 # Put the "code" marker before the char instead of after so that it doesn't
475 # get confused as the $2 (which is what requires us to entity-encode it).
476 # ( "XsX", "XcsX", "X(c?)sX" )
477 58 my ($s, $code, $re) = map { "\0$_$v\0" } '', map { ($_, '('.$_.'?)') } 'c';
478  
479 (
480 58 $k => $s,
481 $k.'_code' => $code,
482 $k.'_re' => qr/$re/,
483 )
484 }
485 qw( amp:& lt:< );
486  
487 # Make the values of this private var available to the tests.
488 1 ▶sub __escape_sequences { %_escape }
Subroutine __escape_sequences: called CC=1 SCAR=0.0
489  
490  
491 # HTML-entity encode any characters configured by the user.
492 # If that doesn't include [&<] then we escape those chars so we can decide
493 # later if we will entity-encode them or put them back verbatim.
494 483 ▶sub _encode_or_escape_entities {
Subroutine _encode_or_escape_entities: called CC=5 SCAR=16.1
495 483 my $self = $_[0];
496 483 my $stash = $self->_private;
497 483 local $_ = $_[1];
498  
499 483 ▶ if( $stash->{encode_amp} ){
Branch: 2 branches
BranchTrueFalseTotal
if ($stash->{'encode_amp'}) { } 461 22 483
elsif (exists $stash->{'html_encode_chars'}) { } 22 0 22
500 461 ▶ if( exists($stash->{html_encode_chars}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $stash->{'html_encode_chars'}) { } 38 423 461
501 # Escape all amps for later processing.
502 # Pass intermediate strings to entity encoder so that it doesn't
503 # process any of the characters of our escape sequences.
504 # Use -1 to get "as many fields as possible" so that we keep leading and
505 # trailing (possibly empty) fields.
506 38 $_ = join $_escape{amp}, map { $self->encode_entities($_) } split /&/, $_, -1;
507 }
508 else {
509 423 s/&/$_escape{amp}/g;
510 }
511 }
512 elsif( exists($stash->{html_encode_chars}) ){
513 22 $_ = $self->encode_entities($_);
514 }
515  
516 s/</$_escape{lt}/g
517 483 ▶ if $stash->{encode_lt};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"encode_lt"} 461 22 483
518  
519 483 return $_;
520 }
521  
522 # From Markdown.pl version 1.0.1 line 1172 (_DoAutoLinks).
523 29 my $EMAIL_MARKER = qr{
524 # < # Opening token is in parent regexp.
525 (?:mailto:)?
526 (
527 [-.\w]+
528 \@
529 [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
530 )
531 >
532 }x;
533  
534 # Process any escapes we put in the text earlier,
535 # now that the text is complete (end of a block).
536 517 ▶sub _process_escapes {
Subroutine _process_escapes: called CC=7 SCAR=19.5
537 517 my $self = $_[0];
538 517 my $stash = $self->_private;
539 517 local $_ = $_[1];
540  
541 # The patterns below are taken from Markdown.pl 1.0.1 _EncodeAmpsAndAngles().
542 # In this case we only want to encode the ones that Markdown won't.
543 # This is overkill but produces nicer looking text (less escaped entities).
544 # If it proves insufficent then we'll just encode them all.
545  
546 # $1: If the escape was in a code sequence, simply replace the original.
547 # $2: If the unescaped value would be followed by characters
548 # that could be interpreted as html, entity-encode it.
549 # else: The character is safe to leave bare.
550  
551 # Neither currently allows $2 to contain '0' so bool tests are sufficient.
552  
553 517 ▶ if( $stash->{encode_amp} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{"encode_amp"}) 482 35 517
554 # Encode & if succeeded by chars that look like an html entity.
555 482 s,$_escape{amp_re}((?:#?[xX]?(?:[0-9a-fA-F]+|\w+);)?),
556 90 ▶ $1 ? '&'.$2 : $2 ? '&amp;'.$2 : '&',egos;
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 44 46 90
$2 ? : 17 29 46
557 }
558  
559 517 ▶ if( $stash->{encode_lt} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{"encode_lt"}) 482 35 517
560 # Encode < if succeeded by chars that look like an html tag.
561 # Leave email addresses (<foo@bar.com>) for Markdown to process.
562 482 s,$_escape{lt_re}((?=$EMAIL_MARKER)|(?:[a-z/?\$!])?),
563 72 ▶ $1 ? '<'.$2 : $2 ? '&lt;'.$2 : '<',egos;
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 32 40 72
$2 ? : 16 24 40
564 }
565  
566 517 return $_;
567 }
568  
569  
570 ## Parsing ##
571  
572 566 ▶sub handle_text {
Subroutine handle_text: called CC=4 SCAR=13.9
Pod undocumented
573 566 my $self = $_[0];
574 566 my $stash = $self->_private;
575 566 local $_ = $_[1];
576  
577 # Unless we're in a code span, verbatim block, or formatted region.
578 566 ▶ unless( $stash->{no_escape} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{'no_escape'}) { } 83 483 566
579  
580 # We could, in theory, alter what gets escaped according to context
581 # (for example, escape square brackets (but not parens) inside link text).
582 # The markdown produced might look slightly nicer but either way you're
583 # at the whim of the markdown processor to interpret things correctly.
584 # For now just escape everything.
585  
586 # Don't let literal characters be interpreted as markdown.
587 483 $_ = $self->_escape_inline_markdown($_);
588  
589 # Entity-encode (or escape for later processing) necessary/desired chars.
590 483 $_ = $self->_encode_or_escape_entities($_);
591  
592 }
593 # If this _is_ a code section, do limited/specific handling.
594 else {
595 # Always escaping these chars ensures that we won't mangle the text
596 # in the unlikely event that a sequence matching our escape occurred in the
597 # input stream (since we're going to escape it and then unescape it).
598 83 ▶ s/&/$_escape{amp_code}/gos if $stash->{encode_amp};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"encode_amp"} 73 10 83
599 83 ▶ s/</$_escape{lt_code}/gos if $stash->{encode_lt};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"encode_lt"} 73 10 83
600 }
601  
602 566 $self->_save($_);
603 }
604  
605 302 ▶sub start_Document {
Subroutine start_Document: called CC=1 SCAR=0.0
Pod undocumented
606 302 my ($self) = @_;
607 302 $self->_new_stack;
608 }
609  
610 302 ▶sub end_Document {
Subroutine end_Document: called CC=6 SCAR=17.9
Pod undocumented
611 302 my ($self) = @_;
612 302 $self->_check_search_header;
613 302 my $end = pop @{ $self->_private->{stacks} };
614  
615 302 ▶ @{ $self->_private->{stacks} } == 0
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_private->{"stacks"};} == 0 or die "Document ended with stacks remaining" 302 0 302
616 or die 'Document ended with stacks remaining';
617  
618 302 my @doc = $self->_chomp_all(join('', @$end)) . $/;
619  
620 302 ▶ if( $self->include_meta_tags ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->include_meta_tags) 6 296 302
621 6 unshift @doc, $self->_build_markdown_head, ($/ x 2);
622 }
623  
624 302 ▶ if( my $encoding = $self->_get_output_encoding ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $self->_get_output_encoding) { } 66 236 302
625 # Do the check outside the loop(s) for efficiency.
626 66 ▶ my $ents = $HAS_HTML_ENTITIES ? \&__entity_encode_ord_he : \&__entity_encode_ord_basic;
Branch: 1 branch
BranchTrueFalseTotal
$Pod::Markdown::HAS_HTML_ENTITIES ? : 47 19 66
627 # Iterate indices to avoid copying large strings.
628 66 for my $i ( 0 .. $#doc ){
629 66 print { $self->{output_fh} } Encode::encode($encoding, $doc[$i], $ents);
630 }
631 }
632 else {
633 236 print { $self->{output_fh} } @doc;
634 }
635 }
636  
637 302 ▶sub _get_output_encoding {
Subroutine _get_output_encoding: called CC=4 SCAR=13.9
638 302 my ($self) = @_;
639  
640 # If 'match_encoding' is set we need to return an encoding.
641 # If pod has no =encoding, Pod::Simple will guess if it sees a high-bit char.
642 # If there are no high-bit chars, encoding is undef.
643 # Use detected_encoding() rather than encoding() because if Pod::Simple
644 # can't use whatever encoding was specified, we probably can't either.
645 # Fallback to 'o_e' if no match is found. This gives the user the choice,
646 # since otherwise there would be no reason to specify 'o_e' *and* 'm_e'.
647 # Fallback to UTF-8 since it is a reasonable default these days.
648  
649 302 ▶ return $self->detected_encoding || $self->output_encoding || 'UTF-8'
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_encoding 14 288 302
Condition100%5/5
$self->detected_encoding || $self->output_encoding || 'UTF-8'
l!l
122
$self->detected_encoding or $self->output_encoding
l!l&&r!l&&!r
842
MC/DC100%2/2
$self->detected_encoding || $self->output_encoding || 'UTF-8'
$self->detected_encoding $self->output_encoding
Truth table100%3/3
A or B
A= $self->detected_encodingB= $self->output_encoding
ABresult
1X1
011
000
650 if $self->match_encoding;
651  
652 # If output encoding wasn't specified, return false.
653 288 return $self->output_encoding;
654 }
655  
656 ## Blocks ##
657  
658 17 ▶sub start_Verbatim {
Subroutine start_Verbatim: called CC=1 SCAR=0.0
Pod undocumented
659 17 my ($self) = @_;
660 17 $self->_new_stack;
661 17 $self->_private->{no_escape} = 1;
662 }
663  
664 17 ▶sub end_Verbatim {
Subroutine end_Verbatim: called CC=1 SCAR=0.0
Pod undocumented
665 17 my ($self) = @_;
666  
667 17 my $text = $self->_pop_stack_text;
668  
669 17 $text = $self->_indent_verbatim($text);
670  
671 17 $self->_private->{no_escape} = 0;
672  
673 # Verbatim blocks do not generate a separate "Para" event.
674 17 $self->_save_block($text);
675 }
676  
677 17 ▶sub _indent_verbatim {
Subroutine _indent_verbatim: called CC=6 SCAR=17.9
678 17 my ($self, $paragraph) = @_;
679  
680 # NOTE: Pod::Simple expands the tabs for us (as suggested by perlpodspec).
681 # Pod::Simple also has a 'strip_verbatim_indent' attribute
682 # but it doesn't sound like it gains us anything over this method.
683  
684 # POD verbatim can start with any number of spaces (or tabs)
685 # markdown should be 4 spaces (or a tab)
686 # so indent any paragraphs so that all lines start with at least 4 spaces
687 17 my @lines = split /\n/, $paragraph;
688 17 my $indent = ' ' x 4;
689 17 foreach my $line ( @lines ){
690 32 ▶ next unless $line =~ m/^( +)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^( +)/ 1 31 32
691 # find the smallest indentation
692 31 ▶ $indent = $1 if length($1) < length($indent);
Branch: 1 branch
BranchTrueFalseTotal
if length $1 < length $indent 12 19 31
693 }
694 17 ▶ if( (my $smallest = length($indent)) < 4 ){
Branch: 1 branch
BranchTrueFalseTotal
if ((my $smallest = length $indent) < 4) 11 6 17
695 # invert to get what needs to be prepended
696 11 $indent = ' ' x (4 - $smallest);
697  
698 # Prepend indent to each line.
699 # We could check /\S/ to only indent non-blank lines,
700 # but it's backward compatible to respect the whitespace.
701 # Additionally, both pod and markdown say they ignore blank lines
702 # so it shouldn't hurt to leave them in.
703 11 ▶ $paragraph = join "\n", map { length($_) ? $indent . $_ : '' } @lines;
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 22 1 23
704 }
705  
706 17 return $paragraph;
707 }
708  
709 354 ▶sub start_Para {
Subroutine start_Para: called CC=1 SCAR=0.0
Pod undocumented
710 354 $_[0]->_new_stack;
711 }
712  
713 354 ▶sub end_Para {
Subroutine end_Para: called CC=1 SCAR=0.0
Pod undocumented
714 354 my ($self) = @_;
715 354 my $text = $self->_pop_stack_text;
716  
717 354 $text = $self->_escape_paragraph_markdown($text);
718  
719 354 $self->_save_block($text);
720 }
721  
722  
723 ## Headings ##
724  
725 51 ▶sub start_head1 { $_[0]->_start_head(1) }
Subroutine start_head1: called CC=1 SCAR=0.0
Pod undocumented
726 51 ▶sub end_head1 { $_[0]->_end_head(1) }
Subroutine end_head1: called CC=1 SCAR=0.0
Pod undocumented
727 11 ▶sub start_head2 { $_[0]->_start_head(2) }
Subroutine start_head2: called CC=1 SCAR=0.0
Pod undocumented
728 11 ▶sub end_head2 { $_[0]->_end_head(2) }
Subroutine end_head2: called CC=1 SCAR=0.0
Pod undocumented
729 1 ▶sub start_head3 { $_[0]->_start_head(3) }
Subroutine start_head3: called CC=1 SCAR=0.0
Pod undocumented
730 1 ▶sub end_head3 { $_[0]->_end_head(3) }
Subroutine end_head3: called CC=1 SCAR=0.0
Pod undocumented
731 0 ▶sub start_head4 { $_[0]->_start_head(4) }
Subroutine start_head4: not called CC=1 SCAR=6.9
Pod undocumented
732 0 ▶sub end_head4 { $_[0]->_end_head(4) }
Subroutine end_head4: not called CC=1 SCAR=6.9
Pod undocumented
733  
734 365 ▶sub _check_search_header {
Subroutine _check_search_header: called CC=3 SCAR=11.0
735 365 my ($self) = @_;
736 # Save the text since the last heading if we want it for metadata.
737 365 ▶ if( my $last = $self->_private->{search_header} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->_private->{"search_header"}) 18 347 365
738 18 for( $self->_private->{$last} = $self->_last_string ){
739 18 s/\A\s+//;
740 18 s/\s+\z//;
741 }
742 }
743 }
744 63 ▶sub _start_head {
Subroutine _start_head: called CC=1 SCAR=0.0
745 63 my ($self) = @_;
746 63 $self->_check_search_header;
747 63 $self->_new_stack;
748 }
749  
750 63 ▶sub _end_head {
Subroutine _end_head: called CC=3 SCAR=11.0
751 63 my ($self, $num) = @_;
752 63 my $h = '#' x $num;
753  
754 63 my $text = $self->_pop_stack_text;
755 $self->_private->{search_header} =
756 63 ▶ $text =~ /NAME/ ? 'Title'
Branch: 2 branches
BranchTrueFalseTotal
$text =~ /NAME/ ? : 9 54 63
$text =~ /AUTHOR/ ? : 9 45 54
757 : $text =~ /AUTHOR/ ? 'Author'
758 : undef;
759  
760 # TODO: option for $h suffix
761 # TODO: put a name="" if $self->{embed_anchor_tags}; ?
762 # https://rt.cpan.org/Ticket/Display.html?id=57776
763 63 $self->_save_block(join(' ', $h, $text));
764 }
765  
766 ## Lists ##
767  
768 # With Pod::Simple->parse_empty_lists(1) there could be an over_empty event,
769 # but what would you do with that?
770  
771 22 ▶sub _start_list {
Subroutine _start_list: called CC=1 SCAR=0.0
772 22 my ($self) = @_;
773 22 $self->_new_stack;
774  
775 # Nest again b/c start_item will pop this to look for preceding content.
776 22 $self->_increase_indent;
777 22 $self->_new_stack;
778 }
779  
780 22 ▶sub _end_list {
Subroutine _end_list: called CC=1 SCAR=0.0
781 22 my ($self) = @_;
782 22 $self->_handle_between_item_content;
783  
784 # Finish the list.
785  
786 # All the child elements should be blocks,
787 # but don't end with a double newline.
788 22 my $text = $self->_chomp_all($self->_pop_stack_text);
789  
790 22 $_[0]->_save_line($text . $/);
791 }
792  
793 65 ▶sub _handle_between_item_content {
Subroutine _handle_between_item_content: called CC=3 SCAR=11.0
794 65 my ($self) = @_;
795  
796 # This might be empty (if the list item had no additional content).
797 65 ▶ if( my $text = $self->_pop_stack_text ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $text = $self->_pop_stack_text) 21 44 65
798 # Else it's a sub-document.
799 # If there are blocks we need to separate with blank lines.
800 21 ▶ if( $self->_private->{last_state}->{blocks} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_private->{'last_state'}{'blocks'}) { } 16 5 21
801 16 $text = $/ . $text;
802 }
803 # If not, we can condense the text.
804 # In this module's history there was a patch contributed to specifically
805 # produce "huddled" lists so we'll try to maintain that functionality.
806 else {
807 5 $text = $self->_chomp_all($text) . $/;
808 }
809 21 $self->_save($text)
810 }
811  
812 65 $self->_decrease_indent;
813 }
814  
815 43 ▶sub _start_item {
Subroutine _start_item: called CC=1 SCAR=0.0
816 43 my ($self) = @_;
817 43 $self->_handle_between_item_content;
818 43 $self->_new_stack;
819 }
820  
821 43 ▶sub _end_item {
Subroutine _end_item: called CC=3 SCAR=11.0
822 43 my ($self, $marker) = @_;
823 43 my $text = $self->_pop_stack_text;
824 43 ▶ $self->_save_line($self->_indent($marker .
Branch: 1 branch
BranchTrueFalseTotal
defined $text && length $text ? : 42 1 43
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
0142
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
825 # Add a space only if there is text after the marker.
826 (defined($text) && length($text) ? ' ' . $text : '')
827 ));
828  
829 # Store any possible contents in a new stack (like a sub-document).
830 43 $self->_increase_indent;
831 43 $self->_new_stack;
832 }
833  
834 7 ▶sub start_over_bullet { $_[0]->_start_list }
Subroutine start_over_bullet: called CC=1 SCAR=0.0
Pod undocumented
835 7 ▶sub end_over_bullet { $_[0]->_end_list }
Subroutine end_over_bullet: called CC=1 SCAR=0.0
Pod undocumented
836  
837 18 ▶sub start_item_bullet { $_[0]->_start_item }
Subroutine start_item_bullet: called CC=1 SCAR=0.0
Pod undocumented
838 18 ▶sub end_item_bullet { $_[0]->_end_item('-') }
Subroutine end_item_bullet: called CC=1 SCAR=0.0
Pod undocumented
839  
840 9 ▶sub start_over_number { $_[0]->_start_list }
Subroutine start_over_number: called CC=1 SCAR=0.0
Pod undocumented
841 9 ▶sub end_over_number { $_[0]->_end_list }
Subroutine end_over_number: called CC=1 SCAR=0.0
Pod undocumented
842  
843 18 ▶sub start_item_number {
Subroutine start_item_number: called CC=1 SCAR=0.0
Pod undocumented
844 18 $_[0]->_start_item;
845 # It seems like this should be a stack,
846 # but from testing it appears that the corresponding 'end' event
847 # comes right after the text (it doesn't surround any embedded content).
848 # See t/nested.t which shows start-item, text, end-item, para, start-item....
849 18 $_[0]->_private->{item_number} = $_[1]->{number};
850 }
851  
852 18 ▶sub end_item_number {
Subroutine end_item_number: called CC=1 SCAR=0.0
Pod undocumented
853 18 my ($self) = @_;
854 18 $self->_end_item($self->_private->{item_number} . '.');
855 }
856  
857 # Markdown doesn't support definition lists
858 # so do regular (unordered) lists with indented paragraphs.
859 6 ▶sub start_over_text { $_[0]->_start_list }
Subroutine start_over_text: called CC=1 SCAR=0.0
Pod undocumented
860 6 ▶sub end_over_text { $_[0]->_end_list }
Subroutine end_over_text: called CC=1 SCAR=0.0
Pod undocumented
861  
862 7 ▶sub start_item_text { $_[0]->_start_item }
Subroutine start_item_text: called CC=1 SCAR=0.0
Pod undocumented
863 7 ▶sub end_item_text { $_[0]->_end_item('-')}
Subroutine end_item_text: called CC=1 SCAR=0.0
Pod undocumented
864  
865  
866 # perlpodspec equates an over/back region with no items to a blockquote.
867 3 ▶sub start_over_block {
Subroutine start_over_block: called CC=1 SCAR=0.0
Pod undocumented
868 # NOTE: We don't actually need to indent for a blockquote.
869 3 $_[0]->_new_stack;
870 }
871  
872 3 ▶sub end_over_block {
Subroutine end_over_block: called CC=2 SCAR=6.9
Pod undocumented
873 3 my ($self) = @_;
874  
875 # Chomp first to avoid prefixing a blank line with a `>`.
876 3 my $text = $self->_chomp_all($self->_pop_stack_text);
877  
878 # NOTE: Paragraphs will already be escaped.
879  
880 # I don't really like either of these implementations
881 # but the join/map/split seems a little better and benches a little faster.
882 # You would lose the last newline but we've already chomped.
883 #$text =~ s{^(.)?}{'>' . (defined($1) && length($1) ? (' ' . $1) : '')}mge;
884 3 ▶ $text = join $/, map { length($_) ? '> ' . $_ : '>' } split qr-$/-, $text;
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 18 4 22
885  
886 3 $self->_save_block($text);
887 }
888  
889 ## Custom Formats ##
890  
891 10 ▶sub start_for {
Subroutine start_for: called CC=2 SCAR=6.9
Pod undocumented
892 10 my ($self, $attr) = @_;
893 10 $self->_new_stack;
894  
895 10 ▶ if( $attr->{target} eq 'html' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"target"} eq "html") 2 8 10
896 # Use another stack so we can indent
897 # (not syntactily necessary but seems appropriate).
898 2 $self->_new_stack;
899 2 $self->_increase_indent;
900 2 $self->_private->{no_escape} = 1;
901 # Mark this so we know to undo it.
902 2 $self->_stack_state->{for_html} = 1;
903 }
904 }
905  
906 10 ▶sub end_for {
Subroutine end_for: called CC=2 SCAR=6.9
Pod undocumented
907 10 my ($self) = @_;
908 # Data gets saved as a block (which will handle indents),
909 # but if there was html we'll alter this, so chomp and save a block again.
910 10 my $text = $self->_chomp_all($self->_pop_stack_text);
911  
912 10 ▶ if( $self->_private->{last_state}->{for_html} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_private->{"last_state"}{"for_html"}) 2 8 10
913 2 $self->_private->{no_escape} = 0;
914 # Save it to the next stack up so we can pop it again (we made two stacks).
915 2 $self->_save($text);
916 2 $self->_decrease_indent;
917 2 $text = join "\n", '<div>', $self->_chomp_all($self->_pop_stack_text), '</div>';
918 }
919  
920 10 $self->_save_block($text);
921 }
922  
923 # Data events will be emitted for any formatted regions that have been enabled
924 # (by default, `markdown` and `html`).
925  
926 5 ▶sub start_Data {
Subroutine start_Data: called CC=1 SCAR=0.0
Pod undocumented
927 5 my ($self) = @_;
928 # TODO: limit this to what's in attr?
929 5 $self->_private->{no_escape}++;
930 5 $self->_new_stack;
931 }
932  
933 5 ▶sub end_Data {
Subroutine end_Data: called CC=1 SCAR=0.0
Pod undocumented
934 5 my ($self) = @_;
935 5 my $text = $self->_pop_stack_text;
936 5 $self->_private->{no_escape}--;
937 5 $self->_save_block($text);
938 }
939  
940 ## Codes ##
941  
942 94 ▶sub start_B { $_[0]->_save('**') }
Subroutine start_B: called CC=1 SCAR=0.0
Pod undocumented
943 47 ▶sub end_B { $_[0]->start_B() }
Subroutine end_B: called CC=1 SCAR=0.0
Pod undocumented
944  
945 46 ▶sub start_I { $_[0]->_save('_') }
Subroutine start_I: called CC=1 SCAR=0.0
Pod undocumented
946 23 ▶sub end_I { $_[0]->start_I() }
Subroutine end_I: called CC=1 SCAR=0.0
Pod undocumented
947  
948 61 ▶sub start_C {
Subroutine start_C: called CC=1 SCAR=0.0
Pod undocumented
949 61 my ($self) = @_;
950 61 $self->_new_stack;
951 61 $self->_private->{no_escape}++;
952 }
953  
954 61 ▶sub end_C {
Subroutine end_C: called CC=1 SCAR=0.0
Pod undocumented
955 61 my ($self) = @_;
956 61 $self->_private->{no_escape}--;
957 61 $self->_save( $self->_wrap_code_span($self->_pop_stack_text) );
958 }
959  
960 # Use code spans for F<>.
961 4 ▶sub start_F { shift->start_C(@_); }
Subroutine start_F: called CC=1 SCAR=0.0
Pod undocumented
962 4 ▶sub end_F { shift ->end_C(@_); }
Subroutine end_F: called CC=1 SCAR=0.0
Pod undocumented
963  
964 93 ▶sub start_L {
Subroutine start_L: called CC=1 SCAR=0.0
Pod undocumented
965 93 my ($self, $flags) = @_;
966 93 $self->_new_stack;
967 93 push @{ $self->_private->{link} }, $flags;
968 }
969  
970 93 ▶sub end_L {
Subroutine end_L: called CC=9 SCAR=22.3
Pod undocumented
971 93 my ($self) = @_;
972 93 ▶ my $flags = pop @{ $self->_private->{link} }
Branch: 1 branch
BranchTrueFalseTotal
my $flags = pop @{$self->_private->{"link"};} or die "Invalid state: link end with no link start" 93 0 93
973 or die 'Invalid state: link end with no link start';
974  
975 93 my ($type, $to, $section) = @{$flags}{qw( type to section )};
976  
977 93 ▶ my $url = (
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'url' ? : 17 76 93
$type eq 'man' ? : 6 70 76
$type eq 'pod' ? : 70 0 70
978 $type eq 'url' ? $to
979 : $type eq 'man' ? $self->format_man_url($to, $section)
980 : $type eq 'pod' ? $self->format_perldoc_url($to, $section)
981 : undef
982 );
983  
984 93 my $text = $self->_pop_stack_text;
985  
986 # NOTE: I don't think the perlpodspec says what to do with L<|blah>
987 # but it seems like a blank link text just doesn't make sense
988 93 ▶ if( !length($text) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (length $text) 4 89 93
989 4 ▶ $text =
Branch: 2 branches
BranchTrueFalseTotal
$section ? : 2 2 4
$to ? : 1 1 2
990 $section ?
991 $to ? sprintf('"%s" in %s', $section, $to)
992 : ('"' . $section . '"')
993 : $to;
994 }
995  
996 # FIXME: What does Pod::Simple::X?HTML do for this?
997 # if we don't know how to handle the url just print the pod back out
998 93 ▶ if (!$url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 93 93
999 0 $self->_save(sprintf 'L<%s>', $flags->{raw});
1000 0 return;
1001 }
1002  
1003 # In the url we need to escape quotes and parentheses lest markdown
1004 # break the url (cut it short and/or wrongfully interpret a title).
1005  
1006 # Backslash escapes do not work for the space and quotes.
1007 # URL-encoding the space is not sufficient
1008 # (the quotes confuse some parsers and produce invalid html).
1009 # I've arbitratily chosen HTML encoding to hide them from markdown
1010 # while mangling the url as litle as possible.
1011 93 $url =~ s/([ '"])/sprintf '&#x%x;', ord($1)/ge;
1012  
1013 # We also need to double any backslashes that may be present
1014 # (lest they be swallowed up) and stop parens from breaking the url.
1015 93 $url =~ s/([\\()])/\\$1/g;
1016  
1017 # TODO: put section name in title if not the same as $text
1018 93 $self->_save('[' . $text . '](' . $url . ')');
1019 }
1020  
1021 1 ▶sub start_X {
Subroutine start_X: called CC=1 SCAR=0.0
Pod undocumented
1022 1 $_[0]->_new_stack;
1023 }
1024  
1025 1 ▶sub end_X {
Subroutine end_X: called CC=1 SCAR=0.0
Pod undocumented
1026 1 my ($self) = @_;
1027 1 my $text = $self->_pop_stack_text;
1028 # TODO: mangle $text?
1029 # TODO: put <a name="$text"> if configured
1030 }
1031  
1032 # A code span can be delimited by multiple backticks (and a space)
1033 # similar to pod codes (C<< code >>), so ensure we use a big enough
1034 # delimiter to not have it broken by embedded backticks.
1035 61 ▶sub _wrap_code_span {
Subroutine _wrap_code_span: called CC=4 SCAR=13.9
1036 61 my ($self, $arg) = @_;
1037 61 my $longest = 0;
1038 61 ▶ while( $arg =~ /([`]+)/g ){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /([`]+)/g) 5 61 66
1039 5 my $len = length($1);
1040 5 ▶ $longest = $len if $longest < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $longest < $len 3 2 5
1041 }
1042 61 my $delim = '`' x ($longest + 1);
1043 61 ▶ my $pad = $longest > 0 ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$longest > 0 ? : 3 58 61
1044 61 return $delim . $pad . $arg . $pad . $delim;
1045 }
1046  
1047 ## Link Formatting (TODO: Move this to another module) ##
1048  
1049  
1050 6 ▶sub format_man_url {
Subroutine format_man_url: called CC=3 SCAR=12.5
Pod documented
1051 6 my ($self, $to) = @_;
1052 6 my ($page, $part) = ($to =~ /^ ([^(]+) (?: \( (\S+) \) )? /x);
1053 6 ▶ return $self->man_url_prefix . ($part || 1) . '/' . ($page || $to);
Condition40%2/5
$part || 1
l!l
60
$page || $to
l!l&&r!l&&!r
600
MC/DC0%0/1
$part || 1
$part
MC/DC0%0/2
$page || $to
$page $to
Truth table50%1/2
A
A= $part
Aresult
11
00
Truth table33%1/3
A || B
A= $pageB= $to
ABresult
1X1
011
000
1054 }
1055  
1056  
1057 70 ▶sub format_perldoc_url {
Subroutine format_perldoc_url: called CC=9 SCAR=22.0
Pod documented
1058 70 my ($self, $name, $section) = @_;
1059  
1060 70 my $url_prefix = $self->perldoc_url_prefix;
1061 70 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $self->is_local_module($name) and defined $self->local_module_url_prefix) 5 65 70
Condition100%6/6
defined $name and $self->is_local_module($name) and defined $self->local_module_url_prefix
!ll&&!rl&&r
6145
defined $name and $self->is_local_module($name)
!ll&&!rl&&r
11509
MC/DC100%3/3
defined $name and $self->is_local_module($name) and defined $self->local_module_url_prefix
defined $name $self->is_local_module($name) defined $self->local_module_url_prefix
Truth table100%4/4
A and B and C
A= defined $nameB= $self->is_local_module($name)C= defined $self->local_module_url_prefix
ABCresult
0XX0
10X0
1100
1111
1062 defined($name)
1063 && $self->is_local_module($name)
1064 && defined($self->local_module_url_prefix)
1065 ) {
1066 5 $url_prefix = $self->local_module_url_prefix;
1067 }
1068  
1069 70 my $url = '';
1070  
1071 # If the link is to another module (external link).
1072 70 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 59 11 70
1073 59 ▶ $url = $url_prefix . ($self->escape_url ? URI::Escape::uri_escape($name) : $name);
Branch: 1 branch
BranchTrueFalseTotal
$self->escape_url ? : 58 1 59
1074 }
1075  
1076 # See https://rt.cpan.org/Ticket/Display.html?id=57776
1077 # for a discussion on the need to mangle the section.
1078 70 ▶ if ($section){
Branch: 1 branch
BranchTrueFalseTotal
if ($section) 42 28 70
1079  
1080 42 ▶ my $method = $url
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 31 11 42
1081 # If we already have a prefix on the url it's external.
1082 ? $self->perldoc_fragment_format
1083 # Else an internal link points to this markdown doc.
1084 : $self->markdown_fragment_format;
1085  
1086 42 ▶ $method = 'format_fragment_' . $method
Branch: 1 branch
BranchTrueFalseTotal
unless ref $method 21 21 42
1087 unless ref($method);
1088  
1089 {
1090 # Set topic to enable code refs to be simple.
1091 42 local $_ = $section;
1092 42 $section = $self->$method($section);
1093 }
1094  
1095 42 $url .= '#' . $section;
1096 }
1097  
1098 70 return $url;
1099 }
1100  
1101  
1102 # TODO: simple, pandoc, etc?
1103  
1104 3 ▶sub format_fragment_markdown {
Subroutine format_fragment_markdown: called CC=2 SCAR=7.0
Pod documented
1105 3 my ($self, $section) = @_;
1106  
1107 # If this is an internal link (to another section in this doc)
1108 # we can't be sure what the heading id's will look like
1109 # (it depends on what is rendering the markdown to html)
1110 # but we can try to follow popular conventions.
1111  
1112 # http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html#header-identifiers-in-html-latex-and-context
1113 #$section =~ s/(?![-_.])[[:punct:]]//g;
1114 #$section =~ s/\s+/-/g;
1115 3 $section =~ s/\W+/-/g;
1116 3 $section =~ s/-+$//;
1117 3 $section =~ s/^-+//;
1118 3 $section = lc $section;
1119 #$section =~ s/^[^a-z]+//;
1120 3 ▶ $section ||= 'section';
Condition50%1/2
$section ||= "section"
l!l
30
MC/DC0%0/1
$section ||= "section"
$section
Truth table50%1/2
A
A= $section
Aresult
11
00
1121  
1122 3 return $section;
1123 }
1124  
1125  
1126 {
1127 # From Pod::Simple::XHTML 3.28.
1128 # The strings gets passed through encode_entities() before idify().
1129 # If we don't do it here the substitutions below won't operate consistently.
1130  
1131 9 ▶ sub format_fragment_pod_simple_xhtml {
Subroutine format_fragment_pod_simple_xhtml: called CC=2 SCAR=6.9
Pod documented
1132 9 my ($self, $t) = @_;
1133  
1134 # encode_entities {
1135 # We need to use the defaults in case html_encode_chars has been customized
1136 # (since the purpose is to match what external sources are doing).
1137  
1138 9 local $self->_private->{html_encode_chars};
1139 9 $t = $self->encode_entities($t);
1140 # }
1141  
1142 # idify {
1143 9 for ($t) {
1144 9 s/<[^>]+>//g; # Strip HTML.
1145 9 s/&[^;]+;//g; # Strip entities.
1146 9 s/^\s+//; s/\s+$//; # Strip white space.
1147 9 s/^([^a-zA-Z]+)$/pod$1/; # Prepend "pod" if no valid chars.
1148 9 s/^[^a-zA-Z]+//; # First char must be a letter.
1149 9 s/[^-a-zA-Z0-9_:.]+/-/g; # All other chars must be valid.
1150 9 s/[-:.]+$//; # Strip trailing punctuation.
1151 }
1152 # }
1153  
1154 9 return $t;
1155 }
1156 }
1157  
1158  
1159 29 ▶sub format_fragment_pod_simple_html {
Subroutine format_fragment_pod_simple_html: called CC=2 SCAR=7.0
Pod documented
1160 9 my ($self, $section) = @_;
1161  
1162 # From Pod::Simple::HTML 3.28.
1163  
1164 # section_name_tidy {
1165 9 $section =~ s/^\s+//;
1166 9 $section =~ s/\s+$//;
1167 9 $section =~ tr/ /_/;
1168 9 $section =~ tr/\x00-\x1F\x80-\x9F//d if 'A' eq chr(65); # drop crazy characters
1169  
1170 #$section = $self->unicode_escape_url($section);
1171 # unicode_escape_url {
1172 0 $section =~ s/([^\x00-\xFF])/'('.ord($1).')'/eg;
1173 # Turn char 1234 into "(1234)"
1174 # }
1175  
1176 9 ▶ $section = '_' unless length $section;
Branch: 1 branch
BranchTrueFalseTotal
unless length $section 0 9 9
1177 9 return $section;
1178 # }
1179 }
1180  
1181  
1182 9 ▶sub format_fragment_metacpan { shift->format_fragment_pod_simple_xhtml(@_); }
Subroutine format_fragment_metacpan: called CC=1 SCAR=0.0
Pod documented
1183 9 ▶sub format_fragment_sco { shift->format_fragment_pod_simple_html(@_); }
Subroutine format_fragment_sco: called CC=1 SCAR=0.0
Pod documented
1184  
1185  
1186 59 ▶sub is_local_module {
Subroutine is_local_module: called CC=1 SCAR=0.0
Pod documented
1187 59 my ($self, $name) = @_;
1188  
1189 59 return ($name =~ $self->local_module_re);
1190 }
1191  
1192 29 1;
1193  
1194 __END__
↑ Summary script/pod2markdown »