File Coverage

blib/lib/HTML/WikiConverter/Oddmuse.pm
Criterion Covered Total %
statement 12 42 28.5
branch 0 14 0.0
condition 0 18 0.0
subroutine 4 11 36.3
pod 0 2 0.0
total 16 87 18.3


line stmt bran cond sub pod time code
1             package HTML::WikiConverter::Oddmuse;
2              
3 1     1   39687 use warnings;
  1         2  
  1         40  
4 1     1   7 use strict;
  1         2  
  1         38  
5              
6 1     1   5 use base 'HTML::WikiConverter';
  1         6  
  1         722  
7              
8             our $VERSION = '0.52';
9              
10 1     1   1084 use Params::Validate ':types';
  1         13429  
  1         1367  
11              
12             =head1 NAME
13              
14             HTML::WikiConverter::Oddmuse - Convert HTML to Oddmuse markup
15              
16             =head1 SYNOPSIS
17              
18             use HTML::WikiConverter;
19             my $wc = new HTML::WikiConverter( dialect => 'Oddmuse' );
20             print $wc->html2wiki( $html );
21              
22             =head1 DESCRIPTION
23              
24             This module contains rules for converting HTML into Oddmuse
25             markup. This dialect module supports most of Oddmuse's text formatting
26             rules described at [1], notably:
27              
28             * bold, strong, italic, emphasized, and underlined text
29             * paragraph blocks
30             * external images
31             * internal and external links
32             * unordered and ordered lists
33             * tables [2]
34              
35             [1] http://www.oddmuse.org/cgi-bin/wiki/Text_Formatting_Rules
36             [2] http://www.oddmuse.org/cgi-bin/wiki/Table_Markup_Extension
37              
38             See L for usage details.
39              
40             =head1 ATTRIBUTES
41              
42             In addition to the regular set of attributes recognized by the
43             L constructor, this dialect also accepts the
44             following attributes:
45              
46             =head2 camel_case
47              
48             Boolean indicating whether CamelCase links are enabled in the target
49             Oddmuse installation. This corresponds to Oddmuse's C<$WikiLinks>
50             configuration parameter. Enabling CamelCase links will turn HTML like
51             this:
52              
53            

CamelCase links are fun.

54              
55             into this Oddmuse markup:
56              
57             CamelCase links are fun.
58              
59             Disabling CamelCase links (the default) would convert that HTML into
60              
61             [[CamelCase]] links are fun.
62              
63             =cut
64              
65             sub attributes { {
66 0     0 0   camel_case => { default => 0, type => BOOLEAN }
67             } }
68              
69             sub rules {
70 0     0 0   my %rules = (
71             b => { start => '*', end => '*' },
72             strong => { alias => 'b' },
73             i => { start => '/', end => '/' },
74             em => { start => '~', end => '~' },
75             u => { start => '_', end => '_' },
76            
77             p => { block => 1, trim => 'both', line_format => 'multi' },
78             img => { replace => \&_image },
79             a => { replace => \&_link },
80              
81             ul => { line_format => 'multi', block => 1 },
82             ol => { alias => 'ul' },
83             li => { start => \&_li_start, trim => 'leading' },
84              
85             # http://www.oddmuse.org/cgi-bin/wiki/Table_Markup_Extension
86             table => { block => 1, line_format => 'multi' },
87             tr => { line_format => 'single', end => "||\n" },
88             td => { start => \&_td_start, end => \&_td_end, trim => 'both' },
89             th => { alias => 'td' },
90              
91             h1 => { start => '=', end => '=', block => 1, line_format => 'single' },
92             h2 => { start => '==', end => '==', block => 1, line_format => 'single' },
93             h3 => { start => '===', end => '===', block => 1, line_format => 'single' },
94             h4 => { start => '====', end => '====', block => 1, line_format => 'single' },
95             h5 => { start => '=====', end => '=====', block => 1, line_format => 'single' },
96             h6 => { start => '======', end => '======', block => 1, line_format => 'single' },
97             );
98              
99 0           return \%rules;
100             }
101              
102             sub _td_start {
103 0     0     my( $self, $node, $rules ) = @_;
104 0   0       my $align = $node->attr('align') || 'left';
105 0   0       my $colspan = $node->attr('colspan') || 1;
106              
107 0           my $prefix = ( '||' ) x $colspan;
108 0 0         my $pad_for_align = $align eq 'left' ? '' : ' ';
109 0           return $prefix.$pad_for_align;
110             }
111              
112             sub _td_end {
113 0     0     my( $self, $node, $rules ) = @_;
114 0   0       my $align = $node->attr('align') || 'left';
115 0 0         return $align eq 'right' ? '' : ' ';
116             }
117              
118             sub _link {
119 0     0     my( $self, $node, $rules ) = @_;
120 0   0       my $url = $node->attr('href') || '';
121 0   0       my $text = $self->get_elem_contents($node) || '';
122            
123 0 0         if( my $title = $self->get_wiki_page($url) ) {
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 "[$url $text]";
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             # Could do something with an 'image_uri' option to handle local images
140 0           return $src;
141             }
142              
143             sub _li_start {
144 0     0     my( $self, $node, $rules ) = @_;
145 0           my @parent_lists = $node->look_up( _tag => qr/ul|ol/ );
146 0           my $prefix = ('*') x @parent_lists;
147 0           return "\n$prefix ";
148             }
149              
150             =head1 AUTHOR
151              
152             David J. Iberri, C<< >>
153              
154             =head1 BUGS
155              
156             Please report any bugs or feature requests to
157             C, or through the web
158             interface at
159             L.
160             I will be notified, and then you'll automatically be notified of
161             progress on your bug as I make changes.
162              
163             =head1 SUPPORT
164              
165             You can find documentation for this module with the perldoc command.
166              
167             perldoc HTML::WikiConverter::Oddmuse
168              
169             You can also look for information at:
170              
171             =over 4
172              
173             =item * AnnoCPAN: Annotated CPAN documentation
174              
175             L
176              
177             =item * CPAN Ratings
178              
179             L
180              
181             =item * RT: CPAN's request tracker
182              
183             L
184              
185             =item * Search CPAN
186              
187             L
188              
189             =back
190              
191             =head1 COPYRIGHT & LICENSE
192              
193             Copyright 2006 David J. Iberri, all rights reserved.
194              
195             This program is free software; you can redistribute it and/or modify
196             it under the same terms as Perl itself.
197              
198             =cut
199              
200             1;