TXT2HTML(1p) | User Contributed Perl Documentation | TXT2HTML(1p) |
txt2html - convert plain text file to HTML
version 3.0
txt2html --help | --manpage
txt2html [ --append_file filename ] [ --append_head
filename ]
[ --body_deco string ] [ --bold_delimiter string ]
[ --bullets string ] [ --bullets_ordered string ] [ --caps_tag
tag ]
{ --custom_heading_regexp regexp } [ --debug ] [ --demoronize ]
[ --default_link_dict filename ] [ --dict_debug n ]
[ --doctype doctype ] [ --eight_bit_clean ] [ --escape_HTML_chars ]
[ --explicit_headings ] [ --extract ] [ --hrule_min n ]
[ --indent_width n ] [ --indent_par_break ]
{ --infile filename | --instring string }
[ --italic_delimiter string ] { --links_dictionaries filename }
[ --link_only ] [ --lower_case_tags ] [ --mailmode ]
[ --make_anchors ] [ --make_tables ] [ --min_caps_length n ]
[ --outfile filename ] [ --par_indent n ]
[ --preformat_trigger_lines n ] [ --endpreformat_trigger_lines
n ]
[ --preformat_start_marker regexp ] [ --preformat_end_marker
regexp ]
[ --preformat_whitespace_min n ] [ --prepend_file filename ]
[ --preserve_indent ] [ --short_line_length n ]
[ --style_url stylesheet_url ] [ --tab_width n ]
[ --table_type type=0/1 ] [ --title title ] [ --titlefirst ]
[ --underline_delimiter string ] [ --underline_length_tolerance
n ]
[ --underline_offset_tolerance n ] [ --unhyphenation ]
[ --use_mosaic_header ] [ --use_preformat_marker ] [ --xhtml ] [file ...]
txt2html converts plain text files to HTML.
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.
One can use txt2html as a filter, outputting the result to STDOUT, or to a given file.
One can define options in a config file as well as on the command-line.
Option names can be abbreviated to the shortest unique name for that option. Options can start with "--" or "-". Boolean options can be negated by preceding them with "no"; options with hash or array values can be added to by giving the option again for each value.
See Getopt::Long for more information.
If the Getopt::ArgvFile module is installed, then groups of options can be read from a file or files designated by the @ character preceding the name. For example:
txt2html @poem_options --outfile poem_glory.html poem_glory.txt
See "Options Files" for more information.
Help options:
General options:
(default: nothing)
(default: nothing)
Note that because this is used as a character class, if you use '-' it must come first. (default:nothing)
-H '^ *\d+\. \w+' -H '^ *\d+\.\d+\. \w+' -H '^ *\d+\.\d+\.\d+\. \w+'
Then lines like
" 1. Examples " " 1.1. Things" and " 4.2.5. Cold Fusion"
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 -EH/--explicit-headings option.
This is a multi-valued option.
(default: none)
(default: 0)
Default : '-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd'
If --xhtml is true, the contents of this is ignored, unless it's empty, in which case no DOCTYPE declaration is output.
(default:-)
This overrides the detection of lists; if something looks like a table, it is taken as a table, and list-checking is not done for that paragraph.
(default: false)
(default: 2)
NOTE for --prebegin and --preend: A zero takes precedence. If one is zero, the other is ignored. If both are zero, entire document is preformatted.
(default: "^(:?(:?<)|<)PRE(:?(:?>)|>)\$")
(default: "^(:?(:?<)|<)/PRE(:?(:?>)|>)\$")
(default: nothing)
--table_type ALIGN=1 --table_type BORDER=0
This determines which types of tables will be recognised when "make_tables" is true. The possible types are ALIGN, PGSQL, BORDER and DELIM. (default: all types are true)
(default: true)
Options can be given in files as well as on the command-line by flagging an option file with @filename in the command-line. Also, the files ~/.txt2htmlrc and ./.txt2htmlrc are checked for options.
The format is as follows: Lines starting with # are comments. Lines enclosed in PoD markers are also comments. Blank lines are ignored. The options themselves should be given the way they would be on the command line, that is, the option name (including the --) followed by its value (if any).
For example:
# set link dictionaries --default_link_dict /home/kat/.TextToHTML.dict # set options for poetry --titlefirst --short_line_length 60
See Getopt::ArgvFile for more information.
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.
/<URL:\s*(\S+?)\s*>/ --> $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 explicitly as a URL with the pattern <URL:foo> (note the < 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) This would give us a link in the form <A HREF="foo"><URL:foo></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:
/<URL:\s*(\S+?)\s*>/ -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.
"HTML::TextToHTML" -io-> http://www.example.com/tools/text_to_html/
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".
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
-e File exists. -z File has zero size. -s File has nonzero size (returns size).
becomes
<table> <tr><td>-e</td><td>File exists.</td></tr> <tr><td>-z</td><td>File has zero size.</td></tr> <tr><td>-s</td><td>File has nonzero size (returns size).</td></tr> </table>
This guesses for each column whether it is intended to be left, centre or right aligned.
BORDER Table Type
This table type has nice borders around it, and will be rendered with a border, like so:
+---------+---------+ | Column1 | Column2 | +---------+---------+ | val1 | val2 | | val3 | val3 | +---------+---------+
The above becomes
<table border="1"> <thead><tr><th>Column1</th><th>Column2</th></tr></thead> <tbody> <tr><td>val1</td><td>val2</td></tr> <tr><td>val3</td><td>val3</td></tr> </tbody> </table>
It can also have an optional caption at the start.
My Caption +---------+---------+ | Column1 | Column2 | +---------+---------+ | val1 | val2 | | val3 | val3 | +---------+---------+
PGSQL Table Type
This format of table is what one gets from the output of a Postgresql query.
Column1 | Column2 ---------+--------- val1 | val2 val3 | val3 (2 rows)
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.
This can also have an optional caption at the start.
Convert one file to HTML
txt2html --infile thing.txt --outfile thing.html
This will create a HTML file called "thing.html" from the plain text file "thing.txt".
Tell me about them.
Pod::Usage HTML::TextToHTML Getopt::Long Getopt::ArgvFile File::Basename YAML::Syck perldoc
Web
These files are only read if the Getopt::ArgvFile module is available on the system.
perl(1) htmltoc(1) HTML::TextToHTML Getopt::Long Getopt::ArgvFile
Kathryn Andersen (RUBYKAT) perlkat AT katspace dot com http//www.katspace.com/
based on txt2html by Seth Golub
Current homepage is https://github.com/resurrecting-open-source-projects/txt2html
Original txt2html script Copyright (c) 1994-2000 Seth Golub seth AT aigeek.com
Copyright (c) 2002-2005 Kathryn Andersen
Copyright (c) 2018-2019 Joao Eriberto Mota Filho
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-11-23 | perl v5.36.0 |