HTML::TextToHTML(3pm)

πŸ“¦
version 3.0 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu questing

πŸ’– HTML::TextToHTML πŸ’–

the perl module bestie β€” HTML::TextToHTML(3pm), User Contributed Perl Documentation, and it's giving "let me make your boring text file cute" energy. πŸ¦‹


🌸 NAME 🌸

HTML::TextToHTML β€” convert plain text file to HTML. periodt πŸ’…


✨ VERSION ✨

version 3.0 β€” iconic, we love a stable queen πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

Straight from the command line:

  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)

yes the ]); is really there, don't @ me, we copy code verbatim in this house πŸ™ˆ


πŸ’œ DESCRIPTION πŸ’œ

HTML::TextToHTML converts plain text files to HTML. Fr fr. And the txt2html script uses this very module to do the exact same glow-up from the command-line. ✨

It supports headings, tables, lists, simple character markup, and hyperlinking, and it's 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 whole point of this tool is to give you an easier way of converting existing text documents to HTML format, giving something nicer than just whapping the text into a big PRE block. 😭


πŸ•°οΈ History πŸ•°οΈ

The original txt2html script was written by Seth Golub (see http://www.aigeek.com/txt2html/), and converted to a perl module by Kathryn Andersen (see http://www.katspace.com/tools/text_to_html/) and made into a sourceforge project by Sun Tong (see http://sourceforge.net/projects/txt2html/). Earlier versions of the HTML::TextToHTML module called the included script texthyper so as not to clash with the original txt2html script, but now the projects have all been merged. UPDATING: currently, the project is available on GitHub at https://github.com/resurrecting-open-source-projects/txt2html 🫢


⭐ OPTIONS ⭐

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. very flexible of them ngl

⚠️ heads up bestie: all option-names must match exactly β€” no abbreviations allowed. No shortcuts, no vibes-based guessing. πŸ’€

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. (so like, they overwrite, no merging, no "both", just replace. periodt.)


  • append_file β€” append_file=>I<filename>

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;

(default: nothing)

  • append_head β€” append_head=>I<filename>

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;

(default: nothing)

  • body_deco β€” body_deco=>I<string>

Body decoration string: a string to be added to the BODY tag so that one can set attributes to the BODY (such as class, style, bgcolor etc). For example, "class='withimage'". it's giving ✨accessorize✨

  • bold_delimiter β€” bold_delimiter=>I<string>

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: #)

  • bullets β€” bullets=>I<string>

This defines what single characters are taken to be "bullet" characters for unordered lists. Note that because this is used as a character class, if you use - it must come first. (default: -=o*\267)

  • bullets_ordered β€” bullets_ordered=>I<string>

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. (placeholder energy 🧚)

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

  • caps_tag β€” caps_tag=>I<tag>

Tag to put around all-caps lines (default: STRONG). If an empty tag is given, then no tag will be put around all-caps lines. SO IF YOU YELL, IT GETS WRAPPED. πŸ’€

  • custom_heading_regexp β€” custom_heading_regexp=>\@custom_headings

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:

    my @custom_headings = ('^ *\d+\. \w+',
                           '^ *\d+\.\d+\. \w+',
                           '^ *\d+\.\d+\.\d+\. \w+');

    ...
        custom_heading_regexp=>\@custom_headings,
    ...

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 "explicit_headings" option. iykyk 😌

This expects a reference to an array of strings.

(default: none)

  • default_link_dict β€” default_link_dict=>I<filename>

The name of the default "user" link dictionary. (default: "$ENV{'HOME'}/.txt2html.dict" -- this is the same as for the txt2html script. If there is no $ENV{HOME} then it is just .txt2html.dict)

  • demoronize β€” demoronize=>1

Convert Microsoft-generated character codes that are non-ISO codes into something more reasonable. (default: true) de-moron-ize... the name ate and I'm obsessed πŸ’…

  • doctype β€” doctype=>I<doctype>

This gets put in the DOCTYPE field at the top of the document, unless it's empty.

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. so watch out bestie

  • eight_bit_clean β€” eight_bit_clean=>1

If false, convert Latin-1 characters to HTML entities. If true, this conversion is disabled; also "demoronize" is set to false, since this also changes 8-bit characters. (default: false)

  • escape_HTML_chars β€” escape_HTML_chars=>1

turn & < > into &amp; &gt; &lt; (default: true) the sacred trio 🫢

  • explicit_headings β€” explicit_headings=>1

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 β€” extract=>1

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) snippet mode, main character energy ✨

  • hrule_min β€” hrule_min=>I<n>

Min number of ---s for an HRule. (default: 4)

  • indent_width β€” indent_width=>I<n>

Indents this many spaces for each level of a list. (default: 2)

  • indent_par_break β€” indent_par_break=>1

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)

  • infile β€” infile=>\@my_files, infile=>['chapter1.txt', 'chapter2.txt']

The name of the input file(s). This expects a reference to an array of filenames.

The special filename - designates STDIN.

See also "inhandle" and "instring".

(default: -)

  • inhandle β€” inhandle=>\@my_handles, inhandle=>[\*MYINHANDLE, \*STDIN]

An array of input filehandles; use this instead of "infile" or "instring" to use a filehandle or filehandles as input.

  • instring β€” instring=>\@my_strings, instring=>[$string1, $string2]

An array of input strings; use this instead of "infile" or "inhandle" to use a string or strings as input. pick your fighter πŸŽ€

  • italic_delimiter β€” italic_delimiter=>I<string>

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: *)

  • underline_delimiter β€” underline_delimiter=>I<string>

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: _)

  • links_dictionaries β€” links_dictionaries=>\@my_link_dicts, links_dictionaries=>['url_links.dict', 'format_links.dict']

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.

  • link_only β€” link_only=>1

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)

  • lower_case_tags β€” lower_case_tags=>1

Force all tags to be in lower-case. no more SCREAMING TAGS, we whisper now 🧚

  • mailmode
mailmode=>1

Deal with mail headers & quoted text. The mail header paragraph is given the class mail_header, and mail-quoted text is given the class quote_mail. (default: false) πŸ’Œ


  • make_anchors β€” make_anchors=>0 Should we try to make anchors in headings? (default: true) 🎯
  • make_links β€” make_links=>0 Should we try to build links? If this is false, then the links dictionaries are not consulted and only structural text-to-HTML conversion is done. (default: true) πŸ”—
  • make_tables β€” make_tables=>1 Should we try to build tables? If true, spots tables and marks them up appropriately. See "Input File Format" for information on how tables should be formatted.

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) πŸ“Š

  • min_caps_length β€” min_caps_length=>I<n> min sequential CAPS for an all-caps line (default: 3) πŸ” 
  • outfile β€” outfile=>I<filename> The name of the output file. If it is "-" then the output goes to Standard Output. (default: - ) πŸ“
  • outhandle The output filehandle; if this is given then the output goes to this filehandle instead of to the file given in "outfile". πŸ–¨οΈ
  • par_indent β€” par_indent=>I<n> Minimum number of spaces indented in first lines of paragraphs. Only used when there's no blank line preceding the new paragraph. (default: 2) πŸ“
  • preformat_trigger_lines β€” preformat_trigger_lines=>I<n> How many lines of preformatted-looking text are needed to switch to <PRE>
  <= 0 : Preformat entire document
     1 : one line triggers
  >= 2 : two lines trigger

(default: 2) 🚦

  • endpreformat_trigger_lines β€” endpreformat_trigger_lines=>I<n> How many lines of unpreformatted-looking text are needed to switch from <PRE>
     <= 0 : Never preformat within document
        1 : one line triggers
     >= 2 : two lines trigger (default: 2)
NOTE for preformat_trigger_lines and endpreformat_trigger_lines: A zero takes precedence. If one is zero, the other is ignored. If both are zero, entire document is preformatted. ⚠️
  • preformat_start_marker β€” preformat_start_marker=>I<regexp> What flags the start of a preformatted section if --use_preformat_marker is true. (default: "^(:?(:?&lt;)|<)PRE(:?(:?&gt;)|>)\$") 🏁
  • preformat_end_marker β€” preformat_end_marker=>I<regexp> What flags the end of a preformatted section if --use_preformat_marker is true. (default: "^(:?(:?&lt;)|<)/PRE(:?(:?&gt;)|>)\$") 🏁
  • preformat_whitespace_min β€” preformat_whitespace_min=>I<n> 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) 🧹
  • prepend_file β€” prepend_file=>I<filename> If you want something prepended to the processed body text, put the filename here. The prepended text will not be processed at all, so make sure it's plain text or correct HTML. (default: nothing) πŸ“Ž
  • preserve_indent β€” preserve_indent=>1 Preserve the first-line indentation of paragraphs marked with indents by replacing the spaces of the first line with non-breaking spaces. (default: false) πŸ“
  • short_line_length β€” short_line_length=>I<n> Lines this short (or shorter) must be intentionally broken and are kept that short. (default: 40) βœ‚οΈ
  • style_url β€” style_url=>I<url> This gives the URL of a stylesheet; a LINK tag will be added to the output. 🎨
  • tab_width β€” tab_width=>I<n> How many spaces equal a tab? (default: 8) πŸ“
  • table_type β€” table_type=>{ ALIGN=>0, PGSQL=>0, BORDER=>1, DELIM=>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) πŸ—‚οΈ
  • title β€” title=>I<title> You can specify a title. Otherwise it will use a blank one. (default: nothing) 🏷️
  • titlefirst β€” titlefirst=>1 Use the first non-blank line as the title. (See also "title") πŸ“Œ
  • underline_length_tolerance β€” underline_length_tolerance=>I<n> How much longer or shorter can underlines be and still be underlines? (default: 1) πŸ“
  • underline_offset_tolerance β€” underline_offset_tolerance=>I<n> How far offset can underlines be and still be underlines? (default: 1) πŸ“
  • unhyphenation β€” unhyphenation=>0 Enables unhyphenation of text. (default: true) πŸ”—
  • use_mosaic_header β€” use_mosaic_header=>1 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) πŸ•°οΈ
  • use_preformat_marker β€” use_preformat_marker=>1 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) 🎬
  • xhtml β€” xhtml=>1 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.

(default: true) ✨


πŸ› DEBUGGING πŸ›

There are global variables for setting types and levels of debugging. These should only be used by developers. πŸ‘©β€πŸ’»

  • $HTML::TextToHTML::Debug β€” $HTML::TextToHTML::Debug = 1; Enable copious debugging output. (default: false) 🐞
  • $HTML::TextToHTML::DictDebug β€” $HTML::TextToHTML::DictDebug = I<n>; Debug mode for link dictionaries. Bitwise-Or what you want to see:
         1: The parsing of the dictionary
         2: The code that will make the links
         4: When each rule matches something
         8: When each tag is created

(default: 0) 🐜


πŸ› οΈ METHODS πŸ› οΈ

new

$conv = new HTML::TextToHTML()

$conv = new HTML::TextToHTML(titlefirst=>1,
    ...
);

Create a new object with new. If arguments are given, these arguments will be used in invocations of other methods.

See "OPTIONS" for the possible values of the arguments. ✨

args

$conv->args(short_line_length=>60,
    titlefirst=>1,
    ....
);

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. πŸ”„

process_chunk

$newstring = $conv->process_chunk($mystring);

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. 🧩

process_para

$newstring = $conv->process_para($mystring);

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. πŸ“„

txt2html

$conv->txt2html(%args);

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. πŸ“š


πŸ”’ PRIVATE METHODS πŸ”’

These are methods used internally, only of interest to developers. 🚧

init_our_data

$self->init_our_data();

Initializes the internal object data. πŸ—οΈ

deal_with_options

$self->deal_with_options();

do extra processing related to particular options βš™οΈ

escape

$newtext = escape($text);

Escape & < and > πŸ”

demoronize_char

$newtext = demoronize_char($text);

Convert Microsoft character entities into characters.

Added by Alan Jackson, alan at ajackson dot org, and based on the demoronize script by John Walker, http://www.fourmilab.ch/ πŸ§™

demoronize_code

$newtext = demoronize_code($text);

convert Microsoft character entities into HTML code πŸ§™

get_tag

$tag = $self->get_tag($in_tag);

$tag = $self->get_tag($in_tag,      tag_type=>TAG_START,
     inside_tag=>'');

output the tag wanted (add the <> and the / if necessary) - output in lower or upper case - do tag-related processing options:

  tag_type=>TAG_START | tag_type=>TAG_END | tag_type=>TAG_EMPTY
  (default start)
  inside_tag=>string (default empty)

🏷️

πŸŽ€ close_tag

$tag = $self->close_tag($in_tag);

Closes the open tag. That's it, that's the whole assignment. You hand it $in_tag, it hands you back $tag, and the tag is closed. Iconic minimalism honestly. πŸ’…


πŸ“ hrule

$self->hrule(para_lines_ref=>$para_lines,
          para_action_ref=>$para_action,
          ind=>0);

Deals with horizontal rules. Those lil --- divider moments. Slay.


🌸 shortline

$self->shortline(line_ref=>$line_ref,
                 line_action_ref=>$line_action_ref,
                 prev_ref=>$prev_ref,
                 prev_action_ref=>$prev_action_ref,
                 prev_line_len=>$prev_line_len);

Deals with short lines. Sometimes a line is just… short. And something has to be done about it. That's the job. πŸ’–


πŸ“§ is_mailheader

if ($self->is_mailheader(rows_ref=>$rows_ref))
{
    ...
}

Is this a mailheader line? Yes/no, no cap. Perfect for an if block, as demonstrated. πŸ¦‹


πŸ’Œ mailheader

$self->mailheader(rows_ref=>$rows_ref);

Deals with a mailheader. The is_mailheader girlies said yes, so now we actually do the thing. ✨


πŸ’¬ mailquote

$self->mailquote(line_ref=>$line_ref,
                 line_action_ref=>$line_action_ref,
                 prev_ref=>$prev_ref,
                 prev_action_ref=>$prev_action_ref,
                 next_ref=>$next_ref);

Deals with quoted mail. You know, the > stacked reply-chain situation. Handles it. 🫢


βž– subtract_modes

$newvector = subtract_modes($vector, $mask);

Subtracts the modes listed in $mask from $vector. Note bestie: no $self-> up in here β€” this one's just a plain function. Gives you back $newvector. βœ‚οΈ


πŸ“ paragraph

$self->paragraph(line_ref=>$line_ref,
                 line_action_ref=>$line_action_ref,
                 prev_ref=>$prev_ref,
                 prev_action_ref=>$prev_action_ref,
                 line_indent=>$line_indent,
                 prev_indent=>$prev_indent,
                 is_fragment=>$is_fragment,
                 ind=>$ind);

Detects paragraph indentation. That's the vibe check it's running. πŸ’œ


πŸ”’ listprefix

($prefix, $number, $rawprefix, $term) = $self->listprefix($line);

Detects and parses a list item. Hands you back the whole squad: $prefix, $number, $rawprefix, and $term. Four values, one call, main character energy. πŸ‘‘


πŸš€ startlist

$self->startlist(prefix=>$prefix,
                 number=>0,
                 rawprefix=>$rawprefix,
                 term=>$term,
                 para_lines_ref=>$para_lines_ref,
                 para_action_ref=>$para_action_ref,
                 ind=>0,
                 prev_ref=>$prev_ref,
                 total_prefix=>$total_prefix);

Starts a list. The beginning of something beautiful. πŸŽ€


πŸ›‘ endlist

$self->endlist(num_lists=>0,
    prev_ref=>$prev_ref,
    line_action_ref=>$line_action_ref);

Ends N lists β€” that's what num_lists is for, and it defaults to 0. Closure, literally. 😌


▢️ continuelist

$self->continuelist(para_lines_ref=>$para_lines_ref,
                    para_action_ref=>$para_action_ref,
                    ind=>0,
                    term=>$term);

Continues a list. Because we are not done listing. We have more to say. πŸ—£οΈ


πŸ“‹ liststuff

$self->liststuff(para_lines_ref=>$para_lines_ref,
                 para_action_ref=>$para_action_ref,
                 para_line_indent_ref=>$para_line_indent_ref,
                 ind=>0,
                 prev_ref=>$prev_ref);

Processes a list β€” this is the higher-level method, so it's the one coordinating the whole list situation. The manager, fr fr. 🧚


πŸ” get_table_type

$table_type = $self->get_table_type(rows_ref=>$rows_ref,
                                    para_len=>0);

Figures out the table type of this table, if there even is one. Returns $table_type. Lowkey detective work. πŸ”Ž


βœ… is_aligned_table

if ($self->is_aligned_table(rows_ref=>$rows_ref, para_len=>0))
{
    ...
}

Checks if the given paragraph-array is an aligned table. Yes or no, that's all. ✨


🐘 is_pgsql_table

if ($self->is_pgsql_table(rows_ref=>$rows_ref, para_len=>0))
{
    ...
}

Checks if the given paragraph-array is a Postgresql table (the ascii format produced by Postgresql). 🐘

A PGSQL table can start with an optional table-caption, and 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-count (N rows)

That's the whole recipe bestie, memorize it. πŸ’…


πŸ–ΌοΈ is_border_table

if ($self->is_border_table(rows_ref=>$rows_ref, para_len=>0))
{
    ...
}

Checks if the given paragraph-array is a Border table.

A BORDER table can start with an optional table-caption, and 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 +------+-----+

Boxed and beautiful. 🎁


πŸ”² is_delim_table

if ($self->is_delim_table(rows_ref=>$rows_ref, para_len=>0))
{
    ...
}

Checks if the given paragraph-array is a Delimited table.

A DELIM table can start with an optional table-caption, then it has at least two rows which start and end and are punctuated by a non-alphanumeric delimiter:

| val1 | val2 |
| val3 | val4 |

Pipes everywhere. We love to see it. 🌈


πŸ—‚οΈ tablestuff

$self->tablestuff(table_type=>0,
                  rows_ref=>$rows_ref,
                  para_len=>0);

Processes a table. The all-purpose table handler. 🫢


πŸ“ make_aligned_table

$self->make_aligned_table(rows_ref=>$rows_ref,
                          para_len=>0);

Makes an Aligned table. 🌟


🐘 make_pgsql_table

$self->make_pgsql_table(rows_ref=>$rows_ref,
                          para_len=>0);

Makes a PGSQL table. 🐘✨


πŸ–ΌοΈ make_border_table

$self->make_border_table(rows_ref=>$rows_ref,
                         para_len=>0);

Makes a BORDER table. Boxes upon boxes. πŸ’–


πŸ”² make_delim_table

$self->make_delim_table(rows_ref=>$rows_ref,
                        para_len=>0);

Makes a Delimited table. πŸŽ€


πŸ“œ is_preformatted

if ($self->is_preformatted($line))
{
    ...
}

Returns true if the passed string is considered to be preformatted. True/false, done. βœ…


βœ‚οΈ split_end_explicit_preformat

$front = $self->split_end_explicit_preformat(para_ref=>$para_ref);

Modifies the given string, and returns the front preformatted part as $front. Yes it modifies $para_ref β€” that's the deal, that's the contract. πŸ™ˆ


🏁 endpreformat

$self->endpreformat(para_lines_ref=>$para_lines_ref,
                    para_action_ref=>$para_action_ref,
                    ind=>0,
                    prev_ref=>$prev_ref);

Ends a preformatted section. Closing time. πŸšͺ


πŸ–₯️ preformat

$self->preformat(mode_ref=>$mode_ref,
                 line_ref=>$line_ref,
                 line_action_ref=>$line_action_ref,
                 prev_ref=>$prev_ref,
                 next_ref=>$next_ref,
                 prev_action_ref);

Detects and processes a preformatted section. Monospace girlies rise up. πŸ’…


βš“ make_new_anchor

$anchor = $self->make_new_anchor($heading_level);

Makes a new anchor. Gives you $anchor back. πŸͺ


πŸ“¬ anchor_mail

$self->anchor_mail($line_ref);

Makes an anchor for a mail section. βœ‰οΈ


🏷️ anchor_heading

$self->anchor_heading($heading_level, $line_ref);

Makes an anchor for a heading. Linkable headings, we stan. πŸ”—


πŸ“Š heading_level

$self->heading_level($style);

Adds a new heading style if this is a new heading style. (If it's not new, it just… knows it. iykyk.) πŸ‘‘


πŸ”˜ is_ul_list_line

if ($self->is_ul_list_line($line))
{
    ...
}

Tests if this line starts a UL list item. Bullet point check. 🎯


🎯 is_heading

if ($self->is_heading(line_ref=>$line_ref, next_ref=>$next_ref))
{
    ...
}

Tests if this line is a heading. Note the needs here, bestie: it has to take account of the next line, because a standard heading is defined by "underlining" the text of the heading. So yeah, it can't do its job without peeking at what comes after. πŸ‘€


πŸ† heading

$self->heading(line_ref=>$line_ref,
    next_ref=>$next_ref);

Makes a heading. It assumes is_heading is true, so don't come at it with a non-heading, okay? πŸ™ˆ


🎨 is_custom_heading

if ($self->is_custom_heading($line))
{
    ...
}

Checks if the given line matches a custom heading. πŸ–ŒοΈ


🌟 custom_heading

$self->custom_heading(line_ref=>$line_ref);

Makes a custom heading. Also assumes is_custom_heading is true β€” see the pattern? Verify first, then commit. πŸ’…


πŸ”— unhyphenate_para

$self->unhyphenate_para($para_ref);

Joins up hyphenated words that are split across lines. The hero we all needed. πŸ¦Έβ€β™€οΈ


🏷️ tagline

$self->tagline($tag, $line_ref);

Puts the given $tag around the given line. Wrapping, but make it fashion. πŸŽ€


πŸ”  iscaps

if ($self->iscaps($line))
{
    ...
}

Checks if a line is all capitals. LIKE THIS. πŸ“’


πŸ“£ caps

$self->caps(line_ref=>$line_ref,
            line_action_ref=>$line_action_ref);

Detects and deals with an all-caps line. Because someone was YELLING in the source text. 😭


✨ do_delim

$self->do_delim(line_ref=>$line_ref,
                line_action_ref=>$line_action_ref,
                delim=>'*',
                tag=>'STRONG');

Deals with a line which has words delimited by the given delimiter. This is the one doing italics, bold and underline formatting β€” the glow-up department. Note the defaults: delim is '*' and tag is 'STRONG'. πŸ’…πŸ”₯


🌐 glob2regexp

$regexp = glob2regexp($glob);

Converts very simple globs to regexps. Emphasis on very simple, bestie. Gives you $regexp. ✨


$self->add_regexp_to_links_table(label=>$label,
                                 pattern=>$pattern,
                                 url=>$url,
                                 switches=>$switches);

Adds the given regexp "link definition" to the links table. 🫢


$self->add_literal_to_links_table(label=>$label,
                                  pattern=>$pattern,
                                  url=>$url,
                                  switches=>$switches);

Adds the given literal "link definition" to the links table. πŸ“–


$self->add_glob_to_links_table(label=>$label,
                               pattern=>$pattern,
                               url=>$url,
                               switches=>$switches);

Adds the given glob "link definition" to the links table. 🌈


πŸ“– parse_dict

$self->parse_dict($dictfile, $dict);

Parses the dictionary file. (See also load_dictionary_links, for things that were stripped.) πŸ”


πŸ› οΈ setup_dict_checking

$self->setup_dict_checking();

Sets up the dictionary checking. Zero arguments, all business. βš™οΈ


if ($self->in_link_context($match, $before))
{
    ...
}

Checks if we are inside a link (<a ...>). Important lore: certain kinds of substitution are not allowed here. So this is the bouncer at the door. 🚫


$self->apply_links(para_ref=>$para_ref,
                   para_action_ref=>$para_action_ref);

Applies links and formatting to this paragraph. The whole glow-up, applied. πŸ’–


$self->check_dictionary_links(line_ref=>$line_ref,
                              line_action_ref=>$line_action_ref);

Checks β€” and alters if need be β€” the bits in this line matching the patterns in the link dictionary. Yes, it can change them. That's the assignment. ✏️


$self->load_dictionary_links();

Loads the dictionary links. That's it. πŸ“¦


🏁 do_file_start

$self->do_file_start($outhandle, $para);

The extra stuff needed for the beginning: HTML headers, and prepending a file if desired. First-impressions department. 🎬


🌱 do_init_call

$self->do_init_call();

Certain things β€” like reading link dictionaries β€” need to be done only once. So this is the once-only zone. Don't call it twice, bestie, it already ate. πŸ“

🌸 FILE FORMATS 🌸

So there are literally two files that can change how your conversion turns out, and you gotta know both:

  • the link dictionary β€” it's got the patterns (like how to spot http links and other lil' things) plus the rules for converting them
  • the input file itself β€” naturally, because the shape of what you feed in matters

A link dictionary file holds patterns to match and what to turn them into. It's called a "link" dictionary because back in the day it was meant to define what a href link even was, but bestie it does so much more than that. That said β€” if you wanna define your own links, please go read up on regular expressions (regexes), because this whole thing leans on them heavily. No cap.

The file is made of comments (lines starting with #), blank lines, and link entries. Every entry is: a regular expression, a -> separator (with optional flags), and a link "result".

In the simplest case, with zero flags, the regex defines the pattern to hunt for, and the result tells you which part of the regex is the actual link. The link that gets generated has the href as the link, and the whole matched pattern as the visible part of the link. Also β€” the first character of the regular expression is taken to be the separator for the regex, so you can use the traditional / separator, or something else like | (which is highkey helpful when your URLs are absolutely stuffed with / 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 explicitly 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 ✨

But 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. It's giving full creative control, honestly.

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. Yes bestie, it runs code. Iconic but dangerous energy.


πŸ’… 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.katspace.com/tools/text_to_html/

πŸ¦‹ Input File Format πŸ¦‹

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

-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.


✨ EXAMPLES ✨

use HTML::TextToHTML;

Create a new object

my $conv = new HTML::TextToHTML();

my $conv = new HTML::TextToHTML(title=>"Wonderful Things",
                        default_link_dict=>$my_link_file,
  );

Add further arguments

$conv->args(short_line_length=>60,
           preformat_trigger_lines=>4,
           caps_tag=>"strong",
  );

Convert a file

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

Make a pipleline

open(IN, "ls |") or die "could not open!";
$conv->txt2html(inhandle=>[\*IN],
                 outfile=>'-',
  );

🫢 NOTES 🫢

  • 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. 😭

πŸ‘‘ REQUIRES πŸ‘‘

HTML::TextToHTML requires Perl 5.8.1 or later.

For installation, it needs:

Module::Build

The txt2html script needs:

Getopt::Long
Getopt::ArgvFile
Pod::Usage
File::Basename

For testing, it also needs:

Test::More

For debugging, it also needs:

YAML::Syck

🌸 INSTALLATION 🌸

Okay bestie, before you do literally anything else: make sure you have the dependencies installed first! (peep the REQUIRES section up there, it's giving required-reading energy πŸ’…)

Some of those modules come standard with more recent versions of perl, but I'm mentioning them anyway, just in case you don't have them. Better safe than sorry, yk? 🫢

If you're sitting there like "girl how do I even install these," try the CPAN module β€” it's an easy way of auto-installing modules from the Comprehensive Perl Archive Network, which is exactly where all the above modules live. Run perldoc perlmodinstall or perldoc CPAN for more info. ✨

To install this module, type this:

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

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

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

Wanna install somewhere other than the default, like a directory under your home directory, say /home/fred/perl? Then go:

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

as your very first step instead. That installs the files underneath /home/fred/perl. πŸŽ€

Then you're gonna need to alter the PERL5LIB variable so it can find the modules, and the PATH variable so it can find the script.

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

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

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

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

Lowkey important: the system links dictionary will be installed as /home/fred/perl/share/txt2html/txt2html.dict. Noted. πŸ“

Now, if you wanna install into a temporary install directory (like if you're building a package), then instead of going

perl Build install

go

perl Build install destdir=/my/temp/dir

and it'll 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. Different vibes entirely, bestie. πŸ’€

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


πŸ› BUGS πŸ›

Please, send to https://github.com/resurrecting-open-source-projects/txt2html/issues

(Yes bestie, that's the whole section. Report your bugs, they wanna hear about it πŸ™ˆ)


πŸ‘€ SEE ALSO πŸ‘€

perl txt2html.


πŸ‘‘ AUTHOR πŸ‘‘

Kathryn Andersen (RUBYKAT)
perlkat AT katspace dot com
http//www.katspace.com/

based on txt2html by Seth Golub


Original txt2html script copyright (c) 1994-2000 Seth Golub <seth AT aigeek.com>

Copyright (c) 2002-2005 by Kathryn Andersen <perlkat AT katspace dot com>

Copyright (c) 2018-2019 Joao Eriberto Mota Filho <eriberto@eriberto.pro.br> Copyright (c) 2019 Bruce Momjian <bruce@momjian.us>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's a wrap, bestie!! πŸŽ‰βœ¨ This whole thing is perl v5.36.0, dated 2022-11-23, and it's the HTML::TextToHTML(3pm) page β€” go forth and convert all the text to HTML, you absolute icon. πŸ’…πŸ¦‹ no cap, you got this 🫢

πŸ“¦ where this page comes from