HTML::TextToHTML converts plain text files to HTML. The txt2html script uses this module to do the same from the command-line.

It supports headings, tables, lists, simple character markup, and hyperlinking, and is highly customizable. It recognizes some of the apparent structure of the source document (mostly whitespace and typographic layout), and attempts to mark that structure explicitly using HTML. The purpose for this tool is to provide an easier way of converting existing text documents to HTML format, giving something nicer than just whapping the text into a big PRE block.

All arguments can be set when the object is created, and further options can be set when calling the actual txt2html method. Arguments to methods can take a hash of arguments.

Note that all option-names must match exactly -- no abbreviations are allowed. The argument-keys are expected to have values matching those required for that argument -- whether that be a boolean, a string, a reference to an array or a reference to a hash. These will replace any value for that argument that might have been there before.

If you want something appended by default, put the filename here. The appended text will not be processed at all, so make sure it's plain text or correct HTML. i.e. do not have things like: Mary Andersen <kitty@example.com> but instead, have: Mary Andersen &lt;kitty@example.com&gt;

If you want something appended to the head by default, put the filename here. The appended text will not be processed at all, so make sure it's plain text or correct HTML. i.e. do not have things like: Mary Andersen <kitty@example.com> but instead, have: Mary Andersen &lt;kitty@example.com&gt;

This defines what character (or string) is taken to be the delimiter of text which is to be interpreted as bold (that is, to be given a STRONG tag). If this is empty, then no bolding of text will be done. (default: #)

This defines what single characters are taken to be "bullet" placeholder characters for ordered lists. Ordered lists are normally marked by a number or letter followed by '.' or ')' or ']' or ':'. If an ordered bullet is used, then it simply indicates that this is an ordered list, without giving explicit numbers.

Note that because this is used as a character class, if you use '-' it must come first. (default:nothing)

Add patterns for headings. Header levels are assigned by regexp in the order seen in the input text. When a line matches a custom header regexp, it is tagged as a header. If it's the first time that particular regexp has matched, the next available header level is associated with it and applied to the line. Any later matches of that regexp will use the same header level. Therefore, if you want to match numbered header lines, you could use something like this:

Would be marked as H1, H2, and H3 (assuming they were found in that order, and that no other header styles were encountered). If you prefer that the first one specified always be H1, the second always be H2, the third H3, etc, then use the "explicit_headings" option.

Don't try to find any headings except the ones specified in the --custom_heading_regexp option. Also, the custom headings will not be assigned levels in the order they are encountered in the document, but in the order they are specified on the custom_heading_regexp option. (default: false)

Extract Mode; don't put HTML headers or footers on the result, just the plain HTML (thus making the result suitable for inserting into another document (or as part of the output of a CGI script). (default: false)

Treat paragraphs marked solely by indents as breaks with indents. That is, instead of taking a three-space indent as a new paragraph, put in a <BR> and three non-breaking spaces instead. (see also --preserve_indent) (default: false)

This defines what character (or string) is taken to be the delimiter of text which is to be interpreted as italic (that is, to be given a EM tag). If this is empty, no italicising of text will be done. (default: *)

This defines what character (or string) is taken to be the delimiter of text which is to be interpreted as underlined (that is, to be given a U tag). If this is empty, no underlining of text will be done. (default: _)

File(s) to use as a link-dictionary. There can be more than one of these. These are in addition to the Global Link Dictionary and the User Link Dictionary. This expects a reference to an array of filenames.

Do no escaping or marking up at all, except for processing the links dictionary file and applying it. This is useful if you want to use the linking feature on an HTML document. If the HTML is a complete document (includes HTML,HEAD,BODY tags, etc) then you'll probably want to use the --extract option also. (default: false)

Minimum number of consecutive whitespace characters to trigger normal preformatting. NOTE: Tabs are expanded to spaces before this check is made. That means if tab_width is 8 and this is 5, then one tab may be expanded to 8 spaces, which is enough to trigger preformatting. (default: 5)

Use this option if you want to force the heading styles to match what Mosaic outputs. (Underlined with "***"s is H1, with "==="s is H2, with "+++" is H3, with "---" is H4, with "~~~" is H5 and with "..." is H6) This was the behavior of txt2html up to version 1.10. (default: false)

Turn on preformatting when encountering "<PRE>" on a line by itself, and turn it off when there's a line containing only "</PRE>". When such preformatted text is detected, the PRE tag will be given the class 'quote_explicit'. (default: off)

Try to make the output conform to the XHTML standard, including closing all open tags and marking empty tags correctly. This turns on --lower_case_tags and overrides the --doctype option. Note that if you add a header or a footer file, it is up to you to make it conform; the header/footer isn't touched by this. Likewise, if you make link-dictionary entries that break XHTML, then this won't fix them, except to the degree of putting all tags into lower-case.

Updates the current arguments/options of the HTML::TextToHTML object. Takes hash of arguments, which will be used in invocations of other methods. See "OPTIONS" for the possible values of the arguments.

Convert a string to a HTML fragment. This assumes that this string is at the least, a single paragraph, but it can contain more than that. This returns the processed string. If you want to pass arguments to alter the behaviour of this conversion, you need to do that earlier, either when you create the object, or with the "args" method.

$newstring = $conv->process_chunk($mystring,
close_tags=>0);

If there are open tags (such as lists) in the input string, process_chunk will automatically close them, unless you specify not to, with the close_tags option.

$newstring = $conv->process_chunk($mystring,
is_fragment=>1);

If you want this string to be treated as a fragment, and not assumed to be a paragraph, set is_fragment to true. If there is more than one paragraph in the string (ie it contains blank lines) then this option will be ignored.

Convert a string to a HTML fragment. This assumes that this string is at the most a single paragraph, with no blank lines in it. If you don't know whether your string will contain blank lines or not, use the "process_chunk" method instead.

This returns the processed string. If you want to pass arguments to alter the behaviour of this conversion, you need to do that earlier, either when you create the object, or with the "args" method.

$newstring = $conv->process_para($mystring,
close_tags=>0);

If there are open tags (such as lists) in the input string, process_para will automatically close them, unless you specify not to, with the close_tags option.

$newstring = $conv->process_para($mystring,
is_fragment=>1);

If you want this string to be treated as a fragment, and not assumed to be a paragraph, set is_fragment to true.

Convert a text file to HTML. Takes a hash of arguments. See "OPTIONS" for the possible values of the arguments. Arguments which have already been set with new or args will remain as they are, unless they are overridden.

then it has a row of +------+-----+
then it has a row of column headings separated by |
then it has a row of +------+-----+
then it has one or more rows of column values separated by |
then it has a row of +------+-----+

There are two files which are used which can affect the outcome of the conversion. One is the link dictionary, which contains patterns (of how to recognise http links and other things) and how to convert them. The other is, naturally, the format of the input file itself.

A link dictionary file contains patterns to match, and what to convert them to. It is called a "link" dictionary because it was intended to be something which defined what a href link was, but it can be used for more than that. However, if you wish to define your own links, it is strongly advised to read up on regular expressions (regexes) because this relies heavily on them.

The file consists of comments (which are lines starting with #) and blank lines, and link entries. Each entry consists of a regular expression, a -> separator (with optional flags), and a link "result".

In the simplest case, with no flags, the regular expression defines the pattern to look for, and the result says what part of the regular expression is the actual link, and the link which is generated has the href as the link, and the whole matched pattern as the visible part of the link. The first character of the regular expression is taken to be the separator for the regex, so one could either use the traditional / separator, or something else such as | (which can be helpful with URLs which are full of / characters).

So, for example, an ftp URL might be defined as:

|ftp:[\w/\.:+\-]+| -> $&

This takes the whole pattern as the href, and the resultant link has the same thing in the href as in the contents of the anchor.

But sometimes the href isn't the whole pattern.

/&lt;URL:\s*(\S+?)\s*&gt;/ --> $1

With the above regex, a () grouping marks the first subexpression, which is represented as $1 (rather than $& the whole expression). This entry matches a URL which was marked explicity as a URL with the pattern <URL:foo> (note the &lt; is shown as the entity, not the actual character. This is because by the time the links dictionary is checked, all such things have already been converted to their HTML entity forms, unless, of course, the escape_HTML_chars option was turned off) This would give us a link in the form <A HREF="foo">&lt;URL:foo&gt;</A>

The h flag

However, if we want more control over the way the link is constructed, we can construct it ourself. If one gives the h flag, then the "result" part of the entry is taken not to contain the href part of the link, but the whole link.

For example, the entry:

/&lt;URL:\s*(\S+?)\s*&gt;/ -h-> <A HREF="$1">$1</A>

will take <URL:foo> and give us <A HREF="foo">foo</A>

However, this is a very powerful mechanism, because it can be used to construct custom tags which aren't links at all. For example, to flag *italicised words* the following entry will surround the words with EM tags.

/\B\*([a-z][a-z -]*[a-z])\*\B/ -hi-> <EM>$1</EM>

The i flag

This turns on ignore case in the pattern matching.

The e flag

This turns on execute in the pattern substitution. This really only makes sense if h is turned on too. In that case, the "result" part of the entry is taken as perl code to be executed, and the result of that code is what replaces the pattern.

The o flag

This marks the entry as a once-only link. This will convert the first instance of a matching pattern, and ignore any others further on.

For example, the following pattern will take the first mention of HTML::TextToHTML and convert it to a link to the module's home page.

For the most part, this module tries to use intuitive conventions for determining the structure of the text input. Unordered lists are marked by bullets; ordered lists are marked by numbers or letters; in either case, an increase in indentation marks a sub-list contained in the outer list.

Headers (apart from custom headers) are distinguished by "underlines" underneath them; headers in all-capitals are distinguished from those in mixed case. All headers, both normal and custom headers, are expected to start at the first line in a "paragraph".

In other words, the following is a header:

I am Head Man
-------------

But the following does not have a header:

I am not a head Man, man
I am Head Man
-------------

Tables require a more rigid convention. A table must be marked as a separate paragraph, that is, it must be surrounded by blank lines. Tables come in different types. For a table to be parsed, its --table_type option must be on, and the --make_tables option must be true.

ALIGN Table Type

Columns must be separated by two or more spaces (this prevents accidental incorrect recognition of a paragraph where interword spaces happen to line up). If there are two or more rows in a paragraph and all rows share the same set of (two or more) columns, the paragraph is assumed to be a table. For example

This can also have an optional caption at the start. This table is also rendered with a border and table-headers like the BORDER type.

DELIM Table Type

This table type is delimited by non-alphanumeric characters, and has to have at least two rows and two columns before it's recognised as a table.

This one is delimited by the '| character:

| val1 | val2 |
| val3 | val3 |

But one can use almost any suitable character such as : # $ % + and so on. This is clever enough to figure out what you are using as the delimiter if you have your data set up like a table. Note that the line has to both begin and end with the delimiter, as well as using it to separate values.

If the underline used to mark a header is off by more than 1, then that part of the text will not be picked up as a header unless you change the value of --underline_length_tolerance and/or --underline_offset_tolerance. People tend to forget this.

Make sure you have the dependencies installed first! (see REQUIRES above)

Some of those modules come standard with more recent versions of perl, but I thought I'd mention them anyway, just in case you may not have them.

If you don't know how to install these, try using the CPAN module, an easy way of auto-installing modules from the Comprehensive Perl Archive Network, where the above modules reside. Do "perldoc perlmodinstall" or "perldoc CPAN" for more information.

To install this module type the following:

perl Build.PL
./Build
./Build test
./Build install

Or, if you're on a platform (like DOS or Windows) that doesn't like the "./" notation, you can do this:

perl Build.PL
perl Build
perl Build test
perl Build install

In order to install somewhere other than the default, such as in a directory under your home directory, like "/home/fred/perl" go

perl Build.PL --install_base /home/fred/perl

as the first step instead.

This will install the files underneath /home/fred/perl.

You will then need to make sure that you alter the PERL5LIB variable to find the modules, and the PATH variable to find the script.

Therefore you will need to change: your path, to include /home/fred/perl/script (where the script will be)

PATH=/home/fred/perl/script:${PATH}

the PERL5LIB variable to add /home/fred/perl/lib

PERL5LIB=/home/fred/perl/lib:${PERL5LIB}

Note that the system links dictionary will be installed as "/home/fred/perl/share/txt2html/txt2html.dict"

If you want to install in a temporary install directory (such as if you are building a package) then instead of going

perl Build install

go

perl Build install destdir=/my/temp/dir

and it will be installed there, with a directory structure under /my/temp/dir the same as it would be if it were installed plain. Note that this is NOT the same as setting --install_base, because certain things are done at build-time which use the install_base info.

See "perldoc perlrun" for more information on PERL5LIB, and see "perldoc Module::Build" for more information on installation options.