Wiki.pm

filter: Statementstmt 96.8% 123 / 127 Branchbran 74.0% 37 / 50 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 97.8% 46 / 47 Podpod 100% 1 / 1 totaltotal 90.4% 209 / 231 CC 29 SCAR 8.8
CC
29
Cov
90%
new
7.2
_handle_text
4.0
_indent_item
4.0
CRAP 2.4
« Wiki/Tiddlywiki.pm ↑ Summary Wiki/Markdown.pm »
1 package Pod::Simple::Wiki;
2  
3 ###############################################################################
4 #
5 # Pod::Simple::Wiki - A class for creating Pod to Wiki filters.
6 #
7 #
8 # Copyright 2003-2015, 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 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #use Pod::Simple::Debug (5);
18 43 ▶use Pod::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 43 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 43 @ISA = qw(Pod::Simple);
22 43 $VERSION = '0.20';
23  
24  
25 ###############################################################################
26 #
27 # The tag to wiki mappings.
28 #
29 43 my $tags = {
30 '<b>' => "'''",
31 '</b>' => "'''",
32 '<i>' => "''",
33 '</i>' => "''",
34 '<tt>' => '"',
35 '</tt>' => '"',
36 '<pre>' => '',
37 '</pre>' => "\n\n",
38  
39 '<h1>' => "\n----\n'''",
40 '</h1>' => "'''\n\n",
41 '<h2>' => "\n'''''",
42 '</h2>' => "'''''\n\n",
43 '<h3>' => "\n''",
44 '</h3>' => "''\n\n",
45 '<h4>' => "\n",
46 '</h4>' => "\n\n",
47 };
48  
49  
50 ###############################################################################
51 #
52 # new()
53 #
54 # Simple constructor inheriting from Pod::Simple.
55 #
56 584 ▶sub new {
Subroutine new: called CC=7 SCAR=19.7
Pod documented
57  
58 584 my $class = shift;
59 584 ▶ my $format = lc( shift || 'wiki' );
Condition50%1/2
shift() || 'wiki'
l!l
5840
MC/DC0%0/1
shift() || 'wiki'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
60 584 ▶ $format = 'mediawiki' if $format eq 'wikipedia';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "wikipedia" 0 584 584
61 584 ▶ $format = 'moinmoin' if $format eq 'moin';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "moin" 0 584 584
62  
63 584 my $module = "Pod::Simple::Wiki::" . ucfirst $format;
64  
65 # Try to load a sub-module unless the format type is 'wiki' in which
66 # case we use this, the parent, module.
67 584 ▶ if ( $format ne 'wiki' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format ne "wiki") 281 303 584
68 281 eval "require $module";
69 281 ▶ die "Module $module not implemented for wiki format $format\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 281 281
70 281 return $module->new( @_ );
71 }
72  
73 303 my $self = Pod::Simple->new( @_ );
74 303 $self->{_wiki_text} = '';
75 303 $self->{_tags} = $tags;
76 303 ▶ $self->{output_fh} ||= *STDOUT{IO};
Condition50%1/2
$self->{"output_fh"} ||= *STDOUT{"IO"}
l!l
0303
MC/DC0%0/1
$self->{"output_fh"} ||= *STDOUT{"IO"}
$self->{"output_fh"}
Truth table50%1/2
A
A= $self->{"output_fh"}
Aresult
11
00
77 303 $self->{_item_indent} = 0;
78 303 $self->{_debug} = 0;
79  
80 # Set Pod::Simple parser options
81 # - Merge contiguous text RT#60304
82 303 $self->merge_text( 1 );
83  
84 # - Ignore X<> (index entries) RT#60307
85 303 $self->nix_X_codes( 1 );
86  
87 303 bless $self, $class;
88 303 return $self;
89 }
90  
91  
92 ###############################################################################
93 #
94 # _debug()
95 #
96 # Sets the debug flag for some Pod::Simple::Wiki debugging. See also the
97 # Pod::Simple::Debug module.
98 #
99 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
100  
101 0 my $self = shift;
102  
103 0 $self->{_debug} = $_[0];
104 }
105  
106  
107 ###############################################################################
108 #
109 # _append()
110 #
111 # Appends some text to the buffered Wiki text.
112 #
113 644 ▶sub _append {
Subroutine _append: called CC=1 SCAR=0.0
114  
115 644 my $self = shift;
116  
117 644 $self->{_wiki_text} .= $_[0];
118 }
119  
120  
121 ###############################################################################
122 #
123 # _output()
124 #
125 # Appends some text to the buffered Wiki text and then emits it. Also resets
126 # the buffer.
127 #
128 1025 ▶sub _output {
Subroutine _output: called CC=2 SCAR=6.9
129  
130 1025 my $self = shift;
131 1025 my $text = $_[0];
132  
133 1025 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 54 971 1025
134  
135 1025 print { $self->{output_fh} } $self->{_wiki_text}, $text;
136  
137 1025 $self->{_wiki_text} = '';
138 }
139  
140  
141 ###############################################################################
142 #
143 # _indent_item()
144 #
145 # Indents an "over-item" to the correct level.
146 #
147 37 ▶sub _indent_item {
Subroutine _indent_item: called CC=4 SCAR=13.9
148  
149 37 my $self = shift;
150 37 my $item_type = $_[0];
151 37 my $item_param = $_[1];
152 37 my $indent_level = $self->{_item_indent};
153  
154 37 ▶ if ( $item_type eq 'bullet' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($item_type eq 'bullet') { } 12 25 37
elsif ($item_type eq 'number') { } 12 13 25
elsif ($item_type eq 'text') { } 13 0 13
155 12 $self->_append( "*" x $indent_level );
156  
157 # This was the way C2 Wiki used to define a bullet list
158 # $self->_append("\t" x $indent_level . '*');
159 }
160 elsif ( $item_type eq 'number' ) {
161 12 $self->_append( "\t" x $indent_level . $item_param );
162 }
163 elsif ( $item_type eq 'text' ) {
164 13 $self->_append( "\t" x $indent_level );
165 }
166 }
167  
168  
169 ###############################################################################
170 #
171 # _skip_headings()
172 #
173 # Formatting in headings doesn't look great or is ignored in some formats.
174 #
175 170 ▶sub _skip_headings {
Subroutine _skip_headings: called CC=1 SCAR=0.0
176  
177 170 my $self = shift;
178  
179 170 return 0;
180 }
181  
182  
183 ###############################################################################
184 #
185 # _append_tag()
186 #
187 # Add an open or close tag to the current text.
188 #
189 270 ▶sub _append_tag {
Subroutine _append_tag: called CC=1 SCAR=0.0
190  
191 270 my $self = shift;
192 270 my $tag = $_[0];
193  
194 270 $self->_append( $self->{_tags}->{$tag} );
195 }
196  
197  
198 ###############################################################################
199 ###############################################################################
200 #
201 # The methods in the following section are required by Pod::Simple to handle
202 # Pod directives and elements.
203 #
204 # The methods _handle_element_start() _handle_element_end() and _handle_text()
205 # are called by Pod::Simple in response to Pod constructs. We use
206 # _handle_element_start() and _handle_element_end() to generate calls to more
207 # specific methods. This is basically a long-hand version of Pod::Simple::
208 # Methody with the addition of location tracking.
209 #
210  
211  
212 ###############################################################################
213 #
214 # _handle_element_start()
215 #
216 # Call a method to handle the start of a element if one has been defined.
217 # We also set a flag to indicate that we are "in" the element type.
218 #
219 1402 ▶sub _handle_element_start {
Subroutine _handle_element_start: called CC=3 SCAR=11.1
220  
221 1402 my $self = shift;
222 1402 my $element = $_[0];
223  
224 1402 $element =~ tr/-/_/;
225  
226 1402 ▶ if ( $self->{_debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 1402 1402
227 0 print ' ' x $self->{_item_indent}, "<$element>\n";
228 }
229  
230 1402 $self->{ "_in_" . $element }++;
231  
232 1402 ▶ if ( my $method = $self->can( '_start_' . $element ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can('_start_' . $element)) 1099 303 1402
233 1099 $method->( $self, $_[1] );
234 }
235 }
236  
237  
238 ###############################################################################
239 #
240 # _handle_element_end()
241 #
242 # Call a method to handle the end of a element if one has been defined.
243 # We also set a flag to indicate that we are "out" of the element type.
244 #
245 1402 ▶sub _handle_element_end {
Subroutine _handle_element_end: called CC=3 SCAR=11.1
246  
247 1402 my $self = shift;
248 1402 my $element = $_[0];
249  
250 1402 $element =~ tr/-/_/;
251  
252 1402 ▶ if ( my $method = $self->can( '_end_' . $element ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can('_end_' . $element)) 1099 303 1402
253 1099 $method->( $self );
254 }
255  
256 1402 $self->{ "_in_" . $element }--;
257  
258 1402 ▶ if ( $self->{_debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 1402 1402
259 0 print "\n", ' ' x $self->{_item_indent}, "</$element>\n\n";
260 }
261 }
262  
263  
264 ###############################################################################
265 #
266 # _handle_text()
267 #
268 # Perform any necessary transforms on the text. This is mainly used to escape
269 # inadvertent CamelCase words.
270 #
271 60 ▶sub _handle_text {
Subroutine _handle_text: called CC=4 SCAR=13.9
272  
273 60 my $self = shift;
274 60 my $text = $_[0];
275  
276 # Split the text into tokens but maintain the whitespace
277 60 my @tokens = split /(\s+)/, $text;
278  
279 60 for ( @tokens ) {
280 250 ▶ next unless /\S/; # Ignore the whitespace
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 95 155 250
281 155 ▶ next if m[^(ht|f)tp://]; # Ignore URLs
Branch: 1 branch
BranchTrueFalseTotal
if m[^(ht|f)tp://] 0 155 155
282 155 s/([A-Z][a-z]+)(?=[A-Z])/$1''''''/g # Escape with 6 single quotes
283  
284 }
285  
286 # Rejoin the tokens and whitespace.
287 60 $self->{_wiki_text} .= join '', @tokens;
288 }
289  
290  
291 ###############################################################################
292 #
293 # Functions to deal with the I<>, B<> and C<> formatting codes.
294 #
295 44 ▶sub _start_I { $_[0]->_append_tag( '<i>' ) unless $_[0]->_skip_headings() }
Subroutine _start_I: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_skip_headings 44 0 44
296 33 ▶sub _start_B { $_[0]->_append_tag( '<b>' ) unless $_[0]->_skip_headings() }
Subroutine _start_B: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_skip_headings 33 0 33
297 12 ▶sub _start_C { $_[0]->_append_tag( '<tt>' ) unless $_[0]->_skip_headings() }
Subroutine _start_C: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_skip_headings 12 0 12
298 11 ▶sub _start_F { $_[0]->_start_I }
Subroutine _start_F: called CC=1 SCAR=0.0
299  
300 44 ▶sub _end_I { $_[0]->_append_tag( '</i>' ) unless $_[0]->_skip_headings() }
Subroutine _end_I: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_skip_headings 44 0 44
301 33 ▶sub _end_B { $_[0]->_append_tag( '</b>' ) unless $_[0]->_skip_headings() }
Subroutine _end_B: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_skip_headings 33 0 33
302 12 ▶sub _end_C { $_[0]->_append_tag( '</tt>' ) unless $_[0]->_skip_headings() }
Subroutine _end_C: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_skip_headings 12 0 12
303 11 ▶sub _end_F { $_[0]->_end_I }
Subroutine _end_F: called CC=1 SCAR=0.0
304  
305  
306 ###############################################################################
307 #
308 # Functions to deal with the Pod =head directives
309 #
310 15 ▶sub _start_head1 { $_[0]->_append_tag( '<h1>' ) }
Subroutine _start_head1: called CC=1 SCAR=0.0
311 10 ▶sub _start_head2 { $_[0]->_append_tag( '<h2>' ) }
Subroutine _start_head2: called CC=1 SCAR=0.0
312 10 ▶sub _start_head3 { $_[0]->_append_tag( '<h3>' ) }
Subroutine _start_head3: called CC=1 SCAR=0.0
313 10 ▶sub _start_head4 { $_[0]->_append_tag( '<h4>' ) }
Subroutine _start_head4: called CC=1 SCAR=0.0
314  
315 15 ▶sub _end_head1 { $_[0]->_append_tag( '</h1>' ); $_[0]->_output() }
Subroutine _end_head1: called CC=1 SCAR=0.0
316 10 ▶sub _end_head2 { $_[0]->_append_tag( '</h2>' ); $_[0]->_output() }
Subroutine _end_head2: called CC=1 SCAR=0.0
317 10 ▶sub _end_head3 { $_[0]->_append_tag( '</h3>' ); $_[0]->_output() }
Subroutine _end_head3: called CC=1 SCAR=0.0
318 10 ▶sub _end_head4 { $_[0]->_append_tag( '</h4>' ); $_[0]->_output() }
Subroutine _end_head4: called CC=1 SCAR=0.0
319  
320  
321 ###############################################################################
322 #
323 # Functions to deal with verbatim paragraphs. We emit the text "as is" for now.
324 # TODO: escape any Wiki formatting in text such as ''code''.
325 #
326 1 ▶sub _start_Verbatim { $_[0]->_append_tag( '<pre>' ) }
Subroutine _start_Verbatim: called CC=1 SCAR=0.0
327 1 ▶sub _end_Verbatim { $_[0]->_append_tag( '</pre>' ); $_[0]->_output() }
Subroutine _end_Verbatim: called CC=1 SCAR=0.0
328  
329  
330 ###############################################################################
331 #
332 # Functions to deal with =over ... =back regions for
333 #
334 # Bulleted lists
335 # Numbered lists
336 # Text lists
337 # Block lists
338 #
339 70 ▶sub _start_over_bullet { $_[0]->{_item_indent}++ }
Subroutine _start_over_bullet: called CC=1 SCAR=0.0
340 70 ▶sub _start_over_number { $_[0]->{_item_indent}++ }
Subroutine _start_over_number: called CC=1 SCAR=0.0
341 83 ▶sub _start_over_text { $_[0]->{_item_indent}++ }
Subroutine _start_over_text: called CC=1 SCAR=0.0
342  
343 70 ▶sub _end_over_bullet {
Subroutine _end_over_bullet: called CC=2 SCAR=6.9
344 70 $_[0]->{_item_indent}--;
345 70 ▶ $_[0]->_output( "\n" ) unless $_[0]->{_item_indent};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"_item_indent"} 48 22 70
346 }
347  
348 70 ▶sub _end_over_number {
Subroutine _end_over_number: called CC=2 SCAR=6.9
349 70 $_[0]->{_item_indent}--;
350 70 ▶ $_[0]->_output( "\n" ) unless $_[0]->{_item_indent};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"_item_indent"} 34 36 70
351 }
352  
353 83 ▶sub _end_over_text {
Subroutine _end_over_text: called CC=2 SCAR=6.9
354 83 $_[0]->{_item_indent}--;
355 83 ▶ $_[0]->_output( "\n" ) unless $_[0]->{_item_indent};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"_item_indent"} 47 36 83
356 }
357  
358 139 ▶sub _start_item_bullet { $_[0]->_indent_item( 'bullet' ) }
Subroutine _start_item_bullet: called CC=1 SCAR=0.0
359 139 ▶sub _start_item_number { $_[0]->_indent_item( 'number', $_[1]->{number} ) }
Subroutine _start_item_number: called CC=1 SCAR=0.0
360 154 ▶sub _start_item_text { $_[0]->_indent_item( 'text' ) }
Subroutine _start_item_text: called CC=1 SCAR=0.0
361  
362 139 ▶sub _end_item_bullet { $_[0]->_output( "\n" ) }
Subroutine _end_item_bullet: called CC=1 SCAR=0.0
363 139 ▶sub _end_item_number { $_[0]->_output( "\n" ) }
Subroutine _end_item_number: called CC=1 SCAR=0.0
364  
365 13 ▶sub _end_item_text { $_[0]->_output( ":\t" ) } # Format specific.
Subroutine _end_item_text: called CC=1 SCAR=0.0
366  
367 11 ▶sub _start_over_block { $_[0]->{_item_indent}++ }
Subroutine _start_over_block: called CC=1 SCAR=0.0
368 11 ▶sub _end_over_block { $_[0]->{_item_indent}-- }
Subroutine _end_over_block: called CC=1 SCAR=0.0
369  
370  
371 ###############################################################################
372 #
373 # _start_Para()
374 #
375 # Special handling for paragraphs that are part of an "over" block.
376 #
377 19 ▶sub _start_Para {
Subroutine _start_Para: called CC=2 SCAR=6.9
378  
379 19 my $self = shift;
380 19 my $indent_level = $self->{_item_indent};
381  
382 19 ▶ if ( $self->{_in_over_block} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_in_over_block"}) 1 18 19
383 1 $self->_append( ( "\t" x $indent_level ) . " :\t" );
384 }
385 }
386  
387  
388 ###############################################################################
389 #
390 # _end_Para()
391 #
392 # Special handling for paragraphs that are part of an "over_text" block.
393 #
394 223 ▶sub _end_Para {
Subroutine _end_Para: called CC=2 SCAR=6.9
395  
396 223 my $self = shift;
397  
398 # Only add a newline if the paragraph isn't part of a text
399 223 ▶ if ( $self->{_in_over_text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_in_over_text'}) { } 113 110 223
400  
401 # Do nothing in this format.
402 }
403 else {
404 110 $self->_output( "\n" );
405 }
406  
407 223 $self->_output( "\n" );
408 }
409  
410  
411 43 1;
412  
413  
414 __END__
« Wiki/Tiddlywiki.pm ↑ Summary Wiki/Markdown.pm »