Skip Menu |
 

This queue is for tickets about the Text-WikiFormat CPAN distribution.

Report information
The Basics
Id: 21330
Status: open
Worked: 2 hours (120 min)
Priority: 0/
Queue: Text-WikiFormat

People
Owner: Nobody in particular
Requestors: derek [...] ximbiot.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.78
Fixed in: (no value)

Attachments


Subject: Allow extended link delimiters to be specified via regex
The attached patch allows a regular expression to be used to specify extended link delimiters.
Subject: text-wikiformat-regexlinkdelim.diff
Index: lib/Ximbiot/Text/WikiFormat.pm =================================================================== RCS file: /cvsroot/Ximbiot-Text-WikiFormat/lib/Ximbiot/Text/WikiFormat.pm,v retrieving revision 1.2 diff -u -p -r1.2 WikiFormat.pm --- lib/Ximbiot/Text/WikiFormat.pm 1 Sep 2006 19:55:40 -0000 1.2 +++ lib/Ximbiot/Text/WikiFormat.pm 5 Sep 2006 13:02:28 -0000 @@ -322,16 +322,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
Download (untitled) / with headers
text/plain 165b
I've now verified that the current tests pass after this patch is applied and the patch to RT-21269 is applied. Probably the RT-21269 patch doesn't affect this one.
Download (untitled) / with headers
text/plain 372b
The patch for 21269 did break this patch, because it was requiring that arrays in the %tags hash be replaced only with arrays and scalars be replaced only with scalars. The newly attached patch (to be used in place of the first) fixes this as well as adding additional tests for both array/scalar merging in merge_hash and tests using a regex in explicit_link_delimiters.
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" );


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.