blib/lib/Text/YAWikiFormater.pm

filter: Statementstmt 86.5% 238 / 275 Branchbran 65.3% 85 / 130 Conditioncond 46.0% 29 / 63 MC/DCmcdc 22.2% 8 / 36 Subroutinesub 81.8% 18 / 22 Podpod 100% 7 / 7 totaltotal 72.2% 385 / 533 CC 101 SCAR 32.1
CC
101
Cov
75%
_handle_image
72.2
urls
33.6
format
23.7
CRAP 24.8
↑ Summary
1 package Text::YAWikiFormater;
2  
3 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use HTML::Entities qw(encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON qw(from_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.51';
11  
12 5 my %plugins = (
13 toc => \&_handle_toc,
14 image => \&_handle_image,
15  
16 restore_code_block => \&_restore_code_block,
17 );
18  
19 5 my %namespaces = (
20 wp => { prefix => 'http://en.wikipedia.org/', category=>':' },
21 gs => { prefix => 'http://www.google.com/search?q=' },
22 );
23  
24 my %closed = (
25 b => qr{(?:(?<!\s)\*\*|\*\*(?!\s))}msix,
26 i => qr{(?<!:)//},
27 u => qr{__},
28 del => qr{(?<!\-)\-\-(?!\-)},
29 tt => qw{''},
30  
31 heads => [qr[^(?=!{1,6}\s)]msix, qr[$]msix, \&_header_id, undef,"\n"],
32  
33 code => [qr[^\{\{\{$]msix,qr[^\}\}\}$]msix, \&_escape_code],
34  
35 blockquote => [qr{^&gt;\s}msix, qr{^(?!&gt;)}msix, qr{^&gt;\s}msix, '',"\n"],
36  
37 lists => [qr{^(?=[\*\#]+\s)}msix, qr{(?:^(?![\*\#\s])|\z)}msix, \&_do_lists],
38  
39 links => [qr{(?=\[\[)}, qr{(?<=\]\])},\&_do_links],
40 links2 => [qr{\s(?=http://)}, qr{\s},\&_do_links],
41  
42 19 ▶ br => [qr{^[\n\s]*(?=$)}msix, qr[$]msix, sub { "<br/><br/>",'',''}],
Subroutine __ANON__: called CC=1 SCAR=0.0
43  
44 0 ▶ comments => [qr{/\*}msix, qr{\*/}msix, sub{ '','',''}],
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 );
46  
47 5 my %nonclosed = (
48 hr => qr{^[-\*]{3,}\s*?$}msix,
49 );
50  
51 5 my @do_first = qw( code lists );
52 # for consistent order
53 5 my @do_second = qw( b i u del tt heads blockquote links links2 br comments );
54  
55 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
56 4 my $class = shift;
57  
58 4 my $self = bless { @_ }, $class;
59  
60 4 ▶ die "body is a mandatory parameter" unless $self->{body};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"body"} 0 4 4
61  
62 4 return $self;
63 }
64  
65 4 ▶sub urls {
Subroutine urls: called CC=23 SCAR=35.1
Pod documented
66 4 my $self= shift;
67 4 my $body = $self->{body};
68  
69 4 ▶ return unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 4 4
70  
71 4 my @links = $body =~m{(\[\[(?:[^\|\]]*)(?:\|(?:[^\]]+))?\]\])}g;
72 4 push @links, $body =~m{\s(https?://\S+)\s}g;
73  
74 4 ▶ my $links = $self->{_links} ||= {};
Condition50%1/2
$self->{"_links"} ||= {}
l!l
04
MC/DC0%0/1
$self->{"_links"} ||= {}
$self->{"_links"}
Truth table50%1/2
A
A= $self->{"_links"}
Aresult
11
00
75  
76 LINK:
77 4 for my $lnk ( @links ) {
78 20 ▶ next if $links->{$lnk};
Branch: 1 branch
BranchTrueFalseTotal
if $links->{$lnk} 0 20 20
79  
80 20 ▶ my $hlnk = $links->{$lnk} ||= {};
Condition50%1/2
$links->{$lnk} ||= {}
l!l
020
MC/DC0%0/1
$links->{$lnk} ||= {}
$links->{$lnk}
Truth table50%1/2
A
A= $links->{$lnk}
Aresult
11
00
81  
82 20 ▶ if ($lnk =~ m{\Ahttps?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lnk =~ m[\Ahttps?://]) 2 18 20
83 2 %$hlnk = ( title => $lnk, href => $lnk, _class => 'external' );
84 2 next LINK;
85 }
86
87 18 ($lnk) = $lnk =~ m{\A\[\[\s*(.*)\s*\]\]\z}g;
88 18 $lnk=~s{\s*\z}[]g;
89  
90 18 my ($label,$link) = split qr{\s*\|\s*}, $lnk, 2;
91 18 ▶ unless ($link) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link) 7 11 18
92 7 $link = $label;
93 7 ▶ if ( $link =~ m{.*[\>\:]\s*([^\>]+)\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ /.*[\>\:]\s*([^\>]+)\z/) 5 2 7
94 5 $label = $1;
95 }
96 }
97  
98 18 $hlnk->{title} = $label;
99 18 $hlnk->{original_to} = $link;
100 18 ▶ if ($link =~ m{\Ahttps?://} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ m[\Ahttps?://]) 1 17 18
101 1 $hlnk->{_class} = 'external';
102 1 $hlnk->{href} = $link;
103 1 next LINK;
104 }
105  
106 17 my ($base,$categ) = ('','/');
107 17 ▶ if ( $link =~ m{\A(\w+):} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ /\A(\w+):/) 2 15 17
108 2 my ($namespace,$lnk) = split qr{:}, $link, 2;
109 2 $link = $lnk;
110 2 ▶ if ( my $nmsp = $namespaces{ $namespace } ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $nmsp = $namespaces{$namespace}) { } 2 0 2
111 2 ▶ if (ref $nmsp eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $nmsp eq 'HASH') { } 2 0 2
elsif (ref $nmsp eq 'CODE') { } 0 0 0
112 2 ▶ $base = $nmsp->{prefix} if $nmsp->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $nmsp->{"prefix"} 2 0 2
113 2 ▶ $categ = $nmsp->{category} if $nmsp->{category};
Branch: 1 branch
BranchTrueFalseTotal
if $nmsp->{"category"} 2 0 2
114 } elsif (ref $nmsp eq 'CODE') {
115 0 ($base, $categ, $lnk) = $nmsp->($namespace,$link);
116 0 ▶ if ( $lnk and $lnk =~ m{\Ahttps?://} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lnk and $lnk =~ m[\Ahttps?://]) { } 0 0 0
elsif ($lnk) { } 0 0 0
Condition0%0/3
$lnk and $lnk =~ m[\Ahttps?://]
!ll&&!rl&&r
000
MC/DC0%0/2
$lnk and $lnk =~ m[\Ahttps?://]
$lnk $lnk =~ m[\Ahttps?://]
Truth table0%0/3
A and B
A= $lnkB= $lnk =~ m[\Ahttps?://]
ABresult
0X0
100
111
117 0 $hlnk->{href} = $lnk;
118 0 $hlnk->{_class}='external';
119 0 next LINK;
120 } elsif ( $lnk ) {
121 0 $link = $lnk;
122 }
123 }
124  
125 } else {
126 0 warn "Unknow namespace: $namespace on $lnk\n";
127 }
128 }
129
130 17 ▶ if ( $categ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($categ) 17 0 17
131 17 $link =~ s{\s*\>\s*}{$categ}g;
132 }
133 17 ▶ if ( $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) 2 15 17
134 2 $link = $base.$link;
135 }
136 17 ▶ unless ( $link =~ m{\Ahttps?://} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link =~ m[\Ahttps?://]) 15 2 17
137 15 $link = urify( $link );
138 }
139 17 $hlnk->{href} = $link;
140 }
141  
142 4 ▶ return wantarray ? %{$self->{_links}} : $self->{_links};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
143 }
144  
145 21 ▶sub urify {
Subroutine urify: called CC=5 SCAR=16.7
Pod documented
146 21 my $link = shift;
147 21 ▶ my $reg = shift || "^\\w\\-\\/\\s\\#";
Condition100%2/2
shift() || '^\\w\\-\\/\\s\\#'
l!l
615
MC/DC100%1/1
shift() || '^\\w\\-\\/\\s\\#'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
148  
149 21 $link =~ s{\s*\z}{}g;
150 21 ▶ $link =~ s{\s*>\s*}{/}g unless $link =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
unless $link =~ m[/] 17 4 21
151  
152 21 $link = encode_entities( $link, $reg );
153 21 $link =~ s{\s+}{-}g;
154 21 ▶ while (my ($ent)=$link=~/\&(\#?\w+);/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ent) = $link =~ /\&(\#?\w+);/) 0 21 21
155 0 ▶ my $ec=$ent=~/(acute|grave|circ|uml|ring|slash|tilde|cedil)$/i?
Branch: 1 branch
BranchTrueFalseTotal
$ent =~ /(acute|grave|circ|uml|ring|slash|tilde|cedil)$/i ? : 0 0 0
156 substr($ent,0,1):'_';
157 0 $link=~s/\&$ent;/$ec/ig;
158 }
159 21 $link="\L$link";
160 21 $link=~s/\_+$//g;
161 21 $link=~s/\_+/\_/g;
162  
163 21 return $link;
164 }
165  
166 0 ▶sub set_links {
Subroutine set_links: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($self, $links) = @_;
168  
169 0 $self->{_links} = $links;
170  
171 0 return;
172 }
173  
174 3 ▶sub format {
Subroutine format: called CC=23 SCAR=31.7
Pod documented
175 3 my $self = shift;
176 3 my $body = $self->{body};
177  
178 3 delete $self->{__headers};
179 3 delete $self->{__toc};
180  
181 3 my %done = ();
182  
183 3 $self->urls();
184  
185 3 $body =~ s{&}{&amp;}g;
186 3 $body =~ s{<}{&lt;}g;
187 3 $body =~ s{>}{&gt;}g;
188  
189 # closed tags
190 3 for my $tag ( @do_first, @do_second, keys %closed ) {
191 78 ▶ next if $done{ $tag }++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$tag}++ 39 39 78
192  
193 my ($re1, $re2, $re3, $re4, $re5, $re6)
194 = ref $closed{ $tag } eq 'ARRAY'
195 24 ? @{ $closed{ $tag } }
196 39 ▶ : ( $closed{ $tag } );
Branch: 1 branch
BranchTrueFalseTotal
ref $closed{$tag} eq 'ARRAY' ? : 24 15 39
197  
198 39 ▶ if (!$re2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $re2) { } 15 24 39
199 15 my $in = 0;
200 15 ▶ while ( $body =~ m{$re1}msix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /$re1/imsx) 10 15 25
201 10 ▶ my $tg = $in ? "</$tag>" :"<$tag>";
Branch: 1 branch
BranchTrueFalseTotal
$in ? : 5 5 10
202 10 $body=~s{$re1}{$tg}msix;
203 10 $in = 1 - $in;
204 }
205 15 ▶ $body.="</$tag>" if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 0 15 15
206 } else {
207 24 ▶ while ($body =~ m{$re1(.*?)$re2}msix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /$re1(.*?)$re2/imsx) 43 24 67
208 43 my $in = $1;
209 43 my ($t1,$t2) = ("<$tag>","</$tag>");
210 43 ▶ if (ref $re3 eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $re3 eq 'Regexp') { } 4 39 43
elsif (ref $re3 eq 'CODE') { } 39 0 39
211 4 ▶ $re4 //= '';
Condition50%1/2
$re4 //= ""
l!l
40
MC/DC0%0/1
$re4 //= ""
$re4
Truth table50%1/2
A
A= $re4
Aresult
11
00
212 4 $in =~ s{ $re3 }{$re4}msixg;
213 } elsif (ref $re3 eq 'CODE') {
214 39 ($t1,$in,$t2) = $re3->($self, $t1, $in, $t2);
215 }
216 43 ▶ $t1 //= '';
Condition50%1/2
$t1 //= ""
l!l
430
MC/DC0%0/1
$t1 //= ""
$t1
Truth table50%1/2
A
A= $t1
Aresult
11
00
217 43 ▶ $in //= '';
Condition50%1/2
$in //= ""
l!l
430
MC/DC0%0/1
$in //= ""
$in
Truth table50%1/2
A
A= $in
Aresult
11
00
218 43 ▶ $t2 //= '';
Condition50%1/2
$t2 //= ""
l!l
430
MC/DC0%0/1
$t2 //= ""
$t2
Truth table50%1/2
A
A= $t2
Aresult
11
00
219 43 ▶ $re5 //= '';
Condition100%2/2
$re5 //= ""
l!l
349
MC/DC100%1/1
$re5 //= ""
$re5
Truth table100%2/2
A
A= $re5
Aresult
11
00
220 43 $body =~ s{$re1(.*?)$re2}{$t1$in$t2$re5}smxi;
221 }
222 }
223 }
224  
225 3 for my $tag ( keys %nonclosed ) {
226 3 my ($re1) = ($nonclosed{ $tag } );
227  
228 3 $body =~ s{ $re1 }{<$tag />}msixg;
229 }
230  
231 3 ▶ while ($body =~ m[(?<!\{)\{\{(\w+)(?:[:\s](.+))?\}\}(?!\})]msix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /(?<!\{)\{\{(\w+)(?:[:\s](.+))?\}\}(?!\})/imsx) 2 3 5
232 2 my ($plugin, $params) = ($1,$2);
233 2 $params = _parse_plugin_params($params);
234  
235 2 my $res = '';
236 2 ▶ if ( $plugins{$plugin} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($plugins{$plugin}) 2 0 2
237 2 ▶ $res = $plugins{ $plugin }->( $self, $plugin, $params ) // '';
Condition50%1/2
$plugins{$plugin}->($self, $plugin, $params) // ""
l!l
20
MC/DC0%0/1
$plugins{$plugin}->($self, $plugin, $params) // ""
$plugins{$plugin}->($self, $plugin, $params)
Truth table50%1/2
A
A= $plugins{$plugin}->($self, $plugin, $params)
Aresult
11
00
238 }
239  
240 2 $body =~ s[(?<!\{)\{\{(\w+)(?:[:\s](.+))?\}\}(?!\})][$res]msix;
241 }
242  
243 3 ▶ while ($body =~ m[\/\+\+(\w+)(?:[:\s*](.+))?\+\+\/]msix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m[\/\+\+(\w+)(?:[:\s*](.+))?\+\+\/]imsx) 1 3 4
244 1 my ($plugin, $params) = ($1,$2);
245 1 $params=~s{\A\s*}{};
246 1 my @params = split qr{\s*,\s*}, $params;
247  
248 1 my $res = '';
249 1 ▶ if ( $plugins{$plugin} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($plugins{$plugin}) 1 0 1
250 1 ▶ $res = $plugins{ $plugin }->( $self, $plugin, @params ) // '';
Condition50%1/2
$plugins{$plugin}->($self, $plugin, @params) // ""
l!l
10
MC/DC0%0/1
$plugins{$plugin}->($self, $plugin, @params) // ""
$plugins{$plugin}->($self, $plugin, @params)
Truth table50%1/2
A
A= $plugins{$plugin}->($self, $plugin, @params)
Aresult
11
00
251 }
252  
253 1 $body =~ s[\/\+\+(\w+)(?:[:\s*](.+))?\+\+\/][$res]msix;
254 }
255
256 3 return $body;
257 }
258  
259 0 ▶sub register_namespace {
Subroutine register_namespace: not called CC=3 SCAR=24.8
Pod documented
260 0 my $class = shift;
261  
262 0 my ($namespace, $info, $override) = @_;
263  
264 $namespaces{ $namespace } = $info
265 0 ▶ if $override or !$namespaces{ $namespace };
Branch: 1 branch
BranchTrueFalseTotal
if $override or not $namespaces{$namespace} 0 0 0
Condition0%0/3
$override or not $namespaces{$namespace}
l!l&&r!l&&!r
000
MC/DC0%0/2
$override or not $namespaces{$namespace}
$override not $namespaces{$namespace}
Truth table0%0/3
A or not(B)
A= $overrideB= not $namespaces{$namespace}
ABresult
1X1
011
000
266 }
267  
268 0 ▶sub register_plugin {
Subroutine register_plugin: not called CC=3 SCAR=24.8
Pod documented
269 0 my $class = shift;
270  
271 0 my ($pluginname, $pluginref, $override) = @_;
272  
273 $plugins{ $pluginname } = $pluginref
274 0 ▶ if $override or !$plugins{ $pluginname };
Branch: 1 branch
BranchTrueFalseTotal
if $override or not $plugins{$pluginname} 0 0 0
Condition0%0/3
$override or not $plugins{$pluginname}
l!l&&r!l&&!r
000
MC/DC0%0/2
$override or not $plugins{$pluginname}
$override not $plugins{$pluginname}
Truth table0%0/3
A or not(B)
A= $overrideB= not $plugins{$pluginname}
ABresult
1X1
011
000
275 }
276  
277 6 ▶sub _header_id {
Subroutine _header_id: called CC=6 SCAR=18.4
278 6 my $self = shift;
279 6 ▶ my $headers = $self->{__headers} ||= {};
Condition100%2/2
$self->{"__headers"} ||= {}
l!l
51
MC/DC100%1/1
$self->{"__headers"} ||= {}
$self->{"__headers"}
Truth table100%2/2
A
A= $self->{"__headers"}
Aresult
11
00
280 6 ▶ my $headnames = $self->{__headnames} ||= {};
Condition100%2/2
$self->{"__headnames"} ||= {}
l!l
51
MC/DC100%1/1
$self->{"__headnames"} ||= {}
$self->{"__headnames"}
Truth table100%2/2
A
A= $self->{"__headnames"}
Aresult
11
00
281 6 ▶ my $toc = $self->{__toc} ||= [];
Condition100%2/2
$self->{"__toc"} ||= []
l!l
51
MC/DC100%1/1
$self->{"__toc"} ||= []
$self->{"__toc"}
Truth table100%2/2
A
A= $self->{"__toc"}
Aresult
11
00
282 6 my ($t1, $in, $t2) = @_;
283  
284 6 my ($type) = $in =~ m{^(!{1,6})\s};
285 6 $in =~ s{^!*\s}{};
286  
287 6 $t1 = 'h'.length($type);
288 6 $t2 = "</$t1>";
289 6 $t1 = "<$t1>";
290  
291 6 my $id = urify($in, "^\\w\\-\\s");
292  
293 6 ▶ if ($headers->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->{$id}) 0 6 6
294 0 my $cnt = 1;
295 0 ▶ $cnt++ while $headers->{"${id}_$cnt"};
Branch: 1 branch
BranchTrueFalseTotal
$$headers{"${id}_$cnt"} and do { ++$cnt } 0 0 0
296 0 $id .= "_$cnt";
297 }
298  
299 6 $headnames->{$id} = $in;
300 6 $headers->{$id} = substr($t1, 2, 1);
301 6 push @$toc, $id;
302  
303 6 substr($t1, -1, 0, " id='$id'");
304  
305 6 return $t1, $in, $t2;
306 }
307  
308 1 ▶sub _escape_code {
Subroutine _escape_code: called CC=1 SCAR=0.0
309 1 my $self = shift;
310  
311 1 my ($t1, $in, $t2) = @_;
312  
313 1 $in=~s{\n}{<br/>\n}gs;
314  
315 1 $self->{__codecnt}++;
316 1 $self->{__codeblock}->{$self->{__codecnt}} = $in;
317  
318 1 return '',"/++restore_code_block: $self->{__codecnt}++/", '';
319 }
320  
321 2 ▶sub _do_lists {
Subroutine _do_lists: called CC=18 SCAR=29.0
322 2 my $self = shift;
323  
324 2 my ($t1, $in, $t2) = @_;
325  
326 2 my @lines = split qr{\n}ms, $in;
327 2 $in = '';
328 2 my $cl = '';
329 2 my $item;
330 2 for my $ln (@lines) {
331 12 ▶ if ( $ln !~ m{^\s} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ln =~ /^\s/) 12 0 12
332 12 ▶ if ($item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) 10 2 12
333 10 $in .= "<li>$item</li>\n";
334 10 $item = '';
335 }
336 12 my ($nl,$l) = $ln =~ m{^([\*\#]+)\s+(.*)$};
337 12 $ln = $l;
338 12 my $close = '';
339 12 my $start = -1;
340 12 ▶ if ($nl ne $cl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl ne $cl) 11 1 12
341 11 for my $i (0..length($cl)-1) {
342 23 ▶ next if !$close and substr($cl,$i,1) eq substr($nl, $i, 1);
Branch: 1 branch
BranchTrueFalseTotal
if not $close and substr($cl, $i, 1) eq substr($nl, $i, 1) 19 4 23
Condition100%3/3
not $close and substr($cl, $i, 1) eq substr($nl, $i, 1)
!ll&&!rl&&r
1319
MC/DC100%2/2
not $close and substr($cl, $i, 1) eq substr($nl, $i, 1)
not $close substr($cl, $i, 1) eq substr($nl, $i, 1)
Truth table100%3/3
not(A) and B
A= not $closeB= substr($cl, $i, 1) eq substr($nl, $i, 1)
ABresult
0X0
100
111
343 4 ▶ $start = $i unless $close;
Branch: 1 branch
BranchTrueFalseTotal
unless $close 3 1 4
344 4 ▶ $close = (substr($cl,$i,1) eq '#' ? "</ol>" : "</ul>").$close;
Branch: 1 branch
BranchTrueFalseTotal
substr($cl, $i, 1) eq '#' ? : 1 3 4
345 }
346 11 ▶ $start = length($cl) if $start == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $start == -1 8 3 11
347 11 ▶ $in.=$close."\n" if $close;
Branch: 1 branch
BranchTrueFalseTotal
if $close 3 8 11
348 11 for my $i ($start..length($nl)-1) {
349 11 ▶ $in.= substr($nl, $i, 1) eq '#'?"<ol>":"<ul>";
Branch: 1 branch
BranchTrueFalseTotal
substr($nl, $i, 1) eq '#' ? : 1 10 11
350 }
351 11 $cl = $nl;
352 }
353 }
354 12 $item .= $ln;
355 }
356 2 ▶ if ($item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) 2 0 2
357 2 $in .= "<li>$item</li>\n";
358 }
359 2 ▶ if ($cl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl) 2 0 2
360 2 for my $i (reverse 0..length($cl)-1) {
361 7 ▶ $in.=substr($cl,$i,1) eq '#' ? "</ol>" : "</ul>";
Branch: 1 branch
BranchTrueFalseTotal
substr($cl, $i, 1) eq '#' ? : 0 7 7
362 }
363 2 $in.="\n";
364 }
365  
366 2 return '',$in,'';
367 }
368  
369 11 ▶sub _do_links {
Subroutine _do_links: called CC=7 SCAR=19.9
370 11 my $self = shift;
371  
372 11 my (undef, $link, undef) = @_;
373  
374 11 $link =~s{\&gt;}[>]g;
375  
376 11 ▶ $self->urls() unless $self->{_links} and $self->{_links}->{$link};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_links"} and $self->{"_links"}{$link} 0 11 11
Condition33%1/3
$self->{"_links"} and $self->{"_links"}{$link}
!ll&&!rl&&r
0011
MC/DC0%0/2
$self->{"_links"} and $self->{"_links"}{$link}
$self->{"_links"} $self->{"_links"}{$link}
Truth table33%1/3
A and B
A= $self->{"_links"}B= $self->{"_links"}{$link}
ABresult
0X0
100
111
377  
378 11 ▶ my $lnk = $self->{_links}->{$link} || {};
Condition50%1/2
$self->{'_links'}{$link} || {}
l!l
110
MC/DC0%0/1
$self->{'_links'}{$link} || {}
$self->{'_links'}{$link}
Truth table50%1/2
A
A= $self->{'_links'}{$link}
Aresult
11
00
379  
380 11 my ($t1,$t2) = ('','</a>');
381  
382 11 $t1 = "<a href='$lnk->{href}'";
383 11 ▶ my $class = $lnk->{class} || $lnk->{_class} || '';
Condition80%4/5
$lnk->{'class'} || $lnk->{'_class'} || ''
l!l
110
$lnk->{"class"} or $lnk->{"_class"}
l!l&&r!l&&!r
0110
MC/DC50%1/2
$lnk->{'class'} || $lnk->{'_class'} || ''
$lnk->{"class"} $lnk->{"_class"}
Truth table66%2/3
A or B
A= $lnk->{"class"}B= $lnk->{"_class"}
ABresult
1X1
011
000
384 11 ▶ if ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 1 10 11
385 1 $t1.=" class='$class'";
386 }
387 11 $t1.='>';
388  
389 11 return $t1, $lnk->{title}, $t2;
390 }
391  
392 1 ▶sub _handle_toc {
Subroutine _handle_toc: called CC=2 SCAR=6.9
393 1 my ($self) = shift;
394  
395 1 my $toc = $self->{__toc};
396 1 my $headers = $self->{__headers};
397 1 my $headnames = $self->{__headnames};
398  
399 1 my $res = "\n";
400 1 for my $head (@$toc) {
401 6 $res.='*'x$headers->{$head};
402
403 6 $res.=' ';
404 6 $res.='[['.$headnames->{$head}.'|#'.$head."]]\n";
405 }
406 1 $res.="\n";
407  
408 1 my $wf = (ref $self)->new(body => $res);
409 1 $res = $wf->format();
410  
411 1 $res = "<div class='toc'>$res</div>";
412  
413 1 return $res;
414 }
415  
416 1 ▶sub _handle_image {
Subroutine _handle_image: called CC=16 SCAR=42.8
417 1 my ($self, $plugin, $params) = @_;
418 1 my $src;
419  
420 1 ▶ if (ref $params eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq 'ARRAY') { } 1 0 1
421 1 $src = shift @$params;
422 1 ▶ if (@$params and ref $params->[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$params and ref $params->[0] eq 'HASH') { } 0 1 1
Condition33%1/3
@$params and ref $params->[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@$params and ref $params->[0] eq "HASH"
@$params ref $params->[0] eq "HASH"
Truth table33%1/3
A and B
A= @$paramsB= ref $params->[0] eq "HASH"
ABresult
0X0
100
111
423 0 $params = $params->[0];
424 } else {
425 1 $params = { @$params };
426 }
427 } else {
428 0 $src = delete $params->{src};
429 }
430  
431 1 ▶ return '<!-- no src - incorrect params? -->' unless $src;
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 1 1
432  
433 1 ▶ if ($src =~ m{\Ahttps?://} and $self->{image_filter}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($src =~ m[\Ahttps?://] and $self->{'image_filter'}) { } 0 1 1
elsif ($self->{'image_mapper'}) { } 0 1 1
Condition33%1/3
$src =~ m[\Ahttps?://] and $self->{"image_filter"}
!ll&&!rl&&r
100
MC/DC0%0/2
$src =~ m[\Ahttps?://] and $self->{"image_filter"}
$src =~ m[\Ahttps?://] $self->{"image_filter"}
Truth table33%1/3
A and B
A= $src =~ m[\Ahttps?://]B= $self->{"image_filter"}
ABresult
0X0
100
111
434 0 $src = $self->{image_filter}->($src, $params);
435 } elsif ($self->{image_mapper}) {
436 0 $src = $self->{image_mapper}->($src, $params);
437 }
438  
439 1 ▶ return '<!-- image filtered/not mapped -->' unless $src;
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 1 1
440  
441 1 my $res = "<img src='$src'";
442 1 ▶ if ( $params->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"size"}) 0 1 1
443 0 my ($w,$h) = $params->{size} =~ m{\A\d+x\d+\z};
444  
445 0 ▶ if ($w and $h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w and $h) 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
446 0 ▶ $params->{width} ||= $w;
Condition0%0/3
$params->{"width"} ||= $w
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{"width"} ||= $w
$params->{"width"} $w
Truth table0%0/3
A ||= B
A= $params->{"width"}B= $w
ABresult
1X1
011
000
447 0 ▶ $params->{height} ||= $h;
Condition0%0/3
$params->{"height"} ||= $h
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{"height"} ||= $h
$params->{"height"} $h
Truth table0%0/3
A ||= B
A= $params->{"height"}B= $h
ABresult
1X1
011
000
448 0 delete $params->{size};
449 }
450 }
451 1 for my $attr ( qw(alt title heigth width) ) {
452 4 ▶ next unless $params->{ $attr };
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$attr} 4 0 4
453 0 my $av = $params->{ $attr };
454 0 $av =~ s{&}{&amp;}g;
455 0 $av =~ s{<}{&gt;}g;
456 0 $av =~ s{>}{&lt;}g;
457 0 $av =~ s{'}{&#39;}g;
458 0 $res.=" $attr='$av'";
459 }
460  
461 1 $res.=' />';
462  
463 #MAYBETODO: support for caption, to allow to frame the images
464 # and add a legend under the image.
465  
466 1 return $res;
467 }
468  
469 1 ▶sub _restore_code_block {
Subroutine _restore_code_block: called CC=1 SCAR=0.0
470 1 my ($self, $plugin, $block) = @_;
471  
472 1 my $res = $self->{__codeblock}->{$block};
473  
474 1 return "<code>$res</code>";
475 }
476  
477 2 ▶sub _parse_plugin_params {
Subroutine _parse_plugin_params: called CC=4 SCAR=14.0
478 2 my $paramstr = shift;
479  
480 2 ▶ return [] unless $paramstr;
Branch: 1 branch
BranchTrueFalseTotal
unless $paramstr 1 1 2
481  
482 1 ▶ unless ($paramstr =~ m(\A\s*[\{\[]) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($paramstr =~ /\A\s*[\{\[]/) 1 0 1
483 1 $paramstr = '['.$paramstr.']';
484 }
485  
486 1 ▶ my $params = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $params = eval { do { &from_json($paramstr, {"utf8", 1}) } } or do (print(STDERR "Error Parsing params: $paramstr ==> $@\n")) 1 0 1
487 1 from_json( $paramstr, { utf8 => 1 })
488 } or do print STDERR "Error Parsing params: $paramstr ==> $@\n";
489 #MAYBETODO: export this error somehow? silent it?
490 # exporting it may be useful - specially while previewing
491 # the result.
492  
493 1 return $params;
494 }
495  
496 5 1;
497 __END__
↑ Summary