File: embedded-links.t

package info (click to toggle)
libtext-mediawikiformat-perl 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 236 kB
  • ctags: 46
  • sloc: perl: 1,848; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!perl

BEGIN { chdir 't' if -d 't' }

use strict;
use warnings;

use Test::More tests => 4;
use Test::NoWarnings;

use Text::MediawikiFormat as => 'wf', process_html => 0;

my $wikitext = <<WIKI;

[[SuperLink|[[Description|Desc]] of the [[Link]]]]

WIKI

{
    my $htmltext = wf ($wikitext);
    is $htmltext,
       qq{<p>[[SuperLink|<a href='Description'>Desc</a> of the }
       . qq{<a href='Link'>Link</a>]]</p>\n},
       '...ignore embedded links by default';
}

{
    # Redefine the delimiters to something different.
    my %tags = (extended_link_delimiters => [qw{[[ ]]}],
		link => \&_make_html_link);

    my $htmltext = wf ($wikitext, \%tags);
    is $htmltext,
       qq{<p><a href='SuperLink'><a href='Description'>Desc</a> of the }
       . qq{<a href='Link'>Link</a></a></p>\n},
       '...processing all embedded links';

    sub _make_html_link
    {
	my ($link) = @_;
	my ($href, $title) = split qr/\|/, $link, 2;
	$title ||= $href;
	return "<a href='$href'>$title</a>";
    }
}

TODO:
{
    # Art Henry's bug; but not sure it's really a bug
    local $TODO = "Unsupported MediaWiki features.";

    my %tags = (link => \&link_handler);

    # Or with the link handler overridden.
    my $htmltext = wf ($wikitext, \%tags);
    is $htmltext,
       "<p>Desc of the </p>\n",
       '...and also work with a handler override.';

    sub link_handler
    {
	my ($link, $opts) = @_;
	($link, my $title) = split /\|/, $link, 2;
	$title ||= $link;
	return $title;
    }
}