blib/lib/Alvis/Wikipedia/WikitextParser.pm untested

filter: Statementstmt 0.0% 0 / 309 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 489 CC 56 SCAR 54.4
CC
56
Cov
0%
_convert_markup
650.0
_convert
72.0
_set_err_state
30.0
CRAP 231.2
1 package Alvis::Wikipedia::WikitextParser;
2  
3 $Alvis::Wikipedia::WikitextParser::VERSION = '0.1';
4  
5 use warnings;
6 use strict;
7  
8 ########################################################################
9 #
10 # Exported constants
11 #
12 ########################################################################
13 #
14 # Text section types
15 #
16 our ($MARKUP,$NOWIKI,$PRE,$MATH,$HIERO,$GALLERY)=
17 ('wiki markup','nowiki','pre','math','hiero','gallery');
18  
19 #######################################################################
20 #
21 # Error message stuff
22 #
23 ######################################################################
24  
25 my ($ERR_OK,
26 $ERR_UNK_SECTION_TYPE,
27 $ERR_NO_TEXT,
28 $ERR_UNDEF_TITLE,
29 $ERR_ONLY_UNDERSCORES_LEFT,
30 $ERR_RELATIVE_PATH,
31 $ERR_OVERLONG_TITLE,
32 $ERR_HTML_CONVERSION,
33 $ERR_MARKUP_SEPARATION
34 )=(0..8);
35 my %ErrMsgs=($ERR_OK=>"",
36 $ERR_UNK_SECTION_TYPE=>"Unrecognized text section type.",
37 $ERR_NO_TEXT=>"No text to separate markup",
38 $ERR_UNDEF_TITLE=>"Undefined title to normalize.",
39 $ERR_ONLY_UNDERSCORES_LEFT=>"Only underscores left in the " .
40 "title to normalize.",
41 $ERR_RELATIVE_PATH=>"Title to be normalized is a path relative " .
42 "to the working directory.",
43 $ERR_OVERLONG_TITLE=>"Title to be normalized is too long.",
44 $ERR_HTML_CONVERSION=>"HTML conversion failed.",
45 $ERR_MARKUP_SEPARATION=>"Separating the wikitext to different " .
46 "categories failed."
47 );
48  
49 sub _set_err_state
50 {
51 my $self=shift;
52 my $errcode=shift;
53 my $errmsg=shift;
54  
55  
56 if (!defined($errcode))
57 {
58 confess("set_err_state() called with an undefined argument.");
59 }
60  
61 if (exists($ErrMsgs{$errcode}))
62 {
63 if ($errcode==$ERR_OK)
64 {
65 $self->{errstr}="";
66 }
67 else
68 {
69 $self->{errstr}.=" " . $ErrMsgs{$errcode};
70 if (defined($errmsg))
71 {
72 $self->{errstr}.=" " . $errmsg;
73 }
74  
75 }
76 }
77 else
78 {
79 confess("Internal error: set_err_state() called with an " .
80 "unrecognized argument ($errcode).")
81 }
82 }
83  
84 sub clearerr
85 {
86 my $self=shift;
87
88 $self->{errstr}="";
89 }
90  
91 sub errmsg
92 {
93 my $self=shift;
94
95 return $self->{errstr};
96 }
97  
98 ####################################################################
99 #
100 # Public methods
101 #
102 ####################################################################
103  
104 sub new
105 {
106 my $proto=shift;
107  
108 my $class=ref($proto)||$proto;
109 my $parent=ref($proto)&&$proto;
110 my $self={};
111 bless($self,$class);
112  
113 $self->_init(@_);
114  
115 return $self;
116 }
117  
118 sub _init
119 {
120 my $self=shift;
121  
122 if (defined(@_))
123 {
124 my %args=@_;
125 @$self{ keys %args }=values(%args);
126 }
127 }
128  
129 sub normalize_title
130 {
131 my $self=shift;
132 my $title=shift;
133  
134 if (!defined($title))
135 {
136 $self->_set_err_state($ERR_UNDEF_TITLE);
137 return undef;
138 }
139  
140 #
141 # subst:, template:, msg: etc.
142 #
143 $title=~s/^\w*://isgo;
144  
145 #
146 # Do a bunch of ill-documented & idiotic normalizations.
147 # 50,000,000 flies can't be wrong.
148 #
149 # space/underscore
150 $title=~s/^\s+//isgo;
151 $title=~s/\s+$//isgo;
152 $title=~s/[ ]+/\_/isgo;
153 $title=~s/\_+/\_/isgo;
154 $title=~s/^\_+//isgo;
155 $title=~s/\_+$//isgo;
156 if ($title=~/^\_+$/)
157 {
158 $self->_set_err_state($ERR_ONLY_UNDERSCORES_LEFT);
159 return undef;
160 }
161  
162 if ($title=~/^\.\.?\/?$/)
163 {
164 $self->_set_err_state($ERR_RELATIVE_PATH);
165 return undef;
166 }
167  
168 $title=~s/\?/\%3F/isgo;
169 $title=~s/\//\&\#47;/isgo;
170  
171 #
172 # There's also a set of characters which "cannot occur" in
173 # titles, but no info on what to do with them...remove/fail?
174 # I'll ignore for now.
175 #
176  
177 if (length($title)>256)
178 {
179 $self->_set_err_state($ERR_OVERLONG_TITLE);
180 return undef;
181 }
182  
183 $title=ucfirst($title);
184  
185 return $title;
186 }
187  
188 sub to_HTML
189 {
190 my $self=shift;
191 my $text=shift;
192  
193 my $HTML=$self->_convert($text);
194 if (!defined($HTML))
195 {
196 $self->_set_err_state($ERR_HTML_CONVERSION);
197 return undef;
198 }
199  
200 return $HTML;
201 }
202  
203 #
204 # This is no great shakes, a two-pass parser that first
205 # separates main types of text and then converts them
206 # according to type
207 # Maybe should be replaced by a Wikitext parser library if one
208 # exists somewhere
209 sub _convert
210 {
211 my $self=shift;
212 my $text=shift;
213  
214 my $sep_text=$self->separate_markup($text);
215 if (!defined($sep_text))
216 {
217 $self->_set_err_state($ERR_MARKUP_SEPARATION," Text:$text");
218 return undef;
219 }
220 my $res="";
221 for my $s (@$sep_text)
222 {
223 my ($type,$t)=@$s;
224  
225 if ($type eq $MARKUP)
226 {
227 $t=$self->_convert_markup($t);
228 }
229 elsif ($type eq $NOWIKI)
230 {
231 $t=$self->_convert_nowiki($t);
232 }
233 elsif ($type eq $PRE)
234 {
235 $t=$self->_convert_pre($t);
236 }
237 elsif ($type eq $MATH)
238 {
239 $t=$self->_convert_math($t);
240 }
241 elsif ($type eq $HIERO)
242 {
243 $t=$self->_convert_hiero($t);
244 }
245 elsif ($type eq $GALLERY)
246 {
247 $t=$self->_convert_gallery($t);
248 }
249 else
250 {
251 $self->_set_err_state($ERR_UNK_SECTION_TYPE,
252 " Type:$type");
253 }
254
255 $res.=$t;
256 }
257  
258 return $res;
259 }
260  
261 sub separate_markup
262 {
263 my $self=shift;
264 my $text=shift;
265  
266 if (!defined($text))
267 {
268 $self->_set_err_state($ERR_NO_TEXT);
269 return undef;
270 }
271 #
272 # separate no-markup sections first
273 #
274 my @text=();
275 while ($text=~/^(.*?)<nowiki>(.*?)<\/nowiki>(.*?)$/isgo)
276 {
277 push(@text,[$MARKUP,$1]);
278 push(@text,[$NOWIKI,$2]);
279 $text=$3;
280 }
281 push(@text,[$MARKUP,$text]);
282  
283 my @text2=();
284 for my $t (@text)
285 {
286 my ($type,$text)=@$t;
287 if ($type eq $MARKUP)
288 {
289 while ($text=~/^(.*?)<math>(.*?)<\/math>(.*?)$/isgo)
290 {
291 push(@text2,[$MARKUP,$1]);
292 push(@text2,[$MATH,$2]);
293 $text=$3;
294 }
295 push(@text2,[$MARKUP,$text]);
296 }
297 else
298 {
299 push(@text2,[$type,$text]);
300 }
301 }
302
303 my @text3=();
304 for my $t (@text2)
305 {
306 my ($type,$text)=@$t;
307 if ($type eq $MARKUP)
308 {
309 while ($text=~/^(.*?)<hiero>(.*?)<\/hiero>(.*?)$/isgo)
310 {
311 push(@text3,[$MARKUP,$1]);
312 push(@text3,[$HIERO,$2]);
313 $text=$3;
314 }
315 push(@text3,[$MARKUP,$text]);
316 }
317 else
318 {
319 push(@text3,[$type,$text]);
320 }
321 }
322
323 my @text4=();
324 for my $t (@text3)
325 {
326 my ($type,$text)=@$t;
327 if ($type eq $MARKUP)
328 {
329 while ($text=~/^(.*?)<gallery>(.*?)<\/gallery>(.*?)$/isgo)
330 {
331 push(@text4,[$MARKUP,$1]);
332 push(@text4,[$GALLERY,$2]);
333 $text=$3;
334 }
335 push(@text4,[$MARKUP,$text]);
336 }
337 else
338 {
339 push(@text4,[$type,$text]);
340 }
341 }
342
343 return \@text4;
344 }
345  
346 sub _convert_nowiki
347 {
348 my $self=shift;
349 my $t=shift;
350
351 return $t;
352 }
353  
354 sub _convert_math
355 {
356 my $self=shift;
357 my $t=shift;
358
359 return "<PRE>$t</PRE>";
360 }
361  
362 sub _convert_hiero
363 {
364 my $self=shift;
365 my $t=shift;
366
367 return "";
368 }
369  
370 sub _convert_gallery
371 {
372 my $self=shift;
373 my $t=shift;
374
375 my $t_g;
376 my @rows=split(/\n/,$t);
377 for my $r (@rows)
378 {
379 if ($r=~/^\s*Image:(.*)$/)
380 {
381 $t_g.=$self->_handle_images($1);
382 }
383 }
384  
385 return $t_g;
386 }
387  
388 sub _convert_template
389 {
390 my $self=shift;
391 my $t=shift;
392
393 return $t;
394 }
395  
396 sub _convert_markup
397 {
398 my $self=shift;
399 my $t=shift;
400  
401 my $res="";
402  
403 my @r=split(/\n/,$t);
404 for (my $i=0;$i<scalar(@r);$i++)
405 {
406 if ($r[$i]=~/^[ ](.*)$/)
407 {
408 $res.="<PRE>\n";
409 $res.="$1\n";
410 $i++;
411 while ($i<scalar(@r) && $r[$i]=~/^[ ](.*)$/)
412 {
413 $res.="$1\n";
414 $i++;
415 }
416 $res.="</PRE>\n";
417 if ($i<scalar(@r))
418 {
419 $i--; # rewind
420 }
421 }
422 elsif ($r[$i]=~/^=====(.*?)=====/)
423 {
424 my $text=$self->_handle_text($1);
425 $res.="\n<H5>$text</H5>\n"
426 }
427 elsif ($r[$i]=~/^====(.*?)====/)
428 {
429 my $text=$self->_handle_text($1);
430 $res.="\n<H4>$text</H4>\n"
431 }
432 elsif ($r[$i]=~/^===(.*?)===/)
433 {
434 my $text=$self->_handle_text($1);
435 $res.="\n<H3>$text</H3>\n"
436 }
437 elsif ($r[$i]=~/^==(.*?)==/)
438 {
439 my $text=$self->_handle_text($1);
440 $res.="\n<H2>$text</H2>\n"
441 }
442 elsif ($r[$i]=~/^([\*\#]+)(.*?)$/)
443 {
444 my @stars=split(//,$1);
445 my $text=$self->_handle_text($2);
446 $res.="\n<UL>\n<LI>$text</LI>\n";
447 $i++;
448 my $nof_levels_open=1;
449 my $curr_level=scalar(@stars);
450 while ($i<scalar(@r) && $r[$i]=~/^([\*\#]+)(.*?)$/)
451 {
452 @stars=split(//,$1);
453 my $text=$self->_handle_text($2);
454 my $level=scalar(@stars);
455 if ($level>$curr_level)
456 {
457 $res.="\n<UL>\n<LI>$text</LI>\n";
458 $nof_levels_open++;
459 }
460 elsif ($level==$curr_level)
461 {
462
463 $res.="\n<LI>$text</LI>\n";
464 }
465 else
466 {
467 $res.="\n</UL>\n</UL>\n<UL>\n<LI>$text\n";
468 $nof_levels_open=$curr_level--;
469 }
470 $curr_level=$level;
471 $i++;
472 }
473  
474 while ($nof_levels_open--)
475 {
476 $res.="\n</UL>\n";
477 }
478 if ($i<scalar(@r))
479 {
480 $i--; # rewind
481 }
482 }
483 elsif ($r[$i]=~/^;(.*?):(.*)$/)
484 {
485 my $term=$self->_handle_text($1);
486 my $definition=$self->_handle_text($2);
487 $res.="\n<DL>\n<DT>$term</DT>\n<DD>$definition</DD>\n</DL>\n"
488 }
489 elsif ($r[$i]=~/^;([^:]*?)$/)
490 {
491 my $term=$self->_handle_text($1);
492 $res.="\n<DL>\n<DT>$term</DT>\n";
493 $i++;
494 if ($i<scalar(@r) && $r[$i]=~/^:(.*)$/)
495 {
496 my $definition=$self->_handle_text($1);
497 $res.="<DD>$definition</DD>\n";
498 }
499 $res.="</DL>\n";
500 }
501 elsif ($r[$i]=~/^:(.*?)$/)
502 {
503 $res.="<DL><DD>";
504 my $text=$r[$i];
505 $text=~s/^://;
506 $res.=$self->_convert_markup($text);
507 $res.="</DD></DL>\n";
508 }
509 elsif ($r[$i]=~/^----/)
510 {
511 $res.="\n<HR>\n";
512 }
513 elsif ($r[$i]=~/^$/)
514 {
515 $res.="\n<P>\n";
516 }
517 elsif ($r[$i]=~/^\{\| (.*)$/)
518 {
519 $res.="\n<TABLE $1>\n"
520 }
521 elsif ($r[$i]=~/^\|\}/)
522 {
523 $res.="\n</TABLE>\n"
524 }
525 elsif ($r[$i]=~/^\|\+(?:.*)(?:\|(.*))$/)
526 {
527 my $text=$self->_handle_text($1);
528 $res.="\n<CAPTION>$text</CAPTION>\n"
529 }
530 elsif ($r[$i]=~/^\|\-(.*?)$/)
531 {
532 $res.="\n<TR $1>\n";
533 }
534 elsif ($r[$i]=~/^\!(.*?)(?:\|(.*))$/)
535 {
536 my $text=$self->_handle_text($2);
537 $res.="\n<TH $1>$text</TH>\n";
538 }
539 elsif ($r[$i]=~/^\|(.*)$/)
540 {
541 my $cells=$self->_handle_text($1);
542 my @cells=split(/\|\|/,$cells);
543 for my $c (@cells)
544 {
545 my ($param,$cell)=split(/\|/,$c);
546 if (!defined($cell))
547 {
548 $cell=$param;
549 $param="";
550 }
551 if (defined($param) && defined($cell))
552 {
553 $res.="\n<TD $param>$cell</TD>\n";
554 }
555 }
556 }
557 else
558 {
559 my $text=$self->_handle_text($r[$i]);
560 $res.="$text\n";
561 }
562 }
563  
564 # $res=$self->_handle_text($res);
565  
566 return $res;
567 }
568  
569 sub _handle_text
570 {
571 my $self=shift;
572 my $txt=shift;
573
574 #
575 # This is not 100% correct, but the corresponding Parser.php
576 # is hacky as h*ll as well and very complicated. Awesome language
577 # design.
578 #
579 $txt=~s/\'\'\'(.*?)\'\'\'/<STRONG>$1<\/STRONG>/isgo;
580 $txt=~s/\'\'(.*?)\'\'/<EM>$1<\/EM>/isgo;
581  
582 #
583 # References & footnotes
584 #
585 $txt=~s/<\s*references\s*\/\s*>//isgo;
586 $txt=~s/<\s*ref\s*>/<OL><LI>/isgo;
587 $txt=~s/<\s*\/ref\s*>/<\/LI><\/OL>/isgo;
588  
589 #
590 #
591 # Lame, but this is basically what Parser.php is forced to fall upon
592 # as well. What you design is what you get.
593 #
594 $txt=~s/\[\[([^\[]*?)\]\]/$self->_handle_internal_link($1)/isgoe;
595 $txt=~s/\[\[Image:(.*?)\]\]/$self->_handle_images($1)/isgoe;
596 $txt=~s/\[([^\[]*?)\]/$self->_handle_external_link($1)/isgoe;
597
598 return $txt;
599 }
600  
601 sub _handle_images
602 {
603 my $self=shift;
604 my $link_txt=shift;
605  
606 my @parts=split(/\|/,$link_txt);
607  
608 if (scalar(@parts)==1)
609 {
610 my $link=$parts[0];
611 $link=~s/ /_/sgo;
612 return "<IMG src=\"wikipedia/images/$link\">$link</IMG>";
613 }
614 elsif (scalar(@parts)>=2)
615 {
616 my ($link,$title)=($parts[0],$parts[$#parts]);
617 $link=~s/ /_/sgo;
618 my $title_txt=$self->_handle_text($title);
619 if (!defined($title_txt))
620 {
621 $title_txt="";
622 }
623 else
624 {
625 $title_txt=~s/^\s+//sgo;
626 $title_txt=~s/\s+$//sgo;
627 }
628 return "<IMG src=\"wikipedia/images/$link\">$title_txt</IMG>";
629 }
630 else
631 {
632 return "[[$link_txt]]";
633 }
634 }
635  
636 sub _handle_external_link
637 {
638 my $self=shift;
639 my $link_txt=shift;
640  
641 if ($link_txt=~/^Image:/i)
642 {
643 return "[$link_txt]";
644 }
645  
646 my @parts=split(/ /,$link_txt);
647 if (scalar(@parts)>1)
648 {
649 my $text=join(" ",@parts[1..$#parts]);
650
651 $parts[0]=~s/ /_/sgo;
652 $text=~s/^\s+//isgo;
653 $text=~s/\s+$//isgo;
654 return "<A href=\"$parts[0]\">$text</A>";
655 }
656 elsif (scalar(@parts)==1)
657 {
658 $parts[0]=~s/ /_/sgo;
659 return "<A href=\"$parts[0]\"></A>";
660 }
661 else
662 {
663 return "[$link_txt]"; # Probably sth is wrong...
664 }
665 }
666  
667 sub _handle_internal_link
668 {
669 my $self=shift;
670 my $link_txt=shift;
671  
672 if ($link_txt=~/^Image:/i)
673 {
674 return "[[$link_txt]]";
675 }
676  
677 my @parts=split(/\|/,$link_txt);
678 if (scalar(@parts)==1)
679 {
680 my $link=$parts[0];
681 my $text=$link;
682 $text=~s/^\s+//isgo;
683 $text=~s/\s+$//isgo;
684 $link=~s/ /_/sgo;
685 return "<A href=\"wikipedia/$link\">$text</A>";
686 }
687 elsif (scalar(@parts)==2)
688 {
689 my ($link,$title)=@parts[0..1];
690 $title=$self->_handle_text($title);
691 $link=~s/ /_/sgo;
692 $title=~s/^\s+//isgo;
693 $title=~s/\s+$//isgo;
694 return "<A href=\"wikipedia/$link\">$title</A>";
695 }
696 else
697 {
698 return "[[$link_txt]]";
699 }
700 }
701  
702 1;