The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Text::WikiFormat - module for translating Wiki formatted text into other formats

SYNOPSIS

use Text::WikiFormat;
my $html = Text::WikiFormat::format($raw);

DESCRIPTION

The original Wiki web site was intended to have a very simple interface to edit and to add pages. Its formatting rules are simple and easy to use. They are also easily translated into other, more complicated markup languages with this module. It creates HTML by default, but can be extended to produce valid POD, DocBook, XML, or any other format imaginable.

The most important function is format(). It is not exported by default.

format()

format() takes one required argument, the text to convert, and returns the converted text. It allows two optional arguments. The first is a reference to a hash of tags. Anything passed in here will override the default tag behavior. These tags are described later. The second argument is a hash reference of options. There are currently limited to:

  • prefix

    The prefix of any links. In HTML mode, this is the path to the Wiki. The actual linked item itself will be appended to the prefix. This is used to create full URIs:

    { prefix => 'http://example.com/wiki.pl?page=' }
  • extended

    A boolean flag, false by default, to use extended linking semantics. This is stolen from the Everything Engine (http://everydevel.com/), where links are marked by square brackets. An optional title may occur before the link target, and is ended with an open pipe. That is to say, these are valid extended links:

    [a valid link]
    [title|link]

    Where the linking semantics of the destination format allow it, the title will be displayed instead of the URI. In HTML terms, this is the content of an A tag, not the contents of the url attribute.

Wiki Format

Wiki formatting is very simple. An item wrapped in three single quotes is marked as strong. An item wrapped in two single quotes is marked as emphasized. Any word with multiple CapitalLetters (e. g., StudlyCaps) will be turned into a link. Four or more hyphen characters at the start of a line create a horizontal line. Newlines are translated into the appropriate tag.

All lists are indented by one tab or four spaces. Lists can be unordered, where each item has its own bullet point. These are marked by a leading asterisk and space. They can also be ordered, where any combination of one or more alphanumeric characters can be followed by a period and an optional space. Any indented text without either marking is considered to be code, and is handled literally.

The following is valid Wiki formatting, with an extended link as marked.

ANormalLink
[let the Sun shine|AnExtendedLink]

    * unordered one
    * unordered two

    1. ordered one
    2. ordered two

    code one
    code two

The first line of a normal paragraph.
The second line of a normal paragraph.  Whee.

EXPORT

If you'd like to make your life more convenient, you can optionally import a subroutine that already has default tags and options set up. This is especially handy if you will be using a prefix:

use Text::WikiFormat prefix => 'http://www.example.com/';
wikiformat( 'some text' );

All tags are interpreted as, well, tags, except for three special keys:

  • prefix, interpreted as a link prefix

  • extended, interpreted as the extended link flag

  • as, interpreted as an alias for the imported function

Use the as flag to control the name by which the imported function is called. For example,

use Text::WikiFormat as => 'formatTextInWikiStyle';
formatTextInWikiStyle( 'some text' );

You might choose a better name, though.

The calling semantics are effectively the same as those of the format() function. Any additional tags or options to the imported function will override the defaults. In this example:

use Text::WikiFormat as => 'wf', extended => 0;
wf( 'some text', {}, { extended => 1 });

extended links will be enabled, though the default is to disable them.

This feature was suggested by Tony Bowden <tony@kasei.com>, but all implementation blame rests solely with me.

GORY DETAILS

Tags

There are two types of Wiki markup: line items and lists. Lists are made up of lines, and can also contain other lists.

Line items

There are two classes of line items: simple tags, and tags that contain data. The simple tags are newline and line. A newline is inserted whenever a newline character (\n) is encountered. A line is inserted whenever four or more dash characters (----) occur at the start of a line. No whitespace is allowed. These default to the BR and HR HTML tags, respectively. To override either, simply pass tags such as:

my $html = format($text, { newline => "\n" });

The three line items are more complex, and require subroutine references. This category includes the strong and emphasized tags as well as links. The first argument passed to the subref will be the data found in between the marks. The second argument is the $opts hash reference. The default action for a strong tag can be reimplemented with this syntax:

my $html = format($text, { strong => sub { "<b>$_[0]</b>" } });

Lists

There are three types of lists: code, unordered, and ordered. Each of these is marked by indentation, either one or more tabs or four or more whitespace characters. (This does not include newlines, however.) Any line that does not fall in any of these three categories is automatically put in a paragraph list.

List entries in the tag hashes must contain array references. The first two items are the tags used at the start of the list and at the end of the list. As you'd expect, the last items contain the tags used at the start and end of each line. Where there needs to be more processing of individual lines, use a subref as the third item. This is how ordered lines are numbered in HTML lists:

my $html = format($text, { ordered => [ '<ol>', "</ol>\n",
	sub { qq|<li value="$_[1]">$_[0]</li>\n| } ] });

The first argument to these subrefs is the text of the line itself, after it has been processed. (The indentation and tokens used to mark this as a list item are removed, and the rest of the line is checked for other line formattings.) The subsequent arguments are captured variables in the regular expression used to find this list type. The regexp for ordered lists is:

qr/^(?:\t+|\s{4,})([\dA-Za-z]+)\.\s*/;

This means that a line must start with one or more tabs or four or more spaces. It must then contain one or more alphanumeric character followed by a single period and optional whitespace. The contents of this last group is saved, and will be passed to the subref as the second argument.

Lists are automatically started and ended as necessary.

Finding lists

Text::WikiFormat uses regular expressions to find lists. These are kept in the %tags hash, under the lists key. To change the regular expression to find code list items, use:

my $html = format($wikitext, { lists => { 
	code => qr/^(?:\t+|\s{4,}):\s+/ }
);

This will require indentation and a colon to mark code lines. A potential shortcut is to use $Text::WikiFormat::indent to match or to change the indentation marker. (If you do change it, the existing list regular expressions may not reflect your modifications. This may be corrected in a future version, but this really ought to be kept a read-only variable as much as possible.)

Finding Lists in the Correct Order

As intrepid bug reporter Tom Hukins pointed out in CPAN RT bug #671, the order in which Text::WikiFormat searches for lists varies by platform and version of Perl. Because some list-finding regular expressions are more specific than others, what's intended to be one type of list may be caught by a different list type.

If you're adding new list types, be aware of this. The listorder entry in %tags exists to force Text::WikiFormat to apply its regexes from most specific to least specific. It contains an array reference. By default, it looks for ordered lists first, unordered lists second, and code references at the end:

Any additional list types will be processed before the built-in types, but their order of execution is not guaranteed unless you set the order explicitly. I can't read all of your minds. :)

AUTHOR

chromatic, chromatic@wgz.org, with much input from the Jellybean team (including Jonathan Paulett). Tony Bowden and Tom Hukins both suggested some useful features. Blame me for the implementation.

BUGS

The link checker in format_line() may fail to detect existing links that do not follow HTML, XML, or SGML style. They may die with some SGML styles too. Sic transit gloria mundi.

TODO

  • Write tests for overriding tags

  • Find a nicer way to mark list as having unformatted lines

  • Optimize format_line() to work on a list of lines

  • Handle nested strong and emphasized markings better

  • Encode links properly (spaces in extended links in make_html_link())

COPYRIGHT

Copyright (c) 2002, chromatic. All rights reserved. This module is distributed under the same terms as Perl itself.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 232:

You forgot a '=back' before '=head2'

Around line 399:

=back without =over