File Coverage

blib/lib/Pod/Simple/Wiki/Mediawiki.pm
Criterion Covered Total %
statement 102 107 95.3
branch 52 66 78.7
condition 5 15 33.3
subroutine 15 16 93.7
pod 2 2 100.0
total 176 206 85.4


line stmt bran cond sub pod time code
1             package Pod::Simple::Wiki::Mediawiki;
2              
3             ###############################################################################
4             #
5             # Pod::Simple::Wiki::Mediawiki - A class for creating Pod to MediaWiki filters.
6             #
7             #
8             # Copyright 2003-2012, John McNamara, jmcnamara@cpan.org
9             #
10             # Documentation after __END__
11             #
12              
13             # perltidy with the following options: -mbl=2 -pt=0 -nola
14              
15 9     9   50 use Pod::Simple::Wiki;
  9         17  
  9         309  
16 9     9   48 use strict;
  9         19  
  9         239  
17 9     9   46 use vars qw(@ISA $VERSION);
  9         16  
  9         14006  
18              
19              
20             @ISA = qw(Pod::Simple::Wiki);
21             $VERSION = '0.19';
22              
23              
24             ###############################################################################
25             #
26             # The tag to wiki mappings.
27             #
28             my $tags = {
29             '' => "'''",
30             '' => "'''",
31             '' => "''",
32             '' => "''",
33             '' => '',
34             '' => '',
35             '
'  => "\n", 
36             '' => "\n\n",
37              
38             '

' => '==',

39             '' => "==\n",
40             '

' => '===',

41             '' => "===\n",
42             '

' => '====',

43             '' => "====\n",
44             '

' => '=====',

45             '' => "=====\n",
46             };
47              
48              
49             ###############################################################################
50             #
51             # The default module options
52             #
53             my $default_opts = {
54             transformer_lists => 0,
55             link_prefix => 0,
56             sentence_case_headers => 0,
57             remove_name_section => 0,
58             };
59              
60              
61             ###############################################################################
62             #
63             # new()
64             #
65             # Simple constructor inheriting from Pod::Simple::Wiki.
66             #
67             sub new {
68              
69 38     38 1 73 my $class = shift;
70 38         70 my $opts = {};
71              
72 38 100       123 if ( ref $_[-1] eq 'HASH' ) {
73 7         14 $opts = pop @_;
74              
75             # Merge custom tags with the default tags, if passed.
76             $opts->{tags} = {
77             %$tags,
78             %{
79 7         36 exists $opts->{custom_tags}
80             ? delete $opts->{custom_tags}
81             : {}
82 7 100       68 }
83             };
84             }
85             else {
86 31         82 $opts->{tags} = $tags;
87             }
88              
89 38         266 $opts = { %$default_opts, %$opts };
90              
91 38         215 my $self = Pod::Simple::Wiki->new( 'wiki', @_ );
92 38         177 $self->{_tags} = $opts->{tags};
93 38         81 $self->{_transformer_lists} = $opts->{transformer_lists};
94 38         119 $self->{_link_prefix} = $opts->{link_prefix};
95 38         76 $self->{_sentence_case_headers} = $opts->{sentence_case_headers};
96 38         62 $self->{_remove_name_section} = $opts->{remove_name_section};
97              
98 38         72 bless $self, $class;
99              
100 38         183 $self->accept_targets( 'mediawiki' );
101 38         816 $self->nbsp_for_S( 1 );
102              
103 38         416 return $self;
104             }
105              
106              
107             ###############################################################################
108             #
109             # _append()
110             #
111             # Appends some text to the buffered Wiki text.
112             #
113             sub _append {
114              
115 190     190   252 my $self = shift;
116              
117 190 100       478 if ( $self->{_indent_text} ) {
118 17         28 $self->{_wiki_text} .= $self->{_indent_text};
119 17         29 $self->{_indent_text} = '';
120             }
121              
122 190         652 $self->{_wiki_text} .= $_[0];
123             }
124              
125              
126             ###############################################################################
127             #
128             # _indent_item()
129             #
130             # Indents an "over-item" to the correct level.
131             #
132             sub _indent_item {
133              
134 51     51   69 my $self = shift;
135 51         78 my $item_type = $_[0];
136 51         63 my $item_param = $_[1];
137 51         74 my $indent_level = $self->{_item_indent};
138              
139 51 100       155 if ( $item_type eq 'bullet' ) {
    100          
    50          
140 16         57 $self->_append( '*' x $indent_level . ' ' );
141             }
142             elsif ( $item_type eq 'number' ) {
143 16         50 $self->_append( '#' x $indent_level . ' ' );
144             }
145             elsif ( $item_type eq 'text' ) {
146 19         70 $self->_append( ':' x ( $indent_level - 1 ) . '; ' );
147             }
148             }
149              
150              
151             ###############################################################################
152             #
153             # Functions to deal with links.
154              
155             sub _start_L {
156              
157 8     8   14 my ( $self, $attr ) = @_;
158              
159 8 50       31 if ( !$self->_skip_headings ) {
160 8         24 $self->_append( '' ); # In case we have _indent_text pending
161             # Flush the text buffer, so it will contain only the link text
162 8         28 $self->_output;
163 8         24 $self->{_link_attr} = $attr; # Save for later
164             }
165             }
166              
167             sub _end_L {
168              
169 8     8   10 my $self = $_[0];
170              
171 8         61 my $attr = delete $self->{_link_attr};
172              
173 8 50 33     56 if ( $attr and my $method = $self->can( '_format_link' ) ) {
174 8         18 $self->{_wiki_text} = $method->( $self, $self->{_wiki_text}, $attr );
175             }
176             }
177              
178              
179             ###############################################################################
180             #
181             # _format_link
182              
183             sub _format_link {
184              
185 8     8   16 my ( $self, $text, $attr ) = @_;
186              
187 8 100       20 if ( $attr->{type} eq 'url' ) {
188 3         6 my $link = $attr->{to};
189              
190 3 100       12 return $link if $attr->{'content-implicit'};
191 2         7 return "[$link $text]";
192             }
193              
194             # Manpage:
195 5 50       15 if ( $attr->{type} eq 'man' ) {
196              
197             # FIXME link to http://www.linuxmanpages.com?
198 0 0       0 return "$text" if $attr->{'content-implicit'};
199 0         0 return "$text ($attr->{to})";
200             }
201              
202 5 50       13 die "Unknown link type $attr->{type}" unless $attr->{type} eq 'pod';
203              
204             # Handle a link within this page:
205 5 100       17 return "[[#$attr->{section}|$text]]" unless defined $attr->{to};
206              
207             # Handle a link to a specific section in another page:
208 4 100       13 if ( defined $attr->{section} ) {
209             return $self->{_link_prefix}
210 2 100       12 ? "[$self->{_link_prefix}$attr->{to}#$attr->{section} $text]"
211             : "[[$attr->{to}#$attr->{section}|$text]]";
212             }
213              
214 2 50       7 if ( $attr->{'content-implicit'} ) {
215             return $self->{_link_prefix}
216 2 100       14 ? "[$self->{_link_prefix}$attr->{to} $attr->{to}]"
217             : "[[$attr->{to}]]";
218             }
219              
220 0         0 return "[[$attr->{to}|$text]]";
221             }
222              
223              
224             ###############################################################################
225             #
226             # _handle_text()
227             #
228             # Perform any necessary transforms on the text. This is mainly used to escape
229             # inadvertent CamelCase words.
230             #
231             sub _handle_text {
232              
233 97     97   750 my $self = shift;
234 97         150 my $text = $_[0];
235              
236 97 100       234 if ( $self->{_sentence_case_headers} ) {
237 2 50       5 if ( $self->{_in_head1} ) {
238 2         5 $text = ucfirst( lc( $text ) );
239             }
240             }
241              
242 97 50       228 if ( !$self->{_in_Data} ) {
243              
244             # Escape colons in definition lists:
245 97 100       210 if ( $self->{_in_item_text} ) {
246 19         41 $text =~ s/:/:/g; # A colon would end the item
247             }
248              
249             # Escape empty lines in verbatim sections:
250 97 100       196 if ( $self->{_in_Verbatim} ) {
251 1         3 $text =~ s/^$/ /mg; # An empty line would split the section
252             }
253              
254 97         158 $text =~ s/\xA0/ /g; # Convert non-breaking spaces to entities
255              
256 97         136 $text =~ s/''/''/g; # It's not a formatting code
257              
258 97         145 $text =~ s/\xA9/©/g; # Convert copyright symbols to entities
259             }
260              
261 97         200 $self->_append( $text );
262             }
263              
264              
265             ###############################################################################
266             #
267             # Functions to deal with =over ... =back regions for
268             #
269             # Bulleted lists
270             # Numbered lists
271             # Text lists
272             # Block lists
273             #
274       19     sub _end_item_text { } # _start_Para will insert the :
275              
276              
277             ###############################################################################
278             #
279             # _start_Para()
280             #
281             # Special handling for paragraphs that are part of an "over" block.
282             #
283             sub _start_Para {
284              
285 34     34   49 my $self = shift;
286 34         61 my $indent_level = $self->{_item_indent};
287              
288 34 100       89 if ( $self->{_in_over_block} ) {
289 1         6 $self->{_indent_text} = ( ':' x $indent_level );
290             }
291              
292 34 100       86 if ( $self->{_in_over_text} ) {
293 16         40 $self->{_indent_text} = "\n" . ( ':' x $indent_level );
294             }
295              
296 34 100       136 if ( $self->{_transformer_lists} ) {
297 2 50 33     8 if ( $self->{_in_over_bullet} || $self->{_in_over_number} ) {
298 2 50       5 if ( $self->{output_string} ) {
299 2         3 chomp( ${ $self->{output_string} } );
  2         6  
300             }
301 2         6 $self->{_indent_text} = "

";

302             }
303             }
304             }
305              
306              
307             ###############################################################################
308             #
309             # _end_Para()
310             #
311             # Special handling for paragraphs that are part of an "over_text" block.
312             #
313             sub _end_Para {
314              
315 34     34   52 my $self = shift;
316              
317             # Only add a newline if the paragraph isn't part of a text
318 34 100 0     128 if ( $self->{_in_over_text} ) {
    50 33        
319              
320             # Do nothing in this format.
321             }
322             elsif ( $self->{_transformer_lists}
323             && ( $self->{_in_over_bullet} || $self->{_in_over_number} ) )
324             {
325              
326 0         0 $self->_output( "

\n" );
327             }
328             else {
329 18         67 $self->_output( "\n" );
330             }
331              
332 34 100 66     222 if ( !$self->{_transformer_lists}
333             || ( !$self->{_in_over_bullet} && !$self->{_in_over_number} ) )
334             {
335              
336 32         97 $self->_output( "\n" );
337             }
338             }
339              
340              
341             ######################################################################
342             #
343             # _end_Data
344             #
345             # Special handling for data paragraphs
346              
347 0     0   0 sub _end_Data { $_[0]->_output( "\n\n" ) }
348              
349              
350             ###############################################################################
351             #
352             # parse_string_document()
353             #
354             # Optional overriding of Pod::Simple method to remove the "NAME" section
355             #
356             sub parse_string_document {
357              
358 38     38 1 29133 my $self = shift;
359              
360 38         189 $self = $self->SUPER::parse_string_document( @_ );
361              
362 38 100       535 if ( $self->{_remove_name_section} ) {
363 9     9   57 no warnings 'uninitialized';
  9         17  
  9         1521  
364 1         2 ${ $self->{output_string} } =~
  1         8  
365 1 50       6 s/^==\s*NAME\s*==\n(?:[\w:]+)(?: - (.*))*/$1||''/iesg;
366             }
367              
368 38         88 return $self;
369             }
370              
371             1;
372              
373              
374             __END__