| 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 | |||
| 17 | # The long options like --version depend on vilistextum being built with | ||
| 18 | # <getopt.h> and getopt_long(). The single-letter options like -v are | ||
| 19 | # always available. | ||
| 20 | |||
| 21 | |||
| 22 | package HTML::FormatText::Vilistextum; | ||
| 23 | use 5.006; | ||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use Carp; | ||
| 27 | use HTML::FormatExternal; | ||
| 28 | our @ISA = ('HTML::FormatExternal'); | ||
| 29 | |||
| 30 | # uncomment this to run the ### lines | ||
| 31 | # use Smart::Comments; | ||
| 32 | |||
| 33 | |||
| 34 | our $VERSION = 26; | ||
| 35 | |||
| 36 | # no left margin by default, no option to add it | ||
| 37 | use constant DEFAULT_LEFTMARGIN => 0; | ||
| 38 | use constant DEFAULT_RIGHTMARGIN => 76; # file text.c has breite=76 | ||
| 39 | |||
| 40 | # return true if vilistextum has its -u "--output-utf-8" option | ||
| 41 | use constant::defer _have_output_utf8 => sub { | ||
| 42 | my ($class) = @_; | ||
| 43 | my $help = $class->_run_version (['vilistextum', '-help']); | ||
| 44 | return (defined $help && $help =~ /\s-u[, ]/); | ||
| 45 | }; | ||
| 46 | |||
| 47 | use constant _WIDE_INPUT_CHARSET => 'entitize'; | ||
| 48 | sub _WIDE_OUTPUT_CHARSET { | ||
| 49 | my ($class) = @_; | ||
| 50 | return ($class->_have_output_utf8() ? 'UTF-8' : 'iso-8859-1'); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub program_full_version { | ||
| 54 | my ($self_or_class) = @_; | ||
| 55 | return $self_or_class->_run_version (['vilistextum', '-v']); | ||
| 56 | } | ||
| 57 | sub program_version { | ||
| 58 | my ($self_or_class) = @_; | ||
| 59 | my $version = $self_or_class->program_full_version; | ||
| 60 | if (! defined $version) { return undef; } | ||
| 61 | |||
| 62 | # eg. "Vilistextum 2.6.9 (22.10.2006)" | ||
| 63 | $version =~ m{^Vilistextum ([0-9][^ ]*)}i | ||
| 64 | or $version =~ /^(.*)/; # whole first line if format not recognised | ||
| 65 | return $1; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _make_run { | ||
| 69 | my ($self, $input_filename, $options) = @_; | ||
| 70 | my @command = ('vilistextum'); | ||
| 71 | |||
| 72 | if (defined $options->{'_width'}) { | ||
| 73 | push @command, '-w', $options->{'_width'}; | ||
| 74 | } | ||
| 75 | |||
| 76 | if ($options->{'output_charset'}) { | ||
| 77 | if (lc($options->{'output_charset'}) eq 'utf-8') { | ||
| 78 | # If asked for utf-8 and no multibyte then don't want to silently give | ||
| 79 | # back latin-1 instead. | ||
| 80 | # Maybe it'd be better to use Encode.pm to convert. | ||
| 81 | if (! $self->_have_output_utf8()) { | ||
| 82 | croak "Output charset $options->{'output_charset'} not available, vilistextum built without multibyte"; | ||
| 83 | } | ||
| 84 | push @command, '-u'; | ||
| 85 | |||
| 86 | } else { | ||
| 87 | # Not sure about croaking on unknown charset. | ||
| 88 | # if $output_charset ne 'latin-1' 'iso-8859-1' | ||
| 89 | # croak "Output charset $options->{'output_charset'} unknown"; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | # 'vilistextum_options' not documented ... | ||
| 94 | push @command, @{$options->{'vilistextum_options'} || []}; | ||
| 95 | |||
| 96 | # "-" means to stdout | ||
| 97 | push @command, $input_filename, '-'; | ||
| 98 | |||
| 99 | return (\@command); | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 | __END__ |