开发者

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?

开发者_StackOverflow中文版

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>';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜