Index: Changes
===================================================================
RCS file: /cvsroot/Text-MediawikiFormat/Changes,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -p -r1.2 -r1.3
--- Changes 7 Sep 2006 20:05:32 -0000 1.2
+++ Changes 7 Sep 2006 20:58:41 -0000 1.3
@@ -1,4 +1,5 @@
0.79
+ - extended_link_delimiters may now be specified as a regex.
- merge_hash() now copies hashes.
- several nesting bugs fixed.
- code refs now preserve indenting after the " ", not before.
Index: lib/Text/MediawikiFormat.pm
===================================================================
RCS file: /cvsroot/Text-MediawikiFormat/lib/Text/MediawikiFormat.pm,v
retrieving revision 1.3
diff -u -p -r1.3 MediawikiFormat.pm
--- lib/Text/MediawikiFormat.pm 7 Sep 2006 20:54:36 -0000 1.3
+++ lib/Text/MediawikiFormat.pm 8 Sep 2006 14:52:35 -0000
@@ -75,17 +75,20 @@ sub default_opts
SCALAR => {
SCALAR => sub { return $_[0] },
ARRAY =>
- sub { # We could create a 1 item array here...
- confess "Attempt to replace array with scalar"
- if defined $_[0];
- return _clone ($_[1]); },
+ sub { # Need to be able to replace scalar with array
+ # for extended_link_delimiters (could be array
+ # or regex).
+ return $_[0]; },
HASH =>
sub { confess "Attempt to replace hash with scalar"
if defined $_[0];
return _clone ($_[1]); } },
ARRAY => {
SCALAR =>
- sub { confess "Attempt to replace scalar with array" },
+ sub { # Need to be able to replace array with scalar
+ # for extended_link_delimiters (could be array
+ # or regex).
+ return _clone ($_[0]); },
ARRAY => sub { return _clone ($_[0]); },
HASH =>
sub { confess "Attempt to replace hash with array" } },
@@ -409,16 +412,24 @@ sub find_extended_links
$text =~ s!(\s+)(($schemas):\S+)!$1 . $tags->{link}->($2, $opts)!egi
if $opts->{absolute_links};
- my ($start, $end) = @{ $tags->{extended_link_delimiters} };
-
- while (my @pieces = find_innermost_balanced_pair( $text, $start, $end ) )
+ if (ref $tags->{extended_link_delimiters} eq "ARRAY")
+ {
+ my ($start, $end) = @{$tags->{extended_link_delimiters}};
+ while (my @pieces = find_innermost_balanced_pair ($text, $start, $end))
{
- my ($tag, $before, $after) = map { defined $_ ? $_ : '' } @pieces;
- my $extended = $tags->{link}->( $tag, $opts ) || '';
- $text = $before . $extended . $after;
- };
+ my ($tag, $before, $after) = map { defined $_ ? $_ : '' } @pieces;
+ my $extended = $tags->{link}->( $tag, $opts ) || '';
+ $text = $before . $extended . $after;
+ }
+ }
+ else
+ {
+ # Regexp
+ $text =~ s/$tags->{extended_link_delimiters}/$tags->{link}->($1,
+ $opts)/ge;
+ }
- return $text;
+ return $text;
}
sub make_html_link
Index: t/explicit.t
===================================================================
RCS file: /cvsroot/Text-MediawikiFormat/t/explicit.t,v
retrieving revision 1.2
diff -u -p -r1.2 explicit.t
--- t/explicit.t 5 Sep 2006 14:27:28 -0000 1.2
+++ t/explicit.t 8 Sep 2006 14:52:35 -0000
@@ -5,7 +5,7 @@ BEGIN { chdir 't' if -d 't' }
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 6;
use Text::MediawikiFormat;
my $wikitext =<<WIKI;
@@ -41,3 +41,75 @@ unlike( $htmltext, qr!Ordinary extended
like( $htmltext, qr!Usemod extended link</a>[^\]]!m,
'...and new delimiters recognised' );
+
+
+
+##
+## Mediawiki thinks [[...]] is a wiki link and [...] is a standard URI href.
+##
+
+# Turn [[Wiki Link|Title]] or [URI Title] into links.
+sub _make_link
+{
+ my ($tag, $opts) = @_;
+
+ my ($href, $title);
+ if ($tag =~ /^\[(#?)([^|]*)(?:(|)(.*))?\]$/)
+ {
+ # Wiki link
+ $href = $opts->{prefix} unless $1;
+ $href .= $1 . $2;
+ # Would normally URI::Escape::uri_escape $2 there, but it's probably
+ # not worth importing that for tests.
+ if ($3)
+ {
+ # Title specified explicitly.
+ if (length $4)
+ {
+ $title = $4;
+ }
+ else
+ {
+ # An empty title asks Mediawiki to strip any parens off the end
+ # of the node name.
+ $2 =~ /^([^(]*)(?:\s*\()?/;
+ $title = $1;
+ }
+ }
+ else
+ {
+ # Title defaults to the node name.
+ $title = $2;
+ }
+ }
+ else
+ {
+ # URI
+ $tag =~ /^(\S*)(?:(\s+)(.*))?$/;
+ $href = $1;
+ if ($2)
+ {
+ $title = $3;
+ }
+ else
+ {
+ $title = ++$opts->{_uri_refs};
+ }
+ $href =~ s/'/%27/g;
+ }
+
+ return "<a href='$href'>$title</a>";
+}
+
+%tags = (
+ extended_link_delimiters => qr/\[(\[[^][]*\]|[^][]*)\]/,
+ link => \&_make_link,
+);
+
+$htmltext = Text::MediawikiFormat::format( $wikitext, \%tags,
+ { extended => 1,
+ implicit_links => 0 } );
+like( $htmltext, qr!'Ordinary'>extended link</a>!m, 'mediawiki style href' );
+like( $htmltext, qr!Usemod extended link</a>!m,
+ 'mediawiki style extended link' );
+
Index: t/merge-hash.t
===================================================================
RCS file: /cvsroot/Text-MediawikiFormat/t/merge-hash.t,v
retrieving revision 1.3
diff -u -p -r1.3 merge-hash.t
--- t/merge-hash.t 7 Sep 2006 20:05:33 -0000 1.3
+++ t/merge-hash.t 8 Sep 2006 14:52:35 -0000
@@ -5,7 +5,7 @@ BEGIN { chdir 't' if -d 't' }
use strict;
use warnings;
-use Test::More tests => 6;
+use Test::More tests => 8;
use_ok( 'Text::MediawikiFormat' ) or exit;
my $full = { foo => { bar => 'baz' } };
@@ -33,3 +33,11 @@ is_deeply( $empty, $full,
$empty = {};
$empty = Text::MediawikiFormat::merge_hash( $zero, $empty );
is_deeply( $empty, $zero, '... and when value is zero but defined' );
+
+my $regexer = { a => "regex" };
+my $arrayer = { a => ["X", "Y", "Z"] };
+my $merged;
+$merged = Text::MediawikiFormat::merge_hash( $regexer, $arrayer );
+is_deeply( $merged, { a => "regex" }, "regexes should replace arrays" );
+$merged = Text::MediawikiFormat::merge_hash( $arrayer, $regexer );
+is_deeply( $merged, { a => ["X", "Y", "Z"] }, "...and vice versa" );