blib/lib/HTML/WikiConverter/FreeStyleWiki.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 88.8% 32 / 36 Conditioncond 76.7% 33 / 43 MC/DCmcdc 47.6% 10 / 21 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 6 totaltotal 83.1% 163 / 196 SCAR 36.2
CC
36
Cov
90%
preprocess_node
12.4
_link
9.1
_image
5.6
CRAP 37.5
↑ Summary Build »
1 package HTML::WikiConverter::FreeStyleWiki;
2 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use parent 'HTML::WikiConverter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Params::Validate ':types';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $VERSION = "0.02";
9  
10 sub attributes {
11 +{
12 5 ▶ p_strict => { default => 0 },
Subroutine attributes: called CC=1 SCAR=0.0
Pod undocumented
13 escape_entities => { default => 0 },
14 preserve_tags => { default => 0, type => BOOLEAN },
15 };
16 }
17  
18 sub rules {
19 15 ▶ my ($self) = @_;
Subroutine rules: called CC=3 SCAR=11.0
Pod undocumented
20 15 my %rules = (
21 hr => { replace => "\n----\n" },
22 br => { replace => \&_br },
23  
24 blockquote => { start => qq{\n}, block => 1, line_format => 'multi', line_prefix => q{""} },
25 p => { end => "\n", block => 1, trim => 'both', line_format => 'multi', line_prefix => '' },
26 i => { start => q{''}, end => q{''} },
27 em => { alias => 'i' },
28 b => { start => q{'''}, end => q{'''} },
29 strong => { alias => 'b' },
30 del => { start => '==', end => '==', },
31 ins => { start => '__', end => '__', },
32  
33 img => { replace => \&_image },
34 a => { replace => \&_link },
35  
36 ul => { line_format => 'multi', block => 1 },
37 ol => { alias => 'ul' },
38 dl => { line_format => 'blocks', block => 1 },
39  
40 li => { start => \&_li_start, trim => 'leading' },
41 dt => { start => '::', trim => 'both', 'end' => "\n" },
42 dd => { line_format => 'multi', line_prefix => ':::' },
43  
44 td => { start => ',', trim => 'both' },
45 th => { alias => 'td' },
46 tr => { end => "\n" },
47  
48 h1 => { start => '!!!', block => 1, trim => 'both', line_format => 'single' },
49 h2 => { start => '!!!', block => 1, trim => 'both', line_format => 'single' },
50 h3 => { start => '!!', block => 1, trim => 'both', line_format => 'single' },
51 h4 => { start => '!', block => 1, trim => 'both', line_format => 'single' },
52 h5 => { start => '!', block => 1, trim => 'both', line_format => 'single' },
53 h6 => { start => '!', block => 1, trim => 'both', line_format => 'single' },
54  
55 pre => { start => qq{\n}, end => "\n", line_format => 'multi', line_prefix => ' ' },
56 );
57  
58 15 ▶ if ($self->preserve_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preserve_tags) 3 12 15
59 3 for my $tag (qw/ big small tt abbr acronym cite code dfn kbd samp var sup sub /) {
60 39 $rules{$tag} = { preserve => 1 }
61 }
62 }
63  
64 15 return \%rules;
65 }
66  
67 # Calculates the prefix that will be placed before each list item.
68 # List item include ordered and unordered list items.
69 sub _li_start {
70 14 ▶ my ( $self, $node, $rules ) = @_;
Subroutine _li_start: called CC=4 SCAR=13.9
71 14 my @parent_lists = $node->look_up( _tag => qr/ul|ol/ );
72 14 my $depth = @parent_lists;
73 14 ▶ if ( defined $node->{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"text"}) 0 14 14
74 0 $node->{text} =~ s/\A\s+//;
75 }
76  
77 14 my $bullet = '';
78 14 ▶ $bullet = '*' if $node->parent->tag eq 'ul';
Branch: 1 branch
BranchTrueFalseTotal
if $node->parent->tag eq "ul" 10 4 14
79 14 ▶ $bullet = '+' if $node->parent->tag eq 'ol';
Branch: 1 branch
BranchTrueFalseTotal
if $node->parent->tag eq "ol" 4 10 14
80  
81 14 my $prefix = ($bullet) x $depth;
82 14 return "\n$prefix ";
83 }
84  
85 sub _image {
86 2 ▶ my ( $self, $node, $rules ) = @_;
Subroutine _image: called CC=5 SCAR=17.2
87 2 ▶ my $url = $node->attr('src') || '';
Condition50%1/2
$node->attr('src') || ''
l!l
20
MC/DC0%0/1
$node->attr('src') || ''
$node->attr('src')
Truth table50%1/2
A
A= $node->attr('src')
Aresult
11
00
88 2 ▶ if ( $url =~ m{page=([^&]*)&(?:amp;)?file=([^&]*)&(?:amp;)?action=ATTACH}msx )
Branch: 2 branches
BranchTrueFalseTotal
if ($url =~ /page=([^&]*)&(?:amp;)?file=([^&]*)&(?:amp;)?action=ATTACH/msx) { } 1 1 2
elsif ($url) { } 1 0 1
89 { # ref_image plugin
90 1 ▶ return sprintf "{{ref_image %s,%s}}", $2, $1 if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 1 0 1
91 }
92 elsif ($url) { # image plugin
93 1 return sprintf "{{image %s}}", $url;
94 }
95 0 return '';
96 }
97  
98 sub _link {
99 22 ▶ my ( $self, $node, $rules ) = @_;
Subroutine _link: called CC=9 SCAR=22.1
100 22 ▶ my $url = $node->attr('href') || '';
Condition50%1/2
$node->attr('href') || ''
l!l
220
MC/DC0%0/1
$node->attr('href') || ''
$node->attr('href')
Truth table50%1/2
A
A= $node->attr('href')
Aresult
11
00
101 22 $url =~ s/&/&/g;
102 22 ▶ my $title = $self->get_wiki_page($url) || $self->extract_wiki_page($url) || '';
Condition80%4/5
$self->get_wiki_page($url) || $self->extract_wiki_page($url) || ''
l!l
814
$self->get_wiki_page($url) or $self->extract_wiki_page($url)
l!l&&r!l&&!r
0814
MC/DC50%1/2
$self->get_wiki_page($url) || $self->extract_wiki_page($url) || ''
$self->get_wiki_page($url) $self->extract_wiki_page($url)
Truth table66%2/3
A or B
A= $self->get_wiki_page($url)B= $self->extract_wiki_page($url)
ABresult
1X1
011
000
103 22 ▶ my $text = $self->get_elem_contents($node) || '';
Condition50%1/2
$self->get_elem_contents($node) || ''
l!l
220
MC/DC0%0/1
$self->get_elem_contents($node) || ''
$self->get_elem_contents($node)
Truth table50%1/2
A
A= $self->get_elem_contents($node)
Aresult
11
00
104 22 ▶ return "[[$text]]" if $title eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $title eq $text 6 16 22
105 16 ▶ return "[[$text|$title]]" if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 2 14 16
106 14 ▶ return $url if $url eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $url eq $text 2 12 14
107  
108 12 ▶ if ( my $relative_url = $self->get_relative_url($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $relative_url = $self->get_relative_url($url)) 5 7 12
109 5 return "[$text|$relative_url]";
110 }
111 7 return "[$text|$url]";
112 }
113  
114 sub get_relative_url {
115 12 ▶ my ( $self, $url ) = @_;
Subroutine get_relative_url: called CC=5 SCAR=16.1
Pod undocumented
116 12 ▶ return unless $self->base_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->base_uri 3 9 12
117 9 $self->base_uri =~ m{/([^/]*)$};
118 9 ▶ my $path = $1 || '';
Condition50%1/2
$1 || ''
l!l
90
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
119 9 my $re_tmp = '(' . quotemeta($path) . '(/[^/]+)?(\?.*)?)$';
120 9 my $re = qr($re_tmp);
121 9 ▶ $url =~ /$re/ or return;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /$re/ or return 4 5 9
122 5 ▶ return $2 ? $1 : $3;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 3 2 5
123 }
124  
125 sub extract_wiki_page {
126 22 ▶ my ( $self, $url ) = @_;
Subroutine extract_wiki_page: called CC=2 SCAR=7.0
Pod undocumented
127 22 my $re_tmp = quotemeta( $self->base_uri ) . '\?page=([^&]+)$';
128 22 my $re = qr($re_tmp);
129 22 ▶ return $url =~ /$re/ && $1;
Condition66%2/3
$url =~ /$re/ && $1
!ll&&!rl&&r
1408
MC/DC50%1/2
$url =~ /$re/ && $1
$url =~ /$re/ $1
Truth table66%2/3
A && B
A= $url =~ /$re/B= $1
ABresult
0X0
100
111
130 }
131  
132 sub _br {
133 30 ▶ my ( $self, $node, $rules ) = @_;
Subroutine _br: called CC=3 SCAR=11.0
134  
135 # print $node->dump;
136 # print $node->right->dump;
137 30 ▶ if ( $node->right and $node->right->tag eq '~text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->right and $node->right->tag eq "~text") 5 25 30
Condition100%3/3
$node->right and $node->right->tag eq "~text"
!ll&&!rl&&r
12135
MC/DC100%2/2
$node->right and $node->right->tag eq "~text"
$node->right $node->right->tag eq "~text"
Truth table100%3/3
A and B
A= $node->rightB= $node->right->tag eq "~text"
ABresult
0X0
100
111
138 5 $node->right->{text} =~ s/\A\s+//msx;
139 5 $node->right->{text} =~ s/\s+\z//msx;
140  
141 # warn join ':', $node->lineage_tag_names;
142 }
143 30 return "\n";
144 }
145  
146 sub postprocess_output {
147 61 ▶ my ( $self, $outref ) = @_;
Subroutine postprocess_output: called CC=1 SCAR=0.0
Pod undocumented
148 61 $$outref =~ s/^""""(?!")/""/gmx; # nested blockquote change to plain blockquote
149 61 $$outref =~ s/^([\*\+]+)\s+/$1/gmx; # delete space on li start
150 }
151  
152 sub preprocess_node {
153 472 ▶ my ( $self, $node ) = @_;
Subroutine preprocess_node: called CC=12 SCAR=25.2
Pod undocumented
154 472 ▶ $self->strip_aname($node) if defined $node->tag and $node->tag eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->tag and $node->tag eq "a" 37 435 472
Condition66%2/3
defined $node->tag and $node->tag eq "a"
!ll&&!rl&&r
043537
MC/DC50%1/2
defined $node->tag and $node->tag eq "a"
defined $node->tag $node->tag eq "a"
Truth table66%2/3
A and B
A= defined $node->tagB= $node->tag eq "a"
ABresult
0X0
100
111
155 472 ▶ $self->caption2para($node) if defined $node->tag and $node->tag eq 'caption';
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->tag and $node->tag eq "caption" 0 472 472
Condition66%2/3
defined $node->tag and $node->tag eq "caption"
!ll&&!rl&&r
154570
MC/DC0%0/2
defined $node->tag and $node->tag eq "caption"
defined $node->tag $node->tag eq "caption"
Truth table66%2/3
A and B
A= defined $node->tagB= $node->tag eq "caption"
ABresult
0X0
100
111
156 472 ▶ if ( $node->tag
Branch: 1 branch
BranchTrueFalseTotal
if ($node->tag and $node->tag eq "br" and $node->right and $node->right->tag and $node->right->tag eq "pre" and $node->parent->tag and $node->parent->tag eq "p") 2 470 472
Condition88%16/18
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag and $node->right->tag eq "pre" and $node->parent->tag and $node->parent->tag eq "p"
!ll&&!rl&&r
46912
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag and $node->right->tag eq "pre" and $node->parent->tag
!ll&&!rl&&r
46903
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag and $node->right->tag eq "pre"
!ll&&!rl&&r
454153
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag
!ll&&!rl&&r
454018
$node->tag and $node->tag eq "br" and $node->right
!ll&&!rl&&r
4421218
$node->tag and $node->tag eq "br"
!ll&&!rl&&r
1542730
MC/DC71%5/7
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag and $node->right->tag eq "pre" and $node->parent->tag and $node->parent->tag eq "p"
$node->tag $node->tag eq "br" $node->right $node->right->tag $node->right->tag eq "pre" $node->parent->tag $node->parent->tag eq "p"
Truth table75%6/8
A and B and C and D and E and F and G
A= $node->tagB= $node->tag eq "br"C= $node->rightD= $node->right->tagE= $node->right->tag eq "pre"F= $node->parent->tagG= $node->parent->tag eq "p"
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
157 and $node->tag eq 'br'
158 and $node->right
159 and $node->right->tag
160 and $node->right->tag eq 'pre'
161 and $node->parent->tag
162 and $node->parent->tag eq 'p' )
163 {
164 2 $node->parent->replace_with_content();
165 }
166 }
167  
168  
169 5 1;
170 __END__
↑ Summary Build »