Revision 3 as of 2006-10-19 10:53:06

Clear message

[#___top NAME WikiConverter.pm_files/up.gif]

HTML::WikiConverter - Convert HTML to wiki markup

[#___top SYNOPSIS WikiConverter.pm_files/up.gif]

  use HTML::WikiConverter;
  my $wc = new HTML::WikiConverter( dialect => 'MediaWiki' );

  # Provide HTML directly
  print $wc->html2wiki( $html );

  # ...which is the same as
  print $wc->html2wiki( html => $html );

  # Or fetch it from a file
  print $wc->html2wiki( file => $path );

  # ...slurp it all at once rather than parsing incrementally
  print $wc->html2wiki( file => $path, slurp => 1 );

  # Or from a URI
  print $wc->html2wiki( uri => $uri );

  # Get a list of installed dialects
  my @dialects = HTML::WikiConverter->available_dialects;

[#___top DESCRIPTION WikiConverter.pm_files/up.gif]

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:

  DokuWiki
  Kwiki
  MediaWiki
  MoinMoin
  Oddmuse
  PbWiki
  PhpWiki
  PmWiki
  SlipSlap
  TikiWiki
  UseMod
  WakkaWiki
  WikkaWiki

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.

[#___top METHODS WikiConverter.pm_files/up.gif]

[#___top new]

  my $wc = new HTML::WikiConverter( dialect => $dialect, %attrs );

Returns a converter for the specified wiki dialect. Croaks if $dialect is not provided or its dialect module is not installed on your system. Additional attributes may be specified in %attrs; see [#ATTRIBUTES "ATTRIBUTES"] for a list of recognized attributes.

[#___top html2wiki]

  $wiki = $wc->html2wiki( $html, %attrs );
  $wiki = $wc->html2wiki( html => $html, %attrs );
  $wiki = $wc->html2wiki( file => $file, %attrs );
  $wiki = $wc->html2wiki( uri => $uri, %attrs );

Converts HTML source to wiki markup for the current dialect. Accepts either an HTML string $html, an file $file, or a URI <$uri> to read from.

Attributes assigned in %attrs (see [#ATTRIBUTES "ATTRIBUTES"]) will augment or override previously assigned attributes for the duration of the html2wiki() call.

[#___top given_html]

  my $html = $wc-&gt;given_html;

Returns the HTML passed to or fetched (ie, from a file or URI) by the last html2wiki() method call. Useful for debugging.

[#___top parsed_html]

  my $parsed_html = $wc-&gt;parsed_html;

Returns [http://search.cpan.org/perldoc?HTML%3A%3ATreeBuilder HTML::TreeBuilder]'s string representation of the last-parsed syntax tree, showing how the input HTML was parsed internally. Also useful for debugging.

[#___top available_dialects]

  my @dialects = HTML::WikiConverter-&gt;available_dialects;

Returns a list of all available dialects by searching the directories in @INC for HTML::WikiConverter:: modules.

[#___top rules_for_tag]

  my $rules = $wc-&gt;rules_for_tag( $tag );

Returns the rules that will be used for converting elements of the given tag. Note that the rules used for a particular tag may depend on the current set of attributes being used.

[#___top ATTRIBUTES WikiConverter.pm_files/up.gif]

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 an H::WC object.

Some dialects allow other attributes in addition to those below, and may override the attributes' default values. Consult the dialect's documentation for details.

[#___top base_uri]

URI to use for converting relative URIs to absolute ones. This effectively ensures that the src and href 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 URIs are only converted to absolute ones if the base_uri argument is present. Defaults to undef.

[#___top dialect]

(Required) Dialect to use for converting HTML into wiki markup. See the [#DESCRIPTION "DESCRIPTION"] section above for a list of dialects. new() will fail if the dialect given is not installed on your system. Use available_dialects() to list installed dialects.

[#___top encoding]

Specifies the encoding used by the HTML to be converted. Also determines the encoding of the wiki markup returned by the html2wiki method. Defaults to "utf8".

[#___top escape_entities]

Potentially unsafe characters found within text nodes can be automatically encoded into their corresponding HTML entities, a feature enabled by giving the escape_entities a true value. Defaults to true.

head2 passthrough_naked_tags

Boolean indicating whether tags with no attributes ("naked" tags) should be removed and replaced with their content. By default, this only applies to non-semantic tags such as <span>, <div>, etc., but does not apply to semantic tags such as <strong>, <address>, etc. To override this behavior and specify the tags that should be considered for passthrough, provide this attribute with a reference to an array of tag names. Defaults to false, but you'll probably want to enable it.

[#___top preprocess]

Code reference that gets invoked after HTML is parsed but before it is converted into wiki markup. The callback is passed two arguments: the HTML::WikiConverter object and a [http://search.cpan.org/perldoc?HTML%3A%3AElement HTML::Element] pointing to the root node of the HTML tree created by [http://search.cpan.org/perldoc?HTML%3A%3ATreeBuilder HTML::TreeBuilder].

[#___top slurp]

Boolean that, if enabled, bypasses HTML::Parser's incremental parsing (thus slurping the file in all at once) of files when reading HTML files. If [http://search.cpan.org/perldoc?File%3A%3ASlurp File::Slurp] is installed, its read_file() function will be used to perform slurping; otherwise, a common Perl idiom will be used for slurping instead. This option is only used if you call html2wiki() with the file argument.

[#___top strip_empty_tags]

Strips elements containing no content (unless those elements legitimately contain no content, such as is the case for br and img tags, for example). Defaults to false.

[#___top strip_tags]

A reference to an array of tags to be removed from the HTML input prior to conversion to wiki markup. Tag names are the same as those used in [http://search.cpan.org/perldoc?HTML%3A%3AElement HTML::Element]. Defaults to [ '~comment', 'head', 'script', 'style' ].

[#___top user_agent]

Specifies the [http://search.cpan.org/perldoc?LWP%3A%3AUserAgent LWP::UserAgent] object to be used when fetching the URI passed to html2wiki(). If unspecified and html2wiki() is passed a URI, a default user agent will be created.

[#___top wiki_uri]

Takes a URI, regular expression, or coderef (or a reference to an array of elements of these types) used to determine which links are to wiki pages: a link whose href parameter matches wiki_uri will be treated as a link to a wiki page. In addition, wiki_uri will be used to extract the title of the wiki page. The way this is done depends on whether the wiki_uri has been set to a string, regexp, or coderef. The default is undef, meaning that all links will be treated as external links by default.

If wiki_uri is a string, it is assumed that URIs to wiki pages are created by joining the wiki_uri with the wiki page title. For example, the English Wikipedia might use "http://en.wikipedia.org/wiki/" as the value of wiki_uri. Ward's wiki might use "http://c2.com/cgi/wiki?".

wiki_uri can also be a regexp that matches URIs to wiki pages and also extracts the page title from them. For example, the English Wikipedia might use qr~[http://en%5C.wikipedia%5C.org/w/index%5C.php%5C?title http://en\.wikipedia\.org/w/index\.php\?title]\=([^&amp;]+)~.

wiki_uri can also be a coderef that takes the current HTML::WikiConverter object and a [http://search.cpan.org/perldoc?URI URI] object. It should return the title of the wiki page extracted from the URI, or undef if the URI doesn't represent a link to a wiki page.

As mentioned above, the wiki_uri attribute can either take a single URI/regexp/coderef element or it may be assigned a reference to an array of any number of these elements. This is useful for wikis that have different ways of creating links to wiki pages. For example, the English Wikipedia might use:

  my $wc = new HTML::WikiConverter(
    dialect =&gt; 'MediaWiki',
    wiki_uri =&gt; [
      'http://en.wikipiedia.org/wiki/',
      sub { pop-&gt;query_param('title') } # requires URI::QueryParam
    ]
  );

[#___top wrap_in_html]

Helps [http://search.cpan.org/perldoc?HTML%3A%3ATreeBuilder HTML::TreeBuilder] parse HTML fragments by wrapping HTML in &lt;html&gt; and &lt;/html&gt; before passing it through html2wiki. Boolean, enabled by default.

[#___top ADDING A DIALECT WikiConverter.pm_files/up.gif]

Consult [http://search.cpan.org/%7Ediberri/HTML-WikiConverter-0.61/lib/HTML/WikiConverter/Dialects.pod HTML::WikiConverter::Dialects] for documentation on how to write your own dialect module for HTML::WikiConverter. Or if you're not up to the task, drop me an email and I'll have a go at it when I get a spare moment.

[#___top SEE ALSO WikiConverter.pm_files/up.gif]

[http://search.cpan.org/perldoc?HTML%3A%3ATree HTML::Tree], [http://search.cpan.org/perldoc?Convert%3A%3AWiki Convert::Wiki]

[#___top AUTHOR WikiConverter.pm_files/up.gif]

David J. Iberri, &lt;diberri@cpan.org&gt;

[#___top BUGS WikiConverter.pm_files/up.gif]

Please report any bugs or feature requests to bug-html-wikiconverter at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-WikiConverter. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

[#___top SUPPORT WikiConverter.pm_files/up.gif]

You can find documentation for this module with the perldoc command.

    perldoc HTML::WikiConverter

You can also look for information at:

[#___top ACKNOWLEDGEMENTS WikiConverter.pm_files/up.gif]

Thanks to Tatsuhiko Miyagawa for suggesting [http://search.cpan.org/perldoc?Bundle%3A%3AHTMLWikiConverter Bundle::HTMLWikiConverter] as well as providing code for the available_dialects() class method.

My thanks also goes to Martin Kudlvasr for catching (and fixing!) a bug in the logic of how HTML files were processed.

Big thanks to Dave Schaefer for the PbWiki dialect and for the idea behind the new attributes() implementation.

[#___top COPYRIGHT &amp; LICENSE WikiConverter.pm_files/up.gif]

Copyright 2006 David J. Iberri, all rights reserved.

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