| 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
| |||||||||||||||
| 29 | 2 | ▶ | $lm = delete $hash->{leftmargin} if exists $hash->{leftmargin}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 2 | ▶ | $rm = delete $hash->{rm} if exists $hash->{rm}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 2 | ▶ | $rm = delete $hash->{rightmargin} if exists $hash->{rightmargin}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | 2 | my $width = $rm - $lm; | |||||||||||||
| 34 | 2 | ▶ | if ( $width < 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 0 | ▶ | warn "Bad margins, ignored" if $^W; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 0 | return; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | 2 | ▶ | if ( $width < 20 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 0 | ▶ | warn "Page probably too narrow" if $^W; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 2 | for ( keys %$hash ) { | |||||||||||||
| 43 | 0 | ▶ | warn "Unknown configure option '$_'" if $^W; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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') || ''
MC/DC0%0/1
$node->attr('alt') || ''
$node->attr('alt')
Truth table50%1/2
A
A= $node->attr('alt')
| |||||||||||||||
| 97 | 1 | my $src = $node->attr('src'); | |||||||||||||
| 98 | |||||||||||||||
| 99 | 1 | $self->out(""); | |||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 122 | 0 | return; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | elsif ( my $href = $node->attr('href') ) { | ||||||||||||||
| 125 | 2 | ▶ | if ( $href =~ /^#/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 208 | 1 | ▶ | if ( $self->{out} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 209 | 1 | ▶ | $self->nl() while $self->{vspace}-- >= 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 227 | 423 | $self->{hspace} = 1; | |||||||||||||
| 228 | 423 | return; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | |||||||||||||||
| 231 | 439 | ▶ | if ( defined $self->{vspace} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 232 | 27 | ▶ | if ( $self->{out} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 233 | 25 | ▶ | $self->nl while $self->{vspace}-- >= 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 241 | 393 | ▶ | if ( $self->{curpos} + length($text) > $self->{rm} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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__ | ||||||||||||||