WikiFormat.pm

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