File Coverage

blib/lib/Pod/Simple/Wiki/Mediawiki.pm
Criterion Covered Total %
statement 103 108 95.3
branch 52 66 78.7
condition 5 15 33.3
subroutine 15 16 93.7
pod 2 2 100.0
total 177 207 85.5


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   40 use Pod::Simple::Wiki;
  9         13  
  9         263  
16 9     9   33 use strict;
  9         12  
  9         297  
17 9     9   37 use vars qw(@ISA $VERSION);
  9         11  
  9         10833  
18              
19              
20             @ISA = qw(Pod::Simple::Wiki);
21             $VERSION = '0.18';
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 57 my $class = shift;
70 38         59 my $opts = {};
71              
72 38 100       108 if ( ref $_[-1] eq 'HASH' ) {
73 7         11 $opts = pop @_;
74              
75             # Merge custom tags with the default tags, if passed.
76 7 100       54 $opts->{tags} = {
77             %$tags,
78             %{
79 7         32 exists $opts->{custom_tags}
80             ? delete $opts->{custom_tags}
81             : {}
82             }
83             };
84             }
85             else {
86 31         71 $opts->{tags} = $tags;
87             }
88              
89 38         175 $opts = { %$default_opts, %$opts };
90              
91 38         180 my $self = Pod::Simple::Wiki->new( 'wiki', @_ );
92 38         173 $self->{_tags} = $opts->{tags};
93 38         68 $self->{_transformer_lists} = $opts->{transformer_lists};
94 38         108 $self->{_link_prefix} = $opts->{link_prefix};
95 38         52 $self->{_sentence_case_headers} = $opts->{sentence_case_headers};
96 38         56 $self->{_remove_name_section} = $opts->{remove_name_section};
97              
98 38         69 bless $self, $class;
99              
100 38         158 $self->accept_targets( 'mediawiki' );
101 38         756 $self->nbsp_for_S( 1 );
102              
103 38         319 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   180 my $self = shift;
116              
117 190 100       350 if ( $self->{_indent_text} ) {
118 17         26 $self->{_wiki_text} .= $self->{_indent_text};
119 17         29 $self->{_indent_text} = '';
120             }
121              
122 190         596 $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   59 my $self = shift;
135 51         55 my $item_type = $_[0];
136 51         47 my $item_param = $_[1];
137 51         71 my $indent_level = $self->{_item_indent};
138              
139 51 100       153 if ( $item_type eq 'bullet' ) {
    100          
    50          
140 16         72 $self->_append( '*' x $indent_level . ' ' );
141             }
142             elsif ( $item_type eq 'number' ) {
143 16         59 $self->_append( '#' x $indent_level . ' ' );
144             }
145             elsif ( $item_type eq 'text' ) {
146 19         75 $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   11 my ( $self, $attr ) = @_;
158              
159 8 50       24 if ( !$self->_skip_headings ) {
160 8         20 $self->_append( '' ); # In case we have _indent_text pending
161             # Flush the text buffer, so it will contain only the link text
162 8         20 $self->_output;
163 8         15 $self->{_link_attr} = $attr; # Save for later
164             }
165             }
166              
167             sub _end_L {
168              
169 8     8   8 my $self = $_[0];
170              
171 8         14 my $attr = delete $self->{_link_attr};
172              
173 8 50 33     42 if ( $attr and my $method = $self->can( '_format_link' ) ) {
174 8         13 $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   12 my ( $self, $text, $attr ) = @_;
186              
187 8 100       19 if ( $attr->{type} eq 'url' ) {
188 3         3 my $link = $attr->{to};
189              
190 3 100       8 return $link if $attr->{'content-implicit'};
191 2         4 return "[$link $text]";
192             }
193              
194             # Manpage:
195 5 50       11 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       15 die "Unknown link type $attr->{type}" unless $attr->{type} eq 'pod';
203              
204             # Handle a link within this page:
205 5 100       52 return "[[#$attr->{section}|$text]]" unless defined $attr->{to};
206              
207             # Handle a link to a specific section in another page:
208 4 100       8 if ( defined $attr->{section} ) {
209 2 100       12 return $self->{_link_prefix}
210             ? "[$self->{_link_prefix}$attr->{to}#$attr->{section} $text]"
211             : "[[$attr->{to}#$attr->{section}|$text]]";
212             }
213              
214 2 50       6 if ( $attr->{'content-implicit'} ) {
215 2 100       11 return $self->{_link_prefix}
216             ? "[$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   712 my $self = shift;
234 97         108 my $text = $_[0];
235              
236 97 100       207 if ( $self->{_sentence_case_headers} ) {
237 2 50       3 if ( $self->{_in_head1} ) {
238 2         4 $text = ucfirst( lc( $text ) );
239             }
240             }
241              
242 97 50       215 if ( !$self->{_in_Data} ) {
243              
244             # Escape colons in definition lists:
245 97 100       223 if ( $self->{_in_item_text} ) {
246 19         38 $text =~ s/:/:/g; # A colon would end the item
247             }
248              
249             # Escape empty lines in verbatim sections:
250 97 100       238 if ( $self->{_in_Verbatim} ) {
251 1         3 $text =~ s/^$/ /mg; # An empty line would split the section
252             }
253              
254 97         178 $text =~ s/\xA0/ /g; # Convert non-breaking spaces to entities
255              
256 97         112 $text =~ s/''/''/g; # It's not a formatting code
257              
258 97         111 $text =~ s/\xA9/©/g; # Convert copyright symbols to entities
259             }
260              
261 97         177 $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     19   33 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   68 my $self = shift;
286 34         51 my $indent_level = $self->{_item_indent};
287              
288 34 100       86 if ( $self->{_in_over_block} ) {
289 1         3 $self->{_indent_text} = ( ':' x $indent_level );
290             }
291              
292 34 100       81 if ( $self->{_in_over_text} ) {
293 16         43 $self->{_indent_text} = "\n" . ( ':' x $indent_level );
294             }
295              
296 34 100       119 if ( $self->{_transformer_lists} ) {
297 2 50 33     7 if ( $self->{_in_over_bullet} || $self->{_in_over_number} ) {
298 2 50       5 if ( $self->{output_string} ) {
299 2         2 chomp( ${ $self->{output_string} } );
  2         5  
300             }
301 2         5 $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   35 my $self = shift;
316              
317             # Only add a newline if the paragraph isn't part of a text
318 34 100 0     115 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         45 $self->_output( "\n" );
330             }
331              
332 34 100 66     145 if ( !$self->{_transformer_lists}
333             || ( !$self->{_in_over_bullet} && !$self->{_in_over_number} ) )
334             {
335              
336 32         70 $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 22186 my $self = shift;
359              
360 38         156 $self = $self->SUPER::parse_string_document( @_ );
361              
362 38 100       448 if ( $self->{_remove_name_section} ) {
363 9     9   54 no warnings 'uninitialized';
  9         13  
  9         1481  
364 1         2 ${ $self->{output_string} } =~
  1         8  
365 1 50       7 s/^==\s*NAME\s*==\n(?:[\w:]+)(?: - (.*))*/$1||''/iesg;
366             }
367              
368 38         67 return $self;
369             }
370              
371             1;
372              
373              
374             __END__