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