GooglewikiFormat.pm

filter: Statementstmt 96.9% 193 / 199 Branchbran 87.9% 51 / 58 Conditioncond 67.7% 21 / 31 MC/DCmcdc 52.9% 9 / 17 Subroutinesub 100% 35 / 35 Podpod 0.0% 0 / 14 totaltotal 87.2% 309 / 354 CC 49 SCAR 14.5
CC
49
Cov
92%
start_block
10.0
process_block
7.7
format
7.0
CRAP 4.3
↑ Summary GooglewikiFormat/Blocks.pm »
1 package Text::GooglewikiFormat;
2
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Text::GooglewikiFormat::Blocks;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 4 ▶use vars qw( $VERSION %tags $indent $code_delimiters);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 $VERSION = '0.05';
13 4 $indent = qr/^(?:\t+|\s{4,})/;
14 4 $code_delimiters = 0;
15 %tags = (
16 indent => qr/^(?:\t+|\s{1,})/,
17 newline => '<br />',
18
19 8 ▶ strong => sub { " <strong>$_[0]</strong> " },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 6 ▶ italic => sub { " <i>$_[0]</i> " },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 2 ▶ strike => sub { qq~ <span style="text-decoration: line-through">$_[0]</span> ~ },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 ▶ superscript => sub { "<sup>$_[0]</sup>" },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 ▶ subscript => sub { "<sub>$_[0]</sub>" },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 ▶ inline => sub { "<tt>$_[0]</tt>" },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 strong_tag => qr/(^|\s+)\*(.+?)\*(\s+|$)/,
26 italic_tag => qr/(^|\s+)_(.+?)_(\s+|$)/,
27 strike_tag => qr/(^|\s+)\~\~(.+?)\~\~(\s+|$)/,
28 superscript_tag => qr/\^(.+?)\^/,
29 subscript_tag => qr/\,\,(.+?)\,\,/,
30 inline_tag => qr/\`(.+?)\`/,
31
32 6 ▶ header => [ '', '', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 6 my $level = length $_[2];
34 6 return "<h$level>", format_line($_[3], @_[-2, -1]), "</h$level>" }
35 ],
36 unordered => ["<ul>", "</ul>", '<li>', " </li>"],
37 ordered => ["<ol>", "</ol>", '<li>', " </li>"],
38
39 7 ▶ code => [ '<pre class="prettyprint">', "</pre>", sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
40 7 my ($line, $level, $args, $tags, $opts) = @_;
41 7 $line =~ s/(^\{\{\{|\}\}\}$)//isg;
42 7 ▶ return (length($line)) ? $line . "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 4 3 7
43 } ],
44 paragraph => [ '<p>', "</p>", '', "<br />", 1 ],
45 quote => [ '<blockquote>', "</blockquote>", '', "\n"],
46 13 ▶ table => [ '<table>', '</table>', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 13 my ($line, $level, $args, $tags, $opts) = @_;
48 13 $line =~ s/(^\|\||\|\|$)//isg;
49 13 $line =~ s/\|\|/\<\/td\>\<td style\=\"border\: 1px solid \#aaa\; padding\: 5px\;\"\>/isg;
50 13 $line = qq~<tr><td style="border: 1px solid #aaa; padding: 5px;">$line</td></tr> ~;
51 13 return $line,
52 } ],
53
54
55 blocks => {
56 header => qr/^(=+)(.+)\1/,
57 ordered => qr/^\#\s*/,
58 unordered => qr/^\*\s*/,
59 quote => qr/^ /,
60 paragraph => qr/^/,
61 table => qr/^\|\|/,
62 },
63
64 8 indented => { map { $_ => 1 } qw( ordered unordered )},
65 4 nests => { map { $_ => 1 } qw( ordered unordered code table ) },
66
67 blockorder =>
68 [qw( header ordered unordered table quote paragraph code )],
69
70 link => \&make_html_link,
71 extended_link_delimiters => [qw( [ ] )],
72 schemas => [ qw( http https ftp mailto gopher ) ],
73 );
74
75 4 ▶sub merge_hash {
Subroutine merge_hash: called CC=5 SCAR=16.1
Pod undocumented
76 4 my ($from, $to) = @_;
77
78 4 ▶ while (my ($key, $value) = each %$from)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$from) 40 4 44
79 {
80 40 ▶ if ((reftype( $value ) || '' ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($value) || "") eq "HASH") 3 37 40
Condition100%2/2
builtin::reftype($value) || ''
l!l
337
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
81 {
82 3 ▶ $to->{$key} = {} unless defined $to->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to->{$key} 0 3 3
83 3 merge_hash( $value, $to->{$key} );
84 3 next;
85 }
86
87 37 $to->{$key} = $value;
88 }
89
90 4 return $to;
91 }
92
93 15 ▶sub format {
Subroutine format: called CC=7 SCAR=19.5
Pod undocumented
94 15 my ($text, $newtags, $opts) = @_;
95
96 15 ▶ $opts ||=
Condition100%2/2
$opts ||= {"prefix", "", "extended", 1, "implicit_links", 1, "absolute_links", 1}
l!l
114
MC/DC100%1/1
$opts ||= {"prefix", "", "extended", 1, "implicit_links", 1, "absolute_links", 1}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
97 {
98 prefix => '', extended => 1, implicit_links => 1, absolute_links => 1
99 };
100
101 15 my %tags = %tags;
102
103 15 ▶ merge_hash( $newtags, \%tags )
Branch: 1 branch
BranchTrueFalseTotal
if defined $newtags and (builtin::reftype($newtags) || "") eq "HASH" 1 14 15
Condition60%3/5
defined $newtags and (builtin::reftype($newtags) || "") eq "HASH"
!ll&&!rl&&r
1401
builtin::reftype($newtags) || ''
l!l
10
MC/DC50%1/2
defined $newtags and (builtin::reftype($newtags) || "") eq "HASH"
defined $newtags builtin::reftype($newtags)
Truth table66%2/3
A and B
A= defined $newtagsB= builtin::reftype($newtags)
ABresult
0X0
100
111
104 if defined $newtags and ( reftype( $newtags ) || '' ) eq 'HASH';
105 check_blocks( \%tags )
106 15 ▶ if exists $newtags->{blockorder} or exists $newtags->{blocks};
Branch: 1 branch
BranchTrueFalseTotal
if exists $newtags->{"blockorder"} or exists $newtags->{"blocks"} 1 14 15
Condition66%2/3
exists $newtags->{"blockorder"} or exists $newtags->{"blocks"}
l!l&&r!l&&!r
1014
MC/DC50%1/2
exists $newtags->{"blockorder"} or exists $newtags->{"blocks"}
exists $newtags->{"blockorder"} exists $newtags->{"blocks"}
Truth table66%2/3
A or B
A= exists $newtags->{"blockorder"}B= exists $newtags->{"blocks"}
ABresult
1X1
011
000
107
108 # find URIs
109 6 ▶ my $finder = URI::Find->new( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
110 6 my($uri, $orig_uri) = @_;
111 # If your link points to an image (that is, if it ends in .png, .gif, .jpg or .jpeg), it will get inserted as an image into the page:
112 6 ▶ if ($uri =~ /\.(jpe?g|png|gif)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /\.(jpe?g|png|gif)$/) { } 2 4 6
113 2 return qq|<img src="$uri" /> |;
114 } else {
115 4 return qq|[$uri]|;
116 }
117 15 } );
118 15 $finder->find(\$text);
119 15 $text =~ s/\[\[(.+?)\]/\[$1/isg; # dirty hack
120
121 15 my @blocks = find_blocks( $text, \%tags, $opts );
122 15 @blocks = merge_blocks( \@blocks );
123 15 @blocks = nest_blocks( \@blocks );
124
125 15 return process_blocks( \@blocks, \%tags, $opts );
126 }
127
128 sub check_blocks
129 1 ▶{
Subroutine check_blocks: called CC=2 SCAR=7.5
Pod undocumented
130 1 my $tags = shift;
131 1 my %blocks = %{ $tags->{blocks} };
132 1 delete @blocks{ @{ $tags->{blockorder} } };
133
134 1 ▶ if (keys %blocks)
Branch: 1 branch
BranchTrueFalseTotal
if (keys %blocks) 0 1 1
135 {
136 0 require Carp;
137 0 Carp::carp(
138 "No order specified for blocks '" . join(', ', keys %blocks )
139 . "'\n"
140 )
141 }
142 }
143
144 sub find_blocks
145 15 ▶{
Subroutine find_blocks: called CC=3 SCAR=11.0
Pod undocumented
146 15 my ($text, $tags, $opts) = @_;
147
148 15 my @blocks;
149 15 for my $line ( split(/\r?\n/, $text) )
150 {
151 68 my $block = start_block( $line, $tags, $opts );
152 68 ▶ push @blocks, $block if $block;
Branch: 1 branch
BranchTrueFalseTotal
if $block 68 0 68
153 }
154
155 15 return @blocks;
156 }
157
158 sub start_block
159 68 ▶{
Subroutine start_block: called CC=10 SCAR=23.0
Pod undocumented
160 68 my ($text, $tags, $opts) = @_;
161 68 ▶ return new_block( 'end', level => 0 ) unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 11 57 68
162
163 # for {{{ }}}
164 57 ▶ if ($text =~ /^\}\}\}$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /^\}\}\}$/) { } 3 54 57
elsif ($Text::GooglewikiFormat::code_delimiters or $text =~ /^\{\{\{$/) { } 7 47 54
165 3 $code_delimiters = 0;
166 3 return new_block( 'end', level => 1 );
167 ▶ } elsif ($code_delimiters or $text =~ /^\{\{\{$/) {
Condition100%3/3
$Text::GooglewikiFormat::code_delimiters or $text =~ /^\{\{\{$/
l!l&&r!l&&!r
4347
MC/DC100%2/2
$Text::GooglewikiFormat::code_delimiters or $text =~ /^\{\{\{$/
$Text::GooglewikiFormat::code_delimiters $text =~ /^\{\{\{$/
Truth table100%3/3
A or B
A= $Text::GooglewikiFormat::code_delimitersB= $text =~ /^\{\{\{$/
ABresult
1X1
011
000
168 7 $code_delimiters = 1;
169 7 return new_block( 'code', level => 1, text => $text, opts => $opts, tags => $tags );
170 }
171
172 47 for my $block (@{ $tags->{blockorder} })
173 {
174 208 my ($line, $level, $indentation) = ( $text, 0, '' );
175
176 208 ▶ if ($tags->{indented}{$block})
Branch: 1 branch
BranchTrueFalseTotal
if ($tags->{"indented"}{$block}) 80 128 208
177 {
178 80 ($level, $line, $indentation) = get_indentation( $tags, $line );
179 80 ▶ next unless $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 66 14 80
180 }
181
182 142 my $marker_removed = length ($line =~ s/$tags->{blocks}{$block}//);
183
184 142 ▶ next unless $marker_removed;
Branch: 1 branch
BranchTrueFalseTotal
unless $marker_removed 95 47 142
185
186 return new_block( $block,
187 47 ▶ args => [ grep { defined } $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
Condition100%2/2
$level || 0
l!l
443
MC/DC100%1/1
$level || 0
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
188 level => $level || 0,
189 opts => $opts,
190 text => $line,
191 tags => $tags,
192 );
193 }
194 }
195
196 # merge_blocks() and nest_blocks()
197 BEGIN
198 4 ▶{
Subroutine BEGIN: called CC=2 SCAR=6.9
199 4 for my $op (qw( merge nest ))
200 {
201 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
202 8 *{ $op . '_blocks' } = sub
203 30 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
204 30 my $blocks = shift;
205 30 ▶ return unless @$blocks;
Branch: 1 branch
BranchTrueFalseTotal
unless @$blocks 0 30 30
206
207 30 my @processed = shift @$blocks;
208
209 30 for my $block (@$blocks)
210 {
211 78 push @processed, $processed[-1]->$op( $block );
212 }
213
214 30 return @processed;
215 8 };
216 }
217 }
218
219 sub process_blocks
220 15 ▶{
Subroutine process_blocks: called CC=3 SCAR=11.0
Pod undocumented
221 15 my ($blocks, $tags, $opts) = @_;
222
223 15 my @open;
224 15 for my $block (@$blocks)
225 {
226 40 ▶ push @open, process_block( $block, $tags, $opts )
Branch: 1 branch
BranchTrueFalseTotal
unless $block->type eq "end" 27 13 40
227 unless $block->type() eq 'end';
228 }
229
230 15 return join('', @open);
231 }
232
233 27 ▶sub process_block {
Subroutine process_block: called CC=7 SCAR=20.4
Pod undocumented
234 27 my ($block, $tags, $opts) = @_;
235
236 my ($start, $end, $start_line, $end_line, $between)
237 27 = @{ $tags->{ $block->type() } };
238
239 27 my @text;
240 27 for my $line ( $block->formatted_text() )
241 {
242 54 ▶ if (blessed( $line ))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($line)) 0 54 54
243 {
244 0 ▶ my $prev_end = pop @text || ();
Condition0%0/3
pop @text || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
pop @text || ()
pop @text ()
Truth table0%0/3
A || B
A= pop @textB= ()
ABresult
1X1
011
000
245 0 push @text, process_block( $line, $tags, $opts ), $prev_end;
246 0 next;
247 }
248
249 54 ▶ if ((reftype( $start_line ) || '' ) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($start_line) || '') eq 'CODE') { } 26 28 54
Condition100%2/2
builtin::reftype($start_line) || ''
l!l
2628
MC/DC100%1/1
builtin::reftype($start_line) || ''
builtin::reftype($start_line)
Truth table100%2/2
A
A= builtin::reftype($start_line)
Aresult
11
00
250 {
251 26 (my $start_line, $line, $end_line) =
252 $start_line->(
253 $line, $block->level(), $block->shift_args(), $tags, $opts
254 );
255 26 push @text, $start_line;
256 }
257 else
258 {
259 28 push @text, $start_line;
260 }
261 54 push @text, $line, $end_line;
262 }
263
264 27 ▶ pop @text if $between;
Branch: 1 branch
BranchTrueFalseTotal
if $between 11 16 27
265
266 27 @text = grep { defined $_ } @text; # remove warnings
267 27 return join('', $start, @text, $end);
268 }
269
270 sub get_indentation
271 80 ▶{
Subroutine get_indentation: called CC=2 SCAR=6.9
Pod undocumented
272 80 my ($tags, $text) = @_;
273
274 80 ▶ return 0, $text unless $text =~ s/($tags->{indent})//;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ s/($tags->{"indent"})// 66 14 80
275 14 return( length( $1 ) + 1, $text, $1 );
276 }
277
278 53 ▶sub format_line {
Subroutine format_line: called CC=2 SCAR=6.9
Pod undocumented
279 53 my ($text, $tags, $opts) = @_;
280 53 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
530
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
281
282 53 $text =~ s!$tags->{strong_tag}!$tags->{strong}->($2, $opts)!eg;
283 53 $text =~ s!$tags->{italic_tag}!$tags->{italic}->($2, $opts)!eg;
284 53 $text =~ s!$tags->{strike_tag}!$tags->{strike}->($2, $opts)!eg;
285 53 $text =~ s!$tags->{superscript_tag}!$tags->{superscript}->($1, $opts)!eg;
286 53 $text =~ s!$tags->{subscript_tag}!$tags->{subscript}->($1, $opts)!eg;
287 53 $text =~ s!$tags->{inline_tag}!$tags->{inline}->($1, $opts)!eg;
288
289 53 $text = find_extended_links( $text, $tags, $opts );
290
291 53 $text =~ s|(?<!["/>=])\b((?:[A-Z][a-z0-9]\w*){2,})|
292 3 $tags->{link}->($1, $opts)|egx;
293
294 53 return $text;
295 }
296
297 sub find_innermost_balanced_pair
298 57 ▶{
Subroutine find_innermost_balanced_pair: called CC=3 SCAR=11.0
Pod undocumented
299 57 my ($text, $open, $close) = @_;
300
301 57 my $start_pos = rindex( $text, $open );
302 57 ▶ return if $start_pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $start_pos == -1 53 4 57
303
304 4 my $end_pos = index( $text, $close, $start_pos );
305 4 ▶ return if $end_pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $end_pos == -1 0 4 4
306
307 4 my $open_length = length( $open );
308 4 my $close_length = length( $close );
309 4 my $close_pos = $end_pos + $close_length;
310 4 my $enclosed_length = $close_pos - $start_pos;
311
312 4 my $enclosed_atom = substr( $text, $start_pos, $enclosed_length );
313 4 return substr( $enclosed_atom, $open_length, 0 - $close_length ),
314 substr( $text, 0, $start_pos ),
315 substr( $text, $close_pos );
316 }
317
318 sub find_extended_links
319 53 ▶{
Subroutine find_extended_links: called CC=4 SCAR=14.0
Pod undocumented
320 53 my ($text, $tags, $opts) = @_;
321
322 53 my $schemas = join('|', @{$tags->{schemas}});
323 0 $text =~ s!(\s+)(($schemas):\S+)!$1 . $tags->{link}->($2, $opts)!egi;
324
325 53 my ($start, $end) = @{ $tags->{extended_link_delimiters} };
326
327 53 ▶ while (my @pieces = find_innermost_balanced_pair( $text, $start, $end ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pieces) = find_innermost_balanced_pair($text, $start, $end)) 4 53 57
328 {
329 4 ▶ my ($tag, $before, $after) = map { defined $_ ? $_ : '' } @pieces;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 12 0 12
330 4 ▶ my $extended = $tags->{link}->( $tag, $opts ) || '';
Condition50%1/2
$tags->{'link'}->($tag, $opts) || ''
l!l
40
MC/DC0%0/1
$tags->{'link'}->($tag, $opts) || ''
$tags->{'link'}->($tag, $opts)
Truth table50%1/2
A
A= $tags->{'link'}->($tag, $opts)
Aresult
11
00
331 4 $text = $before . $extended . $after;
332 };
333
334 53 return $text;
335 }
336
337 7 ▶sub make_html_link {
Subroutine make_html_link: called CC=5 SCAR=16.2
Pod undocumented
338 7 my ($link, $opts) = @_;
339 7 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
70
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
340
341 7 ($link, my $title) = find_link_title( $link, $opts );
342 7 ($link, my $is_relative) = escape_link( $link, $opts );
343
344 my $prefix = ( defined $opts->{prefix} && $is_relative )
345 7 ▶ ? $opts->{prefix} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'prefix'} && $is_relative ? : 3 4 7
Condition66%2/3
defined $opts->{"prefix"} and $is_relative
!ll&&!rl&&r
043
MC/DC50%1/2
defined $opts->{"prefix"} and $is_relative
defined $opts->{"prefix"} $is_relative
Truth table66%2/3
A and B
A= defined $opts->{"prefix"}B= $is_relative
ABresult
0X0
100
111
346
347 7 ▶ unless ($is_relative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_relative) { } 3 4 7
348 4 return qq|<a href="$link" rel="nofollow">$title</a>|;
349 } else {
350 3 return qq|<a href="$prefix$link">$title</a>|;
351 }
352 }
353
354 7 ▶sub escape_link {
Subroutine escape_link: called CC=2 SCAR=6.9
Pod undocumented
355 7 my ($link, $opts) = @_;
356
357 7 my $u = URI->new( $link );
358 7 ▶ return $link if $u->scheme();
Branch: 1 branch
BranchTrueFalseTotal
if $u->scheme 4 3 7
359
360 # it's a relative link
361 3 return( uri_escape( $link ), 1 );
362 }
363
364 7 ▶sub find_link_title {
Subroutine find_link_title: called CC=2 SCAR=6.9
Pod undocumented
365 7 my ($link, $opts) = @_;
366 7 my $title;
367
368 7 ($link, $title) = split(/\s+/, $link, 2);
369 7 ▶ $title = $link unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 5 2 7
370
371 7 return $link, $title;
372 }
373
374 4 'shamelessly adapted from the Jellybean project, directly from Text::WikiFormat';
375
376 __END__
↑ Summary GooglewikiFormat/Blocks.pm »