=head1 NAME
HTML::WikiConverter::Dialects - How to add a dialect
=head1 SYNOPSIS
# In your dialect module:
package HTML::WikiConverter::MySlimWiki;
use HTML::WikiConverter -dialect;
rule b => { start => '**', end => '**' };
rule i => { start => '//', end => '//' };
rule strong => { alias => 'b' };
rule em => { alias => 'i' };
rule hr => { replace => "\n----\n" };
# In a nearby piece of code:
package main;
use Test::More tests => 5;
my $wc = new HTML::WikiConverter(
dialect => 'MySlimWiki'
);
is( $wc->html2wiki( 'text' ), '**text**', b );
is( $wc->html2wiki( 'text' ), '//text//', i );
is( $wc->html2wiki( 'text' ), '**text**', 'strong' );
is( $wc->html2wiki( 'text' ), '//text//', 'em' );
is( $wc->html2wiki( '
' ), '----', 'hr' );
=head1 DESCRIPTION
L (or H::WC, for short) is an HTML to wiki
converter. It can convert HTML source into a variety of wiki markups,
called wiki "dialects". This manual describes how you to create your
own dialect to be plugged into HTML::WikiConverter.
=head1 DIALECTS
Each dialect has a separate dialect module containing rules for
converting HTML into wiki markup specific for that dialect. Currently,
all dialect modules are in the C package space
and subclass HTML::WikiConverter. For example, the MediaWiki dialect
module is L, while PhpWiki's is
L. However, dialect modules need not be
in the C package space; you may just as easily
use C and H::WC will Do The Right Thing.
From now on, I'll be using the terms "dialect" and "dialect module"
interchangeably.
=head2 Subclassing
To interface with H::WC, dialects need to subclass it. Because you'll
probably be wanting the C and C functions as
well, subclassing and importing these functions is done in a single
step:
use HTML::WikiConverter -dialect;
This will add HTML::WikiConverter to your dialect's C<@ISA> and will
import the C and C functions into your dialect's
package.
=head2 Conversion rules
Dialects guide H::WC's conversion process with a set of rules that
define how HTML elements are turned into their wiki counterparts.
Each rule corresponds to an HTML tag (including nonstandard tags), and
there may be any number of rules. Rules are added with the C
function that was imported when you subclassed H::WC (see above).
The syntax for C is as follows:
rule $tag => \%subrules;
where C<$tag> is the name of the HTML tag (e.g., C<"b">, C<"em">, etc.)
and C<%subrules> contains subrules that specify how that tag will be
converted.
=head3 Subrules
The following subrules are recognized:
start
end
preserve
attributes
empty
replace
alias
block
line_format
line_prefix
trim
=head3 A simple example
The following rules could be used for a dialect that uses
C<*asterisks*> for bold and C<_underscores_> for italic text:
rule b => { start => '*', end => '*' };
rule i => { start => '_', end => '_' };
=head3 Aliases
To add CstrongE> and CemE> as aliases of CbE> and
CiE>, use the C subrule:
rule b => { start => '*', end => '*' };
rule i => { start => '_', end => '_' };
rule strong => { alias => 'b' };
rule em => { alias => 'i' };
(The C subrule cannot be used with any other subrule.)
=head3 Blocks
Many dialects separate paragraphs and other block-level elements
with a blank line. To indicate this, use the C subrule:
rule p => { block => 1 };
(To better support nested block elements, if a block elements are
nested inside each other, blank lines are only added to the outermost
element.)
=head3 Line formatting
Many dialects require that the text of an element be contained on a
single line of text, or that it cannot contain any newlines,
etc. These options can be specified using the C subrule,
which can be assigned the value C<"single">, C<"multi">, or
C<"blocks">.
If the element must be contained on a single line, then the
C subrule should be C<"single">. If the element can span
multiple lines, but there can be no blank lines contained within, then
use C<"multi">. If blank lines (which delimit blocks) are allowed,
then use C<"blocks">. For example, paragraphs are specified like so in
the MediaWiki dialect:
rule p => { block => 1, line_format => 'multi', trim => 'both' };
=head3 Trimming whitespace
The C subrule specifies whether leading or trailing whitespace
(or both) should be stripped from the element. To strip leading
whitespace only, use C<"leading">; for trailing whitespace, use
C<"trailing">; for both, use the aptly named C<"both">; for neither
(the default), use C<"none">.
=head3 Line prefixes
Some elements require that each line be prefixed with a particular
string. This is specified with the C subrule. For
example, preformatted text in MediaWiki is prefixed with a space:
rule pre => { block => 1, line_prefix => ' ' };
There is a known bug in H::WC (see
L) with the
processing of whitespace that causes line prefixes to be removed from
the wiki markup after conversion. I'm working on a fix for this.
=head3 Replacement
In some cases, conversion from HTML to wiki markup is as simple as
string replacement. To replace a tag and its contents with a
particular string, use the C subrule. For example, in
PhpWiki, three percent signs, C<"%%%">, represents a line break,
CbrE>, hence:
rule br => { replace => '%%%' };
(The C subrule cannot be used with any other rule.)
=head3 Preserving HTML tags
Some dialects allow a subset of HTML in their markup. While H::WC
ignores unhandled HTML tags by default (i.e., if H::WC encounters a
tag that does not exist in a dialect's rule specification, then the
contents of the tag is simply passed through to the wiki markup), you
may specify that some be preserved using the C subrule. For
example, to allow CfontE> tag in wiki markup:
rule font => { preserve => 1 };
Preserved tags may also specify a list of attributes that may also
passthrough from HTML to wiki markup. This is done with the
C subrule:
rule font => { preserve => 1, attributes => [ qw/ style class / ] };
(The C subrule can only be used if the C subrule
is also present.)
Some HTML elements have no content (e.g., line breaks, images) and the
wiki dialect might require them to be preserved in a more
XHTML-friendly way. To indicate that a preserved tag should have no
content, use the C subrule. This will cause the element to be
replaced with C<"Etag /E"> and no end tag. For example,
MediaWiki handles line breaks like so:
rule br => {
preserve => 1,
attributes => [ qw/ id class title style clear / ],
empty => 1
};
This will convert, for example, C<"Ebr clear='both'E"> into
C<"Ebr clear='both' /E">. Without specifying the C
subrule, this would be converted into the (probably undesirable)
C<"Ebr clear='both'EE/brE">.
(The C subrule can only be used if the C subrule is
also present.)
=head3 Rules that depend on attribute values
In some circumstances, you might want your dialect's conversion rules
to depend on the value of one or more attributes. The problem is that
a dialect's rules are loaded at compile-time (when the dialect module
is imported via C