line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormatText; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Format HTML as plaintext |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
21301
|
use 5.006_001; |
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# We now use Smart::Comments in place of the old DEBUG framework. |
11
|
|
|
|
|
|
|
# this should be commented out in release versions.... |
12
|
|
|
|
|
|
|
##use Smart::Comments; |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
5
|
use base 'HTML::Formatter'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
666
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '2.14'; # VERSION |
17
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
20
|
|
|
|
|
|
|
sub default_values { |
21
|
10
|
|
|
10
|
0
|
39
|
( shift->SUPER::default_values(), |
22
|
|
|
|
|
|
|
lm => 3, # left margin |
23
|
|
|
|
|
|
|
rm => 72, # right margin (actually, maximum text width) |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
28
|
|
|
|
|
|
|
sub configure { |
29
|
1
|
|
|
1
|
0
|
2
|
my ( $self, $hash ) = @_; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
8
|
my $lm = $self->{lm}; |
32
|
1
|
|
|
|
|
2
|
my $rm = $self->{rm}; |
33
|
|
|
|
|
|
|
|
34
|
1
|
50
|
|
|
|
6
|
$lm = delete $hash->{lm} if exists $hash->{lm}; |
35
|
1
|
50
|
|
|
|
5
|
$lm = delete $hash->{leftmargin} if exists $hash->{leftmargin}; |
36
|
1
|
50
|
|
|
|
4
|
$rm = delete $hash->{rm} if exists $hash->{rm}; |
37
|
1
|
50
|
|
|
|
4
|
$rm = delete $hash->{rightmargin} if exists $hash->{rightmargin}; |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
4
|
my $width = $rm - $lm; |
40
|
1
|
50
|
|
|
|
3
|
if ( $width < 1 ) { |
41
|
0
|
0
|
|
|
|
0
|
warn "Bad margins, ignored" if $^W; |
42
|
0
|
|
|
|
|
0
|
return; |
43
|
|
|
|
|
|
|
} |
44
|
1
|
50
|
|
|
|
4
|
if ( $width < 20 ) { |
45
|
0
|
0
|
|
|
|
0
|
warn "Page probably too narrow" if $^W; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
3
|
for ( keys %$hash ) { |
49
|
0
|
0
|
|
|
|
0
|
warn "Unknown configure option '$_'" if $^W; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
2
|
$self->{lm} = $lm; |
53
|
1
|
|
|
|
|
2
|
$self->{rm} = $rm; |
54
|
1
|
|
|
|
|
3
|
$self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
58
|
|
|
|
|
|
|
sub begin { |
59
|
9
|
|
|
9
|
0
|
15
|
my $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
9
|
|
|
|
|
31
|
$self->SUPER::begin; |
62
|
9
|
|
|
|
|
16
|
$self->{curpos} = 0; # current output position. |
63
|
9
|
|
|
|
|
16
|
$self->{maxpos} = 0; # highest value of $pos (used by header underliner) |
64
|
9
|
|
|
|
|
19
|
$self->{hspace} = 0; # horizontal space pending flag |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
68
|
|
|
|
|
|
|
sub end { |
69
|
9
|
|
|
9
|
0
|
26
|
shift->collect("\n"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
73
|
|
|
|
|
|
|
sub header_start { |
74
|
2
|
|
|
2
|
0
|
4
|
my ( $self, $level ) = @_; |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
23
|
$self->vspace( 1 + ( 6 - $level ) * 0.4 ); |
77
|
2
|
|
|
|
|
4
|
$self->{maxpos} = 0; |
78
|
2
|
|
|
|
|
7
|
1; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
82
|
|
|
|
|
|
|
sub header_end { |
83
|
2
|
|
|
2
|
0
|
4
|
my ( $self, $level ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
2
|
50
|
|
|
|
7
|
if ( $level <= 2 ) { |
86
|
2
|
|
|
|
|
3
|
my $line; |
87
|
2
|
100
|
|
|
|
6
|
$line = '=' if $level == 1; |
88
|
2
|
100
|
|
|
|
5
|
$line = '-' if $level == 2; |
89
|
2
|
|
|
|
|
5
|
$self->vspace(0); |
90
|
2
|
|
|
|
|
7
|
$self->out( $line x ( $self->{maxpos} - $self->{lm} ) ); |
91
|
|
|
|
|
|
|
} |
92
|
2
|
|
|
|
|
7
|
$self->vspace(1); |
93
|
2
|
|
|
|
|
5
|
1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
97
|
|
|
|
|
|
|
sub bullet { |
98
|
10
|
|
|
10
|
0
|
113
|
my $self = shift; |
99
|
|
|
|
|
|
|
|
100
|
10
|
|
|
|
|
39
|
$self->SUPER::bullet( $_[0] . ' ' ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
104
|
|
|
|
|
|
|
sub hr_start { |
105
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
8
|
$self->vspace(1); |
108
|
1
|
|
|
|
|
5
|
$self->out( '-' x ( $self->{rm} - $self->{lm} ) ); |
109
|
1
|
|
|
|
|
3
|
$self->vspace(1); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
113
|
|
|
|
|
|
|
sub pre_out { |
114
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# should really handle bold/italic etc. |
117
|
1
|
50
|
|
|
|
4
|
if ( defined $self->{vspace} ) { |
118
|
1
|
50
|
|
|
|
4
|
if ( $self->{out} ) { |
119
|
1
|
|
|
|
|
6
|
$self->nl() while $self->{vspace}-- >= 0; |
120
|
1
|
|
|
|
|
3
|
$self->{vspace} = undef; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
1
|
|
|
|
|
3
|
my $indent = ' ' x $self->{lm}; |
124
|
1
|
|
|
|
|
2
|
my $pre = shift; |
125
|
1
|
|
|
|
|
7
|
$pre =~ s/^/$indent/mg; |
126
|
1
|
|
|
|
|
4
|
$self->collect($pre); |
127
|
1
|
|
|
|
|
3
|
$self->{out}++; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
131
|
|
|
|
|
|
|
sub out { |
132
|
787
|
|
|
787
|
0
|
941
|
my $self = shift; |
133
|
787
|
|
|
|
|
1027
|
my $text = shift; |
134
|
|
|
|
|
|
|
|
135
|
787
|
|
|
|
|
997
|
$text =~ tr/\xA0\xAD/ /d; |
136
|
|
|
|
|
|
|
|
137
|
787
|
100
|
|
|
|
2210
|
if ( $text =~ /^\s*$/ ) { |
138
|
389
|
|
|
|
|
503
|
$self->{hspace} = 1; |
139
|
389
|
|
|
|
|
855
|
return; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
398
|
100
|
|
|
|
811
|
if ( defined $self->{vspace} ) { |
143
|
32
|
100
|
|
|
|
67
|
if ( $self->{out} ) { |
144
|
25
|
|
|
|
|
85
|
$self->nl while $self->{vspace}-- >= 0; |
145
|
|
|
|
|
|
|
} |
146
|
32
|
|
|
|
|
67
|
$self->goto_lm; |
147
|
32
|
|
|
|
|
56
|
$self->{vspace} = undef; |
148
|
32
|
|
|
|
|
52
|
$self->{hspace} = 0; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
398
|
100
|
|
|
|
826
|
if ( $self->{hspace} ) { |
152
|
358
|
100
|
|
|
|
698
|
if ( $self->{curpos} + length($text) > $self->{rm} ) { |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# word will not fit on line; do a line break |
155
|
41
|
|
|
|
|
79
|
$self->nl; |
156
|
41
|
|
|
|
|
83
|
$self->goto_lm; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
else { |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# word fits on line; use a space |
161
|
317
|
|
|
|
|
753
|
$self->collect(' '); |
162
|
317
|
|
|
|
|
473
|
++$self->{curpos}; |
163
|
|
|
|
|
|
|
} |
164
|
358
|
|
|
|
|
495
|
$self->{hspace} = 0; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
398
|
|
|
|
|
927
|
$self->collect($text); |
168
|
398
|
|
|
|
|
660
|
my $pos = $self->{curpos} += length $text; |
169
|
398
|
100
|
|
|
|
793
|
$self->{maxpos} = $pos if $self->{maxpos} < $pos; |
170
|
398
|
|
|
|
|
924
|
$self->{'out'}++; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
174
|
|
|
|
|
|
|
sub goto_lm { |
175
|
103
|
|
|
103
|
0
|
123
|
my $self = shift; |
176
|
|
|
|
|
|
|
|
177
|
103
|
|
|
|
|
155
|
my $pos = $self->{curpos}; |
178
|
103
|
|
|
|
|
113
|
my $lm = $self->{lm}; |
179
|
103
|
100
|
|
|
|
262
|
if ( $pos < $lm ) { |
180
|
73
|
|
|
|
|
90
|
$self->{curpos} = $lm; |
181
|
73
|
|
|
|
|
242
|
$self->collect( " " x ( $lm - $pos ) ); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
186
|
|
|
|
|
|
|
sub nl { |
187
|
92
|
|
|
92
|
0
|
113
|
my $self = shift; |
188
|
|
|
|
|
|
|
|
189
|
92
|
|
|
|
|
109
|
$self->{'out'}++; |
190
|
92
|
|
|
|
|
112
|
$self->{curpos} = 0; |
191
|
92
|
|
|
|
|
227
|
$self->collect("\n"); |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
195
|
|
|
|
|
|
|
sub adjust_lm { |
196
|
30
|
|
|
30
|
0
|
40
|
my $self = shift; |
197
|
|
|
|
|
|
|
|
198
|
30
|
|
|
|
|
39
|
$self->{lm} += $_[0]; |
199
|
30
|
|
|
|
|
58
|
$self->goto_lm; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
203
|
|
|
|
|
|
|
sub adjust_rm { |
204
|
2
|
|
|
2
|
0
|
6
|
shift->{rm} += $_[0]; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
1; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
__END__ |