| Convert-Wiki documentation | Contained in the Convert-Wiki distribution. |
Convert::Wiki - Convert HTML/POD/txt from/to Wiki code
use Convert::Wiki;
my $wiki = Convert::Wiki->new();
$wiki->from_txt ( $txt );
die ("Error: " . $wiki->error()) if $wiki->error;
print $wiki->as_wiki();
$wiki->from_html ( $html );
die ("Error: " . $wiki->error()) if $wiki->error;
print $wiki->as_wiki();
# clear the object manually
$wiki->clear();
$wiki->add_txt ( $txt );
die ("Error: " . $wiki->error()) if $wiki->error;
print $wiki->as_wiki();
Convert::Wiki converts from various formats to various Wiki formats.
Input can come as HTML, POD or plain TXT (like it is written in many READMEs). The data will be converted to an internal, node based format and can then be converted to Wikicode as used by many wikis like the Wikipedia.
$cvt = Convert::Wiki->new();
Creates a new conversion object. It takes an optional list of options. The following are valid:
debug if set, will enable some debug outputs interlink a list of phrases, that if found in a paragraph, are turned into links (into the same Wiki)
The phrases in interlink are searched case-sensitive, and if the found phrase
differs from the searched one, a piped link will be generated. In addition, you
can give a different link target and link name by separating them with | :
Foo # turn "foo" into "[[Foo|foo]]", "Foo" into [Foo] etc Foobar|foo # find "foo", "Foo" etc. and makes them [[Foobar|foo]]
$cvt->as_wiki();
Returns the internally stored contents in wiki code.
$cvt->clear();
Clears the conversion object by resetting the last error and
throwing away all internally stored nodes. There is usually
no need to do this manually, except if you want to reuse
a conversion object with the add methods.
$debugmode = $cvt->debug(); # true or false
Returns true if the debug mode is enabled.
$last_error = $cvt->error();
$cvt->error($error); # set new messags
$cvt->error(''); # clear error
Returns the last error message, or '' for no error.
$cvt->from_txt();
Clears the object via clear() and then converts the given text to the internal format.
print "Nodes: ", $cvt->nodes(), "\n";
Returns the number of nodes the current document consists of. A fresh
Convert::Wiki object has zero, and after you
=head2 EXPORT
None by default.
Tels http://bloodgate.com
Copyright (C) 2004 by Tels
This library is free software; you can redistribute it and/or modify it under the terms of the GPL. See the LICENSE file for more details.
| Convert-Wiki documentation | Contained in the Convert-Wiki distribution. |
############################################################################# # (c) by Tels 2004. # ############################################################################# package Convert::Wiki; use 5.006001; use strict; use warnings; use vars qw/$VERSION/; $VERSION = '0.05'; use Convert::Wiki::Node; ############################################################################# sub new { my $class = shift; my $self = bless {}, $class; my $args = $_[0]; $args = { @_ } if ref($args) ne 'HASH'; $self->clear(); $self->_init($args); } sub _init { my ($self,$args) = @_; $self->{interlink} = []; # default none foreach my $k (keys %$args) { if ($k !~ /^(interlink|debug)\z/) { $self->error ("Unknown option '$k'"); } $self->{$k} = $args->{$k}; } if (ref($self->{interlink}) ne 'ARRAY') { $self->error ("Option 'interlink' needs a list of phrases"); } $self->{nodes} = undef; $self; } sub nodes { my $self = shift; my $node = $self->{nodes}; my $nodes = 0; while (defined $node) { $nodes++; $node = $node->{next}; } $nodes; } sub clear { my $self = shift; $self->{error} = ''; my $node = $self->{nodes}; # break circular references (this should not be necc., but play safe) while (defined $node) { $node->{prev} = undef; my $next = $node->{next}; $node->{next} = undef; $node = $next; } $self->{nodes} = undef; $self; } sub from_txt { my ($self,$txt) = @_; require Convert::Wiki::Txt; $self->_from_txt($txt); $self->_pull_nodes(); } sub _pull_nodes { # ask each node whether it should be removed (based on context) my $self = shift; my $node = $self->{nodes}; while (defined $node) { if ($node->_remove_me()) { $node->{prev}->{next} = $node->{next}; $node->{next}->{prev} = $node->{prev}; my $next = $node->{next}; $node->{prev} = undef; $node->{next} = undef; $node = $next; } else { $node = $node->{next}; } } $self; } sub as_wiki { my $self = shift; my $wiki = ''; my $node = $self->{nodes}; while (defined $node) { $wiki .= $node->as_wiki($self); $node = $node->{next}; } $wiki; } sub error { my $self = shift; $self->{error} = $_[0] if defined $_[0]; $self->{error}; } sub debug { my $self = shift; $self->{debug}; } 1; __END__