blib/lib/XHTML/MediaWiki.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XHTML::MediaWiki;
5 #XHTML::MediaWiki::
6  
7 =head1 NAME
8  
9 XHTML::MediaWiki - Translate Wiki markup into xhtml
10  
11 =cut
12  
13 our $VERSION = '0.11';
14 $VERSION = eval $VERSION;
15  
16 our $DEBUG = 0;
17  
18 =head1 SYNOPSIS
19  
20 use XHTML::MediaWiki;
21 my $mediawiki = XHTML::MediaWiki->new( link_path => "http://example.com/base/" );
22 my $xhtm = $mediawiki->format($text);
23  
24 =head1 DESCRIPTION
25  
26 L<http://www.mediawiki.org/> and its sister projects use the PHP
27 Mediawiki to format their pages. This module attempts to duplicate the
28 Mediawiki formatting rules. Those formatting rules can be simple and
29 easy to use, while providing more advanced options for the power user.
30  
31 =cut
32  
33 2 ▶use Carp qw(carp confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 0 ▶use CGI qw(:standard);
Subroutine BEGIN: called CC=1 SCAR=2.6
35 use Scalar::Util qw(blessed);
36  
37 use HTML::Parser;
38  
39 =head2 Constructors
40  
41 =over 4
42  
43 =item * new( link_path => 'base path' )
44  
45 Create a new XHTML:;MediaWiki object. C<link_path> is used as the base
46 for hyperlinks.
47  
48 =back
49  
50 =cut
51  
52 sub new
53 {
54 my $class = shift;
55  
56 bless {
57 link_path => '',
58 @_
59 }, $class;
60 }
61  
62 =head2 Methods
63  
64 =over 4
65  
66 =item * format()
67  
68 The format method is the only method that needs to be called for the
69 normal operation of this object. You call format() with the raw I<wikitext> and
70 it returns the xhtml representation of that I<wikitext>.
71  
72 =cut
73  
74 sub format
75 {
76 my $self = shift;
77 my $raw = shift;
78  
79 my $cooked = $self->_format($raw);
80  
81 return $cooked;
82 }
83  
84 =item * reset_counters()
85  
86 Call this method to reset the footnote counter.
87  
88 =back
89  
90 =cut
91  
92 sub reset_counters
93 {
94 my $self = shift;
95  
96 $self->{footnote} = 0;
97 }
98  
99 =head2 Overridable Methods
100  
101 The following methods can be overridden to change the functionality of
102 the object.
103  
104 =over 4
105  
106 =item * get_block()
107  
108 If you would like to override the Block objects you can override this method.
109  
110 =cut
111  
112 sub get_block
113 {
114 my $self = shift;
115 my $type = shift;
116  
117 my $ret = 'XHTML::MediaWiki::Block::' . ucfirst($type || 'special');
118 }
119  
120 # This sub recognizes three states:
121 #
122 # 1. undef
123 # Normal wiki processing will be done on this line.
124 #
125 # 2. html
126 # Links and phrasal processing will be done, but formatting should be
127 # ignored.
128 #
129 # 3. nowiki
130 # No further wiki processing should be done.
131 #
132 # Each state may override the lower ones if already set on a given line.
133 #
134  
135 {
136 package XHTML::MediaWiki::Parser::Block::Line;
137  
138 use Params::Validate qw (validate);
139  
140 sub new
141 {
142 my $class = shift;
143 my %p = validate(@_, {
144 state => 1,
145 text => {
146 default => '',
147 },
148 eol => 0,
149 });
150  
151 my $self = bless { %p }, $class;
152  
153 return $self;
154 }
155  
156 sub state
157 {
158 shift->{state};
159 }
160  
161 sub append
162 {
163 my $self = shift;
164 my $text = shift;
165 $self->{text} .= $text;
166 }
167  
168 }
169 {
170 package XHTML::MediaWiki::Parser::Block;
171  
172 use Params::Validate qw (validate);
173 use Carp qw(croak);
174  
175 sub new
176 {
177 my $class = shift;
178 my %p = validate(@_, {
179 type => 1,
180 level => 0,
181 });
182 croak("internal error") if ($p{type} eq 'unordered' && !$p{level});
183 my $self =
184 bless {
185 lines => [],
186 %p,
187 }, $class;
188  
189 return $self;
190 }
191  
192 sub block_type
193 {
194 shift->{type};
195 }
196  
197 sub args
198 {
199 my $self = shift;
200 push(@{$self->{lines}}, $self->{line}) if $self->{line};
201 return (
202 lines => $self->{lines},
203 (level => $self->{level}) x!! $self->{level},
204 );
205 }
206  
207 sub get_line
208 {
209 my $self = shift;
210  
211 $self->{line} ||= XHTML::MediaWiki::Parser::Block::Line->new( state => 'wiki');
212 }
213  
214 sub get_state
215 {
216 my $self = shift;
217  
218 $self->{type};
219 }
220  
221 sub in_nowiki
222 {
223 my $self = shift;
224 my $line = $self->{line};
225  
226 if ($line) {
227 return $line->state eq 'nowiki';
228 } else {
229 return 0;
230 }
231 }
232  
233 sub append_text
234 {
235 my $self = shift;
236 my $text = shift;
237 die "extra arguments" if @_;
238  
239 my $line = $self->get_line();
240 $line->append($text);
241 }
242  
243 sub set_nowiki
244 {
245 my $self = shift;
246  
247 push(@{$self->{lines}}, $self->{line}) if $self->{line};
248 $self->{line} = XHTML::MediaWiki::Parser::Block::Line->new(state => 'nowiki');
249 }
250  
251 sub set_wiki
252 {
253 my $self = shift;
254  
255 push(@{$self->{lines}}, $self->{line}) if $self->{line};
256 $self->{line} = XHTML::MediaWiki::Parser::Block::Line->new(state => 'wiki');
257 }
258  
259 sub is_paragraph
260 {
261 my $self = shift;
262  
263 return $self->{type} eq 'paragraph';
264 }
265  
266 sub is_prewiki
267 {
268 my $self = shift;
269  
270 return $self->{type} eq 'prewiki';
271 }
272  
273 sub set_end_line
274 {
275 my $self = shift;
276 my $cnt = shift or croak "need count";
277  
278 my $line = $self->{line};
279 if (!defined $line) {
280 $line = $self->{lines}[-1] || XHTML::MediaWiki::Parser::Block::Line->new(state => 'dummy');
281 $line->{eol} = $cnt;
282 }
283 for (my $x = 0; $x < $cnt; $x++) {
284 $line->append("\n");;
285 }
286 $self;
287 }
288 }
289  
290 =item * encode()
291  
292 You can override the encode function if you would like to change
293 what is encoded. Currently only &, <, and > are encoded.
294  
295 =cut
296  
297 sub encode
298 {
299 my $text = shift;
300 if (defined $text) {
301 $text =~ s{&}{&amp;}gso;
302 $text =~ s{<}{&lt;}gso;
303 $text =~ s{>}{&gt;}gso;
304 }
305 return $text;
306 }
307  
308 sub _close_to
309 {
310 my $parser = shift;
311 my $tag = shift;
312 my $tagstack = $parser->{tag_stack};
313 my $text = '';
314  
315 if (!@$tagstack) {
316 $text .= "<!-- extra closing $tag -->" if $DEBUG;
317 # ignore extra closing tags
318 } else {
319 while (my $toptag = pop @$tagstack) {
320 $text .= "</$toptag>";
321 last if $tag eq $toptag;
322 }
323 }
324  
325 return $text;
326 }
327  
328 sub _html_tag
329 {
330 my ($parser, $type, $tagname, $orig, $attr) = @_;
331 $tagname =~ s|/$||;
332  
333 if ($parser->in_nowiki && ($type ne 'E' || $tagname ne 'nowiki')) {
334 $parser->append_text(encode($orig));
335 return;
336 }
337 if ($parser->in_state('pre') && ($type ne 'E' || $tagname ne 'pre')) {
338 $parser->append_text(encode($orig));
339 return;
340 }
341 if (my $info = $parser->{tags}{$tagname}) {
342 my $tagstack = $parser->{tag_stack};
343 if ($type eq 'E') {
344 if ($info->{empty}) {
345 warn "empty tags";
346 #skip empty tags;
347 } elsif ($info->{nowiki}) {
348 # my $text = _close_to($parser, $tagname);
349 $parser->end_nowiki();
350 } elsif ($info->{block}) {
351 $parser->close_block();
352 } elsif ($info->{phrase}) {
353 my $text = _close_to($parser, $tagname);
354 $parser->append_text($text);
355 } elsif ($info->{special}) {
356 $parser->close_block();
357 my $text = _close_to($parser, $tagname);
358 $parser->add_block($text);
359 } else {
360 die "helpme $tagname";
361 }
362 } else {
363 if ($info->{empty}) {
364 $parser->append_text("<$tagname/>");
365 } elsif ($info->{nowiki}) {
366 $parser->start_nowiki();
367 # push @$tagstack, $tagname;
368 } elsif (my $blockname = $info->{block}) {
369 $parser->close_block( new_state => $blockname );
370  
371 # $parser->{state} = $blockname;
372 unless ($info->{notag}) {
373 $parser->append_text("<$tagname>");
374 }
375 push @$tagstack, $tagname;
376 } elsif ($info->{phrase}) {
377 push(@$tagstack, $tagname);
378 my $text = "<$tagname>";
379 $parser->append_text($text);
380 } elsif ($info->{special}) {
381 $parser->close_block();
382 push(@$tagstack, $tagname);
383 my $text = "<$tagname>";
384 $parser->add_block($text);
385 } else {
386 die "helpme $tagname";
387 push @$tagstack, $tagname;
388 }
389 }
390 } else {
391 $parser->append_text($parser, encode($orig));
392 }
393  
394 return;
395 }
396  
397 sub _html_comment
398 {
399 # warn "_html_comment: " . join(' ', @_);
400 }
401  
402 sub _html_text
403 {
404 my ($parser, $dtext, $skipped_text, $is_cdata) = @_;
405 my @tagstack = @{$parser->{tag_stack}};
406 my ($newtext, $newstate);
407  
408 if (my ($leading) = ($dtext =~ /^(\n+)/m)) {
409 my $x = length($leading);
410 $parser->end_line($x);
411 $dtext = substr($dtext, $x);
412 }
413  
414 if ($is_cdata && $parser->can_cdata) {
415 $newtext = $dtext;
416 } else {
417 $newtext = encode($dtext);
418 }
419  
420 $parser->append_text($newtext);
421  
422 # warn "Got skipped_text: `$skipped_text'\n[$dtext]\n" if $skipped_text;
423 }
424  
425 {
426 package XHTML::MediaWiki::Parser;
427  
428 use base 'HTML::Parser';
429  
430 use Params::Validate qw (validate);
431  
432 sub can_cdata
433 {
434 my $self = shift;
435 if (my $current = $self->check_current_block) {
436 return $self->{tags}{$current->{type}}{can_cdata};
437 }
438 return 0;
439 }
440  
441 sub end_line
442 {
443 my $self = shift;
444  
445 my $block = $self->get_last_line_block;
446  
447 $block->set_end_line(@_);;
448 }
449  
450 sub state
451 {
452 my $self = shift;
453  
454 my $block = $self->check_current_block;
455 return "none" unless $block;
456 return $block->get_state;
457 }
458  
459 sub in_state
460 {
461 my $self = shift;
462 my $state = shift;
463 die if @_;
464 my $cstate = $self->state;
465  
466 $cstate && $cstate eq $state;
467 }
468  
469 sub in_paragraph
470 {
471 my $self = shift;
472 my $ret = 0;
473 if (my $block = $self->check_current_block) {
474 $ret = $block->is_paragraph;
475 }
476 return $ret;
477 }
478  
479 sub in_prewiki
480 {
481 my $self = shift;
482 my $ret = 0;
483 if (my $block = $self->check_current_block) {
484 $ret = $block->is_prewiki;
485 }
486 return $ret;
487 }
488  
489 sub noformat
490 {
491 my $self = shift;
492  
493 $self->in_state('pre') or $self->in_nowiki();
494 }
495  
496 sub add_block
497 {
498 my $self = shift;
499  
500 if ($self->{current_block}) {
501 push(@{$self->{blocks}},
502 $self->{current_block}
503 );
504 die "This should have been handled by close_block";
505 }
506 my $block = $self->{current_block} = XHTML::MediaWiki::Parser::Block->new(type => 'special');
507 $block->append_text(join('', @_));
508 push(@{$self->{blocks}},
509 $self->{current_block}
510 );
511 $self->{current_block} = undef;
512 }
513  
514 sub close_block
515 {
516 my $self = shift;
517 my %p = validate(@_, {
518 new_state => {
519 optional => 1,
520 },
521 indent => {
522 optional => 1,
523 },
524 auto_merge => {
525 optional => 1,
526 },
527 });
528  
529 my $tagstack = $self->{tag_stack};
530 my $find = undef;
531 my $text = '';
532 if (!@$tagstack) {
533 # nothing to close;
534 } else {
535 for my $tagname (@$tagstack) {
536 my $info = $self->{tags}{$tagname};
537 if ($info->{block}) {
538 $find = $tagname;
539 }
540 }
541 }
542 if ($find) {
543 $text = $self->close_to($find);
544 if ($text) {
545 $self->append_text($text);
546 }
547 }
548  
549 if (my $current = $self->{current_block}) {
550 if ($p{auto_merge} && $p{new_state} eq $self->{current_block}->block_type) {
551 push(@{$current->{lines}}, $current->{line}) if ($current->{line});
552 $current->{line} = undef;
553 } else {
554 push(@{$self->{blocks}},
555 $self->{current_block}
556 );
557 $self->{current_block} = undef;
558 if (my $state = $p{new_state}) {
559 if ($state eq 'ordered' || $state eq 'unordered') {
560 die "Need indent" unless exists $p{indent};
561 $self->{indent} = $p{indent};
562 }
563 $self->{current_block} = XHTML::MediaWiki::Parser::Block->new(
564 type => $state,
565 level => $p{indent},
566 );
567 }
568 }
569 } elsif (my $state = $p{new_state}) {
570 $self->{current_block} = XHTML::MediaWiki::Parser::Block->new(
571 type => $state,
572 level => $p{indent},
573 );
574 }
575  
576 return $self;
577 }
578  
579 sub close_to
580 {
581 my $parser = shift;
582 my $tag = shift;
583 my $tagstack = $parser->{tag_stack};
584 my $text = '';
585  
586 if (!@$tagstack) {
587 $text .= "<!-- extra closing $tag -->" if $DEBUG;
588 # ignore extra closing tags
589 } else {
590 while (my $toptag = pop @$tagstack) {
591 if (! $parser->{tags}{$toptag}{notag}) {
592 $text .= "</$toptag>";
593 }
594 last if $tag eq $toptag;
595 }
596 }
597 return $text;
598 }
599  
600 sub start_nowiki
601 {
602 my $self = shift;
603 my $block = $self->get_current_block;
604  
605 $block->set_nowiki;
606 }
607  
608 sub end_nowiki
609 {
610 my $self = shift;
611 my $block = $self->get_current_block;
612  
613 $block->set_wiki;
614 }
615  
616 sub in_nowiki
617 {
618 my $self = shift;
619  
620 if (my $block = $self->check_current_block) {
621 return $block->in_nowiki;
622 } else {
623 return 0;
624 }
625 }
626  
627 sub check_current_block
628 {
629 my $self = shift;
630  
631 $self->{current_block};
632 }
633  
634 sub get_current_block
635 {
636 my $self = shift;
637  
638 if (!$self->{current_block}) {
639 my $tagstack = $self->{tag_stack};
640 my $new_state = $self->{state} || 'paragraph';
641 delete $self->{state};
642 croak() if $new_state eq 'unordered';
643 $self->{current_block} = XHTML::MediaWiki::Parser::Block->new(type => $new_state);
644 push @{$self->{tag_stack}}, 'paragraph';
645 }
646 return $self->{current_block};
647 }
648  
649 sub get_last_line_block
650 {
651 my $self = shift;
652 my $block = $self->get_current_block;
653  
654 if (! defined $block) {
655 $block = $self->{blocks}[-1];
656 }
657 return $block;
658 }
659  
660 sub append_text
661 {
662 my $self = shift;
663 my $text = shift;
664  
665 my $block = $self->get_current_block;
666  
667 $block->append_text($text);
668 }
669  
670 sub get_blocks
671 {
672 my $self = shift;
673 my @blocks;
674  
675 for my $block (@{$self->{blocks}}) {
676 next unless $block;
677 if ($block->{type} eq 'paragraph' && 0 == @{$block->{lines}} && !$block->{line}) {
678 warn "fix";
679 next;
680 }
681 push @blocks, $block;
682 }
683 @blocks;
684 }
685  
686 sub eof
687 {
688 my $self = shift;
689 $self->close_block();
690 for my $tag (@{$self->{tag_stack}}) {
691 $self->append_text("</$tag>\n");
692 }
693 $self->SUPER::eof(@_);
694 }
695 }
696  
697 sub _find_blocks_in_html
698 {
699 my $self = shift;
700 my $text = shift || "";
701 die if @_;
702  
703 my $parser = XHTML::MediaWiki::Parser->new
704 (start_h => [\&_html_tag, 'self, "S", tagname, text, attr'],
705 end_h => [\&_html_tag, 'self, "E", tagname, text'],
706 comment_h => [\&_html_comment, 'self, text'],
707 text_h => [\&_html_text, 'self, dtext, skipped_text, is_cdata'],
708 marked_sections => 1,
709 boolean_attribute_value => '__TEXT_MEDIAWIKIFORMAT_BOOL__',
710 );
711 $parser->{opts} = {},
712 $parser->{tags} = {
713 b => { phrase => 1 },
714 big => { phrase => 1 },
715 blockquote => { phrase => 1 },
716 br => { empty => 1 },
717 caption => {},
718 center => {},
719 cite => {},
720 code => { phrase => 1 },
721 dd => {},
722 div => {
723 special => 1,
724 },
725 dl => {},
726 dt => {},
727 em => {},
728 font => {},
729  
730 h1 => { block => 'header' },
731 h2 => { block => 'header' },
732 h3 => { block => 'header' },
733 h4 => { block => 'header' },
734 h5 => { block => 'header' },
735 h6 => { block => 'header' },
736  
737 hr => { empty => 1 },
738 i => { },
739 li => { },
740 nowiki => {
741 nowiki => 1,
742 notag => 1,
743 },
744 ol => { },
745 p => { block => 'p' },
746 paragraph => {
747 block => 'paragraph',
748 notag => 1
749 },
750 pre => {
751 block => 'pre',
752 # nowiki => 1,
753 },
754 rb => {},
755 rp => {},
756 rt => {},
757 ruby => {
758 block => 'ruby',
759 can_cdata => 1,
760 },
761 s => {},
762 samp => {},
763 small => {},
764 strike => {},
765 strong => {},
766 sub => {},
767 sup => {},
768 table => {},
769 td => {},
770 th => {},
771 tr => {},
772 tt => {},
773 u => {},
774 ul => {},
775 var => {},
776 };
777 $parser->{tag_stack} = [];
778 $parser->{blocks} = [];
779 $parser->{current_block} = undef;
780  
781 my @lines = split(/\r?\n/, $text);
782  
783 for my $line (@lines) {
784 my $close = 0;
785 die if chomp $line;
786 if ($parser->noformat) {
787 # we are in nowiki or pre block
788 } else {
789 if ($parser->in_prewiki && $line && $line !~ m/^\s+/) {
790 $parser->close_block();
791 }
792 if ($line =~ qr/^(={1,6})\s*(.+?)\s*\1$/) {
793 my $x = length $1;
794 $line = sprintf("<h%d>%s</h%d>\n", $x, $2, $x);
795 $parser->{last} = 'header';
796 } elsif ($line =~ /^$/) {
797 if ($parser->check_current_block) {
798 if ($parser->in_paragraph) {
799 $parser->close_block();
800 } elsif ($parser->in_prewiki) {
801 $parser->close_block();
802 } else {
803 }
804 } else {
805 unless ({header => 1, prewiki => 1}->{$parser->{last} || ''}) {
806 $line = "<br/>";
807 }
808 }
809 } elsif ($line =~ m/^\s(\s*.*)$/) {
810 $line = $1;
811 $parser->close_block( new_state => 'prewiki', auto_merge => 1 );
812  
813 $parser->{last} = 'prewiki';
814 } elsif ($line =~ m/^(#+)\s*(.*)\s*$/) {
815 my $x = length $1;
816 $parser->close_block( new_state => 'ordered', indent => $x );
817 $close = 1;
818 $line = $2;
819 $parser->{last} = 'nested';
820 } elsif ($line =~ m/^(\*+)\s*(.*)\s*$/) {
821 my $x = length $1;
822 $parser->close_block( new_state => 'unordered', indent => $x );
823 $close = 1;
824 $line = $2;
825 $parser->{last} = 'nested';
826 } else {
827 }
828 }
829 next unless $line;
830 $parser->parse($line);
831 $parser->parse("\n");
832  
833 $parser->{empty_lines} = 0;
834  
835 $parser->close_block() if $close;
836 }
837 $parser->eof();
838 my @blocks;
839  
840 for my $block ($parser->get_blocks) {
841 next unless defined $block;
842 my $type = $block->block_type;
843 my $class = $self->get_block($type);
844  
845 my $new_block =
846 $class->new (
847 type => $type,
848 $block->args,
849 formater => $self,
850 );
851 push @blocks, $new_block;
852 }
853  
854 return @blocks;
855 }
856  
857 sub _find_blocks
858 {
859 my $self = shift;
860 my $text = shift;
861  
862 my @blocks;
863  
864 @blocks = $self->_find_blocks_in_html($text);
865  
866 return @blocks;
867 }
868  
869 sub _nest_blocks
870 {
871 my $self = shift;
872 my @blocks = @_;
873 return unless @blocks;
874  
875 my @processed = shift @blocks;
876 for my $block (@blocks)
877 {
878 my @x = $processed[-1]->nest( $block );
879 push @processed, @x;
880 }
881  
882 return @processed;
883 }
884  
885 sub _process_blocks
886 {
887 my $self = shift;
888 my @blocks = @_;
889 my @open;
890 for my $block (@blocks)
891 {
892 push @open, $self->_process_block($block);
893 }
894 return join '', @open ;
895 }
896  
897 sub _process_block
898 {
899 my $self = shift;
900 my ($block, $tags, $opts) = @_;
901 my $type = $block->type();
902  
903 my ($start, $end, $start_line, $end_line, $between);
904 if ($tags->{$type})
905 {
906 ($start, $end, $start_line, $end_line, $between) = @{$tags->{$type}};
907 }
908 else
909 {
910 ($start, $end, $start_line, $end_line) = ('', '', '', '');
911 }
912  
913 my @text = ();
914 for my $line (grep (/^\Q$type\E$/, @{$tags->{unformatted_blocks}})
915 ? $block->text()
916 : $block->formatted_text())
917 {
918 if (blessed $line)
919 {
920 my $prev_end = pop @text || ();
921 push @text, _process_block ($line, $tags, $opts), $prev_end;
922 next;
923 }
924  
925 my @triplets;
926 if ((ref ($start_line) || '') eq 'CODE')
927 {
928 @triplets = $start_line->($line, $block->level(),
929 $block->shift_args(), $tags, $opts);
930 }
931 else
932 {
933 @triplets = ($start_line, $line, $end_line);
934 }
935 push @text, @triplets;
936 }
937  
938 pop @text if $between;
939 return join '', $start, @text, $end;
940 }
941  
942 sub _format
943 {
944 my $self = shift;
945 my $text = shift;
946  
947 my @blocks = $self->_find_blocks($text);
948  
949 @blocks = $self->_nest_blocks(@blocks);
950 my $ret = $self->_process_blocks(@blocks);
951  
952 return $ret;
953 }
954  
955 sub _strong
956 {
957 "<strong>$_[1]</strong>";
958 }
959  
960 =item * emphasized()
961  
962 emphasized controls the output of "<em>" tags.
963  
964 =cut
965  
966 sub emphasized
967 {
968 "<em>$_[1]</em>";
969 }
970  
971 =item * link()
972  
973 The link method is often overridden to modify the display and
974 operation of links.
975  
976 link takes 3 arguments the Link, any extra_text, and the type of the link;
977  
978 The type is true for footnotes.
979  
980 =cut
981  
982 sub link
983 {
984 my $self = shift;
985 my $link = shift || '';
986 my $extra = shift || '';
987 my $type = shift;
988 my $text = $link;
989 if ($type) {
990 $text = ++$self->{footnote};
991 } else {
992 $link = $self->{link_path} . $link;
993 }
994 qq|<a href='$link'>$text$extra</a>|;
995 }
996  
997 =item * find_links()
998  
999 The C<find_links> method is also often overridden in order to change the way
1000 links are detected.
1001  
1002 =cut
1003  
1004 sub find_links
1005 {
1006 my $self = shift;
1007 my $text = shift;
1008  
1009 return '' unless defined $text;
1010  
1011 $text =~ s/\[\[([^\]]*)\]\]([A-Za-z0-9]*)/$self->link($1, $2, 0)/ge;
1012 $text =~ s/\[([a-zA-Z]+:[^\]]*)\]/$self->link($1, '', 1)/ge;
1013  
1014 return $text;
1015 }
1016  
1017 =item * template_text()
1018  
1019 Override this method to control the text that is generated for an unknown template ({{word}}).
1020  
1021 =cut
1022  
1023 sub template_text
1024 {
1025 my $self = shift;
1026 my $text = shift;
1027 die if @_;
1028 '<b style="color: red;">No template for: ' . $text . '</b>';
1029 }
1030  
1031 =item * format_line()
1032  
1033 Override this method to extend or modify line level parsing.
1034  
1035 =cut
1036  
1037 sub format_line
1038 {
1039 my $self = shift;
1040 my $text = shift;
1041  
1042 return '' unless defined $text;
1043  
1044 my $strong_tag = qr/'''(.+?)'''/;
1045 my $emphasized_tag = qr/''(.+?)''/;
1046  
1047 $text =~ s!$strong_tag!$self->_strong($1)!eg;
1048 $text =~ s!$emphasized_tag!$self->emphasized($1)!eg;
1049  
1050 $text = $self->find_links($text);
1051  
1052 my $template_tag = qr/{{\s*([a-zA-Z0-9][a-z0-9|]*)\s*}}/;
1053 $text =~ s!$template_tag!$self->template_text($1)!eg;
1054
1055 return $text;
1056 }
1057  
1058 # BLOCK code is below here and needs to be moved.
1059  
1060 {
1061 package XHTML::MediaWiki::Block::Start;
1062  
1063 use base "XHTML::MediaWiki::Block";
1064 sub formatted_text
1065 {
1066 "<!-- start wiki -->\n";
1067 }
1068 }
1069 {
1070 package XHTML::MediaWiki::Block::Header;
1071  
1072 use base "XHTML::MediaWiki::Block";
1073  
1074 sub formatted_text
1075 {
1076 my $self = shift;
1077 my $formatter = $self->formatter;
1078 my $text = $self->SUPER::formatted_text();
1079  
1080 my $newtext = $text;
1081 $newtext =~ s/<[^>]+>//g;
1082 $newtext =~ s/\s/_/g;
1083 qq|<a name="$newtext"></a>| . $text;
1084 }
1085 }
1086  
1087 {
1088 package XHTML::MediaWiki::Block::Special;
1089 use base "XHTML::MediaWiki::Block";
1090  
1091 sub formatted_text
1092 {
1093 my $self = shift;
1094 my $formatter = $self->formatter;
1095 my $ret_text = '';
1096 for my $line (@{$self->{lines}}) {
1097 die("internal error") unless $line;
1098  
1099 my $text .= $line->{text};
1100 if ($line->{state} eq 'nowiki') {
1101 $ret_text .= $text;
1102 } else {
1103 $ret_text .= $formatter->format_line($text);
1104 }
1105 }
1106 $ret_text;
1107 }
1108 }
1109 {
1110 package XHTML::MediaWiki::Block::P;
1111 use base "XHTML::MediaWiki::Block";
1112  
1113 sub formatted_text
1114 {
1115 my $self = shift;
1116 $self->SUPER::formatted_text(@_) . "\n";
1117 }
1118 }
1119 {
1120 package XHTML::MediaWiki::Block::Paragraph;
1121 use base "XHTML::MediaWiki::Block";
1122  
1123 use Carp qw(croak);
1124  
1125 sub formatted_text
1126 {
1127 my $self = shift;
1128 my $formater = $self->{formater};
1129 my $ret_text = '';
1130  
1131 for my $line (@{$self->{lines}}) {
1132 use Data::Dumper;
1133 warn Dumper $self unless $line;
1134 die("internal error") unless $line;
1135  
1136 my $text .= $line->{text};
1137 if ($line->{state} eq 'nowiki') {
1138 $ret_text .= $text;
1139 } else {
1140 $ret_text .= $formater->format_line($text);
1141 }
1142 }
1143 if ($ret_text =~ m/^\s*$/) {
1144 # return "<!-- skip -->\n";
1145 } else {
1146 return '<p>' . $ret_text . "</p>\n";
1147 }
1148 }
1149 }
1150  
1151 {
1152 package XHTML::MediaWiki::Block::Nested;
1153 use base "XHTML::MediaWiki::Block";
1154  
1155 sub new
1156 {
1157 my $class = shift;
1158 my $self = $class->SUPER::new(@_);
1159  
1160 die caller unless $self->{level};
1161 return $self;
1162 }
1163  
1164 sub formatted_text
1165 {
1166 my $self = shift;
1167  
1168 my $formatter = $self->formatter;
1169 my $text = $self->SUPER::formatted_text(@_);
1170  
1171 my $indent = $self->{level};
1172 my $ret = $self->start_block;
1173  
1174 $ret .= '<li>' . $text;
1175 if ($self->{block}) {
1176 $ret .= $self->{block}->formatted_text();
1177 }
1178 $ret .= "</li>\n";
1179  
1180 for my $x (@{$self->{added}}) {
1181 $ret .= '<li>' . $x->SUPER::formatted_text();
1182 if ($x->{block}) {
1183 $ret .= $x->{block}->formatted_text();
1184 }
1185 $ret .= "</li>";
1186 $ret .= "\n";
1187 }
1188 $ret .= $self->end_block;
1189  
1190 return $ret;
1191 }
1192  
1193 sub level
1194 {
1195 my $self = shift;
1196  
1197 return $self->{level};
1198 }
1199  
1200 sub cmp
1201 {
1202 my $self = shift;
1203 my $cmp_block = shift;
1204 my $ret = 0;
1205  
1206 if (ref($self) eq ref($cmp_block) && $self->level == $cmp_block->level) {
1207 $ret = 1;
1208 }
1209 return $ret;
1210 }
1211  
1212 sub nests
1213 {
1214 1;
1215 }
1216  
1217 sub nest_block
1218 {
1219 my $self = shift;
1220 my $current = $self->{added}->[-1] || $self;
1221 for my $block (@_) {
1222 my $index = $block->level - $self->level;
1223 die 'internal error' if $index <= 0;
1224 if ($index == 1) {
1225 if (my $x = $current->{block}) {
1226 $x->nest($block);
1227 } else {
1228 $current->{block} = $block;
1229 }
1230 } else {
1231 $current->{block} ||= ref($block)->new(
1232 formater => $current->{formater},
1233 type => $current->type,
1234 level => $current->level + 1,
1235 );
1236 $current->{block}->nest($block);
1237 }
1238 }
1239 }
1240 }
1241  
1242 {
1243 package XHTML::MediaWiki::Block::Ordered;
1244 use base "XHTML::MediaWiki::Block::Nested";
1245 sub start_block { "<ol>\n" }
1246 sub end_block { "</ol>\n" }
1247 }
1248 {
1249 package XHTML::MediaWiki::Block::Unordered;
1250 use base "XHTML::MediaWiki::Block::Nested";
1251 sub start_block { "<ul>\n" }
1252 sub end_block { "</ul>\n" }
1253 }
1254 {
1255 package XHTML::MediaWiki::Block::Pre;
1256 use base "XHTML::MediaWiki::Block";
1257  
1258 sub formatted_text {
1259 my $self = shift;
1260 my $text = $self->unformatted_text;
1261  
1262 return $text;
1263 }
1264 }
1265 {
1266 package XHTML::MediaWiki::Block::Prewiki;
1267 use base "XHTML::MediaWiki::Block";
1268  
1269 sub formatted_text
1270 {
1271 my $self = shift;
1272 my $text = $self->SUPER::formatted_text(@_);
1273 $text =~ s/^\s*//;
1274  
1275 return "\n" . '<pre>' . $text . "\n</pre>";
1276 }
1277 }
1278 {
1279 package XHTML::MediaWiki::Block::Ruby;
1280 use base "XHTML::MediaWiki::Block";
1281  
1282 sub formatted_text
1283 {
1284 my $self = shift;
1285 my $text = $self->SUPER::unformatted_text(@_);
1286  
1287 return "Ruby Data";
1288 }
1289 }
1290 {
1291 package XHTML::MediaWiki::Block;
1292 use Params::Validate qw (validate ARRAYREF);
1293  
1294 sub new
1295 {
1296 my $class = shift;
1297 my %p = validate(@_, {
1298 formater => 1,
1299 type => 1,
1300 indent => 0,
1301 level => 0,
1302 lines => ARRAYREF,
1303 args => 0,
1304 });
1305  
1306 bless { %p }, $class
1307 }
1308  
1309 sub merge_block
1310 {
1311 my $self = shift;
1312  
1313 push(@{$self->{added}}, @_);
1314 }
1315  
1316 sub cmp
1317 {
1318 0;
1319 }
1320  
1321 sub merge
1322 {
1323 my $self = shift;
1324 my @ret = @_;
1325  
1326 while (my $block = pop @ret) {
1327 if ($self->cmp($block)) {
1328 $self->merge_block($block);
1329 } else {
1330 push(@ret, $block);
1331 last;
1332 }
1333 }
1334  
1335 @ret;
1336 }
1337  
1338 sub nests
1339 {
1340 return 0;
1341 }
1342  
1343 sub nest
1344 {
1345 my $self = shift;
1346 my @next_blocks = @_;
1347  
1348 @next_blocks = $self->merge(@next_blocks);
1349 while (@next_blocks) {
1350 my $next = $next_blocks[0];
1351 if ($self->nests && $next->nests) {
1352 $self->nest_block(pop @next_blocks);
1353 } else {
1354 last;
1355 }
1356 }
1357  
1358 return @next_blocks;
1359 }
1360  
1361 sub level
1362 {
1363 my $x = shift;
1364 warn $x;
1365 0;
1366 }
1367  
1368 sub type
1369 {
1370 my $self = shift;
1371  
1372 $self->{type};
1373 }
1374  
1375 sub formatter
1376 {
1377 shift->{formater};
1378 }
1379  
1380 sub unformatted_text {
1381 my $self = shift;
1382 my $formater = $self->{formater};
1383 my $text = '';
1384  
1385 for my $line (@{$self->{lines}}) {
1386 die("internal error") unless $line;
1387  
1388 $text .= $line->{text};
1389 }
1390 return $text;
1391 }
1392  
1393 sub formatted_text {
1394 my $self = shift;
1395 my $formater = $self->{formater};
1396 my $text = '';
1397  
1398 for my $line (@{$self->{lines}}) {
1399 die("internal error") unless $line;
1400  
1401 if ($line->{state} eq 'nowiki') {
1402 $text .= $line->{text};
1403 } else {
1404 $text .= $formater->format_line($line->{text});
1405 }
1406 }
1407 return $text;
1408 }
1409 }
1410  
1411 1;
1412 __END__
↑ Summary