blib/lib/Text/KwikiFormatish.pm

filter: Statementstmt 91.0% 254 / 279 Branchbran 69.1% 47 / 68 Conditioncond 85.7% 6 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.1% 59 / 62 Podpod 100% 48 / 48 totaltotal 88.8% 416 / 468 CC 49 SCAR 13.6
CC
49
Cov
87%
_parse_table
13.7
format_table
9.5
lists
9.2
CRAP 3.9
↑ Summary
1 package Text::KwikiFormatish;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our $VERSION = '1.11';
6  
7 6 ▶use CGI::Util qw(escape unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Text::KwikiFormatish - convert Kwikitext into XML-compliant HTML
12  
13 =head1 SYNOPSIS
14  
15 use Text::KwikiFormatish;
16 my $xhtml = Text::KwikiFormatish::format($text);
17  
18 =head1 DESCRIPTION
19  
20 B<NOTE:> I<This> module is based off of the old L<CGI::Kwiki> formatter. Ideally, L<Text::KwikiFormat> would be written to use the new the new L<Kwiki> formatter.
21  
22 L<CGI::Kwiki> includes a formatter (L<CGI::Kwiki::Formatter>) for converting
23 Kwikitext (a nice form of wikitext) to HTML. Unfortunately, it isn't easy to
24 use the formatter outside the L<CGI::Kwiki> environment. Additionally, the HTML
25 produced by the formatter isn't XHTML-1 compliant. This module aims to fix both
26 of these issues and provide an interface similar to L<Text::WikiFormat>.
27  
28 Essentially, this module is the code from Brian Ingerson's
29 L<CGI::Kwiki::Formatter> with a C<format> subroutine, code relating to slides
30 removed, tweaked subroutinesa, and more.
31  
32 Since the wikitext spec for input wikitext for this module differs a little
33 from the default Kwiki formatter, I thought it best to call it "Formatish"
34 instead of *the* Kwiki Format.
35  
36 =cut
37  
38 6 ▶use vars qw($UPPER $LOWER $ALPHANUM $WORD $WIKIWORD @DEFAULTPROCESSORDER);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 6 $UPPER = '\p{UppercaseLetter}';
41 6 $LOWER = '\p{LowercaseLetter}';
42 6 $ALPHANUM = '\p{Letter}\p{Number}';
43 6 $WORD = '\p{Letter}\p{Number}\p{ConnectorPunctuation}';
44 6 $WIKIWORD = "$UPPER$LOWER\\p{Number}\\p{ConnectorPunctuation}";
45  
46 6 @DEFAULTPROCESSORDER = qw(
47 function
48 header_1 header_2 header_3 header_4 header_5 header_6
49 escape_html
50 horizontal_line comment lists
51 code paragraph
52 named_http_link no_http_link http_link
53 no_mailto_link mailto_link
54 no_wiki_link force_wiki_link wiki_link
55 inline negation
56 bold italic underscore
57 mdash
58 table
59 );
60  
61 =head2 format()
62  
63 C<format()> takes one or two arguments, with the first always being the
64 wikitext to translate. The second is a hash of options, but currently the only
65 option supported is C<prefix> in case you want to prefix wiki links with
66 sommething. For example,
67  
68 my $xml = Text::KwikiFormatish::format(
69 $text,
70 prefix => '/wiki/',
71 );
72  
73 =cut
74  
75 5 ▶sub format {
Subroutine format: called CC=2 SCAR=7.0
Pod documented
76 5 my ( $raw, %args ) = @_;
77  
78 # create instance of formatter
79 5 my $f = __PACKAGE__->new();
80  
81 # translate Text::Wikiformat args to Kwiki formatter args
82 5 ▶ $f->{_node_prefix} = $args{prefix} if exists $args{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"prefix"} 0 5 5
83  
84 # do the deed
85 5 return $f->process($raw);
86 }
87  
88 =head1 EXTENDING
89  
90 L<CGI::Kwiki::Formatter> was designed to be subclassable so that the formatting
91 engine could be easily customized. Information on how the Kwiki formatter works
92 can be found at
93 L<HowKwikiFormatterWorks|http://www.kwiki.org/index.cgi?HowKwikiFormatterWorks>.
94  
95 For example, say you wanted to override the markup for strong (bold) text. You
96 decide that it would make much more sense to write strong text as C<HEYthis is
97 bold textHEY>. You would subclass Text::KwikiFormatish and use it like so:
98  
99 package My::Formatter;
100 use base 'Text::KwikiFormatish';
101
102 # I simply copied this from Text/KwikiFormatish.pm and tweaked it
103 sub bold {
104 my ($self, $text) = @_;
105 $text =~ s#(?<![$WORD])HEY(\S.*?\S|\S)HEY(?![$WORD])#<strong>$1</strong>#g;
106 return $text;
107 }
108
109 package main;
110 my $data = join '', <>;
111 print My::Formatter->new->process( $data );
112  
113 =cut
114  
115 =head2 Administrative Methods
116  
117 =over 4
118  
119 =cut
120  
121 =item process( TEXT )
122  
123 Process the given TEXT as KwikiText and return XHTML.
124  
125 =cut
126  
127 6 ▶sub process {
Subroutine process: called CC=2 SCAR=6.9
Pod documented
128 6 my ( $self, $wiki_text ) = @_;
129 6 my $array = [];
130 6 push @$array, $wiki_text . "\n";
131 6 for my $method ( $self->process_order ) {
132 168 $array = $self->_dispatch( $array, $method );
133 }
134 6 return $self->_combine_chunks($array);
135 }
136  
137 =item process_order()
138  
139 C<process_order()> returns a list of the formatting rules that will be applied
140 when C<format> is called for this object. If called with a set of formatting
141 rules (names of class methods), that set of formatting rules will supercede the
142 default set.
143  
144 =cut
145  
146 12 ▶sub process_order {
Subroutine process_order: called CC=2 SCAR=6.9
Pod documented
147 12 my $self = shift;
148 12 ▶ @{ $self->{'process_order'} } = @_ if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 6 12
149 12 return ( @{ $self->{'process_order'} } );
150 }
151  
152 168 ▶sub _dispatch {
Subroutine _dispatch: called CC=5 SCAR=16.4
153 168 my ( $self, $old_array, $method ) = @_;
154 168 ▶ return $old_array unless $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 0 168 168
155 168 my $new_array;
156 168 for my $chunk (@$old_array) {
157 977 ▶ if ( ref $chunk eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chunk eq 'ARRAY') { } 0 977 977
158 0 push @$new_array, $self->_dispatch( $chunk, $method );
159 }
160 else {
161 977 ▶ if ( ref $chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chunk) { } 545 432 977
162 545 push @$new_array, $chunk;
163 }
164 else {
165 432 push @$new_array, $self->$method($chunk);
166 }
167 }
168 }
169 168 return $new_array;
170 }
171  
172 6 ▶sub _combine_chunks {
Subroutine _combine_chunks: called CC=4 SCAR=13.9
173 6 my ( $self, $chunk_array ) = @_;
174 6 my $formatted_text = '';
175 6 for my $chunk (@$chunk_array) {
176 49 ▶ $formatted_text .=
Branch: 2 branches
BranchTrueFalseTotal
ref $chunk eq 'ARRAY' ? : 0 49 49
ref $chunk ? : 28 21 49
177 ( ref $chunk eq 'ARRAY' ) ? $self->_combine_chunks($chunk)
178 : ( ref $chunk ) ? $$chunk
179 : $chunk;
180 }
181 6 return $formatted_text;
182 }
183  
184 =item * new() - the constructor
185  
186 =cut
187  
188 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
189 6 my ( $class, %args ) = @_;
190 6 my $self = {};
191 6 bless $self, $class;
192 6 my %defs = ( node_prefix => './', );
193 6 my %collated = ( %defs, %args );
194 6 foreach my $k ( keys %defs ) {
195 6 $self->{ "_" . $k } = $collated{$k};
196 }
197 6 $self->process_order(@DEFAULTPROCESSORDER);
198 6 $self->init(%args);
199 6 return $self;
200 }
201  
202 =item * init() - called by the constructor immediately after the objects creation
203  
204 =cut
205  
206 6 ▶sub init { }
Subroutine init: called CC=1 SCAR=0.0
Pod documented
207  
208 =back
209  
210 =cut
211  
212 =head2 Helper Methods
213  
214 =over 4
215  
216 =item * split_method( TEXT, REGEXP, METHOD ) - calls METHOD on any matches in TEXT for groups in REGEXP
217  
218 =cut
219  
220 273 ▶sub split_method {
Subroutine split_method: called CC=2 SCAR=6.9
Pod documented
221 273 my ( $self, $text, $regexp, $method ) = @_;
222 273 my $i = 0;
223 273 ▶ map { $i++ % 2 ? \$self->$method($_) : $_ } split $regexp, $text;
Branch: 1 branch
BranchTrueFalseTotal
$i++ % 2 ? : 27 280 307
224 }
225  
226 =item * escape_html( TEXT ) - returns TEXT with HTML entities escaped
227  
228 =cut
229  
230 22 ▶sub escape_html {
Subroutine escape_html: called CC=1 SCAR=0.0
Pod documented
231 22 my ( $self, $text ) = @_;
232 22 $text =~ s/&/&amp;/g;
233 22 $text =~ s/</&lt;/g;
234 22 $text =~ s/>/&gt;/g;
235 22 $text;
236 }
237  
238 =back
239  
240 =cut
241  
242 =head2 Formatter Methods
243  
244 These are the methods you'll probably override most often. They define the
245 regular expressions that the formatter uses to split text as well as what to do
246 with each chunk.
247  
248 Many of these methods have corrosponding C<format_xxx> methods, which take the
249 chunk and format it as XHTML.
250  
251 =over 4
252  
253 =item * function - user-definable functions
254  
255 =cut
256  
257 6 ▶sub function {
Subroutine function: called CC=1 SCAR=0.0
Pod documented
258 6 my ( $self, $text ) = @_;
259 6 $self->split_method( $text, qr{\[\&(\w+\b.*?)\]}, '_function_format', );
260 }
261  
262 5 ▶sub _function_format {
Subroutine _function_format: called CC=2 SCAR=7.1
263 5 my ( $self, $text ) = @_;
264 5 my ( $method, @args ) = split;
265 5 ▶ $self->_isa_function($method)
Branch: 1 branch
BranchTrueFalseTotal
$self->_isa_function($method) ? : 5 0 5
266 ? $self->$method(@args)
267 : "<!-- Function not supported here: $text -->\n";
268 }
269  
270 5 ▶sub _isa_function {
Subroutine _isa_function: called CC=2 SCAR=7.1
271 5 my ( $self, $function ) = @_;
272 5 ▶ defined { map { ( $_, 1 ) } $self->user_functions }->{$function}
Branch: 1 branch
BranchTrueFalseTotal
defined {map({$_, 1;} $self->user_functions)}->{$function} and $self->can($function) 5 0 5
273 and $self->can($function);
274 }
275  
276 =item * table - tabular data
277  
278 =cut
279  
280 20 ▶sub table {
Subroutine table: called CC=3 SCAR=11.0
Pod documented
281 20 my ( $self, $text ) = @_;
282 20 my @array;
283 20 ▶ while ( $text =~ /(.*?)(^\|[^\n]*\|\n.*)/ms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(.*?)(^\|[^\n]*\|\n.*)/ms) 1 20 21
284 1 push @array, $1;
285 1 my $table;
286 1 ( $table, $text ) = $self->_parse_table($2);
287 1 push @array, $table;
288 }
289 20 ▶ push @array, $text if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 20 0 20
290 20 return @array;
291 }
292  
293 1 ▶sub _parse_table {
Subroutine _parse_table: called CC=7 SCAR=26.2
294 1 my ( $self, $text ) = @_;
295 1 my $error = '';
296 1 my $rows;
297 1 ▶ while ( $text =~ s/^(\|(.*)\|\n)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^(\|(.*)\|\n)//) 1 1 2
298 1 $error .= $1;
299 1 my $data = $2;
300 1 my $row = [];
301 1 for my $datum ( split /\|/, $data ) {
302 2 $datum =~ s/^\s*(.*?)\s*$/$1/;
303 2 ▶ if ( $datum =~ s/^<<(\S+)$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($datum =~ s/^<<(\S+)$//) 0 2 2
304 0 my $marker = $1;
305 0 ▶ while ( $text =~ s/^(.*\n)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^(.*\n)//) 0 0 0
306 0 my $line = $1;
307 0 $error .= $line;
308 0 ▶ if ( $line eq "$marker\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "$marker\n") 0 0 0
309 0 $marker = '';
310 0 last;
311 }
312 0 $datum .= $line;
313 }
314 0 ▶ if ( length $marker ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $marker) 0 0 0
315 0 return ( $error, $text );
316 }
317 }
318 2 push @$row, $datum;
319 }
320 1 push @$rows, $row;
321 }
322 1 return ( $self->format_table($rows), $text );
323 }
324  
325 =item * format_table - format the table data as XHTML
326  
327 =cut
328  
329 1 ▶sub format_table {
Subroutine format_table: called CC=9 SCAR=22.5
Pod documented
330 1 my ( $self, $rows ) = @_;
331 1 my $cols = 0;
332 1 for (@$rows) {
333 1 ▶ $cols = @$_ if @$_ > $cols;
Branch: 1 branch
BranchTrueFalseTotal
if @$_ > $cols 1 0 1
334 }
335 1 my $table = qq{<table border="1">\n};
336 1 for my $row (@$rows) {
337 1 $table .= qq{<tr valign="top">\n};
338 1 ▶ for ( my $i = 0; $i < @$row; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$row) 2 1 3
339 2 my $colspan = '';
340 2 ▶ if ( $i == $#{$row} and $cols - $i > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#{$row;} and $cols - $i > 1) 0 2 2
Condition66%2/3
$i == $#{$row;} and $cols - $i > 1
!ll&&!rl&&r
110
MC/DC0%0/2
$i == $#{$row;} and $cols - $i > 1
$i == $#{$row;} $cols - $i > 1
Truth table66%2/3
A and B
A= $i == $#{$row;}B= $cols - $i > 1
ABresult
0X0
100
111
341 0 $colspan = ' colspan="' . ( $cols - $i ) . '"';
342 }
343 2 my $cell = $self->escape_html( $row->[$i] );
344 2 ▶ $cell = qq{<pre>$cell</pre>\n}
Branch: 1 branch
BranchTrueFalseTotal
if $cell =~ /\n/ 0 2 2
345 if $cell =~ /\n/;
346 2 ▶ $cell = '&nbsp;' unless length $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cell 0 2 2
347 2 $table .= qq{<td$colspan>$cell</td>\n};
348 }
349 1 $table .= qq{</tr>\n};
350 }
351 1 $table .= qq{</table>\n};
352 1 return \$table;
353 }
354  
355 =item * no_wiki_link - things that look like wikilinks but are forced not to be
356  
357 =cut
358  
359 18 ▶sub no_wiki_link {
Subroutine no_wiki_link: called CC=1 SCAR=0.0
Pod documented
360 18 my ( $self, $text ) = @_;
361 18 $self->split_method( $text,
362 qr{!([$UPPER](?=[$WORD]*[$UPPER])(?=[$WORD]*[$LOWER])[$WORD]+)},
363 'no_wiki_link_format', );
364 }
365  
366 =item * no_wiki_link_format - typically just the text that could have been a link
367  
368 =cut
369  
370 1 ▶sub no_wiki_link_format {
Subroutine no_wiki_link_format: called CC=1 SCAR=0.0
Pod documented
371 1 my ( $self, $text ) = @_;
372 1 return $text;
373 }
374  
375 =item * wiki_link - a WikiLink
376  
377 =cut
378  
379 19 ▶sub wiki_link {
Subroutine wiki_link: called CC=1 SCAR=0.0
Pod documented
380 19 my ( $self, $text ) = @_;
381 19 $self->split_method( $text,
382 qr{([$UPPER](?=[$WORD]*[$UPPER])(?=[$WORD]*[$LOWER])[$WORD]+)},
383 'wiki_link_format', );
384 }
385  
386 =item * force_wiki_link - a link that normally wouldn't have been one but is forced to be
387  
388 =cut
389  
390 19 ▶sub force_wiki_link {
Subroutine force_wiki_link: called CC=1 SCAR=0.0
Pod documented
391 19 my ( $self, $text ) = @_;
392 19 $self->split_method( $text, qr{(?<!\!)\[([$ALPHANUM\-:]+)\]},
393 'wiki_link_format', );
394 }
395  
396 =item * wiki_link_format - how to format wikilinks as XHTML
397  
398 =cut
399  
400 1 ▶sub wiki_link_format {
Subroutine wiki_link_format: called CC=1 SCAR=0.0
Pod documented
401 1 my ( $self, $text ) = @_;
402 1 my $url = $self->escape($text);
403 1 my $wiki_link = qq{<a href="./$url">$text</a>};
404 1 return $wiki_link;
405 }
406  
407 =item * no_http_link - what normally would have been an HTTP URI, but isn't
408  
409 =cut
410  
411 15 ▶sub no_http_link {
Subroutine no_http_link: called CC=1 SCAR=0.0
Pod documented
412 15 my ( $self, $text ) = @_;
413 15 $self->split_method( $text, qr{(!(?:https?|ftp|irc):\S+?)}m,
414 'no_http_link_format', );
415 }
416  
417 =item * no_http_link_format - typically just the text
418  
419 =cut
420  
421 0 ▶sub no_http_link_format {
Subroutine no_http_link_format: not called CC=1 SCAR=6.9
Pod documented
422 0 my ( $self, $text ) = @_;
423 0 $text =~ s#!##;
424 0 return $text;
425 }
426  
427 =item * http_link - a regular http:// hyperlink
428  
429 =cut
430  
431 15 ▶sub http_link {
Subroutine http_link: called CC=1 SCAR=0.0
Pod documented
432 15 my ( $self, $text ) = @_;
433 15 $self->split_method( $text,
434 qr{((?:https?|ftp|irc):\S+?(?=[),.:;]?\s|$))}m,
435 'http_link_format', );
436 }
437  
438 =item * http_link_format - how to format the given link
439  
440 =cut
441  
442 2 ▶sub http_link_format {
Subroutine http_link_format: called CC=2 SCAR=6.9
Pod documented
443 2 my ( $self, $text ) = @_;
444 2 ▶ if ( $text =~ /^http.*\.(?i:jpg|gif|jpeg|png)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^http.*\.(?i:jpg|gif|jpeg|png)$/) { } 1 1 2
445 1 return $self->img_format($text);
446 }
447 else {
448 1 return $self->link_format($text);
449 }
450 }
451  
452 =item * no_mailto_link - what could have been a mailto: hyperlink
453  
454 =cut
455  
456 17 ▶sub no_mailto_link {
Subroutine no_mailto_link: called CC=1 SCAR=0.0
Pod documented
457 17 my ( $self, $text ) = @_;
458 17 $self->split_method( $text,
459 qr{(![$ALPHANUM][$WORD\-\.]*@[$WORD][$WORD\-\.]+)}m,
460 'no_mailto_link_format', );
461 }
462  
463 =item * no_mailto_link_format - typically just text
464  
465 =cut
466  
467 0 ▶sub no_mailto_link_format {
Subroutine no_mailto_link_format: not called CC=1 SCAR=6.9
Pod documented
468 0 my ( $self, $text ) = @_;
469 0 $text =~ s#!##;
470 0 return $text;
471 }
472  
473 =item * mailto_link - a mailto: hyperlink
474  
475 =cut
476  
477 17 ▶sub mailto_link {
Subroutine mailto_link: called CC=1 SCAR=0.0
Pod documented
478 17 my ( $self, $text ) = @_;
479 17 $self->split_method( $text,
480 qr{([$ALPHANUM][$WORD\-\.]*@[$WORD][$WORD\-\.]+)}m,
481 'mailto_link_format', );
482 }
483  
484 =item * mailto_link_format - how to format the mailto: link
485  
486 =cut
487  
488 1 ▶sub mailto_link_format {
Subroutine mailto_link_format: called CC=2 SCAR=7.1
Pod documented
489 1 my ( $self, $text ) = @_;
490 1 ▶ my $dot = ( $text =~ s/\.$// ) ? '.' : '';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/\.$// ? : 0 1 1
491 1 qq{<a href="mailto:$text">$text</a>$dot};
492 }
493  
494 =item * img_format - inline images
495  
496 =cut
497  
498 1 ▶sub img_format {
Subroutine img_format: called CC=1 SCAR=0.0
Pod documented
499 1 my ( $self, $url ) = @_;
500 1 return qq{<img src="$url" />};
501 }
502  
503 =item * link_format - a helper method for named_http_link_format and http_link_format
504  
505 =cut
506  
507 2 ▶sub link_format {
Subroutine link_format: called CC=2 SCAR=6.9
Pod documented
508 2 my ( $self, $text ) = @_;
509 2 $text =~ s/(^\s*|\s+(?=\s)|\s$)//g;
510 2 my $url = $text;
511 2 ▶ $url = $1 if $text =~ s/(.*?) +//;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ s/(.*?) +// 1 1 2
512 2 $url =~ s/https?:(?!\/\/)//;
513 2 return qq{<a href="$url">$text</a>};
514 }
515  
516 =item * named_http_link - an HTTP URI with a label
517  
518 =cut
519  
520 21 ▶sub named_http_link {
Subroutine named_http_link: called CC=1 SCAR=0.0
Pod documented
521 21 my ( $self, $text ) = @_;
522 21 $self->split_method( $text,
523 qr{(?<!\!)\[([^\[\]]*?(?:https?|ftp|irc):\S.*?)\]},
524 'named_http_link_format', );
525 }
526  
527 =item * named_http_link_format - how to format the named link
528  
529 =cut
530  
531 1 ▶sub named_http_link_format {
Subroutine named_http_link_format: called CC=2 SCAR=7.0
Pod documented
532 1 my ( $self, $text ) = @_;
533 1 ▶ if ( $text =~ m#(.*)((?:https?|ftp|irc):.*)# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(.*)((?:https?|ftp|irc):.*)/) 1 0 1
534 1 $text = "$2 $1";
535 }
536 1 return $self->link_format($text);
537 }
538  
539 =item * inline - code samples or fixed-width font, usually
540  
541 =cut
542  
543 20 ▶sub inline {
Subroutine inline: called CC=1 SCAR=0.0
Pod documented
544 20 my ( $self, $text ) = @_;
545 20 $self->split_method( $text, qr{(?<!\!)\[=(.*?)(?<!\\)\]}, 'inline_format',
546 );
547 }
548  
549 =item * inline_format - how to format inline markup
550  
551 =cut
552  
553 0 ▶sub inline_format {
Subroutine inline_format: not called CC=1 SCAR=6.9
Pod documented
554 0 my ( $self, $text ) = @_;
555 0 "<code>$text</code>";
556 }
557  
558 =item * negation - when not to make an inline format
559  
560 =cut
561  
562 20 ▶sub negation {
Subroutine negation: called CC=1 SCAR=0.0
Pod documented
563 20 my ( $self, $text ) = @_;
564 20 $text =~ s#\!(?=\[)##g;
565 20 return $text;
566 }
567  
568 =item * bold - strong text
569  
570 =cut
571  
572 19 ▶sub bold {
Subroutine bold: called CC=1 SCAR=0.0
Pod documented
573 19 my ( $self, $text ) = @_;
574 19 $text =~ s#(?<![$WORD])\*(\S.*?\S|\S)\*(?![$WORD])#<strong>$1</strong>#g;
575 19 return $text;
576 }
577  
578 =item * italic - emphasized text
579  
580 =cut
581  
582 20 ▶sub italic {
Subroutine italic: called CC=1 SCAR=0.0
Pod documented
583 20 my ( $self, $text ) = @_;
584 20 $text =~ s#(?<![$WORD<])//(\S.*?\S|\S)//(?![$WORD])#<em>$1</em>#g;
585 20 return $text;
586 }
587  
588 =item * underscore - if you reall, really, really feel the need to use underlined text
589  
590 =cut
591  
592 20 ▶sub underscore {
Subroutine underscore: called CC=1 SCAR=0.0
Pod documented
593 20 my ( $self, $text ) = @_;
594 20 $text =~ s#(?<![$WORD])_(\S.*?\S)_(?![$WORD])#<u>$1</u>#g;
595 20 return $text;
596 }
597  
598 =item * code - usually indented text creates blocks of preformatted text
599  
600 =cut
601  
602 15 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod documented
603 15 my ( $self, $text ) = @_;
604 15 $self->split_method( $text, qr{(^ +[^ \n].*?\n)(?-ms:(?=[^ \n]|$))}ms,
605 'code_format', );
606 }
607  
608 =item * code_format - how to format the code blocks
609  
610 =cut
611  
612 3 ▶sub code_format {
Subroutine code_format: called CC=1 SCAR=0.0
Pod documented
613 3 my ( $self, $text ) = @_;
614 3 $self->_code_postformat( $self->_code_preformat($text) );
615 }
616  
617 3 ▶sub _code_preformat {
Subroutine _code_preformat: called CC=1 SCAR=0.0
618 3 my ( $self, $text ) = @_;
619 0 my ($indent) = sort { $a <=> $b } map {length} $text =~ /^( *)\S/mg;
620 3 $text =~ s/^ {$indent}//gm;
621  
622 #return $self->escape_html($text); ## already done in process order
623 3 return $text;
624 }
625  
626 3 ▶sub _code_postformat {
Subroutine _code_postformat: called CC=1 SCAR=0.0
627 3 my ( $self, $text ) = @_;
628 3 return "<pre>$text</pre>\n";
629 }
630  
631 =item * lists - itemized or enumerated lists
632  
633 =cut
634  
635 12 ▶sub lists {
Subroutine lists: called CC=9 SCAR=22.2
Pod documented
636 12 my ( $self, $text ) = @_;
637 12 my $switch = 0;
638 return map {
639 12 my $level = 0;
640 15 my @tag_stack;
641 15 ▶ if ( $switch++ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch++ % 2) 2 13 15
642 2 my $text = '';
643 2 my @lines = /(.*\n)/g;
644 2 for my $line (@lines) {
645 4 $line =~ s/^([0\*]+) //;
646 4 my $new_level = length($1);
647 4 ▶ my $tag = ( $1 =~ /0/ ) ? 'ol' : 'ul';
Branch: 1 branch
BranchTrueFalseTotal
$1 =~ /0/ ? : 1 3 4
648 4 ▶ if ( $new_level > $level ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_level > $level) { } 2 2 4
elsif ($new_level < $level) { } 0 2 2
649 2 for ( 1 .. ( $new_level - $level ) ) {
650 2 push @tag_stack, $tag;
651 2 $text .= "<$tag>\n";
652 }
653 2 $level = $new_level;
654 }
655 elsif ( $new_level < $level ) {
656 0 for ( 1 .. ( $level - $new_level ) ) {
657 0 $tag = pop @tag_stack;
658 0 $text .= "</$tag>\n";
659 }
660 0 $level = $new_level;
661 }
662 4 $text .= "<li>$line</li>";
663 }
664 2 for ( 1 .. $level ) {
665 2 my $tag = pop @tag_stack;
666 2 $text .= "</$tag>\n";
667 }
668 2 $_ = $self->lists_format($text);
669 }
670 15 $_;
671 }
672 split m!(^[0\*]+ .*?\n)(?=(?:[^0\*]|$))!ms, $text;
673 }
674  
675 =item * lists_format - how to format the lists
676  
677 =cut
678  
679 2 ▶sub lists_format {
Subroutine lists_format: called CC=1 SCAR=0.0
Pod documented
680 2 my ( $self, $text ) = @_;
681 2 return $text;
682 }
683  
684 =item * paragraph - normal, boring paragraphs
685  
686 =cut
687  
688 17 ▶sub paragraph {
Subroutine paragraph: called CC=2 SCAR=6.9
Pod documented
689 17 my ( $self, $text ) = @_;
690 17 my $switch = 0;
691 return map {
692 17 ▶ unless ( $switch++ % 2 )
Branch: 1 branch
BranchTrueFalseTotal
unless ($switch++ % 2) 16 5 21
693 {
694 16 $_ = $self->paragraph_format($_);
695 }
696 21 $_;
697 }
698 split m!(\n\s*\n)!ms, $text;
699 }
700  
701 =item * paragraph_format - how to format paragraphs as XHTML
702  
703 =cut
704  
705 16 ▶sub paragraph_format {
Subroutine paragraph_format: called CC=3 SCAR=11.0
Pod documented
706 16 my ( $self, $text ) = @_;
707 16 ▶ return '' if $text =~ /^[\s\n]*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^[\s\n]*$/ 7 9 16
708 9 ▶ return $text if $text =~ /^<(o|u)l>/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^<(o|u)l>/i 2 7 9
709 7 return "<p>\n$text\n</p>\n";
710 }
711  
712 =item * horizontal_line - horizontal rules
713  
714 =cut
715  
716 10 ▶sub horizontal_line {
Subroutine horizontal_line: called CC=1 SCAR=0.0
Pod documented
717 10 my ( $self, $text ) = @_;
718 10 $self->split_method( $text, qr{^(----+)\s*$}m, 'horizontal_line_format',
719 );
720 }
721  
722 =item * horizontal_line_format - horizontal rules as XHTML
723  
724 =cut
725  
726 1 ▶sub horizontal_line_format {
Subroutine horizontal_line_format: called CC=1 SCAR=0.0
Pod documented
727 1 my ($self) = @_;
728 1 my $text = "<hr/>\n";
729 1 return $text;
730 }
731  
732 =item * mdash - long horizontal dashes
733  
734 =cut
735  
736 20 ▶sub mdash {
Subroutine mdash: called CC=1 SCAR=0.0
Pod documented
737 20 my ( $self, $text ) = @_;
738 20 $text =~ s/([$WORD])-{3}([$WORD])/$1&#151;$2/g;
739 20 return $text;
740 }
741  
742 =item * comment - text that doesn't show up in the final markup
743  
744 =cut
745  
746 10 ▶sub comment {
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
747 10 my ( $self, $text ) = @_;
748 10 $self->split_method( $text, qr{^\#\#(.*)$}m, 'comment_line_format', );
749 }
750  
751 =item * comment_line_format - make XML comments out of 'em
752  
753 =cut
754  
755 2 ▶sub comment_line_format {
Subroutine comment_line_format: called CC=1 SCAR=0.0
Pod documented
756 2 my ( $self, $text ) = @_;
757 2 return "<!-- $text -->\n";
758 }
759  
760 =item * header_N and header_N_format - where N is a number from 1 to 6, inclusive
761  
762 =cut
763  
764 6 for my $num ( 1 .. 6 ) {
765 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
766 36 ▶ *{"header_$num"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
767 71 my ( $self, $text ) = @_;
768 71 $self->split_method( $text, qr#^={$num} (.*?)(?: =*)?\n#m,
769 "header_${num}_format", );
770 36 };
771 36 ▶ *{"header_${num}_format"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
772 10 my ( $self, $text ) = @_;
773 10 $text =~ s/=+\s*$//;
774 10 $text = $self->escape_html($text);
775 10 return "<h$num>$text</h$num>\n";
776 36 };
777 }
778  
779 =back
780  
781 =cut
782  
783 =head2 Adding User Functions
784  
785 =over 4
786  
787 =cut
788  
789 =item * user_functions() - returns a list of custom markup plugins to handle
790  
791 The default user functions are C<icon>, C<img> and C<glyph>. In the default markup, plugins are entered in the form of C<[&name arg1 arg2 ...]>.
792  
793 =cut
794  
795 5 ▶sub user_functions {
Subroutine user_functions: called CC=1 SCAR=0.0
Pod documented
796 5 qw(
797 icon
798 img
799 glyph
800 );
801 }
802  
803 =item * icon - inserts the named image with a CSS class of "icon"
804  
805 [&icon /icons/fun.png]
806  
807 =cut
808  
809 1 ▶sub icon {
Subroutine icon: called CC=1 SCAR=0.0
Pod documented
810 1 my ( $self, $href ) = @_;
811 1 return qq( <img src="$href" class="icon" alt="(icon)" /> );
812 }
813  
814 =item * img - inserts a regular image, with an optional title
815  
816 [&img some_image.jpg]
817  
818 [&img another_image.jpg This image will have a title attribute]
819  
820 =cut
821  
822 2 ▶sub img {
Subroutine img: called CC=3 SCAR=11.0
Pod documented
823 2 my ( $self, $href, @title ) = @_;
824 2 ▶ my $title = join( ' ', @title ) || '';
Condition100%2/2
join(' ', @title) || ''
l!l
11
MC/DC100%1/1
join(' ', @title) || ''
join(' ', @title)
Truth table100%2/2
A
A= join(' ', @title)
Aresult
11
00
825 2 my $output = qq( <p style="text-align:center;"><img
826 src="$href" alt="(see caption below)" title="$title"
827 align="middle" border="0" /> );
828 2 ▶ $output .= @title ? "<br/><small>$title</small>" : '';
Branch: 1 branch
BranchTrueFalseTotal
@title ? : 1 1 2
829 2 return $output . '</p>';
830 }
831  
832 =item * glyph - attempts to insert an image that's aligned with the vertical middle of the text, but doesn't work due to the implementation of the parser
833  
834 =cut
835  
836 2 ▶sub glyph {
Subroutine glyph: called CC=2 SCAR=6.9
Pod documented
837  
838 # FIXME - BROKEN! Plugins like to separate the paragraphs
839 2 my ( $self, $href, @title ) = @_;
840 2 ▶ my $title = join( ' ', @title ) || '*';
Condition100%2/2
join(' ', @title) || '*'
l!l
11
MC/DC100%1/1
join(' ', @title) || '*'
join(' ', @title)
Truth table100%2/2
A
A= join(' ', @title)
Aresult
11
00
841 2 return qq( <img
842 src="$href"
843 alt="$title" title="$title"
844 align="middle" border="0"
845 /> );
846 }
847  
848 =back
849  
850 =cut
851  
852 =head1 DIFFERENCES FROM THE CGI::KWIKI FORMATTER
853  
854 =over 4
855  
856 =item * The output of the formatter is XML-compliant.
857  
858 =item * Extra equal signs at the end of headings will be removed from the output for compatibility with other wikitext formats.
859  
860 =item * Italicized text is marked up by two slashes instead of one. This is to prevent weirdness when writing filesystem paths in Kwikitext -- e.g., the text "Check /etc or /var or /usr/" will have unexpected results when formatted in a regular Kwiki.
861  
862 =item * Horizontal rules, marked by four or more hyphens, may be followed by spaces.
863  
864 =item * Processing order of text segments has been changed (tables are processed last)
865  
866 =item * Bold text is marked up as C<E<lt>strongE<gt>> instead of C<E<lt>bE<gt>>
867  
868 =item * "Inline" is marked up as C<E<lt>codeE<gt>> instead of C<E<lt>ttE<gt>>
869  
870 =item * mdashes (really long hyphens) are created with wikitext C<like---this>
871  
872 =item * Tables and code sections are not indented with C<E<lt>blockquoteE<gt>> tags
873  
874 =item * Comments do not have to have a space immediately following the hash
875  
876 =item * Patch to named_link code
877  
878 =item * All code pertaining to slides or Kwiki access control is removed, as neither are within the scope of this module
879  
880 =back
881  
882 =head2 Plugins
883  
884 I've included two plugins, C<img> and C<icon>, to do basic image support besides the standard operation of including an image when the URL ends with a common image extension.
885  
886 =cut
887  
888 =head1 EXAMPLES
889  
890 Here's some kwiki text. (Compare with
891 L<KwikiFormattingRules|http://www.kwiki.org/index.cgi?KwikiFormattingRules>.)
892  
893 = Level 1 Header
894
895 == Level 2 with optional trailing equals ==
896
897 Kwikitext provides a bit more flexibility than regular wikitext.
898
899 All HTML code is <escaped>. Horizontal rules are four or more hyphens:
900
901 ----
902
903 While you can add an mdash---like this.
904
905 ##
906 ## you can add comments in the kwikitext which appear as XML comments
907 ##
908
909 == Links
910
911 === Itemized Lists
912
913 * Fruit
914 ** Oranges
915 ** Apples
916 * Eggs
917 * Salad
918
919 === Enumerated Lists
920
921 ##
922 ## below are zero's, not "oh's"
923 ##
924
925 0 One
926 0 Two
927 0 Three
928
929 * Comments in the wikitext
930 * Easier:
931 ** Bold/strong
932 ** Italic/emphasized
933
934 == More Markup
935
936 *strong or bold text*
937
938 //emphasized or italic text//
939
940 indented text is verbatim (good for code)
941
942 == Links
943
944 WikiLink
945
946 !NotAWikiLink
947
948 http://www.kwiki.org/
949
950 [Kwiki named link http://www.kwiki.org/]
951
952 == Images
953
954 http://search.cpan.org/s/img/cpan_banner.png
955
956 == KwikiFormatish plugins
957
958 This inserts an image with the CSS class of "icon" -- good for inserting a right-aligned image for text to wrap around.
959
960 [&icon /images/logo.gif]
961
962 The following inserts an image with an optional caption:
963
964 [&img /images/graph.gif Last Month's Earnings]
965  
966 =head1 AUTHOR
967  
968 Maintained by Ian Langworth - ian@cpan.org
969  
970 Based on L<CGI::Kwiki::Formatter> by L<Brian
971 Ingerson|http://search.cpan.org/~ingy/>.
972  
973 Thanks to L<Jesse Vincent|http://search.cpan.org/~jesse/> for the
974 C<process_order> patch, related documentation and testing.
975  
976 Additional thanks to Mike Burns, Ari Pollak and Ricardo SIGNES for additional testing.
977  
978 =head1 SEE ALSO
979  
980 L<CGI::Kwiki>, L<CGI::Kwiki::Formatter>, L<Text::WikiFormat>
981  
982 =head1 LICENSE
983  
984 This is free software. You may use it and redistribute it under the same terms
985 as perl itself.
986  
987 =cut
988  
989 6 1;
990  
↑ Summary