简体   繁体   中英

How can I convert plain text to HTML (preferably using Perl)?

Is there a way to take a plain text file and convert it to a simple HTML?

A couple of 'sophisticated' stuff that will be great

  • identify hyper-links.
  • identify (tab delimited) tables.

UPDATE

I just found this HTML::FromText . Checking to see if it meets my needs...

Text::Markdown

Stack Overflow already uses Markdown because it's the best mark-up language targeted to general text to HTML conversion. Named links are explained in the editing help .

Try HTML::TextToHTML :

From the command line:

txt2html I<arguments>

From Scripts:

use HTML::TextToHTML;

# create a new object
my $conv = new HTML::TextToHTML();

# convert a file
$conv->txt2html(infile=>[$text_file],
                 outfile=>$html_file,
                 title=>"Wonderful Things",
                 mail=>1,
  ]);

# reset arguments
$conv->args(infile=>[], mail=>0);

# convert a string
$newstring = $conv->process_chunk($mystring)

You can use lynx with -dump option to achieve that:

use File::Temp;

sub html2Txt {
    my $html = shift;
    my $html_file = File::Temp->new(SUFFIX => '.html');
    print $html_file $html;
    close $html_file;
    return scalar `lynx -dump $html_file 2> /dev/null`;
}

print html2Txt '<h1>Hi there!</h1> Testing <p>Testing</p>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM