Converts plain text to html
version 2.07
use HTML::FromText; text2html( $text, %options ); # or use HTML::FromText (); my $t2h = HTML::FromText->new( \%options ); my $html = $t2h->parse( $html );
\*(C`HTML::FromText\*(C' converts plain text to \s-1HTML\s0. There are a handful of options that shape the conversion. There is a utility function, \*(C`text2html\*(C', that's exported by default. This function is simply a short- cut to the Object Oriented interface described in detail below.
my $t2h = HTML::FromText->new({ paras => 1, blockcode => 1, tables => 1, bullets => 1, numbers => 1, urls => 1, email => 1, bold => 1, underline => 1, });
Constructs a new \*(C`HTML::FromText\*(C' object using the given configuration. The resulting object can parse lots of objects using the \*(C`parse\*(C' method.
Options to \*(C`new\*(C' are passed by name, with the value being either true or false. If true, the option will be turned on. If false, it will be turned off. The following outlines all the options.
Decorators
This option is on by default. All characters that are unsafe for \s-1HTML\s0 display will be encoded using \*(C`HTML::Entities::encode_entities()\*(C'.
This option is off by default. Replaces URLs with links.
This option is off by default. Replaces email addresses with \*(C`mailto:\*(C' links.
This option is off by default. Replaces text surrounded by asterisks (\*(C`*\*(C') with the same text surrounded by \*(C`strong\*(C' tags.
This option is off by default. Replaces text surrownded by underscores (\*(C`_\*(C') with the same text surrounded by \*(C`span\*(C' tags with an underline style.
Output Modes
The following are three output modes and the options associated with them. They are listed in order of precidence. If none of these modes are supplied, the basic decorators are applied to the text in whole.
This option is off by default. Wraps the entire text in \*(C`pre\*(C' tags.
This option is off by default. Preserves line breaks by inserting \*(C`br\*(C' tags at the end of each line. This mode has further options.
This option is off by default. All spaces are \s-1HTML\s0 encoded.
This option is off by default. Preserves paragraphs by wrapping them in \*(C`p\*(C' tags. This mode has further options.
This option is off by default. Convert bulleted lists into unordered lists (\*(C`ul\*(C'). Bullets can be either an asterisk (\*(C`*\*(C') or a hyphen (\*(C`-\*(C'). Lists can be nested.
This option is off by default. Convert numbered lists into ordered lists (\*(C`ol\*(C'). Numbered lists are identified by numerals. Lists may be nested.
This option is off by default. Convert paragraphs identified as headings into \s-1HTML\s0 headings at the appropriate level. The heading \*(C`1. Top\*(C' would be heading level one (\*(C`h1\*(C'). The heading \*(C`2.5.1. Blah\*(C' would be heading level three (\*(C`h3\*(C').
This option is off by default. Convert the first paragraph to a heading level one (\*(C`h1\*(C').
This option is off by default. Convert paragraphs identified as tables to \s-1HTML\s0 tables. Tables are two or more rows and two or more columns. Columns should be separated by two or more spaces.
The following options apply specifically to indented paragraphs. They are listed in order of precidence.
This option is off by default. Convert indented paragraphs to block quotes using the \*(C`blockquote\*(C' tag.
Convert indented paragraphs as \*(C`blockparas\*(C' would, but also preserving line breaks.
Convert indented paragraphs as \*(C`blockquotes\*(C' would, but also preserving spaces using \*(C`pre\*(C' tags.
my $html = $t2h->parse( $text );
Parses text supplied as a single scalar string and returns the \s-1HTML\s0 as a single scalar string. All the tabs in your text will be expanded using \*(C`Text::Tabs::expand()\*(C'.
my $html = text2html( $text, urls => 1, email => 1, );
Functional interface that just wraps the \s-1OO\s0 interface. This function is exported by default. If you don't want it you can \*(C`require\*(C' the module or \*(C`use\*(C' it with an empty list.
require HTML::FromText; # or ... use HTML::FromText ();
Note: At the time of this release, the internals of \*(C`HTML::FromText\*(C' are in a state of development and cannot be expected to stay the same from release to release. I expect that release version 3.00 will be analogous to a 1.00 release of other software. This is because the current maintainer has rewritten this distribution from the ground up for the \*(C`2.x\*(C' series. You have been warned.
The following methods may be used for subclassing \*(C`HTML::FromText\*(C' to create your own text to \s-1HTML\s0 conversions. Each of these methods is passed just one argument, the object ($self), unless otherwise stated.
The structure of $self is as follows for this release.
{ options => { option_name => $value, ... }, text => $text, # as passed to parse(), with tabs expanded html => $html, # the HTML that will be returned from parse() }
pre
Used when \*(C`pre\*(C' mode is specified.
Should set \*(C`$self->{html}\*(C'.
Return value is ignored.
lines
Used when \*(C`lines\*(C' mode is specified.
Implements the \*(C`spaces\*(C' option internally when the option is set to a true value.
Should set \*(C`$self->{html}\*(C'.
Return value is ignored.
paras
Used when the \*(C`paras\*(C' mode is specified.
Splits \*(C`$self->{text}\*(C' into paragraphs internally and sets up \*(C`$self->{paras}\*(C' as follows.
paras => { 0 => { text => $text, # paragraph text html => $html, # paragraph html }, ... # and so on for all paragraphs },
Implements the \*(C`title\*(C' option internally when the option is turned on.
Converts any normal paragraphs to \s-1HTML\s0 paragraphs (surrounded by \*(C`p\*(C' tags) internally.
Should set \*(C`$self->{html}\*(C'.
Return value is ignored.
headings
Used to format headings when the \*(C`headings\*(C' option is turned on.
Return value is ignored.
bullets
Format bulleted lists when the \*(C`bullets\*(C' option is turned on.
Return value is ignored.
numbers
Format numbered lists when the \*(C`numbers\*(C' option is turned on.
Return value is ignored.
tables
Format tables when the \*(C`tables\*(C' option is turned on.
Return value is ignored.
blockparas
Used when the \*(C`blockparas\*(C' option is turned on.
Return value is ignored.
blockquotes
Used when the \*(C`blockquotes\*(C' option is turned on.
Return value is ignored.
blockcode
Used when the \*(C`blockcode\*(C' option is turned on.
Return value is ignored.
urls
Turn urls into links when \*(C`urls\*(C' option is turned on.
Should operate on \*(C`$self->{html}\*(C'.
Return value is ignored.
Turn email addresses into \*(C`mailto:\*(C' links when \*(C`email\*(C' option is turned on.
Should operate on \*(C`$self->{html}\*(C'.
Return value is ignored.
underline
Underline things between _underscores_ when \*(C`underline\*(C' option is turned on.
Should operate on \*(C`$self->{html}\*(C'.
Return value is ignored.
bold
Bold things between *asterisks* when \*(C`bold\*(C' option is turned on.
Should operate on \*(C`$self->{html}\*(C'.
Return value is ignored.
metachars
Encode meta characters when \*(C`metachars\*(C' option is turned on.
Should operate on \*(C`$self->{html}\*(C'.
Return value is ignored.
The output from \*(C`HTML::FromText\*(C' has been updated to pass \s-1XHTML\s0 1.1 validation. Every \s-1HTML\s0 tag that should have a \s-1CSS\s0 class name does. They are prefixed with \*(C`hft-\*(C' and correspond to the names of the options to \*(C`new()\*(C' (or \*(C`text2html()\*(C'). For example \*(C`hft-lines\*(C', \*(C`hft-paras\*(C', and \*(C`hft-urls\*(C'.
One important note is the output for \*(C`underline\*(C'. Because the <u> tag is deprecated in this specification a \*(C`span\*(C' is used with a style attribute of \*(C`text-decoration: underline\*(C'. The class is \*(C`hft- underline\*(C'. If you want to override the \*(C`text-decoration\*(C' style in the \s-1CSS\s0 class you'll need to do so like this.
text-decoration: none !important;
text2html\|(1).
Ricardo \s-1SIGNES\s0 <[email protected]>
Casey West <[email protected]>
Gareth Rees <[email protected]>
This software is copyright (c) 2003 by Casey West.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.