FormatMarkdown.pm

filter: Statementstmt 93.3% 140 / 150 Branchbran 62.5% 35 / 56 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 30 / 30 Podpod 0.0% 0 / 26 totaltotal 77.7% 206 / 265 CC 32 SCAR 18.7
CC
32
Cov
85%
configure
21.6
out
8.0
a_end
5.2
CRAP 6.5
« FormatText.pm ↑ Summary FormatPS.pm »
1 package HTML::FormatMarkdown;
2  
3 # ABSTRACT: Format HTML as Markdown
4  
5  
6 3 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use parent 'HTML::Formatter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '2.16'; # VERSION
13 3 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
14  
15 6 ▶sub default_values {
Subroutine default_values: called CC=1 SCAR=0.0
Pod undocumented
16 6 ( shift->SUPER::default_values(),
17 lm => 0,
18 rm => 70,
19 );
20 }
21  
22 2 ▶sub configure {
Subroutine configure: called CC=11 SCAR=30.7
Pod undocumented
23 2 my ( $self, $hash ) = @_;
24  
25 2 my $lm = $self->{lm};
26 2 my $rm = $self->{rm};
27  
28 2 ▶ $lm = delete $hash->{lm} if exists $hash->{lm};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"lm"} 0 2 2
29 2 ▶ $lm = delete $hash->{leftmargin} if exists $hash->{leftmargin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"leftmargin"} 2 0 2
30 2 ▶ $rm = delete $hash->{rm} if exists $hash->{rm};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"rm"} 0 2 2
31 2 ▶ $rm = delete $hash->{rightmargin} if exists $hash->{rightmargin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"rightmargin"} 2 0 2
32  
33 2 my $width = $rm - $lm;
34 2 ▶ if ( $width < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width < 1) 0 2 2
35 0 ▶ warn "Bad margins, ignored" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
36 0 return;
37 }
38 2 ▶ if ( $width < 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width < 20) 0 2 2
39 0 ▶ warn "Page probably too narrow" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
40 }
41  
42 2 for ( keys %$hash ) {
43 0 ▶ warn "Unknown configure option '$_'" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
44 }
45  
46 2 $self->{lm} = $lm;
47 2 $self->{rm} = $rm;
48 2 $self;
49 }
50  
51 4 ▶sub begin {
Subroutine begin: called CC=1 SCAR=0.0
Pod undocumented
52 4 my $self = shift;
53  
54 4 $self->SUPER::begin();
55 4 $self->{maxpos} = 0;
56 4 $self->{curpos} = 0; # current output position.
57 }
58  
59 4 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod undocumented
60 4 shift->collect("\n");
61 }
62  
63 2 ▶sub header_start {
Subroutine header_start: called CC=1 SCAR=0.0
Pod undocumented
64 2 my ( $self, $level ) = @_;
65  
66 2 $self->vspace(1);
67 2 $self->out( '#' x $level . ' ' );
68 2 1;
69 }
70  
71 2 ▶sub header_end {
Subroutine header_end: called CC=1 SCAR=0.0
Pod undocumented
72 2 my ( $self, $level ) = @_;
73  
74 2 $self->out( ' ' . '#' x $level );
75 2 $self->vspace(1);
76 }
77  
78 4 ▶sub bullet {
Subroutine bullet: called CC=1 SCAR=0.0
Pod undocumented
79 4 my $self = shift;
80  
81 4 $self->SUPER::bullet( $_[0] . ' ' );
82  
83 }
84  
85 1 ▶sub hr_start {
Subroutine hr_start: called CC=1 SCAR=0.0
Pod undocumented
86 1 my $self = shift;
87  
88 1 $self->vspace(1);
89 1 $self->out('- - -');
90 1 $self->vspace(1);
91 }
92  
93 1 ▶sub img_start {
Subroutine img_start: called CC=2 SCAR=7.0
Pod undocumented
94 1 my ( $self, $node ) = @_;
95  
96 1 ▶ my $alt = $node->attr('alt') || '';
Condition50%1/2
$node->attr('alt') || ''
l!l
01
MC/DC0%0/1
$node->attr('alt') || ''
$node->attr('alt')
Truth table50%1/2
A
A= $node->attr('alt')
Aresult
11
00
97 1 my $src = $node->attr('src');
98  
99 1 $self->out("![$alt]($src)");
100 }
101  
102 2 ▶sub a_start {
Subroutine a_start: called CC=3 SCAR=12.7
Pod undocumented
103 2 my ( $self, $node ) = @_;
104  
105 # ignore named anchors
106 2 ▶ if ( $node->attr('name') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->attr('name')) { } 0 2 2
elsif ($node->attr('href') =~ /^#/) { } 0 2 2
107 0 1;
108 }
109 elsif ( $node->attr('href') =~ /^#/ ) {
110 0 1;
111 }
112 else {
113 2 $self->out("[");
114 2 1;
115 }
116 }
117  
118 2 ▶sub a_end {
Subroutine a_end: called CC=4 SCAR=16.4
Pod undocumented
119 2 my ( $self, $node ) = @_;
120  
121 2 ▶ if ( $node->attr('name') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->attr('name')) { } 0 2 2
elsif (my $href = $node->attr('href')) { } 2 0 2
122 0 return;
123 }
124 elsif ( my $href = $node->attr('href') ) {
125 2 ▶ if ( $href =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href =~ /^#/) 0 2 2
126 0 return;
127 }
128 2 $self->out("]($href)");
129 }
130 }
131  
132 1 ▶sub b_start { shift->out("**") }
Subroutine b_start: called CC=1 SCAR=0.0
Pod undocumented
133 1 ▶sub b_end { shift->out("**") }
Subroutine b_end: called CC=1 SCAR=0.0
Pod undocumented
134 1 ▶sub i_start { shift->out("*") }
Subroutine i_start: called CC=1 SCAR=0.0
Pod undocumented
135 1 ▶sub i_end { shift->out("*") }
Subroutine i_end: called CC=1 SCAR=0.0
Pod undocumented
136  
137 1 ▶sub tt_start {
Subroutine tt_start: called CC=2 SCAR=7.6
Pod undocumented
138 1 my $self = shift;
139  
140 1 ▶ if ( $self->{pre} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pre'}) { } 0 1 1
141 0 return 1;
142 }
143 else {
144 1 $self->out("`");
145 }
146 }
147  
148 1 ▶sub tt_end {
Subroutine tt_end: called CC=2 SCAR=7.6
Pod undocumented
149 1 my $self = shift;
150  
151 1 ▶ if ( $self->{pre} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pre'}) { } 0 1 1
152 0 return;
153 }
154 else {
155 1 $self->out("`");
156 }
157 }
158  
159 1 ▶sub blockquote_start {
Subroutine blockquote_start: called CC=1 SCAR=0.0
Pod undocumented
160 1 my $self = shift;
161  
162 1 $self->{blockquote}++;
163 1 $self->vspace(1);
164 1 $self->adjust_rm(-4);
165  
166 1 1;
167 }
168  
169 1 ▶sub blockquote_end {
Subroutine blockquote_end: called CC=1 SCAR=0.0
Pod undocumented
170 1 my $self = shift;
171  
172 1 $self->{blockquote}--;
173 1 $self->vspace(1);
174 1 $self->adjust_rm(+4);
175  
176 }
177  
178 1 ▶sub blockquote_out {
Subroutine blockquote_out: called CC=3 SCAR=11.0
Pod undocumented
179 1 my ( $self, $text ) = @_;
180  
181 1 $self->nl;
182 1 $self->goto_lm;
183  
184 1 my $line = "> ";
185 1 $self->{curpos} += 2;
186  
187 1 foreach my $word ( split /\s/, $text ) {
188 30 $line .= "$word ";
189 30 ▶ if ( ( $self->{curpos} + length($line) ) > $self->{rm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"curpos"} + length($line) > $self->{"rm"}) 4 26 30
190 4 $self->collect($line);
191 4 $self->nl;
192 4 $self->goto_lm;
193 4 $line = "> ";
194 4 $self->{curpos} += 2;
195 }
196 }
197  
198 1 $self->collect($line);
199 1 $self->nl;
200  
201 }
202  
203 # Quoted from HTML::FormatText
204 1 ▶sub pre_out {
Subroutine pre_out: called CC=4 SCAR=13.9
Pod undocumented
205 1 my $self = shift;
206  
207 1 ▶ if ( defined $self->{vspace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"vspace"}) 1 0 1
208 1 ▶ if ( $self->{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out"}) 1 0 1
209 1 ▶ $self->nl() while $self->{vspace}-- >= 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"vspace"}-- >= 0 and do { $self->nl } 2 1 3
210 1 $self->{vspace} = undef;
211 }
212 }
213  
214 1 my $indent = ' ' x $self->{lm};
215 1 $indent .= ' ' x 4;
216 1 my $pre = shift;
217 1 $pre =~ s/^/$indent/mg;
218 1 $self->collect($pre);
219 1 $self->{out}++;
220 }
221  
222 862 ▶sub out {
Subroutine out: called CC=8 SCAR=20.8
Pod undocumented
223 862 my $self = shift;
224 862 my $text = shift;
225  
226 862 ▶ if ( $text =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^\s*$/) 423 439 862
227 423 $self->{hspace} = 1;
228 423 return;
229 }
230  
231 439 ▶ if ( defined $self->{vspace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"vspace"}) 27 412 439
232 27 ▶ if ( $self->{out} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out"}) 25 2 27
233 25 ▶ $self->nl while $self->{vspace}-- >= 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"vspace"}-- >= 0 and do { $self->nl } 48 25 73
234 }
235 27 $self->goto_lm;
236 27 $self->{vspace} = undef;
237 27 $self->{hspace} = 0;
238 }
239  
240 439 ▶ if ( $self->{hspace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hspace"}) 393 46 439
241 393 ▶ if ( $self->{curpos} + length($text) > $self->{rm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'curpos'} + length($text) > $self->{'rm'}) { } 46 347 393
242  
243 # word will not fit on line; do a line break
244 46 $self->nl;
245 46 $self->goto_lm;
246 }
247 else {
248  
249 # word fits on line; use a space
250 347 $self->collect(' ');
251 347 ++$self->{curpos};
252 }
253 393 $self->{hspace} = 0;
254 }
255  
256 439 $self->collect($text);
257 439 my $pos = $self->{curpos} += length $text;
258 439 ▶ $self->{maxpos} = $pos if $self->{maxpos} < $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"maxpos"} < $pos 23 416 439
259 439 $self->{'out'}++;
260 }
261  
262 90 ▶sub goto_lm {
Subroutine goto_lm: called CC=2 SCAR=6.9
Pod undocumented
263 90 my $self = shift;
264  
265 90 my $pos = $self->{curpos};
266 90 my $lm = $self->{lm};
267 90 ▶ if ( $pos < $lm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $lm) 78 12 90
268 78 $self->{curpos} = $lm;
269 78 $self->collect( " " x ( $lm - $pos ) );
270 }
271 }
272  
273 102 ▶sub nl {
Subroutine nl: called CC=1 SCAR=0.0
Pod undocumented
274 102 my $self = shift;
275  
276 102 $self->{'out'}++;
277 102 $self->{curpos} = 0;
278 102 $self->collect("\n");
279 }
280  
281 12 ▶sub adjust_lm {
Subroutine adjust_lm: called CC=1 SCAR=0.0
Pod undocumented
282 12 my $self = shift;
283  
284 12 $self->{lm} += $_[0];
285 12 $self->goto_lm;
286 }
287  
288 2 ▶sub adjust_rm {
Subroutine adjust_rm: called CC=1 SCAR=0.0
Pod undocumented
289 2 shift->{rm} += $_[0];
290 }
291  
292 3 1;
293  
294 __END__
« FormatText.pm ↑ Summary FormatPS.pm »