blib/lib/Alvis/Canonical.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Alvis/Wikipedia/Variables.pm ↑ Summary blib/lib/Alvis/HTML.pm »
1 package Alvis::Canonical;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶use Alvis::HTML;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 $Alvis::Canonical::VERSION = '0.31';
9  
10 #############################################################################
11 #
12 # Converts an original document in some format to an Alvis canonicalDocument
13 #
14 #############################################################################
15  
16 #############################################################################
17 #
18 # Global variables & constants
19 #
20 ##############################################################################
21  
22 my $DEF_WARNINGS=0; # add warning comments about fixes to the doc?
23 my $DEF_CONVERT_CHAR_ENTS=1; # convert "relevant" char ents
24 my $DEF_CONVERT_NUM_ENTS=1; # convert numerical entities
25 my $DEF_SRC_ENC=undef; # guess the source encoding
26  
27 my $DEBUG=0;
28  
29 #############################################################################
30 #
31 # Error message stuff
32 #
33 #############################################################################
34  
35 my $ErrStr;
36 my ($ERR_OK,
37 $ERR_NO_HTML_CONV,
38 $ERR_HTML_CONV,
39 $ERR_CONT2CAN_DOC,
40 $ERR_NO_HTML_CLEAN,
41 $ERR_MISFORMED_REL_URL,
42 $ERR_REL_URL_VS_BASE_MISMATCH
43 )=(0..6);
44 my %ErrMsgs=($ERR_OK=>"",
45 $ERR_NO_HTML_CONV=>"Unable to instantiate the HTML converter",
46 $ERR_HTML_CONV=>"Extracting the contents of HTML failed",
47 $ERR_CONT2CAN_DOC=>"Converting the HTML's contents failed",
48 $ERR_NO_HTML_CLEAN=>"Unable to instantiate the HTML cleaner",
49 $ERR_MISFORMED_REL_URL=>"Misformed relative URL",
50 $ERR_REL_URL_VS_BASE_MISMATCH=>"Cannot match a relative URL " .
51 "and the URL base"
52 );
53  
54 sub _set_err_state
55 {
56 my $self=shift;
57 my $errcode=shift;
58 my $errmsg=shift;
59  
60 if (!defined($errcode))
61 {
62 confess("set_err_state() called with an undefined argument.");
63 }
64  
65 if (exists($ErrMsgs{$errcode}))
66 {
67 if ($errcode==$ERR_OK)
68 {
69 $self->{errstr}="";
70 }
71 else
72 {
73 $self->{errstr}.=" " . $ErrMsgs{$errcode};
74 if (defined($errmsg))
75 {
76 $self->{errstr}.=" " . $errmsg;
77 }
78 }
79 }
80 else
81 {
82 confess("Internal error: set_err_state() called with an " .
83 "unrecognized argument ($errcode).")
84 }
85 }
86  
87 sub errmsg
88 {
89 my $self=shift;
90  
91 return $self->{errstr};
92 }
93  
94 #############################################################################
95 #
96 # Methods
97 #
98 ##############################################################################
99
100 sub new
101 {
102 my $proto=shift;
103  
104 my $class=ref($proto)||$proto;
105 my $parent=ref($proto)&&$proto;
106 my $self={};
107 bless($self,$class);
108  
109  
110 $self->_init(@_);
111  
112 $self->_set_err_state($ERR_OK);
113  
114 # Removes uninteresting HTML tags, fixes the interesting tags and
115 # converts natural language relevant <=#255 character entities to
116 # characters and UTF-8 numerical entities to characters if wanted
117 $self->{htmlConverter}=
118 Alvis::HTML->new(alvisKeep=>0,
119 alvisRemove=>1,
120 obsolete=>1,
121 proprietary=>1,
122 xhtml=>1,
123 wml=>1,
124 keepAll=>1,
125 assertHTML=>0,
126 convertCharEnts=>$self->{convertCharEnts},
127 convertNumEnts=>$self->{convertNumEnts},
128 sourceEncoding=>$self->{sourceEncoding}
129 );
130 if (!defined($self->{htmlConverter}))
131 {
132 $self->_set_err_state($ERR_NO_HTML_CONV,
133 $self->{htmlConverter}->errmsg());
134 return undef;
135 }
136  
137 #
138 # Used for removing all HTML tags from parts of the document
139 # that don't allow any (like section titles)
140 #
141 $self->{htmlTagCleaner}=
142 Alvis::HTML->new(alvisKeep=>1,
143 alvisRemove=>1,
144 obsolete=>1,
145 proprietary=>1,
146 xhtml=>1,
147 wml=>1,
148 keepAll=>1,
149 assertHTML=>0,
150 convertCharEnts=>$self->{convertCharEnts},
151 convertNumEnts=>$self->{convertNumEnts},
152 sourceEncoding=>$self->{sourceEncoding}
153 );
154 if (!defined($self->{htmlTagCleaner}))
155 {
156 $self->_set_err_state($ERR_NO_HTML_CLEAN,
157 $self->{htmlTagCleaner}->errmsg());
158 return undef;
159 }
160  
161 return $self;
162 }
163  
164 sub _init
165 {
166 my $self=shift;
167  
168 $self->{warnings}=$DEF_WARNINGS;
169 $self->{convertCharEnts}=$DEF_CONVERT_CHAR_ENTS;
170 $self->{convertNumEnts}=$DEF_CONVERT_NUM_ENTS;
171 $self->{sourceEncoding}=$DEF_SRC_ENC;
172  
173 if (defined(@_))
174 {
175 my %args=@_;
176 @$self{ keys %args }=values(%args);
177 }
178 }
179  
180 #########################################################################
181 #
182 # Public methods
183 #
184 ######################################################################
185  
186 #
187 # Converts (hopefully) any old dirty HTML to Alvis MS3.2 DTD -valid
188 # canonicalDocument's contents.
189 #
190 sub HTML
191 {
192 my $self=shift;
193 my $html=shift;
194 my $opts=shift; # if a title/base URL is wished for as well, they are
195 # returned in a header hash with the same keys
196 #
197 # title
198 # baseURL
199 # sourceEncoding
200
201 $self->_set_err_state($ERR_OK); # clean the slate
202  
203 my ($title,$baseURL,$src_enc);
204 $title=$opts->{title} if exists($opts->{title} );
205 $baseURL=$opts->{title} if exists($opts->{baseURL} );
206 $src_enc=$self->{sourceEncoding};
207 $src_enc=$opts->{sourceEncoding} if exists($opts->{sourceEncoding} );
208  
209 my ($contents,$header)=
210 $self->{htmlConverter}->clean($html,
211 {title=>$title,
212 baseURL=>$baseURL,
213 sourceEncoding=>$src_enc});
214 if (!defined($contents))
215 {
216 $self->_set_err_state($ERR_HTML_CONV,"In HTML converter: " .
217 $self->{htmlConverter}->errmsg());
218 return (undef,$header)
219 }
220  
221 if ($DEBUG)
222 {
223 open(F,">candoc.cleaned");
224 print F $contents;
225 close(F);
226 }
227  
228  
229 # To safeguard the element contents with regard to XML
230 $contents=$self->_make_txt_XML_safe($contents);
231  
232 # Here goes
233 my $can_doc=$self->_contents2canDoc($contents,$header,$src_enc);
234 if (!defined($can_doc))
235 {
236 $self->_set_err_state($ERR_CONT2CAN_DOC);
237 return (undef,$header);
238 }
239  
240 return ($can_doc,$header);
241 }
242  
243 #########################################################################
244 #
245 # Private methods
246 #
247 ######################################################################
248  
249 sub _contents2canDoc
250 {
251 my $self=shift;
252 my $contents=shift; # contains relevant HTML markup
253 my $header=shift; # will be updated with information like links
254 my $source_encoding=shift;
255  
256 my $can_doc;
257  
258 if ($DEBUG)
259 {
260 open(F,">candoc.cleanNXMLSafe");
261 print F $contents;
262 close(F);
263 }
264 # Convert in order of importance to the structure
265 $can_doc=$self->_handle_sections($contents,$source_encoding);
266 if ($DEBUG)
267 {
268 my $can_doc2=$self->_to_alvis($can_doc);
269 $can_doc2=$self->_pretty_print($can_doc2);
270 open(F,">candoc.aftersections");
271 print F $can_doc2;
272 close(F);
273 }
274 $can_doc=$self->_handle_lists($can_doc);
275 if ($DEBUG)
276 {
277 my $can_doc2=$self->_to_alvis($can_doc);
278 $can_doc2=$self->_pretty_print($can_doc2);
279 open(F,">candoc.afterlists");
280 print F $can_doc2;
281 close(F);
282 }
283 $can_doc=$self->_handle_links($can_doc,$header);
284  
285 if ($DEBUG)
286 {
287 my $can_doc2=$self->_to_alvis($can_doc);
288 $can_doc2=$self->_pretty_print($can_doc2);
289 open(F,">candoc.afterlinks");
290 print F $can_doc2;
291 close(F);
292 }
293 # OK, time to put some make-up on and go out
294 $can_doc=$self->_to_alvis($can_doc);
295 if ($DEBUG)
296 {
297 my $can_doc2=$self->_pretty_print($can_doc);
298 open(F,">candoc.aftertoalvis");
299 print F $can_doc2;
300 close(F);
301 }
302 $can_doc=$self->_pretty_print($can_doc);
303 if ($DEBUG)
304 {
305 open(F,">candoc.afterprettyprint");
306 print F $can_doc;
307 close(F);
308 }
309  
310
311  
312 return $can_doc;
313 }
314  
315 #
316 # &lt;foo&gt; => <foo> for Alvis tags
317 #
318 sub _to_alvis
319 {
320 my $self=shift;
321 my $can_doc=shift;
322  
323 $can_doc=~s/\&lt;((?:\/)?(?:section|list|item|ulink)(?:\s.*?)?)\&gt;/$self->_alvis_tags2chars($1)/sgoe;
324  
325 return $can_doc;
326 }
327  
328 #
329 # Indent and remove empty space
330 #
331 sub _pretty_print
332 {
333 my $self=shift;
334 my $can_doc=shift;
335  
336 # Remove all extra space
337 $can_doc=~s/\n/ /sgo;
338 $can_doc=~s/\s+/ /sgo;
339  
340 # Remove emptiness
341 $can_doc=~s/<item>\s*<\/item>//sgo;
342 $can_doc=~s/<list>\s*<\/list>//sgo;
343 $can_doc=~s/<section>\s*<\/section>//sgo;
344 $can_doc=~s/<((?:item|list|section)(?:\s.*?)?)>\s+/<$1>/sgo;
345 $can_doc=~s/\s+<\/((?:item|list|section)(?:\s.*?)?)>/<\/$1>/sgo;
346  
347 # Indent
348 $self->{tagLevel}=-1;
349 $can_doc=~s/(<(\/)?(section|list|item)(?:\s.*?)?>)/$self->_indent($1,$2,$3)/esgo;
350  
351 return $can_doc;
352 }
353  
354 sub _indent
355 {
356 my $self=shift;
357 my $all=shift;
358 my $end=shift;
359 my $tag=shift;
360  
361 if ($end)
362 {
363 $self->{tagLevel}--;
364 return $all;
365 }
366 else
367 {
368 $self->{tagLevel}++;
369 my $ind=join("",(" " x $self->{tagLevel}));
370 return "\n$ind$all";
371 }
372
373 }
374  
375 #
376 # Find the basic <section> structure. We might add to it, if any
377 # potential <list> would contain anything created here.
378 #
379 sub _handle_sections
380 {
381 my $self=shift;
382 my $contents=shift;
383 my $source_encoding=shift;
384  
385 my $can_doc;
386  
387 # Headers first
388 # Fix the headers -- no interleaving allowed. Makes
389 # any subsequent decisions much easier.
390 $self->{headerOpen}=0;
391 $self->{headerLevel}=0;
392 $contents=~s/(\&lt;(\/)?(?:(?i)H(\d))(?:\s.*?)?\&gt;)/$self->_fix_headers($1,$2,$3)/sgoe;
393 # Convert headers to sections with proper structure
394 $self->{stack}=();
395 $contents=~s/\&lt;(?:(?i)H(\d))(?:\s.*?)?\&gt;(.*?)\&lt;\/[Hh]\1\&gt;/$self->_header($1,$2,$source_encoding)/iesgo;
396 # Close any open headers
397 while (defined(my $open_header=pop(@{$self->{stack}})))
398 {
399 $contents.="\&lt;/section\&gt;";
400 }
401 # There can be free text at either end
402 $contents="\&lt;section\&gt;\n$contents\&lt;/section\&gt;";
403  
404 if ($DEBUG)
405 {
406 open(F,">candoc.afterheaders");
407 my $c2=$self->_to_alvis($contents);
408 $c2=$self->_pretty_print($c2);
409 print F $c2;
410 close(F);
411 }
412 # Now, do DIVs & Ps, in that order, but similarly. They are thought
413 # to define paragraphs and be overruled by headers.
414 $self->{stack}=();
415 $contents=~s/(\&lt;(\/)?((?:(?i)DIV)|section)(?:\s.*?)?\&gt;)/$self->_paragraph($1,$2,$3)/sgoe;
416 # Close all tags left open with </section>
417 while (defined(my $open_alvis_tag=pop(@{$self->{stack}})))
418 {
419 if ($open_alvis_tag=~/^(section|div)$/o)
420 {
421 $contents.="\&lt;/section\&gt;";
422 }
423 else
424 {
425 die("Should be impossible: non-section/div opening tag " .
426 "($open_alvis_tag) left on stack.");
427 }
428 }
429  
430 if ($DEBUG)
431 {
432 open(F,">candoc.afterdivs");
433 my $c2=$self->_to_alvis($contents);
434 $c2=$self->_pretty_print($c2);
435 print F $c2;
436 close(F);
437 }
438  
439 $self->{stack}=();
440 $contents=~s/(\&lt;(\/)?((?:(?i)P)|section)(?:\s.*?)?\&gt;)/$self->_paragraph($1,$2,$3)/sgoe;
441 # close all tags left open with </section>
442 while (defined(my $open_alvis_tag=pop(@{$self->{stack}})))
443 {
444 if ($open_alvis_tag=~/^(section|p)$/o)
445 {
446 $contents.="\&lt;/section\&gt;";
447 }
448 else
449 {
450 die("Should be impossible: non-section/div opening tag " .
451 "($open_alvis_tag) left on stack.");
452 }
453 }
454 if ($DEBUG)
455 {
456 open(F,">candoc.afterps");
457 my $c2=$self->_to_alvis($contents);
458 $c2=$self->_pretty_print($c2);
459 print F $c2;
460 close(F);
461 }
462  
463  
464 return $contents;
465 }
466  
467 #
468 # List-type things into <list>s, or <section>s if they would contain any
469 # already defined <section>s.
470 #
471 sub _handle_lists
472 {
473 my $self=shift;
474 my $contents=shift;
475  
476 my $can_doc;
477  
478 if ($DEBUG)
479 {
480 open(F,">contents.beforelists");
481 print F $contents;
482 close(F);
483 }
484 # OL,UL,DL,TABLE
485 # First pass: Find out which ones do NOT contain already defined
486 # <section>s => <list>s, others => <section>s
487 #
488 $self->{stack}=();
489 $self->{listTagTypes}=(); # list of types, i.e. 'list' or 'section'
490 # in order of appearance for each potential
491 # <list>
492 $contents=~s/(\&lt;(\/)?(DL|UL|OL|TABLE|section)(?:\s.*?)?\&gt;)/$self->_lists_first_pass($1,$2,$3)/isgoe;
493 # Mark all list-type tags left open as <list>s
494 while (defined(my $open_tag=pop(@{$self->{stack}})))
495 {
496 push(@{$self->{listTagTypes}},'list');
497 }
498  
499 #
500 # Second pass: Convert proper list-type tags, including item-type tags,
501 # make others <section>s, and delete any loose end tags
502 #
503 $self->{stack}=();
504 $contents=~s/\&lt;(\/)?(((?i)DL|UL|OL|TABLE|LI|DD|DT|TH|TD|CAPTION))(?:\s.*?)?\&gt;/$self->_lists_second_pass($1,$2)/sgoe;
505 # close all tags left open
506 while (defined(my $open_alvis_tag=pop(@{$self->{stack}})))
507 {
508 if ($open_alvis_tag=~/^(section|list|item)$/)
509 {
510 $contents.="\&lt;/$open_alvis_tag\&gt;";
511 }
512 else
513 {
514 die("Should be impossible: non-section/list/item opening tag " .
515 "($open_alvis_tag) left on stack.");
516 }
517 }
518  
519 return $contents;
520 }
521  
522 #
523 # Convert a single header tag of a given level 1,..,6
524 #
525 sub _header
526 {
527 my $self=shift;
528 my $level=shift;
529 my $contents=shift;
530 my $source_encoding=shift;
531
532 my ($l,$txt,$title);
533 $txt="";
534 #
535 # Close all headers that do not overwrap this one
536 #
537 while (defined($l=pop(@{$self->{stack}})) && $l >= $level)
538 {
539 $txt.="\&lt;/section\&gt;";
540 }
541 push(@{$self->{stack}},$l) if defined $l;
542 push(@{$self->{stack}},$level);
543  
544 #
545 # Remove all HTML tags from the title that goes into the attribute of
546 # <section>, and make it otherwise a valid XML attribute value.
547 # But leave the title as is for the first subsection.
548 #
549 $title=$contents;
550 $title=$self->_struct_ents2chars($title);
551 my $header;
552 ($title,$header)=$self->{htmlTagCleaner}->clean($title,
553 {title=>undef,
554 baseURL=>undef,
555 sourceEncoding=>
556 $source_encoding});
557 if (defined($title))
558 {
559 $title=$self->_make_txt_XML_safe($title);
560 $title=$self->_make_attr_XML_safe($title);
561 $title=$self->_make_title_safe($title);
562 $title=~s/\n/ /sgo;
563 $title=~s/\s+/ /sgo;
564 $title=~s/^\s*//sgo;
565 $title=~s/\s*$//sgo;
566  
567 $txt.="\&lt;section title=\"$title\"\&gt;\n";
568 $txt.="\&lt;section\&gt;$contents\&lt;/section\&gt;\n";
569 }
570 else
571 {
572 $txt.="\&lt;section\&gt;\n";
573 }
574  
575 return $txt;
576 }
577  
578 #
579 # Make it so no headers interleave. If one opens inside another, close
580 # the previous one. If one closes inside another, remove. Either it
581 # has already been closed, or it has no start tag.
582 #
583 sub _fix_headers
584 {
585 my $self=shift;
586 my $all=shift;
587 my $end=shift;
588 my $level=shift;
589
590 my $txt="";
591 if ($end)
592 {
593 if ($self->{headerOpen} && $level==$self->{headerLevel})
594 {
595 $self->{headerOpen}=0;
596 $self->{headerLevel}=0;
597 $txt.=$all;
598 }
599 else
600 {
601 if ($self->{warnings})
602 {
603 $txt.="\&lt;!-- Alvis::Canonical warning: H$level close tag " .
604 "inside another header tag. Fixed. --\&gt;";
605 }
606 }
607 }
608 else # a start tag
609 {
610 if ($self->{headerOpen})
611 {
612 if ($self->{warnings})
613 {
614 $txt.="\&lt;!-- Alvis::Canonical warning: H$level start " .
615 "tag while waiting for a H$self->{headerLevel} " .
616 "closing tag. Fixed. --\&gt;";
617 }
618 $txt.="\&lt;\/H$self->{headerLevel}\&gt;";
619 }
620 $self->{headerOpen}=1;
621 $self->{headerLevel}=$level;
622 $txt.=$all;
623 }
624  
625 return $txt;
626 }
627  
628 #
629 # Convert a DIV or a P. Semantics: make a paragraph break
630 # and let the header-induced section structure prevail.
631 #
632 sub _paragraph
633 {
634 my $self=shift;
635 my $all=shift;
636 my $end=shift;
637 my $tag=shift;
638  
639 my $txt="";
640 # If it's an end tag
641 if ($end)
642 {
643 if ($tag=~/^(div|p)$/io)
644 {
645 if (defined(my $context=pop(@{$self->{stack}})))
646 {
647 if ($context=~/^(div|p)$/o)
648 {
649 $txt.="\&lt;/section\&gt;"; # clean closing
650 }
651 else
652 {
653 # Make a break and restore stack
654 # $txt.="\&lt;/section\&gt;\n\&lt;section\&gt;";
655 push(@{$self->{stack}},$context);
656 }
657 }
658 # Otherwise, remove...it's a loose end tag
659 }
660 elsif ($tag eq 'section')
661 {
662 my $context;
663 while (defined($context=pop(@{$self->{stack}})) &&
664 $context=~/^(div|p)$/o)
665 {
666 # Close any open "paragraph"
667 $txt.="\&lt;/section\&gt;\n";
668 }
669 if (!defined($context))
670 {
671 die("Should be impossible: no open section.");
672 }
673 if ($context ne 'section')
674 {
675 die("Should be impossible: open tag not a section or DIV/P.");
676 }
677 $txt.=$all;
678 }
679 else
680 {
681 die("Should be impossible: unrecognized stack item ($tag).");
682 }
683 }
684 else # a start tag
685 {
686 if ($tag eq 'section')
687 {
688 # Close all immediate DIV/P sections in the context and
689 # restore a header-induced section tag, if any
690 if (defined(my $context=pop(@{$self->{stack}})))
691 {
692 if ($context=~/^(div|p)$/o)
693 {
694 my $open_tag;
695 $txt.="\&lt;/section\&gt;";
696 while (defined($open_tag=pop(@{$self->{stack}})) &&
697 $open_tag=~/^(div|p)$/o)
698 {
699 $txt.="\&lt;/section\&gt;";
700 }
701 if (defined($open_tag))
702 {
703 push(@{$self->{stack}},$open_tag);
704 }
705 }
706 else
707 {
708 push(@{$self->{stack}},$context);
709 }
710 }
711 $txt.=$all;
712 }
713 elsif ($tag=~/^(div|p)$/io)
714 {
715 $txt.="\&lt;section\&gt;";
716 }
717 else
718 {
719 die("Should be impossible: A tag that is neither a " .
720 "section,DIV or P ($tag).");
721 }
722  
723 push(@{$self->{stack}},lc($tag)); # remember to normalize
724 }
725  
726 return $txt;
727 }
728  
729 #
730 # Make a first pass over potential lists, and separate them to
731 # those that contain already-defined <section>s and the rest.
732 # The whole purpose is to build $self->{listTagTypes}, a list
733 # of <list>-type tag result types in order of appearance.
734 #
735 sub _lists_first_pass
736 {
737 my $self=shift;
738 my $all=shift;
739 my $end=shift;
740 my $tag=shift;
741  
742 if ($DEBUG)
743 {
744 warn "FIRST: TAG:$end$tag";
745 warn "FIRST: STACK:",join(",",@{$self->{stack}}) if defined($self->{stack});
746 warn "FIRST: TLIST:",join(",",@{$self->{listTagTypes}}) if defined($self->{listTagTypes});
747 }
748
749 # If it's an end tag
750 if ($end)
751 {
752 if ($tag=~/^(dl|ol|ul|table)$/io)
753 {
754 # OK, the immediate preceding tag is good for a <list>
755 if (defined(my $context=pop(@{$self->{stack}})))
756 {
757 push(@{$self->{listTagTypes}},'list');
758 }
759 }
760 elsif ($tag eq 'section')
761 {
762 # mark all preceding open <list>-type tags as sections
763 while (defined(my $context=pop(@{$self->{stack}})))
764 {
765 push(@{$self->{listTagTypes}},'section');
766 }
767 }
768 else
769 {
770 die("Should be impossible: a non-section/<list>-type start " .
771 "tag ($tag).");
772 }
773 }
774 else # a start tag
775 {
776 if ($tag eq 'section')
777 {
778 # ALL <list>-type tags above need to become sections
779 while (defined(my $context=pop(@{$self->{stack}})))
780 {
781 push(@{$self->{listTagTypes}},'section');
782 }
783 }
784 elsif ($tag=~/^(dl|ol|ul|table)$/io)
785 {
786 push(@{$self->{stack}},lc($tag)); # remember to normalize
787 }
788 else
789 {
790 die("Should be impossible: a non-section/<list>-type start tag " .
791 "($tag)");
792 }
793 }
794  
795 return $all;
796 }
797  
798 #
799 # Make the second pass. This is by far the most complex step.
800 # canonicalDocument lists consist of <list>s that contain <item>s,
801 # and the corresponding HTML tags might occur anywhere, since we
802 # make no assumptions about the cleanliness of the HTML.
803 #
804 sub _lists_second_pass
805 {
806 my $self=shift;
807 my $end=shift;
808 my $tag=shift;
809  
810 if ($DEBUG)
811 {
812 warn "2nd: TAG:$end$tag";
813 warn "2nd: STACK:",join(",",@{$self->{stack}}) if defined($self->{stack});
814 warn "2nd: TLIST:",join(",",@{$self->{listTagTypes}}) if defined($self->{listTagTypes});
815 }
816 my $txt="";
817 #
818 # If it's an end tag
819 #
820 if ($end)
821 {
822 my $context;
823 if (defined($context=pop(@{$self->{stack}})))
824 {
825 if ($tag=~/^(dl|ol|ul|table)$/io)
826 {
827 if ($context eq 'list')
828 {
829 $txt.="\&lt;/list\&gt;";
830 }
831 elsif ($context eq 'section')
832 {
833 $txt.="\&lt;/section\&gt;";
834 }
835 elsif ($context eq 'item')
836 {
837 $txt.="\&lt;/item\&gt;";
838 $context=pop(@{$self->{stack}});
839 if (!defined($context))
840 {
841 die("Should be impossible: item-type tag on " .
842 "stack without a context.");
843 }
844 if ($context ne 'list')
845 {
846 die("Should be impossible: item-type tag on " .
847 "stack with a non-list-type context.");
848 }
849 $txt.="\&lt;/list\&gt;";
850 }
851 else
852 {
853 die("Should be impossible: unrecognized stack item.");
854 }
855 }
856 elsif ($tag=~/^(li|dd|dt|th|td|caption)$/io) # item-type tag
857 {
858 if ($context eq 'item')
859 {
860 $txt.="\&lt;/item\&gt;";
861 # just in case there's some loose text at the end of list
862 $txt.="\&lt;item\&gt;";
863 push(@{$self->{stack}},'item');
864 }
865 else
866 {
867 #
868 # ignore i.e. remove and restore stack
869 #
870 push(@{$self->{stack}},$context);
871 $txt="";
872 }
873 }
874 else
875 {
876 die("Should be impossible: non-list type tag ($tag).");
877 }
878 }
879 # otherwise, remove a loose end tag
880 }
881 else # a start tag
882 {
883 if ($tag=~/^(table|dl|ol|ul)$/io)
884 {
885 # Check out this tag's type (as determined during the 1st pass)
886 my $type=shift(@{$self->{listTagTypes}});
887 if (defined($type))
888 {
889 if ($type eq 'list')
890 {
891 my $context=pop(@{$self->{stack}});
892 if (defined($context))
893 {
894 if ($context eq 'section')
895 {
896 $txt.="\n\&lt;list\&gt;";
897 push(@{$self->{stack}},$context);
898 push(@{$self->{stack}},'list');
899 # just in case there's some loose text in the
900 # beginning
901 $txt.="\&lt;item\&gt;";
902 push(@{$self->{stack}},'item');
903 }
904 elsif ($context=~/^(list|item)$/o)
905 {
906 # otherwise ignore silently .. 1-D lists only
907 # for simplicity + restore the stack
908 push(@{$self->{stack}},$context);
909
910 }
911 else
912 {
913 die("Should be impossible: Unrecognized " .
914 "context type (not 'list', 'item' or " .
915 "'section')($context)");
916 }
917 }
918 else # No context to worry about
919 {
920 $txt.="\n\&lt;list\&gt;";
921 push(@{$self->{stack}},'list');
922 # just in case there's some loose text in the beginning
923 $txt.="\&lt;item\&gt;";
924 push(@{$self->{stack}},'item');
925 }
926 }
927 elsif ($type eq 'section') # section-type list start tag
928 {
929 my $context=pop(@{$self->{stack}});
930 if (defined($context))
931 {
932 if ($context eq 'section')
933 {
934 $txt.="\n\&lt;section\&gt;";
935 push(@{$self->{stack}},$context);
936 push(@{$self->{stack}},'section');
937 }
938 elsif ($context=~/^(list|item)$/o)
939 {
940 # Close the previous lists/items and remove
941 # from the stack
942 $txt.="\&lt;/$context\&gt;";
943 while (defined($context=pop(@{$self->{stack}})) &&
944 $context=~/^(list|item)$/o)
945 {
946 $txt.="\&lt;/$context\&gt;";
947 }
948 if (defined($context))
949 {
950 if ($context eq 'section')
951 {
952 # restore a section context, if any
953 push(@{$self->{stack}},$context);
954 }
955 else
956 {
957 die("Should be impossible: non-section/" .
958 "list/item type context ($context)");
959 }
960 }
961  
962 $txt.="\n\&lt;section\&gt;";
963 push(@{$self->{stack}},'section');
964 }
965 else
966 {
967 die("Should be impossible: Unrecognized " .
968 "context type (non-section/list/item) " .
969 "($context)");
970 }
971 }
972 else # no context for a section start tag
973 {
974 $txt.="\n\&lt;section&gt;";
975 push(@{$self->{stack}},'section');
976 }
977 }
978 else # non-list/section type
979 {
980 die("Should be impossible: list start " .
981 "tag with a funky type ($type).");
982 }
983 }
984 else # no predefined type
985 {
986 die("Should be impossible: list start tag " .
987 "with no corresponding type.");
988 }
989 }
990 elsif ($tag=~/^(li|dd|dt|th|td|caption)$/io) # a potential item start tag
991 {
992 my $context=pop(@{$self->{stack}});
993 if (defined($context))
994 {
995 my $type;
996 if ($context eq 'list')
997 {
998 $txt.="\n \&lt;item\&gt;";
999 push(@{$self->{stack}},$context);
1000 push(@{$self->{stack}},'item');
1001
1002 }
1003 elsif ($context eq 'item')
1004 {
1005 # close the previous item
1006 $txt.="\&lt;/item\&gt;\n \&lt;item\&gt;";
1007 push(@{$self->{stack}},'item');
1008 }
1009 elsif ($context eq 'section')
1010 {
1011 # List start tag missing or converted to a section.
1012 # Several alternatives would make sense, but for now,
1013 # close the preceding section and start a new one.
1014 # Avoids unnecessary structural depth.
1015 $txt.="\&lt;/section\&gt;\n \&lt;section\&gt;";
1016 push(@{$self->{stack}},$context);
1017 }
1018 }
1019 # Otherwise it's a loose item-type start tag => remove
1020 }
1021 else
1022 {
1023 die("Should be impossible: a list tag that is neither of " .
1024 "<list> or <item> type ($tag).");
1025 }
1026 }
1027  
1028 return $txt;
1029 }
1030  
1031 sub _handle_links
1032 {
1033 my $self=shift;
1034 my $can_doc=shift;
1035 my $header=shift;
1036  
1037 #
1038 # Fix links which contain already defined Alvis structures or other
1039 # links (links cannot nest in Alvis)
1040 #
1041 if ($DEBUG)
1042 {
1043 my $can_doc2=$self->_to_alvis($can_doc);
1044 $can_doc2=$self->_pretty_print($can_doc2);
1045 open(F,">candoc.before");
1046 print F $can_doc2;
1047 close(F);
1048 }
1049  
1050 $self->{stack}=();
1051 $can_doc=~s/(\&lt;(\/)?((?:(?i)A|FRAME|IFRAME)|section|list|item)(?:\s.*?)?\&gt;)/$self->_fix_links($1,$2,$3)/sgoe;
1052 # close all tags left open
1053 # warn "STACK:", join("|",@{$self->{stack}});
1054 while (defined(my $open_alvis_tag=pop(@{$self->{stack}})))
1055 {
1056 if ($open_alvis_tag=~/^(a|frame|iframe)$/o)
1057 {
1058 $can_doc.="\&lt;/$open_alvis_tag\&gt;";
1059 }
1060 else
1061 {
1062 die("Should be impossible: non-link opening tag " .
1063 "($open_alvis_tag) left on stack.");
1064 }
1065 }
1066  
1067 if ($DEBUG)
1068 {
1069 $can_doc=$self->_to_alvis($can_doc);
1070 $can_doc=$self->_pretty_print($can_doc);
1071 open(F,">candoc.after");
1072 print F $can_doc;
1073 }
1074  
1075 $self->{stack}=();
1076 $can_doc=~s/\&lt;(A|FRAME|IFRAME)(\s.*?)?\&gt;(.*?)\&lt;\/\1\&gt;/$self->_link($1,$2,$3,$header)/isgoe;
1077  
1078 if ($DEBUG)
1079 {
1080 my $can_doc2=$self->_to_alvis($can_doc);
1081 $can_doc2=$self->_pretty_print($can_doc2);
1082 open(F,">candoc.after_link");
1083 print F $can_doc2;
1084 }
1085  
1086 return $can_doc;
1087 }
1088  
1089 #
1090 # Fixes links so they do not interleave with each other or ANY kind of element
1091 #
1092 sub _fix_links
1093 {
1094 my $self=shift;
1095 my $all=shift;
1096 my $end=shift;
1097 my $tag=shift;
1098  
1099 if ($DEBUG)
1100 {
1101 warn "ALL:$all";
1102 warn "STACK NOW:",join("|",@{$self->{stack}}) if defined($self->{stack});
1103 }
1104  
1105 my $txt="";
1106 # If it's an end tag
1107 if ($end)
1108 {
1109 if ($tag=~/^(a|frame|iframe)$/io)
1110 {
1111 # Close an immediate matching link tag in the context, if any
1112 if (defined(my $context=pop(@{$self->{stack}})))
1113 {
1114 if ($context eq lc($tag))
1115 {
1116 $txt.="\&lt;/$context\&gt;";
1117 }
1118 else
1119 {
1120 # ignore this closing tag, it's misplaced/overruled
1121 push(@{$self->{stack}},$context);
1122 }
1123 }
1124 # ignore this closing tag, it's misplaced/overruled
1125 }
1126 elsif ($tag=~/^(section|list|item)$/o)
1127 {
1128 # Close an immediate link tag in the context, if any
1129 if (defined(my $context=pop(@{$self->{stack}})))
1130 {
1131 if ($context=~/^(a|frame|iframe)$/)
1132 {
1133 $txt.="\&lt;/$context\&gt;";
1134 # close the surrounding structure
1135 if (defined(my $context=pop(@{$self->{stack}})))
1136 {
1137 if ($tag ne $context)
1138 {
1139 die("Should be impossible: mismatch of already " .
1140 "fixed immediate Alvis opening tag ($context) " .
1141 "and closing tag ($tag).");
1142 }
1143
1144 }
1145 else
1146 {
1147 die("Should be impossible: no already fixed " .
1148 "immediate Alvis $tag tag to close surrounding " .
1149 "a link tag.");
1150 }
1151 }
1152 else # non-link context
1153 {
1154 if ($tag ne $context)
1155 {
1156 die("Should be impossible: mismatch of already " .
1157 "fixed immediate Alvis opening tag ($context) " .
1158 "and closing tag ($tag).");
1159 }
1160 }
1161 }
1162 else # no context to close...wtf?
1163 {
1164 die("Should be impossible: no already fixed immediate Alvis " .
1165 "$tag tag to close");
1166 }
1167 $txt.=$all;
1168 }
1169 else
1170 {
1171 die("Should be impossible: unrecognized closing tag type ($tag).");
1172 }
1173 }
1174 else # a start tag
1175 {
1176 # Whatever the tag is,
1177 # close an immediate matching link tag in the context, if any
1178 if (defined(my $context=pop(@{$self->{stack}})))
1179 {
1180 if ($context=~/^(a|frame|iframe)$/)
1181 {
1182 $txt.="\&lt;/$context\&gt;";
1183 }
1184 else
1185 {
1186 push(@{$self->{stack}},$context);
1187 }
1188 }
1189  
1190 push(@{$self->{stack}},lc($tag)); # remember to normalize
1191  
1192 $txt.=$all;
1193 }
1194  
1195 return $txt;
1196 }
1197  
1198 sub _link
1199 {
1200 my $self=shift;
1201 my $tag=shift;
1202 my $params=shift;
1203 my $text=shift;
1204 my $header=shift;
1205  
1206 my $txt="";
1207 my $url;
1208  
1209 my %link=();
1210 $link{type}=lc($tag);
1211 if ($link{type} eq 'a')
1212 {
1213 if (defined($params) && $params=~/href\s*=\s*([\"\'])(.*?)\1/isgo)
1214 {
1215 $url=$self->_handle_url($2,$header->{baseURL});
1216 }
1217 }
1218 elsif ($link{type}=~/^(frame|iframe)$/o)
1219 {
1220 if (defined($params) && $params=~/src\s*=\s*([\'\"])(.*?)\1/isgo)
1221 {
1222 $url=$self->_handle_url($2,$header->{baseURL});
1223 }
1224 }
1225 else
1226 {
1227 die("Should be impossible: Unrecognized link type ($tag).");
1228 }
1229  
1230 $text=~s/^\s+//isgo;
1231 $text=~s/\s+$//isgo;
1232  
1233 # If the URL is ok, proceed
1234 if (defined($url))
1235 {
1236 $url=$self->_make_attr_XML_safe($url);
1237  
1238 $link{url}=$url;
1239  
1240 if (defined($text))
1241 {
1242 $link{text}=$text;
1243 }
1244
1245 push(@{$header->{links}},\%link);
1246  
1247 if (defined($text))
1248 {
1249 $txt="\&lt;ulink url=\"$link{url}\"\&gt;$text\&lt;/ulink\&gt;";
1250 }
1251 else
1252 {
1253 $txt="\&lt;ulink url=\"$link{url}\"\&gt;\&lt;/ulink\&gt;";
1254 }
1255 }
1256 else # remove this non-interesting link (but retain the anchor text)
1257 {
1258 if (defined($text))
1259 {
1260 $txt="$text";
1261 }
1262 else
1263 {
1264 $txt="";
1265 }
1266 }
1267  
1268 return $txt;
1269 }
1270  
1271 sub _handle_url
1272 {
1273 my $self=shift;
1274 my $url=shift;
1275 my $base=shift;
1276  
1277 if ($url=~/^\#/)
1278 {
1279 return undef; # doc-internal
1280 }
1281 elsif ($url=~/^javascript:/)
1282 {
1283 return undef;
1284 }
1285 elsif (defined($base))
1286 {
1287 if ($url=~/^\.\.\//)
1288 {
1289 # a relative path
1290 my $u=$url;
1291 my $n=0;
1292 while ($u=~/^\.\.\/(.*)$/o)
1293 {
1294 $u=$1;
1295 $n++;
1296 }
1297 if (!defined($u))
1298 {
1299 $self->_set_err_state($ERR_MISFORMED_REL_URL,"($url)");
1300 return undef;
1301 }
1302 while ($base=~/^(.*\/).*?\/?$/o && $n>0)
1303 {
1304 $base=$1;
1305 $n--;
1306 }
1307 if (defined($base))
1308 {
1309 $url=$base . $u;
1310 }
1311 else
1312 {
1313 $self->_set_err_state($ERR_REL_URL_VS_BASE_MISMATCH);
1314 return undef;
1315 }
1316 }
1317 elsif ($url!~/^(?:\w*(?:\w|\d|\+|\-|\.)):/iso)
1318 {
1319 # Base-relative
1320 $url=$base . $url;
1321 }
1322 }
1323  
1324 return $url;
1325 }
1326  
1327 sub _loose_txt_at_list_start2items
1328 {
1329 my $self=shift;
1330 my $text=shift;
1331 my $next_tag=shift;
1332  
1333 my $txt="";
1334 if ($text!~/\&lt;(item|section|list)\&gt;/sgo)
1335 {
1336 # loose text
1337 $txt="\&lt;list\&gt;\&lt;item\&gt;$text\&lt;/item\&gt;\&lt;$next_tag\&gt;";
1338 }
1339 else
1340 {
1341 $txt="\&lt;list\&gt;$text\&lt;$next_tag\&gt;";
1342 }
1343  
1344 return $txt;
1345 }
1346  
1347 sub _loose_txt_at_list_end2items
1348 {
1349 my $self=shift;
1350 my $text=shift;
1351  
1352 my $txt="";
1353 if ($text=~/\S/sgo && $text!~/\&lt;\/?(item|section|list)\&gt;/go)
1354 {
1355 # loose text
1356 $txt="\&lt;/item\&gt;\&lt;item\&gt;$text\&lt;/item\&gt;\&lt;/list\&gt;";
1357 }
1358 else
1359 {
1360 $txt="\&lt;/item\&gt;$text\&lt;/list\&gt;";
1361 }
1362  
1363 return $txt;
1364 }
1365  
1366 sub _loose_txt_btw_items2items
1367 {
1368 my $self=shift;
1369 my $text=shift;
1370  
1371 my $txt="";
1372 if ($text=~/\S/sgo && $text!~/\&lt;\/?(item|section|list)\&gt;/go)
1373 {
1374 # loose text
1375 $txt="\&lt;/item\&gt;\&lt;item\&gt;$text\&lt;/item\&gt;\&lt;item\&gt;";
1376 }
1377 else
1378 {
1379 $txt="\&lt;/item\&gt;\&lt;item\&gt;";
1380 }
1381  
1382 return $txt;
1383 }
1384  
1385 sub _struct_chars2ents
1386 {
1387 my $self=shift;
1388 my $text=shift;
1389
1390 if (!defined($text))
1391 {
1392 return undef;
1393 }
1394  
1395 $text=~s/\&/\&amp;/go;
1396 $text=~s/</\&lt;/go;
1397 $text=~s/>/\&gt;/go;
1398  
1399 return $text;
1400 }
1401  
1402 sub _struct_ents2chars
1403 {
1404 my $self=shift;
1405 my $text=shift;
1406
1407 if (!defined($text))
1408 {
1409 return undef;
1410 }
1411  
1412 $text=~s/\&amp;/\&/go;
1413 $text=~s/\&lt;/</go;
1414 $text=~s/\&gt;/>/go;
1415  
1416 return $text;
1417 }
1418  
1419 sub _make_title_safe
1420 {
1421 my $self=shift;
1422 my $text=shift;
1423
1424 if (!defined($text))
1425 {
1426 return undef;
1427 }
1428  
1429 return $self->_make_txt_XML_safe($text);
1430 }
1431  
1432 sub _make_txt_XML_safe
1433 {
1434 my $self=shift;
1435 my $text=shift;
1436  
1437 if (!defined($text))
1438 {
1439 return undef;
1440 }
1441  
1442 $text=$self->_struct_chars2ents($text);
1443  
1444 #
1445 # Remove illegal chars (What a pain XML is!)
1446 #
1447 $text=~tr/\000-\010\013-\014\016-\037//d;
1448  
1449 return $text;
1450 }
1451  
1452 sub _make_attr_XML_safe
1453 {
1454 my $self=shift;
1455 my $text=shift;
1456  
1457 if (!defined($text))
1458 {
1459 return undef;
1460 }
1461  
1462 $text=~s/\"/\&quot;/go;
1463  
1464 return $text;
1465 }
1466  
1467 sub _alvis_tags2chars
1468 {
1469 my $self=shift;
1470 my $contents=shift;
1471  
1472 return "<$contents>";
1473 }
1474  
1475 1;
1476 __END__
« blib/lib/Alvis/Wikipedia/Variables.pm ↑ Summary blib/lib/Alvis/HTML.pm »