blib/lib/HTML/WikiConverter/FreeStyleWiki.pm

filter: Statementstmt 97.3% 72 / 74 Branchbran 86.1% 31 / 36 Conditioncond 76.7% 33 / 43 MC/DCmcdc 47.6% 10 / 21 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 6 totaltotal 82.5% 161 / 195 CC 36 SCAR 19.2
CC
36
Cov
89%
preprocess_node
12.4
_link
10.2
_image
5.6
CRAP 6.8
↑ Summary Build »
1 package HTML::WikiConverter::FreeStyleWiki;
2 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use parent 'HTML::WikiConverter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Params::Validate ':types';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = "0.01";
9  
10 sub attributes {
11 +{
12 4 ▶ 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 12 ▶ my ($self) = @_;
Subroutine rules: called CC=3 SCAR=11.0
Pod undocumented
20 12 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 12 ▶ if ($self->preserve_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preserve_tags) 3 9 12
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 12 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 17 ▶ my ( $self, $node, $rules ) = @_;
Subroutine _link: called CC=10 SCAR=23.3
100 17 ▶ my $url = $node->attr('href') || '';
Condition50%1/2
$node->attr('href') || ''
l!l
170
MC/DC0%0/1
$node->attr('href') || ''
$node->attr('href')
Truth table50%1/2
A
A= $node->attr('href')
Aresult
11
00
101 17 $url =~ s/&/&/g;
102 17 ▶ 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
89
$self->get_wiki_page($url) or $self->extract_wiki_page($url)
l!l&&r!l&&!r
089
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 17 ▶ my $text = $self->get_elem_contents($node) || '';
Condition50%1/2
$self->get_elem_contents($node) || ''
l!l
170
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 17 ▶ return "[[$text]]" if $title eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $title eq $text 6 11 17
105 11 ▶ return "[[$text|$title]]" if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 2 9 11
106 9 ▶ return $url if $url eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $url eq $text 1 8 9
107  
108 8 ▶ if ( my $relative_url = $self->get_relative_url($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $relative_url = $self->get_relative_url($url)) 5 3 8
109 5 ▶ return "[$text|$relative_url]" if $relative_url;
Branch: 1 branch
BranchTrueFalseTotal
if $relative_url 5 0 5
110 }
111 3 return "[$text|$url]";
112 }
113  
114 sub get_relative_url {
115 8 ▶ my ( $self, $url ) = @_;
Subroutine get_relative_url: called CC=4 SCAR=13.9
Pod undocumented
116 8 $self->base_uri =~ m{/([^/]*)$};
117 8 ▶ my $path = $1 || '';
Condition50%1/2
$1 || ''
l!l
80
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
118 8 my $re_tmp = '(' . quotemeta($path) . '(/[^/]+)?(\?.*)?)$';
119 8 my $re = qr($re_tmp);
120 8 ▶ $url =~ /$re/ or return;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /$re/ or return 3 5 8
121 5 ▶ return $2 ? $1 : $3;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 3 2 5
122 }
123  
124 sub extract_wiki_page {
125 17 ▶ my ( $self, $url ) = @_;
Subroutine extract_wiki_page: called CC=2 SCAR=7.0
Pod undocumented
126 17 my $re_tmp = quotemeta( $self->base_uri ) . '\?page=([^&]+)$';
127 17 my $re = qr($re_tmp);
128 17 ▶ return $url =~ /$re/ && $1;
Condition66%2/3
$url =~ /$re/ && $1
!ll&&!rl&&r
908
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
129 }
130  
131 sub _br {
132 30 ▶ my ( $self, $node, $rules ) = @_;
Subroutine _br: called CC=3 SCAR=11.0
133  
134 # print $node->dump;
135 # print $node->right->dump;
136 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
137 5 $node->right->{text} =~ s/\A\s+//msx;
138 5 $node->right->{text} =~ s/\s+\z//msx;
139  
140 # warn join ':', $node->lineage_tag_names;
141 }
142 30 return "\n";
143 }
144  
145 sub postprocess_output {
146 56 ▶ my ( $self, $outref ) = @_;
Subroutine postprocess_output: called CC=1 SCAR=0.0
Pod undocumented
147 56 $$outref =~ s/^""""(?!")/""/gmx; # nested blockquote change to plain blockquote
148 56 $$outref =~ s/^([\*\+]+)\s+/$1/gmx; # delete space on li start
149 }
150  
151 sub preprocess_node {
152 452 ▶ my ( $self, $node ) = @_;
Subroutine preprocess_node: called CC=12 SCAR=25.2
Pod undocumented
153 452 ▶ $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" 32 420 452
Condition66%2/3
defined $node->tag and $node->tag eq "a"
!ll&&!rl&&r
042032
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
154 452 ▶ $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 452 452
Condition66%2/3
defined $node->tag and $node->tag eq "caption"
!ll&&!rl&&r
154370
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
155 452 ▶ 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 450 452
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
44912
$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
44903
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag and $node->right->tag eq "pre"
!ll&&!rl&&r
434153
$node->tag and $node->tag eq "br" and $node->right and $node->right->tag
!ll&&!rl&&r
434018
$node->tag and $node->tag eq "br" and $node->right
!ll&&!rl&&r
4221218
$node->tag and $node->tag eq "br"
!ll&&!rl&&r
1540730
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
156 and $node->tag eq 'br'
157 and $node->right
158 and $node->right->tag
159 and $node->right->tag eq 'pre'
160 and $node->parent->tag
161 and $node->parent->tag eq 'p' )
162 {
163 2 $node->parent->replace_with_content();
164 }
165 }
166  
167  
168 4 1;
169 __END__
↑ Summary Build »