File Coverage

blib/lib/HTML/WikiConverter/PhpWiki.pm
Criterion Covered Total %
statement 9 41 21.9
branch 0 16 0.0
condition 0 15 0.0
subroutine 3 11 27.2
pod 0 2 0.0
total 12 85 14.1


line stmt bran cond sub pod time code
1             package HTML::WikiConverter::PhpWiki;
2              
3 1     1   35954 use warnings;
  1         3  
  1         45  
4 1     1   6 use strict;
  1         2  
  1         38  
5              
6 1     1   7 use base 'HTML::WikiConverter';
  1         6  
  1         2494  
7              
8             our $VERSION = '0.51';
9              
10             =head1 NAME
11              
12             HTML::WikiConverter::PhpWiki - Convert HTML to PhpWiki markup
13              
14             =head1 SYNOPSIS
15              
16             use HTML::WikiConverter;
17             my $wc = new HTML::WikiConverter( dialect => 'PhpWiki' );
18             print $wc->html2wiki( $html );
19              
20             =head1 DESCRIPTION
21              
22             This module contains rules for converting HTML into PhpWiki
23             markup. See L for additional usage details.
24              
25             =cut
26              
27             sub rules {
28 0     0 0   my %rules = (
29             hr => { replace => "\n----\n" },
30             br => { replace => '%%%' },
31              
32             blockquote => { start => \&_blockquote_start, block => 1, line_format => 'multi' },
33             p => { block => 1, trim => 'both', line_format => 'multi' },
34             i => { start => "_", end => "_" },
35             em => { alias => 'i' },
36             b => { start => "*", end => "*" },
37             strong => { alias => 'b' },
38              
39             img => { replace => \&_image },
40             a => { replace => \&_link },
41              
42             ul => { line_format => 'multi', block => 1 },
43             ol => { alias => 'ul' },
44             dl => { line_format => 'blocks', block => 1 },
45              
46             li => { start => \&_li_start, trim => 'leading' },
47             dt => { trim => 'both', end => ":\n" },
48             dd => { line_prefix => ' ' },
49              
50             td => { start => \&_td_start, end => \&_td_end, trim => 'both' },
51             th => { alias => 'td' },
52              
53             h1 => { start => '!!! ', block => 1, trim => 'both', line_format => 'single' },
54             h2 => { start => '!!! ', block => 1, trim => 'both', line_format => 'single' },
55             h3 => { start => '!! ', block => 1, trim => 'both', line_format => 'single' },
56             h4 => { start => '! ', block => 1, trim => 'both', line_format => 'single' },
57             h5 => { start => '! ', block => 1, trim => 'both', line_format => 'single' },
58             h6 => { start => '! ', block => 1, trim => 'both', line_format => 'single' },
59              
60             pre => { preserve => 1 },
61             );
62              
63 0           $rules{$_} = { preserve => 1 } for qw/ big small tt abbr acronym cite code dfn kbd samp var sup sub /;
64 0           return \%rules;
65             }
66              
67             # Calculates the prefix that will be placed before each list item.
68             # List item include ordered and unordered list items.
69             sub _li_start {
70 0     0     my( $self, $node, $rules ) = @_;
71 0           my @parent_lists = $node->look_up( _tag => qr/ul|ol/ );
72 0           my $depth = @parent_lists;
73              
74 0           my $bullet = '';
75 0 0         $bullet = '*' if $node->parent->tag eq 'ul';
76 0 0         $bullet = '#' if $node->parent->tag eq 'ol';
77              
78 0           my $prefix = ( $bullet ) x $depth;
79 0           return "\n$prefix ";
80             }
81              
82             sub _image {
83 0     0     my( $self, $node, $rules ) = @_;
84 0   0       return $node->attr('src') || '';
85             }
86              
87             sub _link {
88 0     0     my( $self, $node, $rules ) = @_;
89 0   0       my $url = $node->attr('href') || '';
90 0   0       my $text = $self->get_elem_contents($node) || '';
91 0           return "[$text|$url]";
92             }
93              
94             # XXX doesn't handle rowspan
95             sub _td_start {
96 0     0     my( $self, $node, $rules ) = @_;
97 0           my @left = $node->left;
98 0 0         return '' unless @left;
99 0           return ( ( ' ' ) x scalar(@left) );
100             }
101              
102             sub _td_end {
103 0     0     my( $self, $node, $rules ) = @_;
104 0 0 0       my $right_tag = $node->right && $node->right->tag ? $node->right->tag : '';
105 0 0         return $right_tag =~ /td|th/ ? " |\n" : "\n";
106             }
107              
108             sub _blockquote_start {
109 0     0     my( $self, $node, $rules ) = @_;
110 0           my @bq_lineage = $node->look_up( _tag => 'blockquote' );
111 0           my $depth = @bq_lineage;
112 0           return "\n" . ( ( ' ' ) x $depth );
113             }
114              
115             sub preprocess_node {
116 0     0 0   my( $self, $node ) = @_;
117 0 0         $self->strip_aname($node) if $node->tag eq 'a';
118 0 0         $self->caption2para($node) if $node->tag eq 'caption';
119              
120             # Bug 17550 (https://rt.cpan.org/Public/Bug/Display.html?id=17550)
121 0 0 0       $node->postinsert(' ') if $node->tag eq 'br' and $node->right and $node->right->tag eq 'br';
      0        
122             }
123              
124             =head1 AUTHOR
125              
126             David J. Iberri, C<< >>
127              
128             =head1 BUGS
129              
130             Please report any bugs or feature requests to
131             C, or through the web
132             interface at
133             L.
134             I will be notified, and then you'll automatically be notified of
135             progress on your bug as I make changes.
136              
137             =head1 SUPPORT
138              
139             You can find documentation for this module with the perldoc command.
140              
141             perldoc HTML::WikiConverter::PhpWiki
142              
143             You can also look for information at:
144              
145             =over 4
146              
147             =item * AnnoCPAN: Annotated CPAN documentation
148              
149             L
150              
151             =item * CPAN Ratings
152              
153             L
154              
155             =item * RT: CPAN's request tracker
156              
157             L
158              
159             =item * Search CPAN
160              
161             L
162              
163             =back
164              
165             =head1 COPYRIGHT & LICENSE
166              
167             Copyright 2006 David J. Iberri, all rights reserved.
168              
169             This program is free software; you can redistribute it and/or modify
170             it under the same terms as Perl itself.
171              
172             =cut
173              
174             1;