| 1 | # Copyright 2008, 2009, 2010, 2013, 2015 Kevin Ryde | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # HTML-FormatExternal is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 4 | # modify it under the terms of the GNU General Public License as published | ||||||||||||||||||||||||||||
| 5 | # by the Free Software Foundation; either version 3, or (at your option) any | ||||||||||||||||||||||||||||
| 6 | # later version. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # HTML-FormatExternal is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||
| 9 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||
| 10 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||||||||||||||||||||
| 11 | # for more details. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||
| 14 | # with HTML-FormatExternal. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package HTML::FormatText::Html2text; | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use HTML::FormatExternal; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | our @ISA = ('HTML::FormatExternal'); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 24 | # use Smart::Comments; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | our $VERSION = 26; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use constant DEFAULT_LEFTMARGIN => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use constant DEFAULT_RIGHTMARGIN => 79; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | my $have_ascii; | |||||||||||||||||||||||||||
| 32 | my $have_utf8; | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use constant::defer _check_help => sub { # run once only | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||
| 34 | 1 | my ($class) = @_; | |||||||||||||||||||||||||||
| 35 | 1 | my $help = $class->_run_version (['html2text', '-help']); | |||||||||||||||||||||||||||
| 36 | 1 | ▶ | $have_ascii = (defined $help && $help =~ /-ascii/); | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $help && $help =~ /-ascii/
MC/DC0%0/2
defined $help && $help =~ /-ascii/
defined $help
$help =~ /-ascii/
Truth table33%1/3
A && B
A= defined $helpB= $help =~ /-ascii/
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | $have_utf8 = (defined $help && $help =~ /-utf8/); | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $help && $help =~ /-utf8/
MC/DC0%0/2
defined $help && $help =~ /-utf8/
defined $help
$help =~ /-utf8/
Truth table33%1/3
A && B
A= defined $helpB= $help =~ /-utf8/
| |||||||||||||||||||||||||||||
| 38 | 1 | return undef; | |||||||||||||||||||||||||||
| 39 | 1 | }; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # return true if the "-ascii" option is available (new in html2text | ||||||||||||||||||||||||||||
| 42 | # version 1.3.2 from Jan 2004) | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub _have_ascii { | ||||||||||||||||||||||||||
|
Subroutine
_have_ascii: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 1 | my ($class) = @_; | |||||||||||||||||||||||||||
| 45 | 1 | $class->_check_help(); | |||||||||||||||||||||||||||
| 46 | 1 | return $have_ascii; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # return true if the "-utf8" option is available (a Debian addition circa 2009) | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _have_utf8 { | ||||||||||||||||||||||||||
|
Subroutine
_have_utf8: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||
| 52 | 0 | $class->_check_help(); | |||||||||||||||||||||||||||
| 53 | 0 | return $have_utf8; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # The Debian -utf8 option can give UTF-8 output. | ||||||||||||||||||||||||||||
| 57 | # For input believe entitized is the only way to be confident of working | ||||||||||||||||||||||||||||
| 58 | # with both original and Debian extended. | ||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | use constant _WIDE_INPUT_CHARSET => 'entitize'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _WIDE_OUTPUT_CHARSET { | ||||||||||||||||||||||||||
|
Subroutine
_WIDE_OUTPUT_CHARSET: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | return ($class->_have_utf8() ? 'UTF-8' : 'iso-8859-1'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 5 | ▶ | sub program_full_version { | ||||||||||||||||||||||||||
|
Subroutine
program_full_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 5 | my ($self_or_class) = @_; | |||||||||||||||||||||||||||
| 68 | 5 | return $self_or_class->_run_version (['html2text','-version'], '2>&1'); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 2 | ▶ | sub program_version { | ||||||||||||||||||||||||||
|
Subroutine
program_version: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 2 | my ($self_or_class) = @_; | |||||||||||||||||||||||||||
| 72 | 2 | my $version = $self_or_class->program_full_version; | |||||||||||||||||||||||||||
| 73 | 2 | ▶ | if (! defined $version) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # eg. "This is html2text, version 1.3.2a" | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $version =~ /^.*version (.*)/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | or $version =~ /^(.*)/; # whole first line if format not recognised | ||||||||||||||||||||||||||||
| 78 | 0 | return $1 . substr($version,0,0); # retain taintedness | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub _make_run { | ||||||||||||||||||||||||||
|
Subroutine
_make_run: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($class, $input_filename, $options) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # -nobs means don't do underlining with "_ backspace X" sequences. | ||||||||||||||||||||||||||||
| 85 | # Backspaces are fun for teletype output, but the intention here is plain | ||||||||||||||||||||||||||||
| 86 | # text. The Debian html2text has -nobs by default anyway. | ||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||
| 88 | 0 | my @command = ('html2text', '-nobs'); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (defined $options->{'_width'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | push @command, '-width', $options->{'_width'}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($class->_have_ascii) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (my $output_charset = $options->{'output_charset'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $output_charset = lc($output_charset); | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($output_charset eq 'ascii' || $output_charset eq 'ansi_x3.4-1968') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$output_charset eq "ascii" or $output_charset eq "ansi_x3.4-1968"
MC/DC0%0/2
$output_charset eq "ascii" or $output_charset eq "ansi_x3.4-1968"
$output_charset eq "ascii"
$output_charset eq "ansi_x3.4-1968"
Truth table0%0/3
A or B
A= $output_charset eq "ascii"B= $output_charset eq "ansi_x3.4-1968"
| |||||||||||||||||||||||||||||
| 98 | 0 | push @command, '-ascii'; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # 'html2text_options' not documented ... | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | push @command, @{$options->{'html2text_options'} || []}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options->{'html2text_options'} || []
MC/DC0%0/1
$options->{'html2text_options'} || []
$options->{'html2text_options'}
Truth table0%0/2
A
A= $options->{'html2text_options'}
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # "html2text -" input filename "-" means read standard input. | ||||||||||||||||||||||||||||
| 107 | # Any other "-foo" starting with "-" is an option and there's no apparent | ||||||||||||||||||||||||||||
| 108 | # "--" to mark the end of options (as of its version 1.3.2a). | ||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||
| 110 | # Normally html2text takes URL style file: or http:, but the debian | ||||||||||||||||||||||||||||
| 111 | # version mangles it to a bare filename only. This makes it hard to | ||||||||||||||||||||||||||||
| 112 | # escape a name suitably to get through both. Instead use standard input | ||||||||||||||||||||||||||||
| 113 | # which both versions read by default. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | return (\@command, | |||||||||||||||||||||||||||
| 116 | '<', $input_filename); | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||||||||