NAME
HTML::WikiConverter - An HTML to wiki markup converter
SYNOPSIS
use HTML::WikiConverter;
my $wc = new HTML::WikiConverter( dialect => 'MediaWiki' );
print $wc->html2wiki( $html );
DESCRIPTION
HTML::WikiConverter
is an HTML to wiki converter. It can convert HTML source into a variety of wiki markups, called wiki "dialects". The following dialects are supported:
DocuWiki
Kwiki
MediaWiki
MoinMoin
Oddmuse
PhpWiki
PmWiki
SlipSlap
TikiWiki
UseMod
WakkaWiki
Note that while dialects usually produce satisfactory wiki markup, not all features of all dialects are supported. Consult individual dialects' documentation for details of supported features. Suggestions for improvements, especially in the form of patches, are very much appreciated.
METHODS
- new
-
my $wc = new HTML::WikiConverter( dialect => $dialect, %attrs );
Returns a converter for the specified dialect. Dies if
$dialect
is not provided or is not installed on your system. Attributes may be specified in%attrs
; see "ATTRIBUTES" for a list of recognized attributes. - html2wiki
-
$wiki = $wc->html2wiki( $html ); $wiki = $wc->html2wiki( html => $html ); $wiki = $wc->html2wiki( file => $path, slurp => $slurp );
Converts HTML source to wiki markup for the current dialect. Accepts either an HTML string
$html
or an HTML file$path
to read from. You may optionally bypassHTML::Parser
's incremental parsing of HTML files by giving$slurp
a true value. - dialect
-
my $dialect = $wc->dialect;
Returns the name of the dialect used to construct this
HTML::WikiConverter
object. - parsed_html
-
my $html = $wc->parsed_html;
Returns
HTML::TreeBuilder
's representation of the last-parsed syntax tree, showing how the input HTML was parsed internally. This is often useful for debugging.
ATTRIBUTES
You may configure HTML::WikiConverter
using a number of attributes. These may be passed as arguments to the new
constructor, or can be called as object methods on a HTML::WikiConverter
object.
- base_uri
-
URI to use for converting relative URIs to absolute ones. This effectively ensures that the
src
andhref
attributes of image and anchor tags, respectively, are absolute before converting the HTML to wiki markup, which is necessary for wiki dialects that handle internal and external links separately. Relative URLs are only converted to absolute ones if thebase_uri
argument is present. Defaults toundef
. - wiki_uri
-
URI used in determining which links are wiki links. This assumes that URLs to wiki pages are created by joining the
wiki_uri
with the (possibly escaped) wiki page name. For example, the English Wikipedia would use"http://en.wikipedia.org/wiki/"
, while Ward's wiki would use"http://c2.com/cgi/wiki?"
. Defaults toundef
. - wrap_in_html
-
Helps
HTML::TreeBuilder
parse HTML fragments by wrapping HTML in<html>
and</html>
before passing it throughhtml2wiki
. Defaults to0
. - strip_comments
-
Removes HTML comments from the input before conversion to wiki markup. Defaults to
1
. - strip_head
-
Removes the HTML
head
element from the input before converting. Defaults to1
. - strip_scripts
-
Removes all HTML
script
elements from the input before converting. Defaults to1
.
Some dialects allow other parameters in addition to these. Consult individual dialect documentation for details.
DIALECTS
HTML::WikiConverter
can convert HTML into markup for a variety of wiki engines. The markup used by a particular engine is called a wiki markup dialect. Support is added for dialects by installing dialect modules which provide the rules for how HTML is converted into that dialect's wiki markup.
Dialect modules are registered in the HTML::WikiConverter::
namespace an are usually given names in CamelCase. For example, the rules for the MediaWiki dialect are provided in HTML::WikiConverter::MediaWiki
. And PhpWiki is specified in HTML::WikiConverter::PhpWiki
.
This section is intended for dialect module authors.
Conversion rules
To interface with HTML::WikiConverter
, dialect modules must define a single rules
class method. It returns a reference to a hash of rules that specify how individual HTML elements are converted to wiki markup.
Supported rules
The following rules are recognized:
start
end
preserve
attributes
empty
replace
alias
block
line_format
line_prefix
trim
Simple rules method
For example, the following rules
method could be used for a wiki dialect that uses *asterisks* for bold and _underscores_ for italic text:
sub rules {
return {
b => { start => '*', end => '*' },
i => { start => '_', end => '_' }
};
}
Aliases
To add <strong> and <em> as aliases of <b> and <i>, use the alias
rule:
sub rules {
return {
b => { start => '*', end => '*' },
strong => { alias => 'b' },
i => { start => '_', end => '_' },
em => { alias => 'i' }
};
}
Note that if you specify the alias
rule, no other rules are allowed.
Blocks
Many wiki dialects separate paragraphs and other block-level elements with a blank line. To indicate this, use the block
rule:
p => { block => 1 }
Note that if a block-level element is nested inside another block-level element, blank lines are only added to the outermost block-level element.
Line formatting
However, many such wiki engines require that the text of a paragraph be contained on a single line of text. Or that a paragraph cannot contain any blank lines. These formatting options can be specified using the line_format
rule, which can be assigned the value "single"
, "multi"
, or "blocks"
.
If the element must be contained on a single line, then the line_format
rule should be "single"
. If the element can span multiple lines, but there can be no blank lines contained within, then it should be "multi"
. If blank lines (which delimit blocks) are allowed, then use "blocks"
. For example, paragraphs are specified like so in the MediaWiki dialect:
p => { block => 1, line_format => 'multi', trim => 'both' }
Trimming whitespace
The trim
rule specifies whether leading or trailing whitespace (or both) should be stripped from the paragraph. To strip leading whitespace only, use "leading"
; for trailing whitespace, use "trailing"
; for both, use the aptly named "both"
; for neither (the default), use "none"
.
Line prefixes
Some multi-line elements require that each line of output be prefixed with a particular string. For example, preformatted text in the MediaWiki dialect is prefixed with a space:
pre => { block => 1, line_prefix => ' ' }
Replacement
In some cases, conversion from HTML to wiki markup is as simple as string replacement. When you want to replace a tag and its contents with a particular string, use the replace
rule. For example, in the PhpWiki dialect, three percent signs '%%%' represents a linebreak <br />
, hence the rule:
br => { replace => '%%%' }
If you specify the replace
rule, no other options are allowed.
Preserving HTML tags
Finally, many wiki dialects allow a subset of HTML in their markup, such as for superscripts, subscripts, and text centering. HTML tags may be preserved using the preserve
rule. For example, to allow the <font> tag in wiki markup, one might say:
font => { preserve => 1 }
Preserved tags may also specify a whitelist of attributes that may also passthrough from HTML to wiki markup. This is done with the attributes
option:
font => { preserve => 1, attributes => [ qw/ font size / ] }
The attributes
rule must be used in conjunction preserve
.
Some HTML elements have no content (e.g. line breaks), and should be preserved specially. To indicate that a preserved tag should have no content, use the empty
rule. This will cause the element to be replaced with "<tag />"
, with no end tag and any attributes you specified. For example, the MediaWiki dialect handles line breaks like so:
br => {
preserve => 1,
attributes => qw/ id class title style clear /,
empty => 1
}
This will convert, e.g., "<br clear='both'>"
into "<br clear='both' />"
. Without specifying the empty
rule, this would be converted into the undesirable "<br clear='both'></br>"
.
The empty
rule must be combined with the preserve
rule.
Dynamic rules
Instead of simple strings, you may use coderefs as values for the start
, end
, replace
, and line_prefix
rules. If you do, the code will be called as a method on the current HTML::WikiConverter
dialect object, and will be passed the current HTML::Element node and a hashref of the dialect's rules for processing elements of that type.
For example, the MoinMoin dialect uses the following rules for lists:
ul => { line_format => 'multi', block => 1, line_prefix => ' ' }
li => { start => \&_li_start, trim => 'leading' }
ol => { alias => 'ul' }
It then defines _li_start
like so:
sub _li_start {
my( $self, $rules ) = @_;
my $bullet = '';
$bullet = '*' if $node->parent->tag eq 'ul';
$bullet = '1.' if $node->parent->tag eq 'ol';
return "\n$bullet ";
}
This ensures that every unordered list item is prefixed with *
and every ordered list item is prefixed with 1.
, required by the MoinMoin syntax. It also ensures that each list item is on a separate line and that there is a space between the prefix and the content of the list item.
Rule validation
Certain rule combinations are not allowed. For example, the replace
and alias
rules cannot be combined with any other rules, and attributes
can only be specified alongside preserve
. Invalid rule combinations will trigger an error when the HTML::WikiConverter
object is instantiated.
Dialect attributes
The attributes that are recognized by the HTML::WikiConverter
are given in the attributes
method, which returns a hash of attribute names and their defaults. Dialects that wish to alter the set of recognized attributes must override this method. For example, to add a boolean attribute called camel_case
with is disabled by default, a dialect would define an attributes
method like so:
sub attributes { (
shift->SUPER::attributes,
camel_case => 0
) }
Attributes defined liks this are given accessor and mutator methods, as in:
my $ok = $wc->camel_case; # accessor
$wc->camel_case(0); # mutator
Preprocessing
The first step in converting HTML source to wiki markup is to parse the HTML into a syntax tree using HTML::TreeBuilder. It is often useful for dialects to preprocess the tree prior to converting it into wiki markup. Dialects that elect to preprocess the tree do so by defining a preprocess_node
object method which will be called on each node of the tree (traversal is done in pre-order). As its only argument the method receives the current HTML::Element node being traversed. It may modify the node or decide to ignore it. The return value of the preprocess_node
method is discarded.
Built-in preprocessors
Because they are so commonly needed, two preprocessing steps are automatically carried out by HTML::WikiConverter
, regardless of the dialect: 1) relative URIs in images and links are converted to absolute URIs (based upon the base_uri
parameter), and 2) ignorable text (e.g. between </td> and <td>) is discarded.
HTML::WikiConverter
also provides additional preprocessing steps that may be explicitly enabled by dialect modules.
- strip_aname
-
Removes from the HTML input any anchor elements that do not contain an
href
attribute. -
Removes table captions and reinserts them as paragraphs before the table.
Dialects may apply these preprocessing steps by calling them as methods on the dialect object inside preprocess_node
. For example:
sub preprocess_node {
my( $self, $node ) = @_;
$self->strip_aname($node);
$self->caption2para($node);
}
Postprocessing
Once the work of converting HTML, it is sometimes useful to postprocess the resulting wiki markup. Postprocessing can be used to clean up whitespace, fix subtle bugs in the markup that can't otherwise be done in the original conversion, etc.
Dialects that want to postprocess the wiki markup should define a postprocess_output
object method that will be called just before thehtml2wiki
method returns to the client. The method will be passed a single argument, a reference to the wiki markup. It may modify the wiki markup that the reference points to. Its return value is discarded.
For example, to convert a series of line breaks to be replaced with a pair of newlines, a dialect might implement this:
sub postprocess_output {
my( $self, $outref ) = @_;
$$outref =~ s/<br>\s*<br>/\n\n/g;
}
(This example assumes that HTML line breaks were replaced with <br>
in the wiki markup.)
Dialect utility methods
HTML::WikiConverter
defines a set of utility methods for use by dialect modules.
- get_elem_contents
-
my $wiki = $wc->get_elem_contents( $node );
Converts the contents of
$node
(i.e. its children) into wiki markup and returns the resulting wiki markup. - get_wiki_page
-
my $title = $wc->get_wiki_page( $url );
Attempts to extract the title of a wiki page from the given URL, returning the title on success, undef on failure. If
wiki_uri
is empty, this method always returnundef
. Assumes that URLs to wiki pages are constructed using <wiki-uri><page-name>. - is_camel_case
-
my $ok = $wc->is_camel_case( $str );
Returns true if
$str
is in CamelCase, false otherwise. CamelCase-ness is determined using the same rules as CGI::Kwiki's formatting module uses. - get_attr_str
-
my $attr_str = $wc->get_attr_str( $node, @attrs );
Returns a string containing the specified attributes in the given node. The returned string is suitable for insertion into an HTML tag. For example, if
$node
refers to the HTML<style id="ht" class="head" onclick="editPage()">Header</span>
and
@attrs
contains "id" and "class", thenget_attr_str
will return 'id="ht" class="head"'.
BUGS
Please report bugs using http://rt.cpan.org.
SEE ALSO
AUTHOR
David J. Iberri <diberri@yahoo.com>
COPYRIGHT
Copyright (c) 2004-2005 David J. Iberri
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html