blib/lib/Text/MultiMarkdown.pm

filter: Statementstmt 94.9% 511 / 538 Branchbran 84.6% 166 / 196 Conditioncond 63.5% 47 / 74 MC/DCmcdc 32.5% 13 / 40 Subroutinesub 100% 55 / 55 Podpod 100% 4 / 4 totaltotal 87.7% 796 / 907 CC 134 SCAR 22.9
CC
134
Cov
90%
markdown
25.5
_DoTables
23.6
_ParseMetaData
14.7
CRAP 9.8
« script/MultiMarkdown.pl ↑ Summary
1 26 require 5.008_000;
2 26 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Text::MultiMarkdown;
5 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 26 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use base qw(Text::Markdown);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use HTML::Entities qw(encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 26 our $VERSION = '1.005';
18 26 our @EXPORT_OK = qw(markdown multimarkdown_to_html);
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 Text::MultiMarkdown - Convert MultiMarkdown syntax to (X)HTML
25  
26 =head1 SYNOPSIS
27  
28 Use it as a function, with or without optional arguments:
29  
30 use Text::MultiMarkdown 'markdown';
31  
32 my $html = markdown($text);
33  
34 my $html = markdown( $text, {
35 empty_element_suffix => '>',
36 tab_width => 2,
37 use_wikilinks => 1,
38 } );
39  
40 Or in the object-oriented interface:
41  
42 use Text::MultiMarkdown;
43  
44 my $m = Text::MultiMarkdown->new;
45 my $html = $m->markdown($text);
46  
47 my $m = Text::MultiMarkdown->new(
48 empty_element_suffix => '>',
49 tab_width => 2,
50 use_wikilinks => 1,
51 );
52 my $html = $m->markdown( $text );
53  
54 =head1 DESCRIPTION
55  
56 Markdown is a text-to-HTML filter; it translates an easy-to-read /
57 easy-to-write structured text format into HTML. Markdown's text format
58 is most similar to that of plain text email, and supports features such
59 as headers, *emphasis*, code blocks, blockquotes, and links.
60  
61 Markdown's syntax is designed not as a generic markup language, but
62 specifically to serve as a front-end to (X)HTML. You can use span-level
63 HTML tags anywhere in a Markdown document, and you can use block level
64 HTML tags (C<< <div> >>, C<< <table> >> etc.). Note that by default
65 Markdown isn't interpreted in HTML block-level elements, unless you add
66 a C<markdown="1"> attribute to the element. See L<Text::Markdown> for
67 details.
68  
69 This module implements the MultiMarkdown markdown syntax extensions from:
70  
71 http://fletcherpenney.net/multimarkdown/
72  
73 =head1 SYNTAX
74  
75 For more information about (original) Markdown's syntax, see:
76  
77 http://daringfireball.net/projects/markdown/
78  
79 This module implements MultiMarkdown, which is an extension to Markdown..
80  
81 The extension is documented at:
82  
83 http://fletcherpenney.net/multimarkdown/
84  
85 and borrows from php-markdown, which lives at:
86  
87 http://michelf.com/projects/php-markdown/extra/
88  
89 This documentation is going to be moved/copied into this module for
90 clearer reading in a future release..
91  
92 =head2 Options
93  
94 MultiMarkdown supports a number of options to its processor which
95 control the behaviour of the output document.
96  
97 These options can be supplied to the constructor, on in a hash with
98 the individual calls to the markdown method. See the synopsis for
99 examples of both of the above styles.
100  
101 The options for the processor are:
102  
103 =over 4
104  
105 =item bibliography_title
106  
107 The title of the generated bibliography, defaults to 'Bibliography'.
108  
109 =item disable_bibliography
110  
111 If true, this disables the MultiMarkdown bibliography/citation handling.
112  
113 =item disable_definition_lists
114  
115 If true, this disables the MultiMarkdown definition list handling.
116  
117 =item disable_footnotes
118  
119 If true, this disables the MultiMarkdown footnotes handling.
120  
121 =item disable_tables
122  
123 If true, this disables the MultiMarkdown table handling.
124  
125 =item empty_element_suffix
126  
127 This option can be used to generate normal HTML output. By default, it
128 is C<< /> >>, which is xHTML, change to C<< > >> for normal HTML.
129  
130 =item heading_ids
131  
132 Controls if C<hX> tags generated have an id attribute. Defaults to true.
133 Turn off for compatibility with the original markdown.
134  
135 =item heading_ids_spaces_to_dash
136  
137 Controls whether spaces in headings should be rendered as "-" characters
138 in the heading ids (for compatibility with GitHub markdown, and others)
139  
140 =item img_ids
141  
142 Controls if C<img> tags generated have an id attribute. Defaults to true.
143 Turn off for compatibility with the original markdown.
144  
145 =item strip_metadata
146  
147 If true, any metadata in the input document is removed from the output
148 document (note - does not take effect in complete document format).
149  
150 =item tab_width
151  
152 Controls indent width in the generated markup, defaults to 4
153  
154 =item transliterated_ids
155  
156 In markdown label values, change accented and other non-ASCII letter
157 characters with L<Text::Unidecode>. If that module is not available,
158 this issues a warning and does nothing. When C<unicode_ids> is true,
159 this is ignored. The default is false.
160  
161 =item unicode_ids
162  
163 In markdown label values, allow any Unicode letter character along
164 with the allowed ASCII symbol characters. This overrules
165 C<transliterated_ids> when true. The default is false.
166  
167 =item use_metadata
168  
169 Controls the metadata options below.
170  
171 =back
172  
173 =head2 Metadata
174  
175 MultiMarkdown supports the concept of 'metadata', which allows you to
176 specify a number of formatting options within the document itself.
177 Metadata should be placed in the top few lines of a file, on value per
178 line as colon separated key/value pairs. The metadata should be
179 separated from the document with a blank line.
180  
181 Most metadata keys are also supported as options to the constructor,
182 or options to the markdown method itself. (Note, as metadata, keys
183 contain space, whereas options the keys are underscore separated.)
184  
185 You can attach arbitrary metadata to a document, which is output in
186 HTML C<< <META> >> tags if unknown, see F<t/11-document_format.t> for
187 an example.
188  
189 These are the known metadata keys:
190  
191 =over 4
192  
193 =item document_format
194  
195 If set to 'complete', MultiMarkdown will render an entire xHTML page,
196 otherwise it will render a document fragment
197  
198 =over 4
199  
200 =item base url
201  
202 This is the base URL for referencing wiki pages. In this is not
203 supplied, all wiki links are relative.
204  
205 =item css
206  
207 Sets a CSS file for the file, if in 'complete' document format.
208  
209 =item title
210  
211 Sets the page title, if in 'complete' document format.
212  
213 =back
214  
215 =item use wikilinks
216  
217 If set to '1' or 'on', causes links that are WikiWords to
218 automatically be processed into links.
219  
220 =back
221  
222 =head2 Class methods
223  
224 =over 4
225  
226 =item new
227  
228 A simple constructor, see the SYNTAX and OPTIONS sections for more information.
229  
230 =cut
231  
232 26 my %defaults;
233 26 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
234 26 %defaults = (
235 use_metadata => 1,
236 base_url => '',
237 tab_width => 4,
238 document_format => '',
239 empty_element_suffix => ' />',
240 use_wikilinks => 0,
241 heading_ids => 1,
242 img_ids => 1,
243 bibliography_title => 'Bibliography',
244 self_url => '',
245 heading_ids_spaces_to_dash => '',
246 );
247  
248 }
249  
250 120 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
251 120 my ($class, %args) = @_;
252  
253 120 my %p = ( %defaults, %args );
254  
255 120 my @binary = qw(use_metadata use_wikilinks);
256 120 ▶ $p{$_} = $p{$_} ? 1 : 0 for @binary;
Branch: 1 branch
BranchTrueFalseTotal
$p{$_} ? : 140 100 240
257  
258 120 ▶ unless( $p{tab_width} =~ m/^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"tab_width"} =~ /^[0-9]+$/) 0 120 120
259 0 carp "tab_width did not look like a decimal number, so using the default 4";
260 0 $p{tab_width} = 4;
261 }
262  
263 120 _process_id_handler( \%args, \%p );
264  
265 120 my $self = { params => \%p };
266 120 ▶ bless $self, ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
22980
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
267 120 return $self;
268 }
269  
270 237 ▶sub _id_handler {
Subroutine _id_handler: called CC=2 SCAR=7.6
271 237 ▶ defined $_[0]->{id_handler} ? $_[0]->{id_handler} : \&_default_id_handler
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'id_handler'} ? : 237 0 237
272 }
273  
274 227 ▶sub _default_id_handler {
Subroutine _default_id_handler: called CC=1 SCAR=0.0
275 227 my ($label) = @_;
276  
277 227 $label =~ s/[^A-Za-z0-9:_.-]//g;
278 227 $label =~ s/\A[^A-Za-z]+//g;
279 227 $label =~ s/-+/-/g;
280 227 $label =~ s/-+\z//g;
281  
282 227 return $label;
283 }
284  
285 26 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
286 26 my $has_unidecode = eval { require Text::Unidecode };
287  
288 22 ▶sub _transliteration_id_handler {
Subroutine _transliteration_id_handler: called CC=2 SCAR=7.2
289 22 my ($label) = @_;
290  
291 22 ▶ unless ($has_unidecode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_unidecode) 0 22 22
292 0 carp "Need Text::Unidecode to for transliterated_ids, but could not load it. Falling back to default id handler";
293 0 return _default_id_handler($label);
294 }
295  
296 22 $label = Text::Unidecode::unidecode($label);
297  
298 22 $label =~ s/\s+//g;
299 22 $label =~ s/\A[^A-Za-z]+//g;
300 22 $label =~ s/-+/-/g;
301 22 $label =~ s/-+\z//g;
302  
303 22 return $label;
304 }
305  
306 {
307 26 ▶no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
308 26 ▶*_transliteration_id_handler = \&_default_id_handler unless $has_unidecode;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_unidecode 0 26 26
309 }
310  
311 30 ▶sub _unicode_id_handler {
Subroutine _unicode_id_handler: called CC=1 SCAR=0.0
312 30 my ($label) = @_;
313  
314 30 $label =~ s/\s+//g;
315 30 $label =~ s/\W+/-/g;
316 30 $label =~ s/\A\P{Letter}+//g;
317 30 $label =~ s/-+/-/g;
318 30 $label =~ s/-+\z//g;
319 30 return $label;
320 }
321  
322 120 ▶sub _process_id_handler {
Subroutine _process_id_handler: called CC=7 SCAR=19.5
323 120 my( $args, $p ) = @_;
324  
325 120 $p->{id_handler} = \&_default_id_handler;
326  
327 120 ▶ if ( exists $args->{unicode_ids} and $args->{unicode_ids} and exists $args->{transliterated_ids} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"unicode_ids"} and $args->{"unicode_ids"} and exists $args->{"transliterated_ids"}) 3 117 120
Condition83%5/6
exists $args->{"unicode_ids"} and $args->{"unicode_ids"} and exists $args->{"transliterated_ids"}
!ll&&!rl&&r
11433
exists $args->{"unicode_ids"} and $args->{"unicode_ids"}
!ll&&!rl&&r
11406
MC/DC66%2/3
exists $args->{"unicode_ids"} and $args->{"unicode_ids"} and exists $args->{"transliterated_ids"}
exists $args->{"unicode_ids"} $args->{"unicode_ids"} exists $args->{"transliterated_ids"}
Truth table75%3/4
A and B and C
A= exists $args->{"unicode_ids"}B= $args->{"unicode_ids"}C= exists $args->{"transliterated_ids"}
ABCresult
0XX0
10X0
1100
1111
328 3 warn "ignoring transliterated_ids because unicode_ids is true\n";
329 3 delete $args->{transliterated_ids};
330 }
331  
332 120 ▶ if ( $args->{unicode_ids} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'unicode_ids'}) { } 6 114 120
elsif ($args->{'transliterated_ids'}) { } 3 111 114
333 6 $p->{id_handler} = \&_unicode_id_handler
334 }
335 elsif ( $args->{transliterated_ids} ) {
336 3 ▶ warn "Need Text::Unidecode to transliterate labels, but could not load it\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $has_unidecode 0 3 3
337 unless $has_unidecode;
338 3 $p->{id_handler} = \&_transliteration_id_handler;
339 }
340 }
341 }
342  
343 =back
344  
345 =head2 Instance methods
346  
347 =over 4
348  
349 =item markdown( MARKDOWN_TEXT [, HASHREF] )
350  
351 This is the legacy interface to this module, but it does too much and
352 is a poor name. For the function form, use C<multimarkdown_to_html>
353 instead. At the moment that's just a wrapper for C<markdown> in the
354 functional form. For the object-oriented forms, use C<to_html> instead.
355 That's also just a wrapper for this, but will later change to enforce
356 object-orientedness (i.e. exclude the functional form).
357  
358 And now the legacy stuff.
359  
360 This works as either a class method, instance method, or exportable
361 function:
362  
363 my $html = Text::MultiMarkdown->markdown( $text );
364  
365 my $mm = Text::MultiMarkdown->new;
366 my $html = $mm->markdown($text);
367  
368 use Text::MultiMarkdown qw(markdown);
369 my $html = markdown( $text );
370  
371 Any of these forms take an optional HASH_REF argument for options. These
372 are the options for this module or the parent class L<Text::Markdown>:
373  
374 my $html = Text::MultiMarkdown->markdown( $text, { ... } );
375  
376 my $mm = Text::MultiMarkdown->new;
377 my $html = $mm->markdown($text, { ... });
378  
379 use Text::MultiMarkdown qw(markdown);
380 my $html = markdown( $text, { ... } );
381  
382 To make this work in all these cases, since this was the legacy design,
383 various unsavory things have to happen.
384  
385 When called as a class method, a new object is constructed. We guess
386 that it's a class method by looking at the first argument and seeing
387 that it looks like a Perl package name. In prior versions this was
388 documented to not work, but there was also a TODO test for it to work.
389 So, now it works. This might fail if the entire markdown text is exactly
390 a valid Perl package name.
391  
392 If the first argument is a blessed reference, we guess that this is
393 an instance method. With the optional HASH_REF argument this constructs
394 a new argument with all of the settings of the original object and the
395 stuff in HASH_REF. This might fail if you have some weird case where
396 you call this as a function but pass as the TEXT argument an object that
397 has overloaded stringification .
398  
399 =cut
400  
401 =begin comment
402  
403 =end comment
404  
405 There are these situations:
406  
407 CLASS->markdown( TEXT );
408 CLASS->markdown( TEXT, HASHREF );
409  
410 OBJ->markdown( TEXT );
411 OBJ->markdown( TEXT, HASHREF );
412  
413 markdown( TEXT );
414 markdown( TEXT, HASHREF );
415  
416 These are really:
417  
418 markdown( CLASS, TEXT )
419 markdown( CLASS, TEXT, HASHREF )
420  
421 markdown( OBJ, TEXT )
422 markdown( OBJ, TEXT, HASHREF )
423  
424 markdown( TEXT );
425 markdown( TEXT, HASHREF );
426  
427 Which breaks down to these groups:
428  
429 1) markdown( TEXT );
430  
431 2.1) markdown( TEXT, HASHREF );
432 2.2) markdown( CLASS, TEXT )
433 2.3) markdown( OBJ, TEXT )
434  
435 3.1) markdown( CLASS, TEXT, HASHREF )
436 3.2) markdown( OBJ, TEXT, HASHREF )
437  
438 In 1), 2.2), and 3.1), we should make a new object and then do our
439 thing.
440  
441 In 3.1), the previous version specifically said that we can't call
442 this as a class method.
443  
444 In 3.2), we need to merge the options in the existing object with
445 the new options. This was never a documented feature though.
446  
447 Part of the tickyness is that interface for Text::Markdown. We need
448 to pass the HASHREF to _CleanUpRunData in the SUPER class
449  
450 =cut
451  
452 282 ▶sub _looks_like_class {
Subroutine _looks_like_class: called CC=1 SCAR=0.0
453 282 local $_ = $_[0];
454 282 m/\A\w+(?:::\w+)+\z/;
455 }
456  
457 181 ▶sub markdown {
Subroutine markdown: called CC=21 SCAR=32.4
Pod documented
458 181 my( $self, $text, $options ) = do {
459 181 ▶ if ( @_ == 1 and ! ref $_[0] ) { # Case 1
Branch: 6 branches
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 8 173 181
elsif (@_ == 2 and not _looks_like_class($_[0]) and ref $_[1] eq ref {}) { } 28 145 173
elsif (@_ == 2 and _looks_like_class($_[0]) and not ref $_[1]) { } 7 138 145
elsif (@_ == 2 and builtin::blessed($_[0]) and not ref $_[1]) { } 102 36 138
elsif (@_ == 3 and _looks_like_class($_[0]) and not ref $_[1] and ref $_[2] eq ref {}) { } 14 22 36
elsif (@_ == 3 and builtin::blessed($_[0]) and not ref $_[1] and ref $_[2] eq ref {}) { } 22 0 22
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
17308
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
460 8 ( __PACKAGE__->new, $_[0], {} );
461 ▶ } elsif ( @_ == 2 and ! _looks_like_class($_[0]) and ref $_[1] eq ref {} ) { # Case 2.1
Condition100%6/6
@_ == 2 and not _looks_like_class($_[0]) and ref $_[1] eq ref {}
!ll&&!rl&&r
4310228
@_ == 2 and not _looks_like_class($_[0])
!ll&&!rl&&r
367130
MC/DC100%3/3
@_ == 2 and not _looks_like_class($_[0]) and ref $_[1] eq ref {}
@_ == 2 not _looks_like_class($_[0]) ref $_[1] eq ref {}
Truth table100%4/4
A and not(B) and C
A= @_ == 2B= not _looks_like_class($_[0])C= ref $_[1] eq ref {}
ABCresult
0XX0
10X0
1100
1111
462 28 ( __PACKAGE__->new( %{ $_[1] } ), $_[0], $_[1] );
463 ▶ } elsif ( @_ == 2 and _looks_like_class($_[0]) and ! ref $_[1] ) { # Case 2.2
Condition83%5/6
@_ == 2 and _looks_like_class($_[0]) and not ref $_[1]
!ll&&!rl&&r
13807
@_ == 2 and _looks_like_class($_[0])
!ll&&!rl&&r
361027
MC/DC66%2/3
@_ == 2 and _looks_like_class($_[0]) and not ref $_[1]
@_ == 2 _looks_like_class($_[0]) not ref $_[1]
Truth table75%3/4
A and B and not(C)
A= @_ == 2B= _looks_like_class($_[0])C= not ref $_[1]
ABCresult
0XX0
10X0
1100
1111
464 7 ( $_[0]->new, $_[1] );
465 ▶ } elsif ( @_ == 2 and blessed($_[0]) and ! ref $_[1] ) { # Case 2.3
Condition66%4/6
@_ == 2 and builtin::blessed($_[0]) and not ref $_[1]
!ll&&!rl&&r
360102
@_ == 2 and builtin::blessed($_[0])
!ll&&!rl&&r
360102
MC/DC33%1/3
@_ == 2 and builtin::blessed($_[0]) and not ref $_[1]
@_ == 2 builtin::blessed($_[0]) not ref $_[1]
Truth table50%2/4
A and B and not(C)
A= @_ == 2B= builtin::blessed($_[0])C= not ref $_[1]
ABCresult
0XX0
10X0
1100
1111
466 102 ( $_[0], $_[1], {} );
467 ▶ } elsif ( @_ == 3 and _looks_like_class($_[0]) and ! ref $_[1] and ref $_[2] eq ref {} ) { # Case 3.1
Condition66%6/9
@_ == 3 and _looks_like_class($_[0]) and not ref $_[1] and ref $_[2] eq ref {}
!ll&&!rl&&r
22014
@_ == 3 and _looks_like_class($_[0]) and not ref $_[1]
!ll&&!rl&&r
22014
@_ == 3 and _looks_like_class($_[0])
!ll&&!rl&&r
02214
MC/DC25%1/4
@_ == 3 and _looks_like_class($_[0]) and not ref $_[1] and ref $_[2] eq ref {}
@_ == 3 _looks_like_class($_[0]) not ref $_[1] ref $_[2] eq ref {}
Truth table40%2/5
A and B and not(C) and D
A= @_ == 3B= _looks_like_class($_[0])C= not ref $_[1]D= ref $_[2] eq ref {}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
468 14 ( $_[0]->new( %{ $_[2]} ), $_[1], $_[2] );
469 ▶ } elsif ( @_ == 3 and blessed($_[0]) and ! ref $_[1] and ref $_[2] eq ref {} ) { # Case 3.2
Condition33%3/9
@_ == 3 and builtin::blessed($_[0]) and not ref $_[1] and ref $_[2] eq ref {}
!ll&&!rl&&r
0022
@_ == 3 and builtin::blessed($_[0]) and not ref $_[1]
!ll&&!rl&&r
0022
@_ == 3 and builtin::blessed($_[0])
!ll&&!rl&&r
0022
MC/DC0%0/4
@_ == 3 and builtin::blessed($_[0]) and not ref $_[1] and ref $_[2] eq ref {}
@_ == 3 builtin::blessed($_[0]) not ref $_[1] ref $_[2] eq ref {}
Truth table20%1/5
A and B and not(C) and D
A= @_ == 3B= builtin::blessed($_[0])C= not ref $_[1]D= ref $_[2] eq ref {}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
470 22 my %merged = ( %{ $_[0]->{params} }, %{ $_[2] } );
471 22 my $new = $_[0]->new( %merged );
472 22 ( $new, $_[1], $_[2] );
473 } else {
474 0 carp "Unrecognized arguments for markdown()";
475 0 return;
476 }
477 };
478  
479 181 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 7 174 181
480  
481 181 %$self = (%{ $self->{params} }, %$options, params => $self->{params});
482 181 $self->_CleanUpRunData($options);
483  
484 181 return $self->_Markdown($text);
485 }
486  
487 =item multimarkdown_to_html
488  
489 For the functional interface, you should use this instead of C<markdown>
490 because it's a better name. At the moment it's the same as calling
491 C<markdown>, but eventually this will diverge from the object-oriented
492 form C<to_html>, which is also a better name.
493  
494 =cut
495  
496 18 ▶sub multimarkdown_to_html {
Subroutine multimarkdown_to_html: called CC=1 SCAR=0.0
Pod documented
497 18 markdown(@_);
498 }
499  
500 =item to_html
501  
502 As a class or instance method, you should use this instead of C<markdown>
503 because it's a better name. At the moment it's the same as calling
504 C<markdown>, but eventually this will diverge from the functional
505 form C<multimarkdown_to_html>, which is also a better name.
506  
507 =cut
508  
509 31 ▶sub to_html {
Subroutine to_html: called CC=1 SCAR=0.0
Pod documented
510 31 markdown(@_);
511 }
512  
513 181 ▶sub _CleanUpRunData {
Subroutine _CleanUpRunData: called CC=1 SCAR=0.0
514 181 my ($self, $options) = @_;
515 # Clear the global hashes. If we don't clear these, you get conflicts
516 # from other articles when generating a page which contains more than
517 # one article (e.g. an index page that shows the N most recent
518 # articles):
519 181 $self->{_crossrefs} = {};
520 181 $self->{_footnotes} = {};
521 181 $self->{_references} = {};
522 181 $self->{_used_footnotes} = []; # Why do we need 2 data structures for footnotes? FIXME
523 181 $self->{_used_references} = []; # Ditto for references
524 181 $self->{_citation_counter} = 0;
525 181 $self->{_metadata} = {};
526 181 $self->{_attributes} = {}; # Used for extra attributes on links / images.
527  
528 181 $self->SUPER::_CleanUpRunData($options);
529 }
530  
531 181 ▶sub _Markdown {
Subroutine _Markdown: called CC=9 SCAR=22.0
532 #
533 # Main function. The order in which other subs are called here is
534 # essential. Link and image substitutions need to happen before
535 # _EscapeSpecialChars(), so that any *'s or _'s in the <a>
536 # and <img> tags get encoded.
537 #
538 # Can't think of any good way to make this inherit from the Markdown version as ordering is so important, so I've left it.
539 181 my ($self, $text) = @_;
540  
541 181 $text = $self->_CleanUpDoc($text);
542  
543 # MMD only. Strip out MetaData
544 181 ▶ $text = $self->_ParseMetaData($text) if ($self->{use_metadata} || $self->{strip_metadata});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"use_metadata"} or $self->{"strip_metadata"} 176 5 181
Condition100%3/3
$self->{"use_metadata"} or $self->{"strip_metadata"}
l!l&&r!l&&!r
17515
MC/DC100%2/2
$self->{"use_metadata"} or $self->{"strip_metadata"}
$self->{"use_metadata"} $self->{"strip_metadata"}
Truth table100%3/3
A or B
A= $self->{"use_metadata"}B= $self->{"strip_metadata"}
ABresult
1X1
011
000
545  
546 # Turn block-level HTML blocks into hash entries
547 181 $text = $self->_HashHTMLBlocks($text, {interpret_markdown_on_attribute => 1});
548  
549 181 $text = $self->_StripLinkDefinitions($text);
550  
551 # MMD only
552 181 $text = $self->_StripMarkdownReferences($text);
553  
554 181 $text = $self->_RunBlockGamut($text, {wrap_in_p_tags => 1});
555  
556 # MMD Only
557 181 ▶ $text = $self->_DoMarkdownCitations($text) unless $self->{disable_bibliography};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_bibliography"} 178 3 181
558 181 ▶ $text = $self->_DoFootnotes($text) unless $self->{disable_footnotes};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_footnotes"} 178 3 181
559  
560 181 $text = $self->_UnescapeSpecialChars($text);
561  
562 # MMD Only
563 # This must follow _UnescapeSpecialChars
564 181 $text = $self->_UnescapeWikiWords($text);
565 181 ▶ $text = $self->_FixFootnoteParagraphs($text) unless $self->{disable_footnotes}; # TODO: remove. Doesn't make any difference to test suite pass/failure
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_footnotes"} 178 3 181
566 181 ▶ $text .= $self->_PrintFootnotes() unless $self->{disable_footnotes};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_footnotes"} 178 3 181
567 181 ▶ $text .= $self->_PrintMarkdownBibliography() unless $self->{disable_bibliography};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_bibliography"} 178 3 181
568  
569 181 $text = $self->_ConvertCopyright($text);
570  
571 # MMD Only
572 181 ▶ if (lc($self->{document_format}) =~ /^complete\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($self->{'document_format'}) =~ /^complete\s*$/) { } 4 177 181
573 4 return $self->_xhtmlMetaData() . "<body>\n" . $text . "\n</body>\n</html>";
574 }
575 else {
576 177 return $self->_textMetaData() . $text . "\n";
577 }
578  
579 }
580  
581 #
582 # Routines which are overridden for slightly different behaviour in MultiMarkdown
583 #
584  
585 # Delegate to super class, then do wiki links
586 946 ▶sub _RunSpanGamut {
Subroutine _RunSpanGamut: called CC=2 SCAR=6.9
587 946 my ($self, $text) = @_;
588  
589 946 $text = $self->SUPER::_RunSpanGamut($text);
590  
591 # Process WikiWords
592 946 ▶ if ($self->_UseWikiLinks()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_UseWikiLinks) 34 912 946
593 34 $text = $self->_DoWikiLinks($text);
594  
595 # And then reprocess anchors and images
596 # FIXME - This is needed exactly why?
597 34 $text = $self->_DoImages($text);
598 34 $text = $self->_DoAnchors($text);
599 }
600  
601 946 return $text;
602 }
603  
604 # Don't do Wiki Links in Headers, otherwise delegate to super class
605 # Do tables stright after headers
606 240 ▶sub _DoHeaders {
Subroutine _DoHeaders: called CC=1 SCAR=0.0
607 240 my ($self, $text) = @_;
608  
609 240 local $self->{use_wikilinks} = 0;
610  
611 240 $text = $self->SUPER::_DoHeaders($text);
612  
613 # Do tables to populate the table id's for cross-refs
614 # (but after headers as the tables can contain cross-refs to other things, so we want the header cross-refs)
615 240 $text = $self->_DoTables($text);
616 }
617  
618 343 ▶sub _DoLists {
Subroutine _DoLists: called CC=2 SCAR=7.1
619 343 my ($self, $text) = @_;
620 $text = $self->_DoDefinitionLists($text)
621 343 ▶ unless $self->{disable_definition_lists};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_definition_lists"} 343 0 343
622 343 $self->SUPER::_DoLists($text);
623 }
624  
625 343 ▶sub _DoDefinitionLists {
Subroutine _DoDefinitionLists: called CC=2 SCAR=6.9
626 343 my ($self, $text) = @_;
627 # Uses the syntax proposed by Michel Fortin in PHP Markdown Extra
628  
629 343 my $less_than_tab = $self->{tab_width} -1;
630  
631 343 my $line_start = qr{
632 [ ]{0,$less_than_tab}
633 }mx;
634  
635 343 my $term = qr{
636 $line_start
637 [^:\s][^\n]*\n
638 }sx;
639  
640 343 my $definition = qr{
641 \n?[ ]{0,$less_than_tab}
642 \:[ \t]+(.*?)\n
643 ((?=\n?\:)|\n|\Z) # Lookahead for next definition, two returns,
644 # or the end of the document
645 }sx;
646  
647 343 my $definition_block = qr{
648 ((?:$term)+) # $1 = one or more terms
649 ((?:$definition)+) # $2 = by one or more definitions
650 }sx;
651  
652 343 my $definition_list = qr{
653 (?:$definition_block\n*)+ # One ore more definition blocks
654 }sx;
655  
656 343 $text =~ s{
657 ($definition_list) # $1 = the whole list
658 }{
659 1 my $list = $1;
660 1 my $result = $1;
661  
662 1 $list =~ s{
663 (?:$definition_block)\n*
664 }{
665 2 my $terms = $1;
666 2 my $defs = $2;
667  
668 2 $terms =~ s{
669 [ ]{0,$less_than_tab}
670 (.*)
671 \s*
672 }{
673 4 my $term = $1;
674 4 my $result = "";
675 4 $term =~ s/^\s*(.*?)\s*$/$1/;
676 4 ▶ if ($term !~ /^\s*$/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($term =~ /^\s*$/) 2 2 4
677 2 $result = "<dt>" . $self->_RunSpanGamut($1) . "</dt>\n";
678 }
679 4 $result;
680 }xmge;
681  
682 2 $defs =~ s{
683 $definition
684 }{
685 3 my $def = $1 . "\n";
686 3 $def =~ s/^[ ]{0,$self->{tab_width}}//gm;
687 3 "<dd>\n" . $self->_RunBlockGamut($def) . "\n</dd>\n";
688 }xsge;
689  
690 2 $terms . $defs . "\n";
691 }xsge;
692  
693 1 "<dl>\n" . $list . "</dl>\n\n";
694 }xsge;
695  
696 343 return $text
697 }
698  
699 # Generating headers automatically generates X-refs in MultiMarkdown (always)
700 # Also, by default, you get id attributes added to your headers, you can turn this
701 # part of the MultiMarkdown behaviour off with the heading_ids flag.
702 91 ▶sub _GenerateHeader {
Subroutine _GenerateHeader: called CC=3 SCAR=11.0
703 91 my ($self, $level, $id) = @_;
704  
705 91 ▶ my $label = $self->{heading_ids} ? $self->_Header2Label($id) : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'heading_ids'} ? : 89 2 91
706 91 my $header = $self->_RunSpanGamut($id);
707  
708 91 ▶ if ($label ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($label ne "") 89 2 91
709 89 $self->{_crossrefs}{$label} = "#$label";
710 89 $self->{_titles}{$label} = $header;
711 89 $label = qq{ id="$label"};
712 }
713  
714 91 return "<h$level$label>$header</h$level>\n\n";
715 }
716  
717 # Protect Wiki Links in Code Blocks (if wiki links are turned on), then delegate to super class.
718 368 ▶sub _EncodeCode {
Subroutine _EncodeCode: called CC=2 SCAR=6.9
719 368 my ($self, $text) = @_;
720  
721 368 ▶ if ($self->_UseWikiLinks()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_UseWikiLinks) 3 365 368
722 3 $text =~ s/([A-Z]+[a-z\x80-\xff]+[A-Z][A-Za-z\x80-\xff]*)/\\$1/gx;
723 }
724  
725 368 return $self->SUPER::_EncodeCode($text);
726 }
727  
728 # Full function pulled out of Text::Markdown as MultiMarkdown supports supplying extra 'attributes' with links and
729 # images which are then pushed back into the generated HTML, and this needs a different regex. It should be possible
730 # to extract the just the regex from Text::Markdown, and use that here, but I haven't done so yet.
731 # Strip footnote definitions at the same time as stripping link definitions.
732 # Also extract images and then replace them straight back in (code smell!) to be able to cross reference images
733 181 ▶sub _StripLinkDefinitions {
Subroutine _StripLinkDefinitions: called CC=5 SCAR=16.1
734 #
735 # Strips link definitions from text, stores the URLs and titles in
736 # hash references.
737 #
738 181 my ($self, $text) = @_;
739  
740 181 ▶ $text = $self->_StripFootnoteDefinitions($text) unless $self->{disable_footnotes};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disable_footnotes"} 178 3 181
741  
742 181 my $less_than_tab = $self->{tab_width} - 1;
743  
744 # Link defs are in the form: ^[id]: url "optional title"
745 # FIXME - document attributes here.
746 181 ▶ while ($text =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s( # Pattern altered for MultiMarkdown # in order to not match citations or footnotes ^[ ]{0,$less_than_tab}\[([^#^].*)\]: # id = $1 [ \t]* \n? # maybe *one* newline [ \t]* <?(\S+?)>? # url = $2 [ \t]* \n? # maybe one newline [ \t]* (?: (?<=\s) # lookbehind for whitespace ["(] (.+?) # title = $3 [")] [ \t]* )? # title is optional # MultiMarkdown addition for attribute support \n? ( # Attributes = $4 (?<=\s) # lookbehind for whitespace (([ \t]*\n)?[ \t]*((\S+=\S+)|(\S+=".*?")))* )? [ \t]* # /addition (?:\n+|\Z) )[]mx) 45 181 226
747 # Pattern altered for MultiMarkdown
748 # in order to not match citations or footnotes
749 ^[ ]{0,$less_than_tab}\[([^#^].*)\]: # id = $1
750 [ \t]*
751 \n? # maybe *one* newline
752 [ \t]*
753 <?(\S+?)>? # url = $2
754 [ \t]*
755 \n? # maybe one newline
756 [ \t]*
757 (?:
758 (?<=\s) # lookbehind for whitespace
759 ["(]
760 (.+?) # title = $3
761 [")]
762 [ \t]*
763 )? # title is optional
764  
765 # MultiMarkdown addition for attribute support
766 \n?
767 ( # Attributes = $4
768 (?<=\s) # lookbehind for whitespace
769 (([ \t]*\n)?[ \t]*((\S+=\S+)|(\S+=".*?")))*
770 )?
771 [ \t]*
772 # /addition
773 (?:\n+|\Z)
774 }
775 {}mx) {
776 45 $self->{_urls}{lc $1} = $self->_EncodeAmpsAndAngles( $2 ); # Link IDs are case-insensitive
777 45 ▶ if ($3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3) 12 33 45
778 12 $self->{_titles}{lc $1} = $3;
779 12 $self->{_titles}{lc $1} =~ s/"/&quot;/g;
780 }
781  
782 # MultiMarkdown addition "
783 45 ▶ if ($4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($4) 4 41 45
784 4 $self->{_attributes}{lc $1} = $4;
785 }
786 # /addition
787 }
788  
789 181 $text = $self->_GenerateImageCrossRefs($text);
790  
791 181 return $text;
792 }
793  
794 # Add the extra cross-references to headers that MultiMarkdown supports, and also
795 # the additional link attributes.
796 208 ▶sub _GenerateAnchor {
Subroutine _GenerateAnchor: called CC=7 SCAR=20.1
797 # FIXME - Fugly, change to named params?
798 208 my ($self, $whole_match, $link_text, $link_id, $url, $title, $attributes) = @_;
799  
800 # Allow automatic cross-references to headers
801 208 ▶ if (defined $link_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link_id) 123 85 208
802 123 my $label = $self->_Header2Label($link_id);
803 123 ▶ if (defined $self->{_crossrefs}{$label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_crossrefs"}{$label}) 8 115 123
804 8 ▶ $url ||= $self->{_crossrefs}{$label};
Condition33%1/3
$url ||= $self->{"_crossrefs"}{$label}
l!l&&r!l&&!r
080
MC/DC0%0/2
$url ||= $self->{"_crossrefs"}{$label}
$url $self->{"_crossrefs"}{$label}
Truth table33%1/3
A ||= B
A= $urlB= $self->{"_crossrefs"}{$label}
ABresult
1X1
011
000
805 }
806 123 ▶ if ( defined $self->{_titles}{$label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_titles"}{$label}) 11 112 123
807 11 ▶ $title ||= $self->{_titles}{$label};
Condition33%1/3
$title ||= $self->{"_titles"}{$label}
l!l&&r!l&&!r
0110
MC/DC0%0/2
$title ||= $self->{"_titles"}{$label}
$title $self->{"_titles"}{$label}
Truth table33%1/3
A ||= B
A= $titleB= $self->{"_titles"}{$label}
ABresult
1X1
011
000
808 }
809 123 ▶ $attributes ||= $self->_DoAttributes($label);
Condition66%2/3
$attributes ||= $self->_DoAttributes($label)
l!l&&r!l&&!r
03120
MC/DC50%1/2
$attributes ||= $self->_DoAttributes($label)
$attributes $self->_DoAttributes($label)
Truth table66%2/3
A ||= B
A= $attributesB= $self->_DoAttributes($label)
ABresult
1X1
011
000
810 }
811 208 return $self->SUPER::_GenerateAnchor($whole_match, $link_text, $link_id, $url, $title, $attributes);
812 }
813  
814 # Add the extra cross-references to images that MultiMarkdown supports, and also
815 # the additional attributes.
816 6 ▶sub _GenerateImage {
Subroutine _GenerateImage: called CC=5 SCAR=16.8
817 # FIXME - Fugly, change to named params?
818 6 my ($self, $whole_match, $alt_text, $link_id, $url, $title, $attributes) = @_;
819  
820 6 ▶ if (defined $alt_text && length $alt_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alt_text and length $alt_text) 6 0 6
Condition33%1/3
defined $alt_text and length $alt_text
!ll&&!rl&&r
006
MC/DC0%0/2
defined $alt_text and length $alt_text
defined $alt_text length $alt_text
Truth table33%1/3
A and B
A= defined $alt_textB= length $alt_text
ABresult
0X0
100
111
821 6 my $label = $self->_Header2Label($alt_text);
822 6 $self->{_crossrefs}{$label} = "#$label";
823 6 ▶ $attributes .= $self->{img_ids} ? qq{ id="$label"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'img_ids'} ? : 6 0 6
824 }
825  
826 6 ▶ $attributes .= $self->_DoAttributes($link_id) if defined $link_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $link_id 4 2 6
827  
828 6 $self->SUPER::_GenerateImage($whole_match, $alt_text, $link_id, $url, $title, $attributes);
829 }
830  
831  
832 #
833 # MultiMarkdown specific routines
834 #
835  
836 # FIXME - This is really really ugly!
837 176 ▶sub _ParseMetaData {
Subroutine _ParseMetaData: called CC=14 SCAR=26.9
838 176 my ($self, $text) = @_;
839 176 my $clean_text = "";
840  
841 176 my ($inMetaData, $currentKey) = (1, '');
842  
843 176 foreach my $line ( split /\n/, $text ) {
844 3359 ▶ $line =~ /^\s*$/ and $inMetaData = 0 and $clean_text .= $line and next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*$/ and $inMetaData = 0 and $clean_text .= $line and next 0 3359 3359
Condition60%3/5
$line =~ /^\s*$/ and $inMetaData = 0 and $clean_text .= $line
!ll&&!rl&&r
335900
$line =~ /^\s*$/ and $inMetaData = 0
!ll
21531206
MC/DC0%0/2
$line =~ /^\s*$/ and $inMetaData = 0 and $clean_text .= $line
$line =~ /^\s*$/ $clean_text .= $line
Truth table33%1/3
A and B
A= $line =~ /^\s*$/B= $clean_text .= $line
ABresult
0X0
100
111
845 3359 ▶ if ($inMetaData) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inMetaData) { } 193 3166 3359
846 193 ▶ next unless $self->{use_metadata}; # We can come in here as use_metadata => 0, strip_metadata => 1
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"use_metadata"} 2 191 193
847 191 ▶ if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]+?):\s*(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([a-zA-Z0-9][0-9a-zA-Z _-]+?):\s*(.*)$/) { } 40 151 191
848 40 $currentKey = $1;
849 40 $currentKey =~ s/ / /g;
850 40 ▶ $self->{_metadata}{$currentKey} = defined $2 ? $2 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 40 0 40
851 40 ▶ if (lc($currentKey) eq "format") {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $currentKey eq "format") 2 38 40
852 2 $self->{document_format} = $self->{_metadata}{$currentKey};
853 }
854 40 ▶ if (lc($currentKey) eq "base url") {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $currentKey eq "base url") 12 28 40
855 12 $self->{base_url} = $self->{_metadata}{$currentKey};
856 }
857 40 ▶ if (lc($currentKey) eq "bibliography title") {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $currentKey eq "bibliography title") 0 40 40
858 0 $self->{bibliography_title} = $self->{_metadata}{$currentKey};
859 0 $self->{bibliography_title} =~ s/\s*$//;
860 }
861 }
862 else {
863 151 ▶ if ($currentKey eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($currentKey eq "") 150 1 151
864 # No metadata present
865 150 $clean_text .= "$line\n";
866 150 $inMetaData = 0;
867 150 next;
868 }
869 1 ▶ if ($line =~ /^\s*(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(.+)$/) 1 0 1
870 1 $self->{_metadata}{$currentKey} .= "\n$1";
871 }
872 }
873 }
874 else {
875 3166 $clean_text .= "$line\n";
876 }
877 }
878  
879 # Recheck for leading blank lines
880 176 $clean_text =~ s/^\n+//s;
881  
882 176 return $clean_text;
883 }
884  
885 # FIXME - This is really ugly, why do we match stuff and substitute it with the thing we just matched?
886 181 ▶sub _GenerateImageCrossRefs {
Subroutine _GenerateImageCrossRefs: called CC=3 SCAR=11.0
887 181 my ($self, $text) = @_;
888  
889 #
890 # First, handle reference-style labeled images: ![alt text][id]
891 #
892 181 $text =~ s{
893 ( # wrap whole match in $1
894 !\[
895 (.*?) # alt text = $2
896 \]
897  
898 [ ]? # one optional space
899 (?:\n[ ]*)? # one optional newline followed by spaces
900  
901 \[
902 (.*?) # id = $3
903 \]
904  
905 )
906 }{
907 7 my $whole_match = $1;
908 7 my $alt_text = $2;
909 7 my $link_id = lc $3;
910  
911 7 ▶ if ($link_id eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_id eq "") 2 5 7
912 2 $link_id = lc $alt_text; # for shortcut links like ![this][].
913 }
914  
915 7 $alt_text =~ s/"/&quot;/g;
916  
917 7 ▶ if (defined $self->{_urls}{$link_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_urls"}{$link_id}) 4 3 7
918 4 my $label = $self->_Header2Label($alt_text);
919 4 $self->{_crossrefs}{$label} = "#$label";
920 }
921  
922 7 $whole_match;
923 }xsge;
924  
925 #
926 # Next, handle inline images: ![alt text](url "optional title")
927 # Don't forget: encode * and _
928  
929 181 $text =~ s{
930 ( # wrap whole match in $1
931 !\[
932 (.*?) # alt text = $2
933 \]
934 \( # literal paren
935 [ \t]*
936 <?(\S+?)>? # src url = $3
937 [ \t]*
938 ( # $4
939 (['"]) # quote char = $5
940 (.*?) # title = $6
941 \5 # matching quote
942 [ \t]*
943 )? # title is optional
944 \)
945 )
946 }{
947 7 my $result;
948 7 my $whole_match = $1;
949 7 my $alt_text = $2;
950  
951 7 $alt_text =~ s/"/&quot;/g;
952 7 my $label = $self->_Header2Label($alt_text);
953 7 $self->{_crossrefs}{$label} = "#$label";
954 7 $whole_match;
955 }xsge;
956  
957 181 return $text;
958 }
959  
960 178 ▶sub _StripFootnoteDefinitions {
Subroutine _StripFootnoteDefinitions: called CC=2 SCAR=6.9
961 178 my ($self, $text) = @_;
962 178 my $less_than_tab = $self->{tab_width} - 1;
963  
964 178 ▶ while ($text =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \n\[\^([^\n]+?)\]\:[ \t]*# id = $1 \n? (.*?)\n{1,2} # end at new paragraph ((?=\n[ ]{0,$less_than_tab}\S)|\Z) # Lookahead for non-space at line-start, or end of doc /\n/sx) 7 178 185
965 \n\[\^([^\n]+?)\]\:[ \t]*# id = $1
966 \n?
967 (.*?)\n{1,2} # end at new paragraph
968 ((?=\n[ ]{0,$less_than_tab}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
969 }
970 {\n}sx)
971 {
972 7 my $id = $1;
973 7 my $footnote = "$2\n";
974 7 $footnote =~ s/^[ ]{0,$self->{tab_width}}//gm;
975  
976 7 $self->{_footnotes}{$self->_Id2Footnote($id)} = $footnote;
977 }
978  
979 178 return $text;
980 }
981  
982 178 ▶sub _DoFootnotes {
Subroutine _DoFootnotes: called CC=5 SCAR=16.3
983 178 my ($self, $text) = @_;
984  
985 178 ▶ return '' unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 178 178
986  
987 # First, run routines that get skipped in footnotes
988 178 foreach my $label (sort keys %{ $self->{_footnotes} }) {
989 7 my $footnote = $self->_RunBlockGamut($self->{_footnotes}{$label}, {wrap_in_p_tags => 1});
990 7 $footnote = $self->_UnescapeSpecialChars($footnote);
991 7 $footnote = $self->_DoMarkdownCitations($footnote);
992 7 $self->{_footnotes}{$label} = $footnote;
993 }
994  
995 178 my $footnote_counter = 0;
996  
997 178 $text =~ s{
998 \[\^(.*?)\] # id = $1
999 }{
1000 7 my $result = '';
1001 7 my $id = $self->_Id2Footnote($1);
1002  
1003 7 ▶ if (defined $self->{_footnotes}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_footnotes"}{$id}) 7 0 7
1004 7 $footnote_counter++;
1005 7 ▶ if ($self->{_footnotes}{$id} =~ /^glossary:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_footnotes'}{$id} =~ /^glossary:/i) { } 0 7 7
1006 0 $result = qq{<a href="$self->{self_url}#fn:$id" id="fnref:$id" class="footnote glossary">$footnote_counter</a>};
1007 }
1008 else {
1009 7 $result = qq{<a href="$self->{self_url}#fn:$id" id="fnref:$id" class="footnote">$footnote_counter</a>};
1010 }
1011 7 push (@{ $self->{_used_footnotes} }, $id);
1012 }
1013 7 $result;
1014 }xsge;
1015  
1016 178 return $text;
1017 }
1018  
1019 # TODO: remove. Doesn't make any difference to test suite pass/failure
1020 178 ▶sub _FixFootnoteParagraphs {
Subroutine _FixFootnoteParagraphs: called CC=1 SCAR=0.0
1021 178 my ($self, $text) = @_;
1022  
1023 178 $text =~ s(^<p></footnote>)(</footnote>)gm;
1024  
1025 178 return $text;
1026 }
1027  
1028 178 ▶sub _PrintFootnotes {
Subroutine _PrintFootnotes: called CC=6 SCAR=19.9
1029 178 my ($self) = @_;
1030 178 my $footnote_counter = 0;
1031 178 my $result;
1032  
1033 178 foreach my $id (@{ $self->{_used_footnotes} }) {
1034 7 $footnote_counter++;
1035 7 my $footnote = $self->{_footnotes}{$id};
1036  
1037 7 $footnote =~ s/(<\/(p(re)?|ol|ul)>)$//;
1038 7 my $footnote_closing_tag = $1;
1039 7 ▶ $footnote_closing_tag = '' if !defined $footnote_closing_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $footnote_closing_tag 0 7 7
1040  
1041 7 ▶ if ($footnote =~ s/^glossary:\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($footnote =~ s/^glossary:\s*//i) { } 0 7 7
1042 # Add some formatting for glossary entries
1043  
1044 0 $footnote =~ s{
1045 ^(.*?) # $1 = term
1046 \s*
1047 (?:\(([^\(\)]*)\)[^\n]*)? # $2 = optional sort key
1048 \n
1049 }{
1050 0 my $glossary = qq{<span class="glossary name">$1</span>};
1051  
1052 0 ▶ if ($2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2) 0 0 0
1053 0 $glossary.= qq{<span class="glossary sort" style="display:none">$2</span>};
1054 };
1055  
1056 0 $glossary . q{:<p>};
1057 }egsx;
1058  
1059 0 $result .= qq{<li id="fn:$id">$footnote<a href="$self->{self_url}#fnref:$id" class="reversefootnote">&#160;&#8617;</a>$footnote_closing_tag</li>\n\n};
1060 }
1061 else {
1062 7 $result .= qq{<li id="fn:$id">$footnote<a href="$self->{self_url}#fnref:$id" class="reversefootnote">&#160;&#8617;</a>$footnote_closing_tag</li>\n\n};
1063 }
1064 }
1065  
1066 178 ▶ if ($footnote_counter > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($footnote_counter > 0) { } 4 174 178
1067 4 $result = qq[\n\n<div class="footnotes">\n<hr$self->{empty_element_suffix}\n<ol>\n\n] . $result . "</ol>\n</div>";
1068 }
1069 else {
1070 174 $result = "";
1071 }
1072  
1073 178 return $result;
1074 }
1075  
1076 237 ▶sub _Header2Label {
Subroutine _Header2Label: called CC=2 SCAR=6.9
1077 237 my ($self, $header) = @_;
1078 237 my $label = lc $header;
1079 237 ▶ $label =~ s/ +/-/g if $self->{heading_ids_spaces_to_dash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"heading_ids_spaces_to_dash"} 43 194 237
1080  
1081 237 return $self->_id_handler->($label);
1082 }
1083  
1084 14 ▶sub _Id2Footnote {
Subroutine _Id2Footnote: called CC=1 SCAR=0.0
1085 # Since we prepend "fn:", we can allow leading digits in footnotes
1086 14 my ($self, $id) = @_;
1087 14 my $footnote = lc $id;
1088 14 $footnote =~ s/[^A-Za-z0-9:_.-]//g; # Strip illegal characters
1089 14 return $footnote;
1090 }
1091  
1092 4 ▶sub _xhtmlMetaData {
Subroutine _xhtmlMetaData: called CC=5 SCAR=16.1
1093 4 my ($self) = @_;
1094 # FIXME: Should not assume encoding
1095 4 my $result; # FIXME: This breaks some things in IE 6- = qq{<?xml version="1.0" encoding="UTF-8" ?>\n};
1096  
1097 # This screws up xsltproc - make sure to use `-nonet -novalid` if you
1098 # have difficulty
1099 4 $result .= qq{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n};
1100  
1101 4 $result.= "<html>\n\t<head>\n";
1102  
1103 4 foreach my $key (sort keys %{$self->{_metadata}} ) {
1104 15 ▶ if (lc($key) eq "title") {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $key eq 'title') { } 3 12 15
elsif (lc $key eq 'css') { } 3 9 12
elsif (lc $key eq 'xhtml header') { } 1 8 9
1105 3 $result.= "\t\t<title>" . encode_entities($self->{_metadata}{$key}) . "</title>\n";
1106 }
1107 elsif (lc($key) eq "css") {
1108 3 $result.= qq[\t\t<link type="text/css" rel="stylesheet" href="$self->{_metadata}{$key}"$self->{empty_element_suffix}\n];
1109 }
1110 elsif( lc($key) eq "xhtml header") {
1111 1 $result .= qq[\t\t$self->{_metadata}{$key}\n]
1112 }
1113 else {
1114 $result.= qq[\t\t<meta name="] . encode_entities($key) . qq[" ]
1115 8 . qq[content="] . encode_entities($self->{_metadata}{$key}) . qq["$self->{empty_element_suffix}\n];
1116 }
1117 }
1118 4 $result.= "\t</head>\n";
1119  
1120 4 return $result;
1121 }
1122  
1123 177 ▶sub _textMetaData {
Subroutine _textMetaData: called CC=4 SCAR=13.9
1124 177 my ($self) = @_;
1125 177 my $result = "";
1126  
1127 177 ▶ return $result if $self->{strip_metadata};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strip_metadata"} 2 175 177
1128  
1129 175 foreach my $key (sort keys %{$self->{_metadata}} ) {
1130 23 $result .= "$key: $self->{_metadata}{$key}\n";
1131 }
1132 175 $result =~ s/\s*\n/<br$self->{empty_element_suffix}\n/g;
1133  
1134 175 ▶ if ($result ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne "") 16 159 175
1135 16 $result.= "\n";
1136 }
1137  
1138 175 return $result;
1139 }
1140  
1141 1348 ▶sub _UseWikiLinks {
Subroutine _UseWikiLinks: called CC=4 SCAR=14.0
1142 1348 my ($self) = @_;
1143 1348 ▶ return 1 if $self->{use_wikilinks};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"use_wikilinks"} 54 1294 1348
1144 1294 my ($k) = grep { /use wikilinks/i } keys %{$self->{_metadata}};
1145 1294 ▶ return unless $k;
Branch: 1 branch
BranchTrueFalseTotal
unless $k 1277 17 1294
1146 17 ▶ return 1 if $self->{_metadata}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_metadata"}{$k} 17 0 17
1147 0 return;
1148 }
1149  
1150 33 ▶sub _CreateWikiLink {
Subroutine _CreateWikiLink: called CC=1 SCAR=0.0
1151 33 my ($self, $title) = @_;
1152  
1153 33 my $id = $title;
1154 33 $id =~ s/ /_/g;
1155 33 $id =~ s/__+/_/g;
1156 33 $id =~ s/^_//g;
1157 33 $id =~ s/_$//;
1158  
1159 33 $title =~ s/_/ /g;
1160  
1161 33 return "[$title](" . $self->{base_url} . "$id)";
1162 }
1163  
1164 34 ▶sub _DoWikiLinks {
Subroutine _DoWikiLinks: called CC=2 SCAR=7.0
1165  
1166 34 my ($self, $text) = @_;
1167 34 my $WikiWord = '[A-Z]+[a-z\x80-\xff]+[A-Z][A-Za-z\x80-\xff]*';
1168 34 my $FreeLinkPattern = "([-,.()' _0-9A-Za-z\x80-\xff]+)";
1169  
1170 34 ▶ if ($self->_UseWikiLinks()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_UseWikiLinks) 34 0 34
1171 # FreeLinks
1172 34 $text =~ s{
1173 \[\[($FreeLinkPattern)\]\]
1174 }{
1175 1 my $label = $1;
1176 1 $label =~ s{
1177 ([\s\>])($WikiWord)
1178 }{
1179 0 $1 ."\\" . $2
1180 }xsge;
1181  
1182 1 $self->_CreateWikiLink($label)
1183 }xsge;
1184  
1185 # WikiWords
1186 34 $text =~ s{
1187 ([\s])($WikiWord)
1188 }{
1189 29 $1 . $self->_CreateWikiLink($2)
1190 }xsge;
1191  
1192 # Catch WikiWords at beginning of text
1193 34 $text =~ s{^($WikiWord)
1194 }{
1195 3 $self->_CreateWikiLink($1)
1196 }xse;
1197 }
1198  
1199  
1200 34 return $text;
1201 }
1202  
1203 181 ▶sub _UnescapeWikiWords {
Subroutine _UnescapeWikiWords: called CC=1 SCAR=0.0
1204 181 my ($self, $text) = @_;
1205 181 my $WikiWord = '[A-Z]+[a-z\x80-\xff]+[A-Z][A-Za-z\x80-\xff]*';
1206  
1207 # Unescape escaped WikiWords
1208 181 $text =~ s/(?<=\B)\\($WikiWord)/$1/g;
1209  
1210 181 return $text;
1211 }
1212  
1213 240 ▶sub _DoTables {
Subroutine _DoTables: called CC=23 SCAR=31.6
1214 240 my ($self, $text) = @_;
1215  
1216 240 ▶ return $text if $self->{disable_tables};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disable_tables"} 4 236 240
1217  
1218 236 my $less_than_tab = $self->{tab_width} - 1;
1219  
1220 # Algorithm inspired by PHP Markdown Extra's
1221 # <http://www.michelf.com/projects/php-markdown/>
1222  
1223 # Reusable regexp's to match table
1224  
1225 236 my $line_start = qr{
1226 [ ]{0,$less_than_tab}
1227 }mx;
1228  
1229 236 my $table_row = qr{
1230 [^\n]*?\|[^\n]*?\n
1231 }mx;
1232  
1233 236 my $first_row = qr{
1234 $line_start
1235 \S+.*?\|.*?\n
1236 }mx;
1237  
1238 236 my $table_rows = qr{
1239 (\n?$table_row)
1240 }mx;
1241  
1242 236 my $table_caption = qr{
1243 $line_start
1244 \[.*?\][ \t]*\n
1245 }mx;
1246  
1247 236 my $table_divider = qr{
1248 $line_start
1249 [\|\-\:\.][ \-\|\:\.]* \| [ \-\|\:\.]*
1250 }mx;
1251  
1252 236 my $whole_table = qr{
1253 ($table_caption)? # Optional caption
1254 ($first_row # First line must start at beginning
1255 ($table_row)*?)? # Header Rows
1256 $table_divider # Divider/Alignment definitions
1257 $table_rows+ # Body Rows
1258 ($table_caption)? # Optional caption
1259 }mx;
1260  
1261  
1262 # Find whole tables, then break them up and process them
1263  
1264 236 $text =~ s{
1265 ^($whole_table) # Whole table in $1
1266 (\n|\Z) # End of file or 2 blank lines
1267 }{
1268 3 my $table = $1;
1269 3 my $result = "<table>\n";
1270 3 my @alignments;
1271 3 my $use_row_header = 0;
1272  
1273 # Add Caption, if present
1274  
1275 3 ▶ if ($table =~ s/^$line_start\[\s*(.*?)\s*\](\[\s*(.*?)\s*\])?[ \t]*$//m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table =~ s/^$line_start\[\s*(.*?)\s*\](\[\s*(.*?)\s*\])?[ \t]*$//m) 2 1 3
1276 2 ▶ if (defined $3) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $3) { } 2 0 2
1277 # add caption id to cross-ref list
1278 2 my $table_id = $self->_Header2Label($3);
1279 2 $result .= qq{<caption id="$table_id">} . $self->_RunSpanGamut($1). "</caption>\n";
1280  
1281 2 $self->{_crossrefs}{$table_id} = "#$table_id";
1282 2 $self->{_titles}{$table_id} = "$1";
1283 }
1284 else {
1285 0 $result .= "<caption>" . $self->_RunSpanGamut($1). "</caption>\n";
1286 }
1287 }
1288  
1289 # If a second "caption" is present, treat it as a summary
1290 # However, this is not valid in XHTML 1.0 Strict
1291 # But maybe in future
1292  
1293 # A summary might be longer than one line
1294 3 ▶ if ($table =~ s/\n$line_start\[\s*(.*?)\s*\][ \t]*\n/\n/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table =~ s/\n$line_start\[\s*(.*?)\s*\][ \t]*\n/\n/s) 0 3 3
1295 # $result .= "<summary>" . $self->_RunSpanGamut($1) . "</summary>\n";
1296 }
1297  
1298 # Now, divide table into header, alignment, and body
1299  
1300 # First, add leading \n in case there is no header
1301  
1302 3 $table = "\n" . $table;
1303  
1304 # Need to be greedy
1305  
1306 3 $table =~ s/\n($table_divider)\n(($table_rows)+)//s;
1307  
1308 3 my $alignment_string = $1;
1309 3 my $body = $2;
1310  
1311 # Process column alignment
1312 3 ▶ while ($alignment_string =~ /\|?\s*(.+?)\s*(\||\Z)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alignment_string =~ /\|?\s*(.+?)\s*(\||\Z)/gs) 8 3 11
1313 8 my $cell = $self->_RunSpanGamut($1);
1314 8 ▶ if ($cell =~ /\:$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /\:$/) { } 4 4 8
1315 4 ▶ if ($cell =~ /^\:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /^\:/) { } 2 2 4
1316 2 $result .= qq[<col align="center"$self->{empty_element_suffix}\n];
1317 2 push(@alignments,"center");
1318 }
1319 else {
1320 2 $result .= qq[<col align="right"$self->{empty_element_suffix}\n];
1321 2 push(@alignments,"right");
1322 }
1323 }
1324 else {
1325 4 ▶ if ($cell =~ /^\:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /^\:/) { } 0 4 4
1326 0 $result .= qq[<col align="left"$self->{empty_element_suffix}\n];
1327 0 push(@alignments,"left");
1328 }
1329 else {
1330 4 ▶ if (($cell =~ /^\./) || ($cell =~ /\.$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /^\./ or $cell =~ /\.$/) { } 0 4 4
Condition33%1/3
$cell =~ /^\./ or $cell =~ /\.$/
l!l&&r!l&&!r
004
MC/DC0%0/2
$cell =~ /^\./ or $cell =~ /\.$/
$cell =~ /^\./ $cell =~ /\.$/
Truth table33%1/3
A or B
A= $cell =~ /^\./B= $cell =~ /\.$/
ABresult
1X1
011
000
1331 0 $result .= qq[<col align="char"$self->{empty_element_suffix}\n];
1332 0 push(@alignments,"char");
1333 }
1334 else {
1335 4 $result .= "<col$self->{empty_element_suffix}\n";
1336 4 push(@alignments,"");
1337 }
1338 }
1339 }
1340 }
1341  
1342 # Process headers
1343 3 $table =~ s/^\n+//s;
1344  
1345 3 $result .= "<thead>\n";
1346  
1347 # Strip blank lines
1348 3 $table =~ s/\n[ \t]*\n/\n/g;
1349  
1350 3 foreach my $line (split(/\n/, $table)) {
1351 # process each line (row) in table
1352 3 $result .= "<tr>\n";
1353 3 my $count=0;
1354 3 ▶ while ($line =~ /\|?\s*([^\|]+?)\s*(\|+|\Z)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\|?\s*([^\|]+?)\s*(\|+|\Z)/gs) 8 3 11
1355 # process contents of each cell
1356 8 my $cell = $self->_RunSpanGamut($1);
1357 8 my $ending = $2;
1358 8 my $colspan = "";
1359 8 ▶ if ($ending =~ s/^\s*(\|{2,})\s*$/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ending =~ s/^\s*(\|{2,})\s*$/$1/) 1 7 8
1360 1 $colspan = " colspan=\"" . length($ending) . "\"";
1361 }
1362 8 $result .= "\t<th$colspan>$cell</th>\n";
1363 8 ▶ if ( $count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 0) 3 5 8
1364 3 ▶ if ($cell =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /^\s*$/) { } 1 2 3
1365 1 $use_row_header = 1;
1366 }
1367 else {
1368 2 $use_row_header = 0;
1369 }
1370 }
1371 8 $count++;
1372 }
1373 3 $result .= "</tr>\n";
1374 }
1375  
1376 # Process body
1377  
1378 3 $result .= "</thead>\n<tbody>\n";
1379  
1380 3 foreach my $line (split(/\n/, $body)) {
1381 # process each line (row) in table
1382 13 ▶ if ($line =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/) 2 11 13
1383 2 $result .= "</tbody>\n\n<tbody>\n";
1384 2 next;
1385 }
1386 11 $result .= "<tr>\n";
1387 11 my $count=0;
1388 11 ▶ while ($line =~ /\|?\s*([^\|]+?)\s*(\|+|\Z)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\|?\s*([^\|]+?)\s*(\|+|\Z)/gs) 27 11 38
1389 # process contents of each cell
1390 26 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1391 27 my $cell = $self->_RunSpanGamut($1);
1392 27 my $ending = $2;
1393 27 my $colspan = "";
1394 27 my $cell_type = "td";
1395 27 ▶ if ($count == 0 && $use_row_header == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 0 and $use_row_header == 1) 0 27 27
Condition66%2/3
$count == 0 and $use_row_header == 1
!ll&&!rl&&r
16110
MC/DC0%0/2
$count == 0 and $use_row_header == 1
$count == 0 $use_row_header == 1
Truth table66%2/3
A and B
A= $count == 0B= $use_row_header == 1
ABresult
0X0
100
111
1396 0 $cell_type = "th";
1397 }
1398 27 ▶ if ($ending =~ s/^\s*(\|{2,})\s*$/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ending =~ s/^\s*(\|{2,})\s*$/$1/) 6 21 27
1399 6 $colspan = " colspan=\"" . length($ending) . "\"";
1400 }
1401 27 ▶ if ($alignments[$count] !~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $alignments[$count] =~ /^\s*$/) { } 14 13 27
1402 14 $result .= "\t<$cell_type$colspan align=\"$alignments[$count]\">$cell</$cell_type>\n";
1403 }
1404 else {
1405 13 $result .= "\t<$cell_type$colspan>$cell</$cell_type>\n";
1406 }
1407 27 $count++;
1408 }
1409 11 $result .= "</tr>\n";
1410 }
1411  
1412 3 $result .= "</tbody>\n</table>\n";
1413 3 $result
1414 }egmx;
1415  
1416 236 my $table_body = qr{
1417 ( # wrap whole match in $2
1418  
1419 (.*?\|.*?)\n # wrap headers in $3
1420  
1421 [ ]{0,$less_than_tab}
1422 ($table_divider) # alignment in $4
1423  
1424 ( # wrap cells in $5
1425 $table_rows
1426 )
1427 )
1428 }mx;
1429  
1430 236 return $text;
1431 }
1432  
1433 127 ▶sub _DoAttributes {
Subroutine _DoAttributes: called CC=4 SCAR=13.9
1434 127 my ($self, $id) = @_;
1435 127 my $result = "";
1436  
1437 127 ▶ if (defined $self->{_attributes}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_attributes"}{$id}) 6 121 127
1438 6 ▶ while ($self->{_attributes}{$id} =~ s/(\S+)="(.*?)"//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_attributes"}{$id} =~ s/(\S+)="(.*?)"//) 3 6 9
1439 3 $result .= qq{ $1="$2"};
1440 }
1441 6 ▶ while ($self->{_attributes}{$id} =~ /(\S+)=(\S+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_attributes"}{$id} =~ /(\S+)=(\S+)/g) 9 6 15
1442 9 $result .= qq{ $1="$2"};
1443 }
1444 }
1445  
1446 127 return $result;
1447 }
1448  
1449 181 ▶sub _StripMarkdownReferences {
Subroutine _StripMarkdownReferences: called CC=2 SCAR=6.9
1450 181 my ($self, $text) = @_;
1451 181 my $less_than_tab = $self->{tab_width} - 1;
1452  
1453 181 ▶ while ($text =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \n\[\#(.+?)\]:[ \t]* # id = $1 \n? (.*?)\n{1,2} # end at new paragraph ((?=\n[ ]{0,$less_than_tab}\S)|\Z) # Lookahead for non-space at line-start, or end of doc /\n/sx) 5 181 186
1454 \n\[\#(.+?)\]:[ \t]* # id = $1
1455 \n?
1456 (.*?)\n{1,2} # end at new paragraph
1457 ((?=\n[ ]{0,$less_than_tab}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
1458 }
1459 {\n}sx)
1460 {
1461 5 my $id = $1;
1462 5 my $reference = "$2\n";
1463  
1464 5 $reference =~ s/^[ ]{0,$self->{tab_width}}//gm;
1465  
1466 5 $reference = $self->_RunBlockGamut($reference, {wrap_in_p_tags => 0});
1467  
1468 5 $self->{_references}{$id} = $reference;
1469 }
1470  
1471 181 return $text;
1472 }
1473  
1474 185 ▶sub _DoMarkdownCitations {
Subroutine _DoMarkdownCitations: called CC=8 SCAR=21.2
1475 185 my ($self, $text) = @_;
1476  
1477 185 $text =~ s{
1478 \[([^\[]*?)\] # citation text = $1
1479 [ ]? # one optional space
1480 (?:\n[ ]*)? # one optional newline followed by spaces
1481 \[\#(.*?)\] # id = $2
1482 }{
1483 6 my $result;
1484 6 my $anchor_text = $1;
1485 6 my $id = $2;
1486 6 my $count;
1487  
1488 6 ▶ if (defined $self->{_references}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_references'}{$id}) { } 6 0 6
1489 6 my $citation_counter=0;
1490  
1491 # See if citation has been used before
1492 6 foreach my $old_id (@{ $self->{_used_references} }) {
1493 12 $citation_counter++;
1494 12 ▶ $count = $citation_counter if ($old_id eq $id);
Branch: 1 branch
BranchTrueFalseTotal
if $old_id eq $id 3 9 12
1495 }
1496  
1497 6 ▶ if (! defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 3 3 6
1498 3 $count = ++$self->{_citation_counter};
1499 3 push (@{ $self->{_used_references} }, $id);
1500 }
1501  
1502 6 $result = qq[<span class="markdowncitation"> (<a href="#$id">$count</a>];
1503  
1504 6 ▶ if ($anchor_text ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($anchor_text ne "") 5 1 6
1505 5 $result .= qq[, <span class="locator">$anchor_text</span>];
1506 }
1507  
1508 6 $result .= ")</span>";
1509 }
1510 else {
1511 # No reference exists
1512 0 $result = qq[<span class="externalcitation"> (<a id="$id">$id</a>];
1513  
1514 0 ▶ if ($anchor_text ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($anchor_text ne "") 0 0 0
1515 0 $result .= qq[, <span class="locator">$anchor_text</span>];
1516 }
1517  
1518 0 $result .= ")</span>";
1519 }
1520  
1521 6 ▶ if ($self->_Header2Label($anchor_text) eq "notcited"){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_Header2Label($anchor_text) eq "notcited") 1 5 6
1522 1 $result = qq[<span class="notcited" id="$id"/>];
1523 }
1524 6 $result;
1525 }xsge;
1526  
1527 185 return $text;
1528 }
1529  
1530 178 ▶sub _PrintMarkdownBibliography {
Subroutine _PrintMarkdownBibliography: called CC=3 SCAR=11.0
1531 178 my ($self) = @_;
1532 178 my $citation_counter = 0;
1533 178 my $result;
1534  
1535 178 foreach my $id (@{ $self->{_used_references} }) {
1536 3 $citation_counter++;
1537 3 $result .= qq|<div id="$id"><p>[$citation_counter] <span class="item">$self->{_references}{$id}</span></p></div>\n\n|;
1538 }
1539 178 $result .= "</div>";
1540  
1541 178 ▶ if ($citation_counter > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($citation_counter > 0) { } 1 177 178
1542 1 $result = qq[\n\n<div class="bibliography">\n<hr$self->{empty_element_suffix}\n<p>$self->{bibliography_title}</p>\n\n] . $result;
1543 }
1544 else {
1545 177 $result = "";
1546 }
1547  
1548 178 return $result;
1549 }
1550  
1551 26 1;
1552  
1553 =back
1554  
1555 =head1 BUGS
1556  
1557 Open an issue in the GitHub repo:
1558  
1559 https://github.com/briandfoy/text-multimarkdown/issues
1560  
1561 Please include with your report: (1) the example input; (2) the output
1562 you expected; (3) the output Markdown actually produced.
1563  
1564 =head1 VERSION HISTORY
1565  
1566 See the Changes file for detailed release notes for this version.
1567  
1568 =head1 AUTHOR
1569  
1570 =over 4
1571  
1572 =item * John Gruber http://daringfireball.net/
1573  
1574 =item * PHP port and other contributions by Michel Fortin http://michelf.com/
1575  
1576 =item * MultiMarkdown changes by Fletcher Penney http://fletcher.freeshell.org/
1577  
1578 =item * CPAN Module Text::MultiMarkdown (based on Text::Markdown by Sebastian Riedel) originally by Darren Kulp (http://kulp.ch/)
1579  
1580 =item * This module was maintained by: Tomas Doran http://www.bobtfish.net/
1581  
1582 =item * This module is currently maintained by brian d foy
1583  
1584 =back
1585  
1586 =head1 THIS DISTRIBUTION
1587  
1588 Please note that this distribution is a fork of Fletcher Penny's MultiMarkdown project,
1589 and it I<is not> in any way blessed by him.
1590  
1591 Whilst this code aims to be compatible with the original MultiMarkdown (and incorporates
1592 and passes the MultiMarkdown test suite) whilst fixing a number of bugs in the original -
1593 there may be differences between the behaviour of this module and MultiMarkdown. If you find
1594 any differences where you believe Text::MultiMarkdown behaves contrary to the MultiMarkdown spec,
1595 please report them as bugs.
1596  
1597 =head1 SOURCE CODE
1598  
1599 You can find the source code repository for L<Text::Markdown> and L<Text::MultiMarkdown>
1600 on GitHub at <http://github.com/bobtfish/text-markdown>.
1601  
1602 =head1 COPYRIGHT AND LICENSE
1603  
1604 Original Code Copyright (c) 2003-2004 John Gruber
1605 <http://daringfireball.net/>
1606 All rights reserved.
1607  
1608 MultiMarkdown changes Copyright (c) 2005-2006 Fletcher T. Penney
1609 <http://fletcher.freeshell.org/>
1610 All rights reserved.
1611  
1612 Text::MultiMarkdown changes Copyright (c) 2006-2009 Darren Kulp
1613 <http://kulp.ch> and Tomas Doran <http://www.bobtfish.net>
1614  
1615 Redistribution and use in source and binary forms, with or without
1616 modification, are permitted provided that the following conditions are
1617 met:
1618  
1619 * Redistributions of source code must retain the above copyright notice,
1620 this list of conditions and the following disclaimer.
1621  
1622 * Redistributions in binary form must reproduce the above copyright
1623 notice, this list of conditions and the following disclaimer in the
1624 documentation and/or other materials provided with the distribution.
1625  
1626 * Neither the name "Markdown" nor the names of its contributors may
1627 be used to endorse or promote products derived from this software
1628 without specific prior written permission.
1629  
1630 This software is provided by the copyright holders and contributors "as
1631 is" and any express or implied warranties, including, but not limited
1632 to, the implied warranties of merchantability and fitness for a
1633 particular purpose are disclaimed. In no event shall the copyright owner
1634 or contributors be liable for any direct, indirect, incidental, special,
1635 exemplary, or consequential damages (including, but not limited to,
1636 procurement of substitute goods or services; loss of use, data, or
1637 profits; or business interruption) however caused and on any theory of
1638 liability, whether in contract, strict liability, or tort (including
1639 negligence or otherwise) arising in any way out of the use of this
1640 software, even if advised of the possibility of such damage.
1641  
1642 =cut
« script/MultiMarkdown.pl ↑ Summary