txt2html
view release on metacpan or search on metacpan
lib/HTML/TextToHTML.pm view on Meta::CPAN
# replace non-ASCII characters with character entities.
if (!$self->{eight_bit_clean})
{
my @chars = split(//, $para);
foreach $_ (@chars)
{
$_ = $char_entities{$_} if defined($char_entities{$_});
}
$para = join('', @chars);
}
$self->{__prev_para_action} = $para_action;
return $para;
} # process_para
=head2 txt2html
$conv->txt2html(%args);
Convert a text file to HTML. Takes a hash of arguments. See
L</OPTIONS> for the possible values of the arguments. Arguments which
have already been set with B<new> or B<args> will remain as they are,
unless they are overridden.
=cut
sub txt2html ($;$)
{
my $self = shift;
if (@_)
{
$self->args(@_);
}
$self->do_init_call();
my $outhandle;
my $outhandle_needs_closing;
# set up the output
if ($self->{outhandle})
{
$outhandle = $self->{outhandle};
$outhandle_needs_closing = 1;
}
elsif ($self->{outfile} eq "-")
{
$outhandle = *STDOUT;
$outhandle_needs_closing = 0;
}
else
{
open($outhandle, "> " . $self->{outfile})
|| die "Error: unable to open ", $self->{outfile}, ": $!\n";
$outhandle_needs_closing = 1;
}
# slurp up a paragraph at a time, a file at a time
local $/ = "";
my $para = '';
my $count = 0;
my $print_count = 0;
my @sources = ();
my $source_type;
if ($self->{infile} and @{$self->{infile}})
{
@sources = @{$self->{infile}};
$source_type = 'file';
}
elsif ($self->{inhandle} and @{$self->{inhandle}})
{
@sources = @{$self->{inhandle}};
$source_type = 'filehandle';
}
elsif ($self->{instring} and @{$self->{instring}})
{
@sources = @{$self->{instring}};
$source_type = 'string';
}
my $inhandle;
my $inhandle_needs_closing = 0;
foreach my $source (@sources)
{
$inhandle = undef;
if ($source_type eq 'file')
{
if (!$source or $source eq '-')
{
$inhandle = *STDIN;
$inhandle_needs_closing = 0;
}
else
{
if (-f $source && open($inhandle, $source))
{
$inhandle_needs_closing = 1;
}
else # error
{
warn "Could not open $source\n";
next;
}
}
}
elsif ($source_type eq 'filehandle')
{
$inhandle = $source;
$inhandle_needs_closing = 1;
}
if ($source_type eq 'string')
{
# process the string
$para = $_;
$para =~ s/\n$//; # trim the endline
if ($count == 0)
{
$self->do_file_start($outhandle, $para);
}
$self->{__done_with_sect_link} = [];
( run in 2.851 seconds using v1.01-cache-2.11-cpan-364913b4093 )