File Coverage

blib/lib/HTML/WikiConverter/Wikispaces.pm
Criterion Covered Total %
statement 15 86 17.4
branch 0 46 0.0
condition 0 25 0.0
subroutine 5 13 38.4
pod 0 1 0.0
total 20 171 11.7


line stmt bran cond sub pod time code
1             package HTML::WikiConverter::Wikispaces;
2 1     1   25244 use base 'HTML::WikiConverter';
  1         3  
  1         734  
3              
4 1     1   7 use warnings;
  1         3  
  1         33  
5 1     1   7 use strict;
  1         6  
  1         44  
6              
7 1     1   1104 use URI;
  1         9308  
  1         31  
8 1     1   10 use File::Basename;
  1         2  
  1         1524  
9             our $VERSION = '0.02';
10              
11             =head1 NAME
12              
13             HTML::WikiConverter::Wikispaces - Convert HTML to Wikispaces markup
14              
15             =head1 SYNOPSIS
16              
17             use HTML::WikiConverter;
18             my $wc = new HTML::WikiConverter( dialect => 'Wikispaces' );
19             print $wc->html2wiki( $html );
20              
21             =head1 DESCRIPTION
22              
23             This module contains rules for converting HTML into Wikispaces
24             markup. See L for additional usage details.
25              
26             =cut
27              
28             sub rules {
29 0     0 0   my %rules = (
30             hr => { replace => "\n----\n" },
31              
32             h1 => { start => '= ', end => ' =', block => 1, trim => 'both', line_format => 'single' },
33             h2 => { start => '== ', end => ' ==', block => 1, trim => 'both', line_format => 'single' },
34             h3 => { start => '=== ', end => ' ===', block => 1, trim => 'both', line_format => 'single' },
35             # wikispaces does not support H4-H6, so just map to H3
36             h4 => { start => '=== ', end => ' ===', block => 1, trim => 'both', line_format => 'single' },
37             h5 => { start => '=== ', end => ' ===', block => 1, trim => 'both', line_format => 'single' },
38             h6 => { start => '=== ', end => ' ===', block => 1, trim => 'both', line_format => 'single' },
39              
40             img => { replace => \&_image },
41              
42             b => { start => "**", end => "**", trim => 'leading'},
43             strong => { alias => 'b' },
44             i => { start => "//", end => "//" },
45             em => { alias => 'i' },
46             u => { start => "__", end => "__"},
47             code => { start => "\n[[code]]\n", end => "\n[[code]]\n"},
48             tt => { start => "{{", end => "}}"},
49              
50             # from PhpWiki
51             blockquote => { start => \&_blockquote_start, block => 1, line_format => 'multi' },
52             p => { block => 1, trim => 'both', line_format => 'multi' },
53              
54             a => { replace => \&_link},
55              
56             ul => { line_format => 'multi', block => 1 },
57             ol => { alias => 'ul' },
58             dl => { alias => 'ul' },
59              
60             li => { start => \&_li_start, trim => 'leading' },
61             dt => { alias => 'li' },
62             dd => { alias => 'li' },
63              
64             # from PmWiki
65             table => { block => 1 },
66             tr => { start => "\n||", line_format => 'multi' },
67             td => { start => \&_td_start, end => \&_td_end, trim => 'both' },
68             th => { alias => 'td' },
69              
70             pre => { preserve => 1 }
71             );
72              
73 0           return \%rules;
74             }
75              
76             # Calculates the prefix that will be placed before each list item.
77             # List item include ordered and unordered list items.
78             sub _li_start {
79 0     0     my( $self, $node, $rules ) = @_;
80 0           my @parent_lists = $node->look_up( _tag => qr/ul|ol|dl/ );
81              
82 0           my $prefix = '';
83 0           foreach my $parent ( @parent_lists ) {
84 0           my $bullet = '';
85 0 0         $bullet = '*' if $parent->tag eq 'ul';
86 0 0         $bullet = '#' if $parent->tag eq 'ol';
87             # just map definition lists onto unordered lists
88 0 0         $bullet = '*' if $parent->tag eq 'dl';
89 0 0 0       $bullet = '*' if $parent->tag eq 'dl' and $node->tag eq 'dt';
90 0           $prefix = $bullet.$prefix;
91             }
92              
93 0           return "\n$prefix ";
94             }
95              
96             # derived from MediaWiki
97             sub _image {
98 0     0     my( $self, $node, $rules ) = @_;
99 0 0         return '' unless $node->attr('src');
100              
101 0           my $img = $node->attr('src');
102             # if it's not an absolute path then strip to just the base name
103 0 0         if($img !~ m/^https?:\/\//) {
104 0           $img = basename( URI->new($node->attr('src'))->path );
105             }
106 0   0       my $alt = $node->attr('alt') || '';
107 0   0       my $align = $node->attr('align') || '';
108 0   0       my $title = $node->attr('title') || '';
109 0   0       my $width = $node->attr('width') || '';
110 0   0       my $height = $node->attr('height') || '';
111            
112 0           my $ret = "[[image:$img";
113 0 0         $ret .= " alt=\"$alt\"" if $alt;
114 0 0         $ret .= " align=\"$align\"" if $align;
115 0 0         $ret .= " width=\"$width\"" if $width;
116 0 0         $ret .= " height=\"$height\"" if $height;
117 0 0         $ret .= " caption=\"$title\"" if $title;
118 0           $ret .= "]]";
119 0           return $ret;
120             }
121              
122             # derived from PmWiki
123             sub _anchor {
124 0     0     my( $self, $node, $rules ) = @_;
125 0   0       my $name = $node->attr('name') || '';
126             # Wikispaces does not support anchors begining with non-alphabetic characters, so prefix all anchors with ws_
127 0 0         $name = 'ws_'.$name if $name;
128 0           my $text = $self->get_elem_contents($node);
129 0           my $ret = "[[#$name]]";
130 0 0         $ret .= "$text" if $text;
131 0           return $ret;
132             }
133              
134             # derived from PmWiki
135             sub _link {
136 0     0     my( $self, $node, $rules ) = @_;
137 0 0         return $self->_anchor($node, $rules) if $node->attr('name');
138              
139 0   0       my $url = $node->attr('href') || '';
140             # if it's not an absolute path then strip to just the base name
141 0 0         if($url !~ m/^https?:\/\//) {
142 0           my($link,$path,$suffix) = fileparse($url,qr{\..*});
143 0           my $r = rindex($suffix,'#');
144 0 0         if($r!=-1) {
145 0           $suffix = substr $suffix, $r+1;
146 0           $link .= '#ws_'.$suffix;
147             }
148 0   0       my $text = $self->get_elem_contents($node) || '';
149            
150 0 0         return "[[$link]]" if $text eq $link;
151 0           return "[[$link|$text]]";
152             } else {
153 0           return $url;
154             }
155             }
156              
157             # tables derived from PmWiki
158             sub _td_start {
159 0     0     my( $self, $node, $rules ) = @_;
160 0   0       my $colspan = $node->attr('colspan') || 1;
161 0           my $prefix = ( '||' ) x ($colspan-1);
162 0 0         $prefix .= $node->tag eq 'th' ? '~' : '';
163              
164 0   0       my $align = $node->attr('align') || 'left';
165 0   0       my $style = $node->attr('style') || '';
166 0 0         $align = 'center' if $style eq 'text-align: center;';
167 0 0         $align = 'right' if $style eq 'text-align: right;';
168              
169 0 0         $prefix .= '= ' if $align eq 'center';
170 0 0         $prefix .= '> ' if $align eq 'right';
171 0 0         $prefix .= ' ' if $align eq 'left';
172              
173 0           return $prefix;
174             }
175              
176             sub _td_end {
177 0     0     my( $self, $node, $rules ) = @_;
178 0           my $suffix = ' ||';
179            
180 0           return $suffix;
181             }
182              
183             # blockquote derived from PmWiki
184             sub _blockquote_start {
185 0     0     my( $self, $node, $rules ) = @_;
186 0           my @parent_bqs = $node->look_up( _tag => 'blockquote' );
187 0           my $depth = @parent_bqs;
188            
189 0           my $start = ( '>' ) x $depth;
190 0           return "\n".$start.' ';
191             }
192              
193             =head1 AUTHOR
194              
195             Martin Budden, C<< >>
196              
197             Heavily based on other HTML to wikitext modules written by David J. Iberri, C<< >>
198              
199             =head1 BUGS
200              
201             Please report any bugs or feature requests to
202             C, or through the web
203             interface at
204             L.
205             I will be notified, and then you'll automatically be notified of
206             progress on your bug as I make changes.
207              
208             =head1 SUPPORT
209              
210             You can find documentation for this module with the perldoc command.
211              
212             perldoc HTML::WikiConverter::Wikispaces
213              
214             You can also look for information at:
215              
216             =over 4
217              
218             =item * AnnoCPAN: Annotated CPAN documentation
219              
220             L
221              
222             =item * CPAN Ratings
223              
224             L
225              
226             =item * RT: CPAN's request tracker
227              
228             L
229              
230             =item * Search CPAN
231              
232             L
233              
234             =back
235              
236             =head1 COPYRIGHT & LICENSE
237              
238             Copyright 2008 Martin Budden, all rights reserved.
239              
240             Heavily based on other HTML to wikitext modules written by David J. Iberri
241              
242             This program is free software; you can redistribute it and/or modify
243             it under the same terms as Perl itself.
244              
245             =cut
246              
247             1;