line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::WikiConverter::Txt2tags; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
31740
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
7
|
use base 'HTML::WikiConverter'; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2563
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
HTML::WikiConverter::Txt2tags - Convert HTML to Txt2tags markup |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use HTML::WikiConverter; |
17
|
|
|
|
|
|
|
my $wc = new HTML::WikiConverter( dialect => 'Txt2tags' ); |
18
|
|
|
|
|
|
|
print $wc->html2wiki( $html ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
or |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
html2wiki --dialect Txt2tags file.html |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
or |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
curl --silent http://theody.net/elements.html | html2wiki --dialect Txt2tags > elements.t2t |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This module contains rules for converting HTML into Txt2tags |
32
|
|
|
|
|
|
|
markup. See L for additional usage details. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
See L for more information on Txt2tags. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub attributes { { |
41
|
0
|
|
|
0
|
0
|
|
camel_case => { default => 0 } |
42
|
|
|
|
|
|
|
} } |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub rules { |
45
|
0
|
|
|
0
|
0
|
|
my %rules = ( |
46
|
|
|
|
|
|
|
b => { start => '**', end => '**' }, |
47
|
|
|
|
|
|
|
strong => { alias => 'b' }, |
48
|
|
|
|
|
|
|
i => { start => '//', end => '//' }, |
49
|
|
|
|
|
|
|
em => { alias => 'i' }, |
50
|
|
|
|
|
|
|
cite => { alias => 'i' }, |
51
|
|
|
|
|
|
|
u => { start => '__', end => '__' }, |
52
|
|
|
|
|
|
|
s => { start => '--', end => '--' }, |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
tt => { start => '``', end => '``' }, |
55
|
|
|
|
|
|
|
code => { alias => 'tt' }, |
56
|
|
|
|
|
|
|
a => { replace => \&_link }, |
57
|
|
|
|
|
|
|
img => { replace => \&_image }, |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
pre => { start => "```\n", end => "\n```", block => 1, line_format => 'blocks', trim => 'none' }, |
60
|
|
|
|
|
|
|
blockquote => { start => "\n", line_prefix => "\t", block => 1, line_format => 'multi', trim => 'leading' }, |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
p => { block => 1, trim => 'none', line_format => 'multi' }, |
63
|
|
|
|
|
|
|
br => { start => "\n", trim => 'leading' }, |
64
|
|
|
|
|
|
|
hr => { replace => "\n--------------------\n" }, |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sup => { preserve => 1 }, |
67
|
|
|
|
|
|
|
sub => { preserve => 1 }, |
68
|
|
|
|
|
|
|
del => { alias => 's' }, |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
ul => { line_format => 'multi', block => 1, line_prefix => ' ' }, |
72
|
|
|
|
|
|
|
ol => { alias => 'ul' }, |
73
|
|
|
|
|
|
|
li => { start => \&_li_start, trim => 'leading' }, |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
table => { block => 1 }, |
76
|
|
|
|
|
|
|
tr => { start => "\n", line_format => 'single' }, |
77
|
|
|
|
|
|
|
td => { start => \&_td_start, end => \&_td_end, trim => 'both' }, |
78
|
|
|
|
|
|
|
th => { alias => 'td' }, |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
dt => { start => ":", end => "\n"}, |
81
|
|
|
|
|
|
|
dd => { start => " ", end => "\n"}, |
82
|
|
|
|
|
|
|
span => { preserve => 0 }, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$rules{h1} = { start => "LINEBREAK= ", end => ' =LINEBREAK', block => 1, trim => 'both', line_format => 'single', line_prefix => "" }; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$rules{h2} = { start => "LINEBREAK== ", end => " ==LINEBREAK", block => 1, trim => 'both', line_format => 'single', line_prefix => ""}; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
$rules{h3} = { start => "LINEBREAK=== ", end => " ===LINEBREAK", block => 1, trim => 'both', line_format => 'single', line_prefix => "" }; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
$rules{h4} = { start => "LINEBREAK==== ", end => ' ====LINEBREAK', block => 1, trim => 'both', line_format => 'single', line_prefix => "" }; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
$rules{h5} = { start => "LINEBREAK===== ", end => ' =====LINEBREAK', block => 1, trim => 'both', line_format => 'single' }; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
$rules{h6} = { start => "LINEBREAK====== ", end => ' ======LINEBREAK', block => 1, trim => 'both', line_format => 'single' }; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
return \%rules; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub postprocess_output { |
102
|
0
|
|
|
0
|
0
|
|
my( $self, $outref ) = @_; |
103
|
0
|
|
|
|
|
|
$$outref =~ s~^>+\s+~~gm; # rm empty blockquote prefixes |
104
|
0
|
|
|
|
|
|
$$outref =~ s~^(>+)~$1 ~gm; # add space after prefix for clarity |
105
|
0
|
|
|
|
|
|
$$outref =~ s/LINEBREAK/\n\n/gm; # forces linebreak when \n are not enough |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _li_start { |
110
|
0
|
|
|
0
|
|
|
my( $self, $node, $rules ) = @_; |
111
|
0
|
|
|
|
|
|
my $bullet = ''; |
112
|
0
|
0
|
|
|
|
|
$bullet = '-' if $node->parent->tag eq 'ul'; |
113
|
0
|
0
|
|
|
|
|
$bullet = '+' if $node->parent->tag eq 'ol'; |
114
|
0
|
|
|
|
|
|
return "\n$bullet "; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _link { |
118
|
0
|
|
|
0
|
|
|
my( $self, $node, $rules ) = @_; |
119
|
0
|
|
0
|
|
|
|
my $url = $node->attr('href') || ''; |
120
|
0
|
|
0
|
|
|
|
my $text = $self->get_elem_contents($node) || ''; |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if( my $title = $self->get_wiki_page($url) ) { |
123
|
|
|
|
|
|
|
# [[MiXed cAsE]] ==> MiXed cAsE |
124
|
0
|
|
|
|
|
|
$title =~ s/_/ /g; |
125
|
0
|
0
|
0
|
|
|
|
return $text if $self->camel_case and lc $title eq lc $text and $self->is_camel_case($text); |
|
|
|
0
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
return "[[$text]]" if lc $text eq lc $title; |
127
|
0
|
|
|
|
|
|
return "[[$title|$text]]"; |
128
|
|
|
|
|
|
|
} else { |
129
|
0
|
0
|
|
|
|
|
return $url if $url eq $text; |
130
|
0
|
|
|
|
|
|
return "[$text $url]"; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _image { |
135
|
0
|
|
|
0
|
|
|
my( $self, $node, $rules ) = @_; |
136
|
0
|
|
0
|
|
|
|
my $src = $node->attr('src') || ''; |
137
|
0
|
0
|
|
|
|
|
return '' unless $src; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
0
|
|
|
|
my $w = $node->attr('width') || 0; |
140
|
0
|
|
0
|
|
|
|
my $h = $node->attr('height') || 0; |
141
|
0
|
0
|
0
|
|
|
|
if( $w and $h ) { |
|
|
0
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
$src .= "?${w}x${h}"; |
143
|
|
|
|
|
|
|
} elsif( $w ) { |
144
|
0
|
|
|
|
|
|
$src .= "?${w}"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
0
|
|
|
|
my $class = $node->attr('class') || ''; |
148
|
0
|
0
|
0
|
|
|
|
my $padleft = $class eq 'mediaright' || $class eq 'mediacenter' ? ' ' : ''; |
149
|
0
|
0
|
0
|
|
|
|
my $padright = $class eq 'medialeft' || $class eq 'mediacenter' ? ' ' : ''; |
150
|
0
|
|
|
|
|
|
$src = "$padleft$src$padright"; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# All images considered external |
153
|
0
|
|
0
|
|
|
|
my $caption = $node->attr('title') || $node->attr('alt') || ''; |
154
|
0
|
0
|
|
|
|
|
return "[$src]" if $caption; |
155
|
0
|
|
|
|
|
|
return "[$src]"; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _td_start { |
159
|
0
|
|
|
0
|
|
|
my( $self, $node, $rules ) = @_; |
160
|
0
|
0
|
|
|
|
|
my $prefix = $node->tag eq 'th' ? '|' : '|'; |
161
|
0
|
|
|
|
|
|
$prefix .= ' '; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
0
|
|
|
|
my $class = $node->attr('class') || ''; |
164
|
0
|
0
|
0
|
|
|
|
$prefix .= ' ' if $class eq 'rightalign' or $class eq 'centeralign'; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
return $prefix; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _td_end { |
170
|
0
|
|
|
0
|
|
|
my( $self, $node, $rules ) = @_; |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $end = ' '; |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
0
|
|
|
|
my $class = $node->attr('class') || ''; |
175
|
0
|
0
|
0
|
|
|
|
$end .= ' ' if $class eq 'leftalign' or $class eq 'centeralign'; |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
0
|
|
|
|
my $colspan = $node->attr('colspan') || 1; |
178
|
|
|
|
|
|
|
|
179
|
0
|
0
|
|
|
|
|
my @right_cells = grep { $_->tag && $_->tag =~ /th|td/ } $node->right; |
|
0
|
|
|
|
|
|
|
180
|
0
|
0
|
0
|
|
|
|
return $end if $colspan == 1 and @right_cells; |
181
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
my $suffix = $node->tag eq 'th' ? '|' : '|'; |
183
|
0
|
|
|
|
|
|
$suffix = ( $suffix ) x $colspan; |
184
|
0
|
|
|
|
|
|
return $end.$suffix; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 AUTHOR |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Eric Forgeot C<< >>, based on David J. Iberri's work |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 BUGS |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Please report any bugs or feature requests through the web |
194
|
|
|
|
|
|
|
interface at |
195
|
|
|
|
|
|
|
L. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 SUPPORT |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
perldoc HTML::WikiConverter::Txt2tags |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
You can also look for information at: |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=over 4 |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
L |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=item * CPAN Ratings |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
L |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item * Search CPAN |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=back |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Based on the DokuWiki.pm converter. Many thanks to David J. Iberri, C<< >> for writing HTML::WikiConverter. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Copyright 2006 David J. Iberri, all rights reserved. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
234
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=cut |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
1; |