forked from ingydotnet/wikitext-pm
/
Emitter.pm
98 lines (75 loc) · 1.96 KB
/
Emitter.pm
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package WikiText::HTML::Emitter;
use strict;
use warnings;
use base 'WikiText::Emitter';
use CGI::Util;
my $type_tags = {
b => 'strong',
i => 'em',
wikilink => 'a',
};
sub uri_escape {
$_ = shift;
s/ /\%20/g;
return $_;
}
sub begin_node {
my $self = shift;
my $node = shift;
my $type = $node->{type};
my $tag = $type_tags->{$type} || $type;
# XXX For tables maybe...
# $tag =~ s/-.*//;
$self->{output} .=
($tag =~ /^(br|hr)$/)
? "<$tag />\n"
: ($type eq "wikilink")
? $self->begin_wikilink($node)
: "<$tag>" .
($tag =~ /^(ul|ol|table|tr)$/ ? "\n" : "");
}
sub begin_wikilink {
my $self = shift;
my $node = shift;
my $tag = $node->{type};
my $link = $self->{callbacks}{wikilink}
? $self->{callbacks}{wikilink}->($node)
: CGI::Util::escape($node->{attributes}{target});
my $class = $node->{attributes}{class};
$class = $class ? qq{ class="$class"} : '';
return qq{<a href="$link"$class>};
}
sub end_node {
my $self = shift;
my $node = shift;
my $type = $node->{type};
my $tag = $type_tags->{$type} || $type;
$tag =~ s/-.*//;
return if ($tag =~ /^(br|hr)$/);
if ($tag eq "wikilink") {
$self->{output} .= '</a>';
return;
}
$self->{output} .= "</$tag>" .
($tag =~ /^(p|hr|ul|ol|li|h\d|table|tr|td)$/ ? "\n" : "");
}
sub text_node {
my $self = shift;
my $text = shift;
$self->{output} .= "$text";
}
1;
=head1 NAME
WikiText::HTML::Emitter - A WikiText Receiver That Generates HTML
=head1 SYNOPSIS
use WikiText::HTML::Emitter;
=head1 DESCRIPTION
This receiver module, when hooked up to a parser, produces HTML.
=head1 AUTHOR
Ingy döt Net <ingy@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2008. Ingy döt Net.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
=cut