Annotation of loncom/homework/cleanxml/post_xml.pm, revision 1.10
1.1 damieng 1: # The LearningOnline Network
2: # Third step to clean a file.
3: #
1.10 ! damieng 4: # $Id: post_xml.pm,v 1.9 2016/01/21 22:09:38 damieng Exp $
1.1 damieng 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: #!/usr/bin/perl
31:
32: package Apache::post_xml;
33:
34: use strict;
35: use utf8;
36: use warnings;
37:
38: use File::Basename;
39: use File::Temp qw/ tempfile /;
40: use Cwd 'abs_path';
41: use XML::LibXML;
42: use HTML::TokeParser; # used to parse sty files
43: use Tie::IxHash; # for ordered hashes
1.7 damieng 44: use tth;
45: use Apache::html_to_xml;
1.1 damieng 46:
47: no warnings 'recursion'; # yes, fix_paragraph is using heavy recursion, I know
48:
49: # these are constants
1.4 damieng 50: my @block_elements = ('parameter','location','answer','foil','image','polygon','rectangle','text','conceptgroup','itemgroup','item','label','data','function','array','unit','answergroup','functionplotresponse','functionplotruleset','functionplotelements','functionplotcustomrule','essayresponse','hintpart','formulahint','numericalhint','reactionhint','organichint','optionhint','radiobuttonhint','stringhint','customhint','mathhint','formulahintcondition','numericalhintcondition','reactionhintcondition','organichintcondition','optionhintcondition','radiobuttonhintcondition','stringhintcondition','customhintcondition','mathhintcondition','imageresponse','foilgroup','datasubmission','textfield','hiddensubmission','radiobuttonresponse','rankresponse','matchresponse','import','style','script','window','block','library','notsolved','part','postanswerdate','preduedate','problem','problemtype','randomlabel','bgimg','labelgroup','randomlist','solved','while','tex','print','web','gnuplot','curve','Task','IntroParagraph','ClosingParagraph','Question','QuestionText','Setup','Instance','InstanceText','Criteria','CriteriaText','GraderNote','languageblock','instructorcomment','dataresponse','togglebox','standalone','comment','drawimage','allow','displayduedate','displaytitle','responseparam','organicstructure','scriptlib','parserlib','drawoptionlist','spline','backgroundplot','plotobject','plotvector','drawvectorsum','functionplotrule','functionplotvectorrule','functionplotvectorsumrule','axis','key','xtics','ytics','title','xlabel','ylabel','hiddenline','dtm');
51: my @inline_like_block = ('stringresponse','optionresponse','numericalresponse','formularesponse','mathresponse','organicresponse','reactionresponse','customresponse','externalresponse', 'hint', 'hintgroup','translated','lang'); # inline elements treated like blocks for pretty print and some other things
1.1 damieng 52: my @responses = ('stringresponse','optionresponse','numericalresponse','formularesponse','mathresponse','organicresponse','reactionresponse','customresponse','externalresponse','essayresponse','radiobuttonresponse','matchresponse','rankresponse','imageresponse','functionplotresponse');
53: my @block_html = ('html','head','body','section','h1','h2','h3','h4','h5','h6','div','p','ul','ol','li','table','tbody','tr','td','th','dl','dt','dd','pre','noscript','hr','address','blockquote','object','applet','embed','map','form','fieldset','iframe','center','frameset');
54: my @no_newline_inside = ('import','parserlib','scriptlib','data','function','label','xlabel','ylabel','tic','text','rectangle','image','title','h1','h2','h3','h4','h5','h6','li','td','p');
1.6 damieng 55: my @preserve_elements = ('script','answer','pre','style');
1.1 damieng 56: my @accepting_style = ('section','h1','h2','h3','h4','h5','h6','div','p','li','td','th','dt','dd','pre','blockquote');
57: my @latex_math = ('\alpha', '\theta', '\omicron', '\tau', '\beta', '\vartheta', '\pi', '\upsilon', '\gamma', '\gamma', '\varpi', '\phi', '\delta', '\kappa', '\rho', '\varphi', '\epsilon', '\lambda', '\varrho', '\chi', '\varepsilon', '\mu', '\sigma', '\psi', '\zeta', '\nu', '\varsigma', '\omega', '\eta', '\xi',
58: '\Gamma', '\Lambda', '\Sigma', '\Psi', '\Delta', '\Xi', '\Upsilon', '\Omega', '\Theta', '\Pi', '\Phi',
59: '\pm', '\cap', '\diamond', '\oplus', '\mp', '\cup', '\bigtriangleup', '\ominus', '\times', '\uplus', '\bigtriangledown', '\otimes', '\div', '\sqcap', '\triangleleft', '\oslash', '\ast', '\sqcup', '\triangleright', '\odot', '\star', '\vee', '\lhd$', '\bigcirc', '\circ', '\wedge', '\rhd$', '\dagger', '\bullet', '\setminus', '\unlhd$', '\ddagger', '\cdot', '\wr', '\unrhd$', '\amalg', '+', '-',
60: '\leq', '\geq', '\equiv', '\models', '\prec', '\succ', '\sim', '\perp', '\preceq', '\succeq', '\simeq', '\mid', '\ll', '\gg', '\asymp', '\parallel', '\subset', '\supset', '\approx', '\bowtie', '\subseteq', '\supseteq', '\cong', '\Join$', '\sqsubset$', '\sqsupset$', '\neq', '\smile', '\sqsubseteq', '\sqsupseteq', '\doteq', '\frown', '\in', '\ni', '\propto', '\vdash', '\dashv',
61: '\colon', '\ldotp', '\cdotp',
62: '\leftarrow', '\longleftarrow', '\uparrow', '\Leftarrow', '\Longleftarrow', '\Uparrow', '\rightarrow', '\longrightarrow', '\downarrow', '\Rightarrow', '\Longrightarrow', '\Downarrow', '\leftrightarrow', '\longleftrightarrow', '\updownarrow', '\Leftrightarrow', '\Longleftrightarrow', '\Updownarrow', '\mapsto', '\longmapsto', '\nearrow', '\hookleftarrow', '\hookrightarrow', '\searrow', '\leftharpoonup', '\rightharpoonup', '\swarrow', '\leftharpoondown', '\rightharpoondown', '\nwarrow', '\rightleftharpoons', '\leadsto$',
63: '\ldots', '\cdots', '\vdots', '\ddots', '\aleph', '\prime', '\forall', '\infty', '\hbar', '\emptyset', '\exists', '\Box$', '\imath', '\nabla', '\neg', '\Diamond$', '\jmath', '\surd', '\flat', '\triangle', '\ell', '\top', '\natural', '\clubsuit', '\wp', '\bot', '\sharp', '\diamondsuit', '\Re', '\|', '\backslash', '\heartsuit', '\Im', '\angle', '\partial', '\spadesuit', '\mho$',
64: '\sum', '\bigcap', '\bigodot', '\prod', '\bigcup', '\bigotimes', '\coprod', '\bigsqcup', '\bigoplus', '\int', '\bigvee', '\biguplus', '\oint', '\bigwedge',
65: '\arccos', '\cos', '\csc', '\exp', '\ker', '\limsup', '\min', '\sinh', '\arcsin', '\cosh', '\deg', '\gcd', '\lg', '\ln', '\Pr', '\sup', '\arctan', '\cot', '\det', '\hom', '\lim', '\log', '\sec', '\tan', '\arg', '\coth', '\dim', '\inf', '\liminf', '\max', '\sin', '\tanh',
66: '\uparrow', '\Uparrow', '\downarrow', '\Downarrow', '\updownarrow', '\Updownarrow', '\lfloor', '\rfloor', '\lceil', '\rceil', '\langle', '\rangle', '\backslash',
67: '\rmoustache', '\lmoustache', '\rgroup', '\lgroup', '\arrowvert', '\Arrowvert', '\bracevert',
68: '\hat{', '\acute{', '\bar{', '\dot{', '\breve{', '\check{', '\grave{', '\vec{', '\ddot{', '\tilde{',
69: '\widetilde{', '\widehat{', '\overleftarrow{', '\overrightarrow{', '\overline{', '\underline{', '\overbrace{', '\underbrace{', '\sqrt{', '\sqrt[', '\frac{'
70: );
71: # list of elements that can contain style elements:
72: my @containing_styles = ('library','problem',@responses,'foil','item','text','hintgroup','hintpart','label','part','preduedate','postanswerdate','solved','notsolved','block','while','web','standalone','problemtype','languageblock','translated','lang','window','windowlink','togglebox','instructorcomment','body','section','div','p','li','dd','td','th','blockquote','object','applet','video','audio','canvas','fieldset','button',
73: 'span','strong','em','b','i','sup','sub','code','kbd','samp','tt','ins','del','var','small','big','u','font');
74: my @html_styles = ('span', 'strong', 'em' , 'b', 'i', 'sup', 'sub', 'tt', 'var', 'small', 'big', 'u');
75:
76: my $warnings; # 1 = print warnings
77:
78:
79: # Parses the XML document and fixes many things to turn it into a document matching the schema.
1.2 damieng 80: # @param {reference} textref - reference to the text of the document
81: # @param {string} file_path - path of the file being processed (we only extract the directory path from it)
82: # @param {string} res_dir - path of res directory parent (without the / at the end)
83: # @param {boolean} warn - 1 to print warnings, 0 otherwise
84: # @returns the text of the document as a byte string.
1.1 damieng 85: sub post_xml {
1.2 damieng 86: my ($textref, $file_path, $res_dir, $warn) = @_;
1.1 damieng 87: $warnings = $warn;
88:
89: my $dom_doc = XML::LibXML->load_xml(string => $textref);
90:
91: my $root = fix_structure($dom_doc);
92:
93: remove_elements($root, ['startouttext','startoutext','startottext','startouttex','startouttect','atartouttext','starouttext','starttextout','starttext','starttextarea','endouttext','endoutext','endoutttext','endouttxt','endouutext','ednouttext','endouttex','endoouttext','endouttest','endtextout','endtextarea','startpartmarker','endpartmarker','basefont','x-claris-tagview','x-claris-window','x-sas-window']);
94:
95: remove_empty_attributes($root);
96:
97: fix_attribute_case($root);
98:
1.3 damieng 99: replace_m($root);
1.1 damieng 100:
101: my @all_block = (@block_elements, @block_html);
1.2 damieng 102: add_sty_blocks($file_path, $res_dir, $root, \@all_block); # must come before the subs using @all_block
1.1 damieng 103:
104: fix_block_styles($root, \@all_block);
105: $root->normalize();
106:
107: fix_fonts($root, \@all_block);
108:
109: replace_u($root);
110:
111: remove_bad_cdata_sections($root);
112:
113: add_cdata_sections($root);
114:
115: fix_style_element($root);
116:
117: fix_tables($root);
118:
119: fix_lists($root);
120:
121: fix_wrong_name_for_img($root); # should be before replace_deprecated_attributes_by_css
122:
123: replace_deprecated_attributes_by_css($root);
124:
125: replace_center($root, \@all_block); # must come after replace_deprecated_attributes_by_css
126:
127: replace_nobr($root);
128:
129: remove_useless_notsolved($root);
130:
1.9 damieng 131: fix_comments($root);
132:
1.1 damieng 133: fix_paragraphs_inside($root, \@all_block);
134:
135: remove_empty_style($root);
136:
137: fix_empty_lc_elements($root);
138:
1.10 ! damieng 139: reduce_empty_p($root);
! 140:
1.1 damieng 141: lowercase_attribute_values($root);
142:
143: replace_numericalresponse_unit_attribute($root);
144:
145: replace_functions_by_elements($root);
146:
147: pretty($root, \@all_block);
148:
149: replace_tm_dtm($root);
150:
151: return $dom_doc->toString(); # byte string !
152: }
153:
154: sub fix_structure {
155: my ($doc) = @_;
156: # the root element has already been added in pre_xml
157: my $root = $doc->documentElement;
158: # inside the root, replace html, problem and library elements by their content
1.5 damieng 159: my @toreplace = ('html','problem','library','Task');
1.1 damieng 160: foreach my $name (@toreplace) {
161: my @elements = $root->getElementsByTagName($name);
162: foreach my $element (@elements) {
163: replace_by_children($element);
164: }
165: }
166: # insert all link and style elements inside a new head element
167: my $current_node = undef;
168: my @heads = $doc->getElementsByTagName('head');
169: my @links = $doc->getElementsByTagName('link');
170: my @styles = $doc->getElementsByTagName('style');
171: my @titles = $doc->getElementsByTagName('title');
172: if (scalar(@titles) > 0) {
173: # NOTE: there is a title element in gnuplot, not to be confused with the one inside HTML head
174: for (my $i=0; $i<scalar(@titles); $i++) {
175: my $title = $titles[$i];
176: my $found_gnuplot = 0;
177: my $ancestor = $title->parentNode;
178: while (defined $ancestor) {
179: if ($ancestor->nodeName eq 'gnuplot') {
180: $found_gnuplot = 1;
181: last;
182: }
183: $ancestor = $ancestor->parentNode;
184: }
185: if ($found_gnuplot) {
186: splice(@titles, $i, 1);
187: $i--;
188: }
189: }
190: }
191: if (scalar(@heads) > 0 || scalar(@titles) > 0 || scalar(@links) > 0 || scalar(@styles) > 0) {
192: my $htmlhead = $doc->createElement('head');
193: foreach my $head (@heads) {
194: my $next;
195: for (my $child=$head->firstChild; defined $child; $child=$next) {
196: $next = $child->nextSibling;
197: $head->removeChild($child);
198: if ($child->nodeType != XML_ELEMENT_NODE ||
199: string_in_array(['title','script','style','meta','link','import','base'], $child->nodeName)) {
200: $htmlhead->appendChild($child);
201: } else {
202: # this should not be in head
203: insert_after_or_first($root, $child, $current_node);
204: }
205: }
206: $head->parentNode->removeChild($head);
207: }
208: foreach my $child (@titles, @links, @styles) {
209: $child->parentNode->removeChild($child);
210: $htmlhead->appendChild($child);
211: }
212: insert_after_or_first($root, $htmlhead, $current_node);
213: $current_node = $htmlhead;
214: }
215: # body
216: my $htmlbody = undef;
217: my @bodies = $doc->getElementsByTagName('body');
218: if (scalar(@bodies) > 0) {
219: # TODO: fix content and position of body elements
220: if ($root->nodeName eq 'problem') {
221: foreach my $body (@bodies) {
222: replace_by_children($body);
223: }
224: }
225: }
226: # add all the meta elements afterwards when they are LON-CAPA meta. Remove all HTML meta.
227: my @meta_names = ('abstract','author','authorspace','avetries','avetries_list','clear','comefrom','comefrom_list','copyright','correct','count','course','course_list','courserestricted','creationdate','dependencies','depth','difficulty','difficulty_list','disc','disc_list','domain','end','field','firstname','generation','goto','goto_list','groupname','helpful','highestgradelevel','hostname','id','keynum','keywords','language','lastname','lastrevisiondate','lowestgradelevel','middlename','mime','modifyinguser','notes','owner','permanentemail','scope','sequsage','sequsage_list','standards','start','stdno','stdno_list','subject','technical','title','url','username','value','version');
228: my @metas = $doc->getElementsByTagName('meta');
229: foreach my $meta (@metas) {
230: $meta->parentNode->removeChild($meta);
231: my $name = $meta->getAttribute('name');
232: my $content = $meta->getAttribute('content');
233: if (defined $name && defined $content && string_in_array(\@meta_names, lc($name))) {
234: my $lcmeta = $doc->createElement('meta');
235: $lcmeta->setAttribute('name', lc($name));
236: $lcmeta->setAttribute('content', $content);
237: insert_after_or_first($root, $lcmeta, $current_node);
238: $current_node = $lcmeta;
239: }
240: }
241: return($root);
242: }
243:
244: # insert the new child under parent after the reference child, or as the first child if the reference child is not defined
245: sub insert_after_or_first {
246: my ($parent, $newchild, $refchild) = @_;
247: if (defined $refchild) {
248: $parent->insertAfter($newchild, $refchild);
249: } elsif (defined $parent->firstChild) {
250: $parent->insertBefore($newchild, $parent->firstChild);
251: } else {
252: $parent->appendChild($newchild);
253: }
254: }
255:
256: # removes all elements with given names inside the node, but keep the content
257: sub remove_elements {
258: my ($node, $to_remove) = @_;
259: my $nextChild;
260: for (my $child=$node->firstChild; defined $child; $child=$nextChild) {
261: $nextChild = $child->nextSibling;
262: my $type = $node->nodeType;
263: if ($type == XML_ELEMENT_NODE) {
264: if (string_in_array($to_remove, $child->nodeName)) {
265: my $first_non_white = $child->firstChild;
266: if (defined $first_non_white && $first_non_white->nodeType == XML_TEXT_NODE &&
267: $first_non_white->nodeValue =~ /^\s*$/) {
268: $first_non_white = $first_non_white->nextSibling;
269: }
270: if (defined $first_non_white) {
271: $nextChild = $first_non_white;
272: replace_by_children($child);
273: } else {
274: $node->removeChild($child);
275: }
276: } else {
277: remove_elements($child, $to_remove);
278: }
279: }
280: }
281: }
282:
283: # removes some attributes that have an invalid empty value
284: sub remove_empty_attributes {
285: my ($root) = @_;
286: my $doc = $root->ownerDocument;
287: # this list is based on validation errors in the MSU subset (it could be more complete if it was based on the schema)
288: my @attributes = (
289: ['curve', ['pointsize']],
290: ['foil', ['location']],
291: ['foilgroup', ['checkboxoptions', 'options', 'texoptions']],
292: ['gnuplot', ['pattern', 'texwidth']],
293: ['img', ['height', 'texheight', 'texwidth', 'texwrap', 'width']],
294: ['import', ['importmode']],
295: ['optionresponse', ['max']],
296: ['organicstructure', ['options']],
297: ['radiobuttonresponse', ['max']],
298: ['randomlabel', ['height', 'texwidth', 'width']],
299: ['stringresponse', ['type']],
300: ['textline', ['size']],
301: );
302: foreach my $element_attributes (@attributes) {
303: my $element_name = $element_attributes->[0];
304: my $attribute_names = $element_attributes->[1];
305: my @elements = $doc->getElementsByTagName($element_name);
306: foreach my $element (@elements) {
307: foreach my $attribute_name (@$attribute_names) {
308: my $value = $element->getAttribute($attribute_name);
309: if (defined $value && $value =~ /^\s*$/) {
310: $element->removeAttribute($attribute_name);
311: }
312: }
313: }
314: }
315: }
316:
317: # fixes the case for a few attributes that are not all lowercase
318: # (the HTML parser used in html_to_xml turns everything lowercase, which is a good thing in general)
319: sub fix_attribute_case {
320: my ($root) = @_;
321: my $doc = $root->ownerDocument;
322: my @attributes = (
323: ['labelgroup', ['TeXsize']],
324: ['h1', ['TeXsize']],
325: ['h2', ['TeXsize']],
326: ['h3', ['TeXsize']],
327: ['h4', ['TeXsize']],
328: ['h5', ['TeXsize']],
329: ['h6', ['TeXsize']],
330: # font and basefont have a TeXsize but will be removed
331: ['optionresponse', ['TeXlayout']],
332: ['itemgroup', ['TeXitemgroupwidth']],
333: ['Task', ['OptionalRequired']],
334: ['Question', ['OptionalRequired','Mandatory']],
335: ['Instance', ['OptionalRequired','Disabled']],
336: ['Criteria', ['Mandatory']],
337: ['table', ['TeXwidth','TeXtheme']],
338: ['td', ['TeXwidth']],
339: ['th', ['TeXwidth']],
340: ['img', ['TeXwidth','TeXheight','TeXwrap']],
341: );
342: foreach my $element_attributes (@attributes) {
343: my $element_name = $element_attributes->[0];
344: my $attribute_names = $element_attributes->[1];
345: my @elements = $doc->getElementsByTagName($element_name);
346: foreach my $element (@elements) {
347: foreach my $attribute_name (@$attribute_names) {
348: my $value = $element->getAttribute(lc($attribute_name));
349: if (defined $value) {
350: $element->removeAttribute(lc($attribute_name));
351: $element->setAttribute($attribute_name, $value);
352: }
353: }
354: }
355: }
356: }
357:
358: # Replaces m by HTML, tm and/or dtm (which will be replaced by <m> later, but they are useful
359: # to know if the element is a block element or not).
360: # m might contain non-math LaTeX, while tm and dtm may only contain math.
361: sub replace_m {
362: my ($root) = @_;
363: my $doc = $root->ownerDocument;
364: # search for variable declarations
365: my @variables = ();
366: my @scripts = $root->getElementsByTagName('script');
367: foreach my $script (@scripts) {
368: my $type = $script->getAttribute('type');
369: if (defined $type && $type eq 'loncapa/perl') {
370: if (defined $script->firstChild && $script->firstChild->nodeType == XML_TEXT_NODE) {
371: my $text = $script->firstChild->nodeValue;
372: # NOTE: we are not interested in replacing "@value", only "$value"
373: # this regexp is for " $a = ..." and " $a[...] = ..."
374: while ($text =~ /^[ \t]*\$([a-zA-Z_0-9]+)(?:\[[^\]]+\])?[ \t]*=/gm) {
375: if (!string_in_array(\@variables, $1)) {
376: push(@variables, $1);
377: }
378: }
379: # this regexp is for "...; $a = ..." and "...; $a[...] = ..."
380: while ($text =~ /^[^'"\/;]+;[ \t]*\$([a-zA-Z_0-9]+)(?:\[[^\]]+\])?[ \t]*=/gm) {
381: if (!string_in_array(\@variables, $1)) {
382: push(@variables, $1);
383: }
384: }
385: # this regexp is for " @a = ..."
386: while ($text =~ /^[ \t]*\@([a-zA-Z_0-9]+)[ \t]*=/gm) {
387: if (!string_in_array(\@variables, $1)) {
388: push(@variables, $1);
389: }
390: }
391: # this regexp is for " ($a, $b, $c) = ..."
392: my @matches = ($text =~ /^[ \t]*\([ \t]*\$([a-zA-Z_0-9]+)(?:[ \t]*,[ \t]*\$([a-zA-Z_0-9]+))*[ \t]*\)[ \t]*=/gm);
393: foreach my $match (@matches) {
394: if (!defined $match) {
395: next; # not sure why it happens, but it does
396: }
397: if (!string_in_array(\@variables, $match)) {
398: push(@variables, $match);
399: }
400: }
401: # and this one is for "push @a"
402: while ($text =~ /^[ \t]*push @([a-zA-Z_0-9]+)[ \t,]*/gm) {
403: if (!string_in_array(\@variables, $1)) {
404: push(@variables, $1);
405: }
406: }
407: # use the opportunity to report usage of <m> in Perl scripts
408: if ($text =~ /^[^#].*<m[ >]/m) {
409: if ($warnings) {
410: print "WARNING: <m> is used in a script, it should be converted by hand\n";
411: }
412: }
413: }
414: }
415: }
416: my @ms = $root->getElementsByTagName('m');
417: foreach my $m (@ms) {
418: if (!defined $m->firstChild) {
419: $m->parentNode->removeChild($m);
420: next;
421: }
422: if (defined $m->firstChild->nextSibling || $m->firstChild->nodeType != XML_TEXT_NODE) {
423: if ($warnings) {
424: print "WARNING: m value is not simple text\n";
425: }
426: next;
427: }
428: my $text = $m->firstChild->nodeValue;
429: my $text_before_variable_replacement = $text;
430: my $var_key1 = 'dfhg3df54hg65hg4';
431: my $var_key2 = 'dfhg654d6f5g4h5f';
432: my $eval = defined $m->getAttribute('eval') && $m->getAttribute('eval') eq 'on';
433: if ($eval) {
434: # replace variables
435: foreach my $variable (@variables) {
436: my $replacement = $var_key1.$variable.$var_key2;
437: $text =~ s/\$$variable(?![a-zA-Z])/$replacement/ge;
438: $text =~ s/\$\{$variable\}/$replacement/ge;
439: }
440: }
441: # check if the expression is enclosed in math separators: $ $$ \( \) \[ \]
442: # if so, replace the whole node by dtm or tm
443: my $new_text;
444: my $new_node_name;
445: if ($text =~ /^\s*\$\$([^\$]*)\$\$\s*$/) {
446: $new_node_name = 'dtm';
447: $new_text = $1;
448: } elsif ($text =~ /^\s*\\\[(.*)\\\]\s*$/s) {
449: $new_node_name = 'dtm';
450: $new_text = $1;
451: } elsif ($text =~ /^\s*\$([^\$]*)\$\s*$/) {
452: $new_node_name = 'tm';
453: $new_text = $1;
454: } elsif ($text =~ /^\s*\\\((.*)\\\)\s*$/s) {
455: $new_node_name = 'tm';
456: $new_text = $1;
457: }
458: if (defined $new_node_name) {
459: if ($eval) {
460: foreach my $variable (@variables) {
461: my $replacement = $var_key1.$variable.$var_key2;
462: $new_text =~ s/$replacement([a-zA-Z])/\${$variable}$1/g;
463: $new_text =~ s/$replacement/\$$variable/g;
464: }
465: }
466: my $new_node = $doc->createElement($new_node_name);
467: if ($eval) {
468: $new_node->setAttribute('eval', 'on');
469: }
470: $new_node->appendChild($doc->createTextNode($new_text));
471: $m->parentNode->replaceChild($new_node, $m);
472: next;
473: }
474: if ($text !~ /\$|\\\(|\\\)|\\\[|\\\]/) {
475: # there are no math separators inside
476: # try to guess if this is meant as math
477: my $found_math = 0;
478: foreach my $symbol (@latex_math) {
479: if (index($text, $symbol) != -1) {
480: $found_math = 1;
481: last;
482: }
483: }
484: if ($found_math) {
485: # interpret the whole text as LaTeX inline math
486: my $new_node = $doc->createElement('tm');
487: if ($eval) {
488: $new_node->setAttribute('eval', 'on');
489: }
490: $new_node->appendChild($doc->createTextNode($text_before_variable_replacement));
491: $m->parentNode->replaceChild($new_node, $m);
492: next;
493: }
494: # no math symbol found, we will convert the text with tth
495: }
496:
497: # there are math separators inside, even after hiding variables, or there was no math symbol
498:
499: # hide math parts inside before running tth
500: my $math_key1 = '#ghjgdh5hg45gf';
501: my $math_key2 = '#';
502: my @maths = ();
503: my @separators = (['$$','$$'], ['\\(','\\)'], ['\\[','\\]'], ['$','$']);
504: foreach my $seps (@separators) {
505: my $sep1 = $seps->[0];
506: my $sep2 = $seps->[1];
507: my $pos1 = index($text, $sep1);
508: if ($pos1 == -1) {
509: next;
510: }
511: my $pos2 = index($text, $sep2, $pos1+length($sep1));
512: while ($pos1 != -1 && $pos2 != -1) {
513: my $replace = substr($text, $pos1, $pos2+length($sep2)-$pos1);
514: push(@maths, $replace);
515: my $by = $math_key1.scalar(@maths).$math_key2;
516: $text = substr($text, 0, $pos1).$by.substr($text, $pos2+length($sep2));
517: $pos1 = index($text, $sep1);
518: if ($pos1 != -1) {
519: $pos2 = index($text, $sep2, $pos1+length($sep1));
520: }
521: }
522: }
523: # get HTML as text from tth
524: my $html_text = tth($text);
525: # replace math by replacements
526: for (my $i=0; $i < scalar(@maths); $i++) {
527: my $math = $maths[$i];
528: $math =~ s/&/&/g;
529: $math =~ s/</</g;
530: $math =~ s/>/>/g;
531: if ($math =~ /^\$\$(.*)\$\$$/s) {
532: $math = '<dtm>'.$1.'</dtm>';
533: } elsif ($math =~ /^\\\[(.*)\\\]$/s) {
534: $math = '<dtm>'.$1.'</dtm>';
535: } elsif ($math =~ /^\\\((.*)\\\)$/s) {
536: $math = '<tm>'.$1.'</tm>';
537: } elsif ($math =~ /^\$(.*)\$$/s) {
538: $math = '<tm>'.$1.'</tm>';
539: }
540: my $replace = $math_key1.($i+1).$math_key2;
541: $html_text =~ s/$replace/$math/;
542: }
543: # replace variables if necessary
544: if ($eval) {
545: foreach my $variable (@variables) {
546: my $replacement = $var_key1.$variable.$var_key2;
547: $html_text =~ s/$replacement([a-zA-Z])/\${$variable}$1/g;
548: $html_text =~ s/$replacement/\$$variable/g;
549: }
550: }
551: my $fragment = html_to_dom($html_text);
552: $doc->adoptNode($fragment);
553: $m->parentNode->replaceChild($fragment, $m);
554:
555: }
556: }
557:
558: # Returns the HTML equivalent of LaTeX input, using tth
559: sub tth {
560: my ($text) = @_;
1.7 damieng 561: my $output = &tth::tth($text);
562: my $errorstring = &tth::ttherror();
563: if ($errorstring) {
564: die $errorstring;
565: }
1.1 damieng 566: # hopefully the temp file will not be removed before this point (otherwise we should use unlink_on_destroy 0)
567: $output =~ s/^\s*|\s*$//;
568: $output =~ s/<div class="p"><!----><\/div>/<br\/>/; # why is tth using such ugly markup for \newline ?
569: return $output;
570: }
571:
572: # transform simple HTML into a DOM fragment (which will need to be adopted by the document)
573: sub html_to_dom {
574: my ($text) = @_;
575: $text = '<root>'.$text.'</root>';
1.7 damieng 576: my $textref = Apache::html_to_xml::html_to_xml(\$text);
1.1 damieng 577: utf8::upgrade($$textref); # otherwise the XML parser fails when the HTML parser turns into a character
578: my $dom_doc = XML::LibXML->load_xml(string => $textref);
579: my $root = $dom_doc->documentElement;
580: remove_empty_style($root);
581: my $fragment = $dom_doc->createDocumentFragment();
582: my $next;
583: for (my $n=$root->firstChild; defined $n; $n=$next) {
584: $next = $n->nextSibling;
585: $root->removeChild($n);
586: $fragment->appendChild($n);
587: }
588: return($fragment);
589: }
590:
591: # Use the linked sty files to guess which newly defined elements should be considered blocks.
592: # Also adds to @containing_styles the sty elements that contain styles.
593: # @param {string} fn - the file path (we only extract the directory path from it)
1.2 damieng 594: # @param {string} res_dir - path of res directory parent (without the / at the end)
595: # @param {Element} root - the root element
1.1 damieng 596: sub add_sty_blocks {
1.2 damieng 597: my ($fn, $res_dir, $root, $all_block) = @_;
1.1 damieng 598: my $doc = $root->ownerDocument;
599: my @parserlibs = $doc->getElementsByTagName('parserlib');
600: my @libs = ();
601: foreach my $parserlib (@parserlibs) {
602: if (defined $parserlib->firstChild && $parserlib->firstChild->nodeType == XML_TEXT_NODE) {
603: my $value = $parserlib->firstChild->nodeValue;
604: $value =~ s/^\s+|\s+$//g;
605: if ($value ne '') {
606: push(@libs, $value);
607: }
608: }
609: }
610: my ($name, $path, $suffix) = fileparse($fn);
611: foreach my $sty (@libs) {
612: if (substr($sty, 0, 1) eq '/') {
1.2 damieng 613: $sty = $res_dir.$sty;
1.1 damieng 614: } else {
615: $sty = $path.$sty;
616: }
617: my $new_elements = parse_sty($sty, $all_block);
618: better_guess($root, $new_elements, $all_block);
619: my $new_blocks = $new_elements->{'block'};
620: my $new_inlines = $new_elements->{'inline'};
621: push(@$all_block, @{$new_blocks});
622: #push(@inlines, @{$new_inlines}); # we are not using a list of inline elements at this point
623: }
624: }
625:
626: ##
627: # Parses a sty file and returns lists of block and inline elements.
628: # @param {string} fn - the file path
629: ##
630: sub parse_sty {
631: my ($fn, $all_block) = @_;
632: my @blocks = ();
633: my @inlines = ();
634: my $p = HTML::TokeParser->new($fn);
635: if (! $p) {
636: die "post_xml.pl: parse_sty: Error reading $fn\n";
637: }
638: $p->empty_element_tags(1);
639: my $in_definetag = 0;
640: my $in_render = 0;
641: my %newtags = ();
642: my $newtag = '';
643: my $is_block = 0;
644: while (my $token = $p->get_token) {
645: if ($token->[0] eq 'S') {
646: my $tag = lc($token->[1]);
647: if ($tag eq 'definetag') {
648: $in_definetag = 1;
649: $is_block = 0;
650: my $attributes = $token->[2];
651: $newtag = $attributes->{'name'};
652: if (substr($newtag, 0, 1) eq '/') {
653: $newtag = substr($newtag, 1);
654: }
655: } elsif ($in_definetag && $tag eq 'render') {
656: $in_render = 1;
657: $is_block = 0;
658: } elsif ($in_render) {
659: if (string_in_array($all_block, $tag)) {
660: $is_block = 1;
661: }
662: }
663: } elsif ($token->[0] eq 'E') {
664: my $tag = lc($token->[1]);
665: if ($tag eq 'definetag') {
666: $in_definetag = 0;
667: if (defined $newtags{$newtag}) {
668: $newtags{$newtag} = $newtags{$newtag} || $is_block;
669: } else {
670: $newtags{$newtag} = $is_block;
671: }
672: } elsif ($in_definetag && $tag eq 'render') {
673: $in_render = 0;
674: }
675: }
676: }
677: foreach $newtag (keys(%newtags)) {
678: if ($newtags{$newtag} == 1) {
679: push(@blocks, $newtag);
680: } else {
681: push(@inlines, $newtag);
682: }
683: }
684: return {'block'=>\@blocks, 'inline'=>\@inlines};
685: }
686:
687: ##
688: # Marks as block the elements that contain block elements in the input file.
689: # Also adds to @containing_styles the sty elements that contain styles.
690: # @param {string} fn - the file path
691: # @param {Hash<string,Array>} new_elements - contains arrays in 'block' and 'inline'
692: ##
693: sub better_guess {
694: my ($root, $new_elements, $all_block) = @_;
695: my $new_blocks = $new_elements->{'block'};
696: my $new_inlines = $new_elements->{'inline'};
697:
698: my @change = (); # change these elements from inline to block
699: foreach my $tag (@{$new_inlines}) {
700: my @nodes = $root->getElementsByTagName($tag);
701: NODE_LOOP: foreach my $node (@nodes) {
702: for (my $child=$node->firstChild; defined $child; $child=$child->nextSibling) {
703: if ($child->nodeType == XML_ELEMENT_NODE) {
704: if (string_in_array($all_block, $child->nodeName) || string_in_array($new_blocks, $child->nodeName)) {
705: push(@change, $tag);
706: last NODE_LOOP;
707: }
708: }
709: }
710: }
711: }
712: foreach my $inline (@change) {
713: my $index = 0;
714: $index++ until $new_inlines->[$index] eq $inline;
715: splice(@{$new_inlines}, $index, 1);
716: push(@{$new_blocks}, $inline);
717: }
718: # add to @containing_styles when a style is used inside
719: # NOTE: some sty elements will be added even though they should not, but if we don't do that
720: # all style will be removed in the sty elements.
721: foreach my $tag ((@{$new_blocks}, @{$new_inlines})) {
722: my @nodes = $root->getElementsByTagName($tag);
723: NODE_LOOP: foreach my $node (@nodes) {
724: for (my $child=$node->firstChild; defined $child; $child=$child->nextSibling) {
725: if ($child->nodeType == XML_ELEMENT_NODE) {
726: if (string_in_array(\@html_styles, $child->nodeName)) {
727: push(@containing_styles, $tag);
728: last NODE_LOOP;
729: }
730: }
731: }
732: }
733: }
734: }
735:
736: # When a style element contains a block, move the style inside the block where it is allowed.
737: # style/block/other -> block/style/other
738: # When a style is used where it is not allowed, move it inside its children or remove it (unless it contains only text)
739: # element_not_containing_styles/style/other -> element_not_containing_styles/other/style (except if other is a style)
740: # The fix is not perfect in the case of element_not_containing_styles/style1/style2/block/text (style1 will be lost):
741: # element_not_containing_styles/style1/style2/block/text -> element_not_containing_styles/block/style2/text
742: # (a solution to this problem would be to merge the styles in a span)
743: # NOTE: .sty defined elements might have been added to @containing_styles by better_guess().
744: sub fix_block_styles {
745: my ($element, $all_block) = @_;
746: my $doc = $element->ownerDocument;
747: if (string_in_array(\@html_styles, $element->nodeName)) {
748: # move spaces out of the style element
749: if (defined $element->firstChild && $element->firstChild->nodeType == XML_TEXT_NODE) {
750: my $child = $element->firstChild;
751: if ($child->nodeValue =~ /^(\s+)(\S.*)$/s) {
752: $element->parentNode->insertBefore($doc->createTextNode($1), $element);
753: $child->setData($2);
754: }
755: }
756: if (defined $element->lastChild && $element->lastChild->nodeType == XML_TEXT_NODE) {
757: my $child = $element->lastChild;
758: if ($child->nodeValue =~ /^(.*\S)(\s+)$/s) {
759: $element->parentNode->insertAfter($doc->createTextNode($2), $element);
760: $child->setData($1);
761: }
762: }
763:
764: my $found_block = 0;
765: for (my $child=$element->firstChild; defined $child; $child=$child->nextSibling) {
766: if ($child->nodeType == XML_ELEMENT_NODE && string_in_array($all_block, $child->nodeName)) {
767: $found_block = 1;
768: last;
769: }
770: }
771: my $no_style_here = !string_in_array(\@containing_styles, $element->parentNode->nodeName);
772: if ($found_block || $no_style_here) {
773: # there is a block or the style is not allowed here,
774: # the style element has to be replaced by its modified children
775: my $s; # a clone of the style
776: my $next;
777: for (my $child=$element->firstChild; defined $child; $child=$next) {
778: $next = $child->nextSibling;
779: if ($child->nodeType == XML_ELEMENT_NODE && (string_in_array($all_block, $child->nodeName) ||
780: $child->nodeName eq 'br' || $no_style_here)) {
781: # avoid inverting a style with a style with $no_style_here (that would cause endless recursion)
782: if (!$no_style_here || (!string_in_array(\@html_styles, $child->nodeName) &&
783: string_in_array(\@containing_styles, $child->nodeName))) {
784: # block node or inline node when the style is not allowed:
785: # move all children inside the style, and make the style the only child
786: $s = $element->cloneNode();
787: my $next2;
788: for (my $child2=$child->firstChild; defined $child2; $child2=$next2) {
789: $next2 = $child2->nextSibling;
790: $child->removeChild($child2);
791: $s->appendChild($child2);
792: }
793: $child->appendChild($s);
794: }
795: $s = undef;
796: } elsif (($child->nodeType == XML_TEXT_NODE && $child->nodeValue !~ /^\s*$/) ||
797: $child->nodeType == XML_ELEMENT_NODE) {
798: # if the style is allowed, move text and inline nodes inside the style
799: if (!$no_style_here) {
800: if (!defined $s) {
801: $s = $element->cloneNode();
802: $element->insertBefore($s, $child);
803: }
804: $element->removeChild($child);
805: $s->appendChild($child);
806: }
807: } else {
808: # do not put other nodes inside the style
809: $s = undef;
810: }
811: }
812: # now replace by children and fix them
813: my $parent = $element->parentNode;
814: for (my $child=$element->firstChild; defined $child; $child=$next) {
815: $next = $child->nextSibling;
816: $element->removeChild($child);
817: $parent->insertBefore($child, $element);
818: if ($child->nodeType == XML_ELEMENT_NODE) {
819: fix_block_styles($child, $all_block);
820: }
821: }
822: $parent->removeChild($element);
823: return;
824: }
825: }
826: # otherwise fix all children
827: my $next;
828: for (my $child=$element->firstChild; defined $child; $child=$next) {
829: $next = $child->nextSibling;
830: if ($child->nodeType == XML_ELEMENT_NODE) {
831: fix_block_styles($child, $all_block);
832: }
833: }
834: }
835:
836: # removes empty font elements and font elements that contain at least one block element
837: # replaces other font elements by equivalent span
838: sub fix_fonts {
839: my ($root, $all_block) = @_;
840: my $doc = $root->ownerDocument;
841: my @fonts = $root->getElementsByTagName('font');
842: @fonts = reverse(@fonts); # to deal with the ancestor last in the case of font/font
843: foreach my $font (@fonts) {
844: my $block = 0;
845: for (my $child=$font->firstChild; defined $child; $child=$child->nextSibling) {
846: if (string_in_array($all_block, $child->nodeName) || string_in_array(\@inline_like_block, $child->nodeName)) {
847: $block = 1;
848: last;
849: }
850: }
851: if (!defined $font->firstChild || $block) {
852: # empty font or font containing block elements
853: # replace this node by its content
854: replace_by_children($font);
855: } else {
856: # replace by equivalent span
857: my $color = get_non_empty_attribute($font, 'color');
858: my $size = get_non_empty_attribute($font, 'size');
859: my $face = get_non_empty_attribute($font, 'face');
860: if (defined $face) {
861: $face =~ s/^,|,$//;
862: }
863: if (!defined $color && !defined $size && !defined $face) {
864: # useless font element: replace this node by its content
865: replace_by_children($font);
866: next;
867: }
868: my $replacement;
869: tie (my %properties, 'Tie::IxHash', ());
870: if (!defined $color && !defined $size && defined $face && lc($face) eq 'symbol') {
871: $replacement = $doc->createDocumentFragment();
872: } else {
873: $replacement = $doc->createElement('span');
874: my $css = '';
875: if (defined $color) {
876: $color =~ s/^x/#/;
877: $properties{'color'} = $color;
878: }
879: if (defined $size) {
880: my %hash = (
881: '1' => 'x-small',
882: '2' => 'small',
883: '3' => 'medium',
884: '4' => 'large',
885: '5' => 'x-large',
886: '6' => 'xx-large',
887: '7' => '300%',
888: '-1' => 'small',
889: '-2' => 'x-small',
890: '+1' => 'large',
891: '+2' => 'x-large',
892: '+3' => 'xx-large',
893: '+4' => '300%',
894: );
895: my $value = $hash{$size};
896: if (!defined $value) {
897: $value = 'medium';
898: }
899: $properties{'font-size'} = $value;
900: }
901: if (defined $face) {
902: if (lc($face) ne 'symbol' && lc($face) ne 'bold') {
903: $properties{'font-family'} = $face;
904: }
905: }
906: set_css_properties($replacement, \%properties);
907: }
908: if (defined $face && lc($face) eq 'symbol') {
909: # convert all content to unicode
910: my $next;
911: for (my $child=$font->firstChild; defined $child; $child=$next) {
912: $next = $child->nextSibling;
913: if ($child->nodeType == XML_TEXT_NODE) {
914: my $value = $child->nodeValue;
915: $value =~ tr/ABGDEZHQIKLMNXOPRSTUFCYWabgdezhqiklmnxoprVstufcywJjv¡«¬®/ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρςστυφχψωϑϕϖϒ↔←→/;
916: $child->setData($value);
917: }
918: }
919: }
920: # replace the font node
921: if ($replacement->nodeType == XML_ELEMENT_NODE && !defined $font->previousSibling &&
922: !defined $font->nextSibling && string_in_array(\@accepting_style, $font->parentNode->nodeName)) {
923: # use CSS on the parent block and replace font by its children instead of using a new element
924: set_css_properties($font->parentNode, \%properties);
925: replace_by_children($font);
926: } else {
927: # move all font children inside the replacement (span or fragment)
928: my $next;
929: for (my $child=$font->firstChild; defined $child; $child=$next) {
930: $next = $child->nextSibling;
931: $font->removeChild($child);
932: $replacement->appendChild($child);
933: }
934: # replace font
935: $font->parentNode->replaceChild($replacement, $font);
936: }
937: }
938: }
939: $root->normalize();
940: }
941:
942: # replaces u by <span style="text-decoration: underline">
943: sub replace_u {
944: my ($root) = @_;
945: my $doc = $root->ownerDocument;
946: my @us = $root->getElementsByTagName('u');
947: foreach my $u (@us) {
948: my $span = $doc->createElement('span');
949: $span->setAttribute('style', 'text-decoration: underline');
950: my $next;
951: for (my $child=$u->firstChild; defined $child; $child=$next) {
952: $next = $child->nextSibling;
953: $u->removeChild($child);
954: $span->appendChild($child);
955: }
956: $u->parentNode->replaceChild($span, $u);
957: }
958: }
959:
960: # removes CDATA sections tags that have not been parsed correcty by the HTML parser
961: # also removes bad comments in script elements
962: sub remove_bad_cdata_sections {
963: my ($root) = @_;
964: my $doc = $root->ownerDocument;
965: foreach my $name (@preserve_elements) {
966: my @nodes = $root->getElementsByTagName($name);
967: foreach my $node (@nodes) {
968: if (defined $node->firstChild && $node->firstChild->nodeType == XML_TEXT_NODE) {
969: my $value = $node->firstChild->nodeValue;
970: if ($name eq 'script' && (!defined $node->getAttribute('type') || $node->getAttribute('type') ne 'loncapa/perl') &&
971: !defined $node->firstChild->nextSibling && $value =~ /^(\s*)<!--(.*)-->(\s*)$/) {
972: # web browsers interpret that as a real comment when it is on 1 line, but the Perl HTML parser thinks it is the script
973: # -> turning it back into a comment
974: # (this is only true for Javascript script elements, since LON-CAPA does not parse loncapa/perl scripts in the same way)
975: $node->removeChild($node->firstChild);
976: $node->appendChild($doc->createComment($2));
977: next;
978: }
979: # at the beginning:
980: $value =~ s/^(\s*)<!\[CDATA\[/$1/; # <![CDATA[
981: $value =~ s/^(\s*)\/\*\s*<!\[CDATA\[\s*\*\//$1/; # /* <![CDATA[ */
982: $value =~ s/^(\s*)\/\/\s*<!\[CDATA\[/$1/; # // <![CDATA[
983: $value =~ s/^(\s*)(\/\/)?\s*<!--/$1/; # // <!--
984: # at the end:
985: $value =~ s/\/\/\s*\]\]>(\s*)$/$1/; # // ]]>
986: $value =~ s/\]\]>(\s*)$/$1/; # ]]>
987: $value =~ s/(\/\/)?\s*-->(\s*)$/$2/; # // -->
988: $value =~ s/\/\*\s*\]\]>\s*\*\/(\s*)$/$1/; # /* ]]> */
989:
990: $value = "\n".$value."\n";
991: $value =~ s/\s*(\n[ \t]*)/$1/;
992: $value =~ s/\s+$/\n/;
993: $node->firstChild->setData($value);
994: }
995: }
996: }
997: }
998:
999: # adds CDATA sections to scripts
1000: sub add_cdata_sections {
1001: my ($root) = @_;
1002: my $doc = $root->ownerDocument;
1003: my @scripts = $root->getElementsByTagName('script');
1004: my @answers = $root->getElementsByTagName('answer');
1005: foreach my $answer (@answers) {
1006: my $ancestor = $answer->parentNode;
1007: my $found_capa_response = 0;
1008: while (defined $ancestor) {
1009: if ($ancestor->nodeName eq 'numericalresponse' || $ancestor->nodeName eq 'formularesponse') {
1010: $found_capa_response = 1;
1011: last;
1012: }
1013: $ancestor = $ancestor->parentNode;
1014: }
1015: if (!$found_capa_response) {
1016: push(@scripts, $answer);
1017: }
1018: }
1019: foreach my $script (@scripts) {
1020: # use a CDATA section in the normal situation, for any script
1021: my $first = $script->firstChild;
1022: if (defined $first && $first->nodeType == XML_TEXT_NODE && !defined $first->nextSibling) {
1023: my $cdata = $doc->createCDATASection($first->nodeValue);
1024: $script->replaceChild($cdata, $first);
1025: }
1026: }
1027: }
1028:
1029: # removes "<!--" and "-->" at the beginning and end of style elements
1030: sub fix_style_element {
1031: my ($root) = @_;
1032: my @styles = $root->getElementsByTagName('style');
1033: foreach my $style (@styles) {
1034: if (defined $style->firstChild && $style->firstChild->nodeType == XML_TEXT_NODE &&
1035: !defined $style->firstChild->nextSibling) {
1036: my $text = $style->firstChild->nodeValue;
1037: if ($text =~ /^\s*<!--(.*)-->\s*$/s) {
1038: $style->firstChild->setData($1);
1039: }
1040: }
1041: }
1042: }
1043:
1044: # create missing cells at the end of table rows
1045: sub fix_tables {
1046: my ($root) = @_;
1047: my @tables = $root->getElementsByTagName('table');
1048: foreach my $table (@tables) {
1049: fix_cells($table);
1050: foreach my $tbody ($table->getChildrenByTagName('tbody')) {
1051: fix_cells($tbody);
1052: }
1053: foreach my $thead ($table->getChildrenByTagName('thead')) {
1054: fix_cells($thead);
1055: }
1056: foreach my $tfoot ($table->getChildrenByTagName('tfoot')) {
1057: fix_cells($tfoot);
1058: }
1059: }
1060: }
1061:
1062: # create missing cells at the end of table rows
1063: sub fix_cells {
1064: my ($table) = @_; # could actually be table, tbody, thead or tfoot
1065: my $doc = $table->ownerDocument;
1066: my @nb_cells = ();
1067: my $max_nb_cells = 0;
1068: my @rowspans = ();
1069: my @trs = $table->getChildrenByTagName('tr');
1070: foreach my $tr (@trs) {
1071: my $nb_cells;
1072: if (defined $rowspans[0]) {
1073: $nb_cells = shift(@rowspans);
1074: } else {
1075: $nb_cells = 0;
1076: }
1077: for (my $cell=$tr->firstChild; defined $cell; $cell=$cell->nextSibling) {
1078: if ($cell->nodeName eq 'td' || $cell->nodeName eq 'th') {
1079: my $colspan = $cell->getAttribute('colspan');
1080: if (defined $colspan && $colspan =~ /^\s*[0-9]+\s*$/) {
1081: $nb_cells += $colspan;
1082: } else {
1083: $nb_cells++;
1084: }
1085: my $rowspan = $cell->getAttribute('rowspan');
1086: if (defined $rowspan && $rowspan =~ /^\s*[0-9]+\s*$/) {
1087: for (my $i=0; $i < $rowspan-1; $i++) {
1088: if (!defined $rowspans[$i]) {
1089: $rowspans[$i] = 1;
1090: } else {
1091: $rowspans[$i]++;
1092: }
1093: }
1094: }
1095: }
1096: }
1097: push(@nb_cells, $nb_cells);
1098: if ($nb_cells > $max_nb_cells) {
1099: $max_nb_cells = $nb_cells;
1100: }
1101: }
1102: foreach my $tr (@trs) {
1103: my $nb_cells = shift(@nb_cells);
1104: if ($nb_cells < $max_nb_cells) {
1105: for (1..($max_nb_cells - $nb_cells)) {
1106: $tr->appendChild($doc->createElement('td'));
1107: }
1108: }
1109: }
1110: }
1111:
1112: # replaces ul/ul by ul/li/ul and the same for ol (using the previous li if possible)
1113: # also adds a ul element when a li has no ul/ol ancestor
1114: sub fix_lists {
1115: my ($root) = @_;
1116: my $doc = $root->ownerDocument;
1117: my @uls = $root->getElementsByTagName('ul');
1118: my @ols = $root->getElementsByTagName('ol');
1119: my @lists = (@uls, @ols);
1120: foreach my $list (@lists) {
1121: my $next;
1122: for (my $child=$list->firstChild; defined $child; $child=$next) {
1123: $next = $child->nextSibling;
1124: if ($child->nodeType == XML_ELEMENT_NODE && string_in_array(['ul','ol'], $child->nodeName)) {
1125: my $previous = $child->previousNonBlankSibling(); # note: non-DOM method
1126: $list->removeChild($child);
1127: if (defined $previous && $previous->nodeType == XML_ELEMENT_NODE && $previous->nodeName eq 'li') {
1128: $previous->appendChild($child);
1129: } else {
1130: my $li = $doc->createElement('li');
1131: $li->appendChild($child);
1132: if (!defined $next) {
1133: $list->appendChild($li);
1134: } else {
1135: $list->insertBefore($li, $next);
1136: }
1137: }
1138: }
1139: }
1140: }
1141: my @lis = $root->getElementsByTagName('li');
1142: foreach my $li (@lis) {
1143: my $found_list_ancestor = 0;
1144: my $ancestor = $li->parentNode;
1145: while (defined $ancestor) {
1146: if ($ancestor->nodeName eq 'ul' || $ancestor->nodeName eq 'ol') {
1147: $found_list_ancestor = 1;
1148: last;
1149: }
1150: $ancestor = $ancestor->parentNode;
1151: }
1152: if (!$found_list_ancestor) {
1153: # replace li by ul and add li under ul
1154: my $ul = $doc->createElement('ul');
1155: $li->parentNode->insertBefore($ul, $li);
1156: $li->parentNode->removeChild($li);
1157: $ul->appendChild($li);
1158: # add all other li afterwards inside ul (there might be text nodes in-between)
1159: my $next = $ul->nextSibling;
1160: while (defined $next) {
1161: my $next_next = $next->nextSibling;
1162: if ($next->nodeType == XML_TEXT_NODE && $next->nodeValue =~ /^\s*$/ &&
1163: defined $next_next && $next_next->nodeType == XML_ELEMENT_NODE && $next_next->nodeName eq 'li') {
1164: $next->parentNode->removeChild($next);
1165: $ul->appendChild($next);
1166: $next = $next_next;
1167: $next_next = $next_next->nextSibling;
1168: }
1169: if ($next->nodeType == XML_ELEMENT_NODE && $next->nodeName eq 'li') {
1170: $next->parentNode->removeChild($next);
1171: $ul->appendChild($next);
1172: } else {
1173: last;
1174: }
1175: $next = $next_next;
1176: }
1177: }
1178: }
1179: }
1180:
1181: # Some "image" elements are actually img element with a wrong name. This renames them.
1182: # Amazingly enough, "<image src=..." displays an image in some browsers
1183: # ("image" has existed at some point as an experimental HTML element).
1184: sub fix_wrong_name_for_img {
1185: my ($root) = @_;
1186: my @images = $root->getElementsByTagName('image');
1187: foreach my $image (@images) {
1188: if (!defined $image->getAttribute('src')) {
1189: next;
1190: }
1191: my $found_correct_ancestor = 0;
1192: my $ancestor = $image->parentNode;
1193: while (defined $ancestor) {
1194: if ($ancestor->nodeName eq 'drawimage' || $ancestor->nodeName eq 'imageresponse') {
1195: $found_correct_ancestor = 1;
1196: last;
1197: }
1198: $ancestor = $ancestor->parentNode;
1199: }
1200: if ($found_correct_ancestor) {
1201: next;
1202: }
1203: # this really has to be renamed "img"
1204: $image->setNodeName('img');
1205: }
1206: }
1207:
1208: # Replaces many deprecated attributes and replaces them by equivalent CSS when possible
1209: sub replace_deprecated_attributes_by_css {
1210: my ($root) = @_;
1211:
1212: fix_deprecated_in_tables($root);
1213:
1214: fix_deprecated_in_table_rows($root);
1215:
1216: fix_deprecated_in_table_cells($root);
1217:
1218: fix_deprecated_in_lists($root);
1219:
1220: fix_deprecated_in_list_items($root);
1221:
1222: fix_deprecated_in_hr($root);
1223:
1224: fix_deprecated_in_img($root);
1225:
1226: fix_deprecated_in_body($root);
1227:
1228: fix_align_attribute($root);
1229: }
1230:
1231: # Replaces deprecated attributes in tables
1232: sub fix_deprecated_in_tables {
1233: my ($root) = @_;
1234: my @tables = $root->getElementsByTagName('table');
1235: foreach my $table (@tables) {
1236: tie (my %new_properties, 'Tie::IxHash', ());
1237: my $align = $table->getAttribute('align');
1238: if (defined $align) {
1239: $table->removeAttribute('align');
1240: $align = lc(trim($align));
1241: }
1242: if ($table->parentNode->nodeName eq 'center' || (defined $align && $align eq 'center') ||
1243: (defined $table->parentNode->getAttribute('align') && $table->parentNode->getAttribute('align') eq 'center')) {
1244: $new_properties{'margin-left'} = 'auto';
1245: $new_properties{'margin-right'} = 'auto';
1246: }
1247: if (defined $align && ($align eq 'left' || $align eq 'right')) {
1248: $new_properties{'float'} = $align;
1249: }
1250: my $width = $table->getAttribute('width');
1251: if (defined $width) {
1252: $table->removeAttribute('width');
1253: $width = trim($width);
1254: if ($width =~ /^[0-9]+$/) {
1255: $width .= 'px';
1256: }
1257: if ($width ne '') {
1258: $new_properties{'width'} = $width;
1259: }
1260: }
1261: my $height = $table->getAttribute('height');
1262: if (defined $height) {
1263: $table->removeAttribute('height');
1264: # no replacement for table height
1265: }
1266: my $bgcolor = $table->getAttribute('bgcolor');
1267: if (defined $bgcolor) {
1268: $table->removeAttribute('bgcolor');
1269: $bgcolor = trim($bgcolor);
1270: $bgcolor =~ s/^x\s*//;
1271: if ($bgcolor ne '') {
1272: $new_properties{'background-color'} = $bgcolor;
1273: }
1274: }
1275: my $frame = $table->getAttribute('frame');
1276: if (defined $frame) {
1277: $table->removeAttribute('frame');
1278: $frame = lc(trim($frame));
1279: if ($frame eq 'void') {
1280: $new_properties{'border'} = 'none';
1281: } elsif ($frame eq 'above') {
1282: $new_properties{'border-top'} = '1px solid black';
1283: } elsif ($frame eq 'below') {
1284: $new_properties{'border-bottom'} = '1px solid black';
1285: } elsif ($frame eq 'hsides') {
1286: $new_properties{'border-top'} = '1px solid black';
1287: $new_properties{'border-bottom'} = '1px solid black';
1288: } elsif ($frame eq 'vsides') {
1289: $new_properties{'border-left'} = '1px solid black';
1290: $new_properties{'border-right'} = '1px solid black';
1291: } elsif ($frame eq 'lhs') {
1292: $new_properties{'border-left'} = '1px solid black';
1293: } elsif ($frame eq 'rhs') {
1294: $new_properties{'border-right'} = '1px solid black';
1295: } elsif ($frame eq 'box') {
1296: $new_properties{'border'} = '1px solid black';
1297: } elsif ($frame eq 'border') {
1298: $new_properties{'border'} = '1px solid black';
1299: }
1300: }
1301: if (scalar(keys %new_properties) > 0) {
1302: set_css_properties($table, \%new_properties);
1303: }
1304: # we can't replace the border attribute without creating a style block, but we can improve things like border="BORDER"
1305: my $border = $table->getAttribute('border');
1306: if (defined $border) {
1307: $border = trim($border);
1308: if ($border !~ /^\s*[0-9]+\s*(px)?\s*$/) {
1309: $table->setAttribute('border', '1');
1310: }
1311: }
1312: }
1313:
1314: }
1315:
1316: # Replaces deprecated attributes in tr elements
1317: sub fix_deprecated_in_table_rows {
1318: my ($root) = @_;
1319: my @trs = $root->getElementsByTagName('tr');
1320: foreach my $tr (@trs) {
1321: my $old_properties = get_css_properties($tr);
1322: tie (my %new_properties, 'Tie::IxHash', ());
1323: my $bgcolor = $tr->getAttribute('bgcolor');
1324: if (defined $bgcolor) {
1325: $tr->removeAttribute('bgcolor');
1326: if (!defined $old_properties->{'background-color'}) {
1327: $bgcolor = trim($bgcolor);
1328: $bgcolor =~ s/^x\s*//;
1329: if ($bgcolor ne '') {
1330: $new_properties{'background-color'} = $bgcolor;
1331: }
1332: }
1333: }
1334: my $align = $tr->getAttribute('align');
1335: if (defined $align && $align !~ /\s*char\s*/i) {
1336: $tr->removeAttribute('align');
1337: if (!defined $old_properties->{'text-align'}) {
1338: $align = lc(trim($align));
1339: if ($align ne '') {
1340: $new_properties{'text-align'} = $align;
1341: }
1342: }
1343: }
1344: my $valign = $tr->getAttribute('valign');
1345: if (defined $valign) {
1346: $tr->removeAttribute('valign');
1347: if (!defined $old_properties->{'vertical-align'}) {
1348: $valign = lc(trim($valign));
1349: if ($valign ne '') {
1350: $new_properties{'vertical-align'} = $valign;
1351: }
1352: }
1353: }
1354: if (scalar(keys %new_properties) > 0) {
1355: set_css_properties($tr, \%new_properties);
1356: }
1357: }
1358: }
1359:
1360: # Replaces deprecated attributes in table cells (td and th)
1361: sub fix_deprecated_in_table_cells {
1362: my ($root) = @_;
1363: my @tds = $root->getElementsByTagName('td');
1364: my @ths = $root->getElementsByTagName('th');
1365: my @cells = (@tds, @ths);
1366: foreach my $cell (@cells) {
1367: my $old_properties = get_css_properties($cell);
1368: tie (my %new_properties, 'Tie::IxHash', ());
1369: my $width = $cell->getAttribute('width');
1370: if (defined $width) {
1371: $cell->removeAttribute('width');
1372: if (!defined $old_properties->{'width'}) {
1373: $width = trim($width);
1374: if ($width =~ /^[0-9]+$/) {
1375: $width .= 'px';
1376: }
1377: if ($width ne '') {
1378: $new_properties{'width'} = $width;
1379: }
1380: }
1381: }
1382: my $height = $cell->getAttribute('height');
1383: if (defined $height) {
1384: $cell->removeAttribute('height');
1385: if (!defined $old_properties->{'height'}) {
1386: $height = trim($height);
1387: if ($height =~ /^[0-9]+$/) {
1388: $height .= 'px';
1389: }
1390: if ($height ne '') {
1391: $new_properties{'height'} = $height;
1392: }
1393: }
1394: }
1395: my $bgcolor = $cell->getAttribute('bgcolor');
1396: if (defined $bgcolor) {
1397: $cell->removeAttribute('bgcolor');
1398: if (!defined $old_properties->{'background-color'}) {
1399: $bgcolor = trim($bgcolor);
1400: $bgcolor =~ s/^x\s*//;
1401: if ($bgcolor ne '') {
1402: $new_properties{'background-color'} = $bgcolor;
1403: }
1404: }
1405: }
1406: my $align = $cell->getAttribute('align');
1407: if (defined $align && $align !~ /\s*char\s*/i) {
1408: $cell->removeAttribute('align');
1409: if (!defined $old_properties->{'text-align'}) {
1410: $align = lc(trim($align));
1411: if ($align ne '') {
1412: $new_properties{'text-align'} = $align;
1413: }
1414: }
1415: }
1416: my $valign = $cell->getAttribute('valign');
1417: if (defined $valign) {
1418: $cell->removeAttribute('valign');
1419: if (!defined $old_properties->{'vertical-align'}) {
1420: $valign = lc(trim($valign));
1421: if ($valign ne '') {
1422: $new_properties{'vertical-align'} = $valign;
1423: }
1424: }
1425: }
1426: if (scalar(keys %new_properties) > 0) {
1427: set_css_properties($cell, \%new_properties);
1428: }
1429: }
1430: }
1431:
1432: # Replaces deprecated attributes in lists (ul and ol)
1433: sub fix_deprecated_in_lists {
1434: my ($root) = @_;
1435: my @uls = $root->getElementsByTagName('ul');
1436: my @ols = $root->getElementsByTagName('ol');
1437: my @lists = (@uls, @ols);
1438: foreach my $list (@lists) {
1439: my $type = $list->getAttribute('type');
1440: if (defined $type) {
1441: my $lst = list_style_type($type);
1442: if (defined $lst) {
1443: $list->removeAttribute('type');
1444: if (!defined get_css_property($list, 'list-style-type')) {
1445: set_css_property($list, 'list-style-type', $lst);
1446: }
1447: }
1448: }
1449: }
1450: }
1451:
1452: # Replaces deprecated attributes in list items (li)
1453: sub fix_deprecated_in_list_items {
1454: my ($root) = @_;
1455: my @lis = $root->getElementsByTagName('li');
1456: foreach my $li (@lis) {
1457: my $type = $li->getAttribute('type');
1458: if (defined $type) {
1459: my $lst = list_style_type($type);
1460: if (defined $lst) {
1461: $li->removeAttribute('type');
1462: if (!defined get_css_property($li, 'list-style-type')) {
1463: set_css_property($li, 'list-style-type', $lst);
1464: }
1465: }
1466: }
1467: }
1468: }
1469:
1470: # returns the CSS list-style-type value equivalent to the given type attribute for a list or list item
1471: sub list_style_type {
1472: my ($type) = @_;
1473: my $value;
1474: $type = trim($type);
1475: if (lc($type) eq 'circle') {
1476: $value = 'circle';
1477: } elsif (lc($type) eq 'disc') {
1478: $value = 'disc';
1479: } elsif (lc($type) eq 'square') {
1480: $value = 'square';
1481: } elsif ($type eq 'a') {
1482: $value = 'lower-latin';
1483: } elsif ($type eq 'A') {
1484: $value = 'upper-latin';
1485: } elsif ($type eq 'i') {
1486: $value = 'lower-roman';
1487: } elsif ($type eq 'I') {
1488: $value = 'upper-roman';
1489: } elsif ($type eq '1') {
1490: $value = 'decimal';
1491: }
1492: return $value;
1493: }
1494:
1495: # Replaces deprecated attributes in hr
1496: sub fix_deprecated_in_hr {
1497: my ($root) = @_;
1498: my @hrs = $root->getElementsByTagName('hr');
1499: foreach my $hr (@hrs) {
1500: tie (my %new_properties, 'Tie::IxHash', ());
1501: my $align = $hr->getAttribute('align');
1502: if (defined $align) {
1503: $align = lc(trim($align));
1504: if ($align eq 'left') {
1505: $new_properties{'text-align'} = 'left';
1506: $new_properties{'margin-left'} = '0';
1507: } elsif ($align eq 'right') {
1508: $new_properties{'text-align'} = 'right';
1509: $new_properties{'margin-right'} = '0';
1510: }
1511: $hr->removeAttribute('align');
1512: }
1513: my $color = $hr->getAttribute('color');
1514: if (defined $color) {
1515: $color = trim($color);
1516: $color =~ s/^x\s*//;
1517: if ($color ne '') {
1518: $new_properties{'color'} = $color;
1519: $new_properties{'background-color'} = $color;
1520: }
1521: $hr->removeAttribute('color');
1522: }
1523: my $noshade = $hr->getAttribute('noshade');
1524: my $size = $hr->getAttribute('size');
1525: if (defined $noshade) {
1526: $new_properties{'border-width'} = '0';
1527: if (!defined $color) {
1528: $new_properties{'color'} = 'gray';
1529: $new_properties{'background-color'} = 'gray';
1530: }
1531: if (!defined $size) {
1532: $size = '2';
1533: }
1534: $hr->removeAttribute('noshade');
1535: }
1536: if (defined $size) {
1537: $size = trim($size);
1538: if ($size ne '') {
1539: $new_properties{'height'} = $size.'px';
1540: }
1541: if (defined $hr->getAttribute('size')) {
1542: $hr->removeAttribute('size');
1543: }
1544: }
1545: my $width = $hr->getAttribute('width');
1546: if (defined $width) {
1547: $width = trim($width);
1548: if ($width ne '') {
1549: if ($width !~ /\%$/) {
1550: $width .= 'px';
1551: }
1552: $new_properties{'width'} = $width;
1553: }
1554: $hr->removeAttribute('width');
1555: }
1556: if (scalar(keys %new_properties) > 0) {
1557: set_css_properties($hr, \%new_properties);
1558: }
1559: }
1560: }
1561:
1562: # Replaces deprecated attributes in img
1563: sub fix_deprecated_in_img {
1564: my ($root) = @_;
1565: my @imgs = $root->getElementsByTagName('img');
1566: foreach my $img (@imgs) {
1567: my $old_properties = get_css_properties($img);
1568: tie (my %new_properties, 'Tie::IxHash', ());
1569: my $align = $img->getAttribute('align');
1570: if (defined $align) {
1571: $align = lc(trim($align));
1572: if ($align eq 'middle' || $align eq 'top' || $align eq 'bottom') {
1573: $img->removeAttribute('align');
1574: if (!defined $old_properties->{'vertical-align'}) {
1575: $new_properties{'vertical-align'} = $align;
1576: }
1577: } elsif ($align eq 'left' || $align eq 'right') {
1578: $img->removeAttribute('align');
1579: if (!defined $old_properties->{'float'}) {
1580: $new_properties{'float'} = $align;
1581: }
1582: } elsif ($align eq 'center' || $align eq '') {
1583: $img->removeAttribute('align');
1584: }
1585: }
1586: my $border = $img->getAttribute('border');
1587: if (defined $border) {
1588: $border = lc(trim($border));
1589: if ($border =~ /^[0-9]+\s*(px)?$/) {
1590: $img->removeAttribute('border');
1591: if (!defined $old_properties->{'border'}) {
1592: if ($border !~ /px$/) {
1593: $border .= 'px';
1594: }
1595: $new_properties{'border'} = $border.' solid black';
1596: }
1597: }
1598: }
1599: my $hspace = $img->getAttribute('hspace');
1600: if (defined $hspace) {
1601: $hspace = lc(trim($hspace));
1602: if ($hspace =~ /^[0-9]+\s*(px)?$/) {
1603: $img->removeAttribute('hspace');
1604: if (!defined $old_properties->{'margin-left'} || !defined $old_properties->{'margin-right'}) {
1605: if ($hspace !~ /px$/) {
1606: $hspace .= 'px';
1607: }
1608: $new_properties{'margin-left'} = $hspace;
1609: $new_properties{'margin-right'} = $hspace;
1610: }
1611: }
1612: }
1613: if (scalar(keys %new_properties) > 0) {
1614: set_css_properties($img, \%new_properties);
1615: }
1616: }
1617: }
1618:
1619: # Replaces deprecated attributes in htmlbody (the style attribute could be used in a div for output)
1620: sub fix_deprecated_in_body {
1621: my ($root) = @_;
1622: my $doc = $root->ownerDocument;
1623: my @bodies = $root->getElementsByTagName('htmlbody');
1624: foreach my $body (@bodies) {
1625: my $old_properties = get_css_properties($body);
1626: tie (my %new_properties, 'Tie::IxHash', ());
1627: my $bgcolor = $body->getAttribute('bgcolor');
1628: if (defined $bgcolor) {
1629: $body->removeAttribute('bgcolor');
1630: if (!defined $old_properties->{'background-color'}) {
1631: $bgcolor = trim($bgcolor);
1632: $bgcolor =~ s/^x\s*//;
1633: if ($bgcolor ne '') {
1634: $new_properties{'background-color'} = $bgcolor;
1635: }
1636: }
1637: }
1638: my $color = $body->getAttribute('text');
1639: if (defined $color) {
1640: $body->removeAttribute('text');
1641: if (!defined $old_properties->{'color'}) {
1642: $color = trim($color);
1643: $color =~ s/^x\s*//;
1644: if ($color ne '') {
1645: $new_properties{'color'} = $color;
1646: }
1647: }
1648: }
1649: my $background = $body->getAttribute('background');
1650: if (defined $background && ($background =~ /\.jpe?g$|\.gif|\.png/i)) {
1651: $body->removeAttribute('background');
1652: if (!defined $old_properties->{'background-image'}) {
1653: $background = trim($background);
1654: if ($background ne '') {
1655: $new_properties{'background-image'} = 'url('.$background.')';
1656: }
1657: }
1658: }
1659: # NOTE: these attributes have never been standard and are better removed with no replacement
1660: foreach my $bad ('bottommargin', 'leftmargin', 'rightmargin', 'topmargin', 'marginheight', 'marginwidth') {
1661: if ($body->hasAttribute($bad)) {
1662: $body->removeAttribute($bad);
1663: }
1664: }
1665: # NOTE: link alink and vlink require a <style> block to be converted
1666: my $link = $body->getAttribute('link');
1667: my $alink = $body->getAttribute('alink');
1668: my $vlink = $body->getAttribute('vlink');
1669: if (defined $link || defined $alink || defined $vlink) {
1670: my $head;
1671: my @heads = $root->getElementsByTagName('htmlhead');
1672: if (scalar(@heads) > 0) {
1673: $head = $heads[0];
1674: } else {
1675: $head = $doc->createElement('htmlhead');
1676: $root->insertBefore($head, $root->firstChild);
1677: }
1678: my $style = $doc->createElement('style');
1679: $head->appendChild($style);
1680: my $css = "\n";
1681: if (defined $link) {
1682: $body->removeAttribute('link');
1683: $link = trim($link);
1684: $link =~ s/^x\s*//;
1685: $css .= ' a:link { color:'.$link.' }';
1686: $css .= "\n";
1687: }
1688: if (defined $alink) {
1689: $body->removeAttribute('alink');
1690: $alink = trim($alink);
1691: $alink =~ s/^x\s*//;
1692: $css .= ' a:active { color:'.$alink.' }';
1693: $css .= "\n";
1694: }
1695: if (defined $vlink) {
1696: $body->removeAttribute('vlink');
1697: $vlink = trim($vlink);
1698: $vlink =~ s/^x\s*//;
1699: $css .= ' a:visited { color:'.$vlink.' }';
1700: $css .= "\n";
1701: }
1702: $css .= ' ';
1703: $style->appendChild($doc->createTextNode($css));
1704: }
1705: if (scalar(keys %new_properties) > 0) {
1706: set_css_properties($body, \%new_properties);
1707: } elsif (!$body->hasAttributes) {
1708: $body->parentNode->removeChild($body);
1709: }
1710: }
1711: }
1712:
1713: # replaces <div align="center"> by <div style="text-align:center;">
1714: # also for p and h1..h6
1715: sub fix_align_attribute {
1716: my ($root) = @_;
1717: my @nodes = $root->getElementsByTagName('div');
1718: push(@nodes, $root->getElementsByTagName('p'));
1719: for (my $i=1; $i<=6; $i++) {
1720: push(@nodes, $root->getElementsByTagName('h'.$i));
1721: }
1722: foreach my $node (@nodes) {
1723: my $align = $node->getAttribute('align');
1724: if (defined $align) {
1725: $node->removeAttribute('align');
1726: $align = trim($align);
1727: if ($align ne '' && !defined get_css_property($node, 'text-align')) {
1728: set_css_property($node, 'text-align', lc($align));
1729: }
1730: }
1731: }
1732: }
1733:
1734: # replace center by a div or remove it if there is a table inside
1735: sub replace_center {
1736: my ($root, $all_block) = @_;
1737: my $doc = $root->ownerDocument;
1738: my @centers = $root->getElementsByTagName('center');
1739: foreach my $center (@centers) {
1740: if ($center->getChildrenByTagName('table')->size() > 0) { # note: getChildrenByTagName is not DOM (LibXML specific)
1741: replace_by_children($center);
1742: } else {
1743: if ((!defined $center->previousSibling ||
1744: ($center->previousSibling->nodeType == XML_TEXT_NODE && $center->previousSibling->nodeValue =~ /^\s*$/ && !defined $center->previousSibling->previousSibling)) &&
1745: (!defined $center->nextSibling ||
1746: ($center->nextSibling->nodeType == XML_TEXT_NODE && $center->nextSibling->nodeValue =~ /^\s*$/ && !defined $center->nextSibling->nextSibling)) &&
1747: string_in_array(\@accepting_style, $center->parentNode->nodeName)) {
1748: # use CSS on the parent block and replace center by its children
1749: set_css_property($center->parentNode, 'text-align', 'center');
1750: replace_by_children($center);
1751: } else {
1752: # use p or div ? check if there is a block inside
1753: my $found_block = 0;
1754: for (my $child=$center->firstChild; defined $child; $child=$child->nextSibling) {
1755: if ($child->nodeType == XML_ELEMENT_NODE && string_in_array($all_block, $child->nodeName)) {
1756: $found_block = 1;
1757: last;
1758: }
1759: }
1760: my $new_node;
1761: if ($found_block) {
1762: $new_node = $doc->createElement('div');
1763: $new_node->setAttribute('style', 'text-align: center; margin: 0 auto');
1764: } else {
1765: $new_node = $doc->createElement('p');
1766: $new_node->setAttribute('style', 'text-align: center');
1767: }
1768: my $next;
1769: for (my $child=$center->firstChild; defined $child; $child=$next) {
1770: $next = $child->nextSibling;
1771: $center->removeChild($child);
1772: $new_node->appendChild($child);
1773: }
1774: $center->parentNode->replaceChild($new_node, $center);
1775: }
1776: }
1777: }
1778: }
1779:
1780: # replaces <nobr> by <span style="white-space:nowrap">
1781: sub replace_nobr {
1782: my ($root) = @_;
1783: my @nobrs = $root->getElementsByTagName('nobr');
1784: foreach my $nobr (@nobrs) {
1785: if (!defined $nobr->previousSibling && !defined $nobr->nextSibling &&
1786: string_in_array(\@accepting_style, $nobr->parentNode->nodeName)) {
1787: # use CSS on the parent block
1788: set_css_property($nobr->parentNode, 'white-space', 'nowrap');
1789: replace_by_children($nobr);
1790: } else {
1791: $nobr->setNodeName('span');
1792: $nobr->setAttribute('style', 'white-space:nowrap');
1793: }
1794: }
1795: }
1796:
1797: # removes notsolved tags in the case <hintgroup showoncorrect="no"><notsolved>...</notsolved></hintgroup>
1798: # and in the case <notsolved><hintgroup showoncorrect="no">...</hintgroup></notsolved>
1799: sub remove_useless_notsolved {
1800: my ($root) = @_;
1801: my @hintgroups = $root->getElementsByTagName('hintgroup');
1802: foreach my $hintgroup (@hintgroups) {
1803: my $showoncorrect = get_non_empty_attribute($hintgroup, 'showoncorrect');
1804: if (!defined $showoncorrect || $showoncorrect eq 'no') {
1805: my @notsolveds = $hintgroup->getElementsByTagName('notsolved');
1806: foreach my $notsolved (@notsolveds) {
1807: replace_by_children($notsolved);
1808: }
1809: }
1810: my $parent = $hintgroup->parentNode;
1811: if ($parent->nodeName eq 'notsolved' && scalar(@{$parent->nonBlankChildNodes()}) == 1) {
1812: replace_by_children($parent);
1813: }
1814: }
1815: }
1816:
1.9 damieng 1817: # Use <pre> for multi-line comments without elements.
1818: sub fix_comments {
1819: my ($root) = @_;
1820: my $doc = $root->ownerDocument;
1821: my @comments = $root->getElementsByTagName('comment');
1822: foreach my $comment (@comments) {
1823: my $first = $comment->firstChild;
1824: if (defined $first) {
1825: if ($first->nodeType == XML_TEXT_NODE && $first->nodeValue =~ /\n/ &&
1826: !defined $first->nextSibling) {
1827: my $pre = $doc->createElement('pre');
1828: $comment->removeChild($first);
1829: $comment->appendChild($pre);
1830: $pre->appendChild($first);
1831: }
1832: }
1833: }
1834: }
1835:
1.1 damieng 1836: # adds a paragraph inside if needed and calls fix_paragraph for all paragraphs (including new ones)
1837: sub fix_paragraphs_inside {
1838: my ($node, $all_block) = @_;
1839: # blocks in which paragrahs will be added:
1.8 damieng 1840: my @blocks_with_p = ('loncapa','library','problem','part','problemtype','window','block','while','postanswerdate','preduedate','languageblock','instructorcomment','togglebox','standalone','body','form');
1841: my @fix_p_if_br_or_p = (@responses,'foil','item','text','label','hintgroup','hintpart','hint','web','windowlink','div','li','dd','td','th','blockquote','solved','notsolved');
1.1 damieng 1842: if ((string_in_array(\@blocks_with_p, $node->nodeName) && paragraph_needed($node)) ||
1843: (string_in_array(\@fix_p_if_br_or_p, $node->nodeName) && paragraph_inside($node))) {
1844: # if non-empty, add paragraphs where needed between all br and remove br
1845: # (it would be easier to just put everything in a p and fix it afterwards, but there are performance issues
1846: # when a paragraph has many blocks directly inside)
1847: my $doc = $node->ownerDocument;
1848: my $p = undef;
1849: my @new_children = ();
1850: my $next;
1851: for (my $child=$node->firstChild; defined $child; $child=$next) {
1852: $next = $child->nextSibling;
1853: $node->removeChild($child);
1854: if ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName eq 'br') {
1855: if (defined $p) {
1856: push(@new_children, $p);
1857: } else {
1858: push(@new_children, $doc->createElement('p'));
1859: }
1860: $p = undef;
1861: } elsif ($child->nodeType == XML_ELEMENT_NODE && string_in_array(\@inline_like_block, $child->nodeName)) {
1862: # inline_like_block: use the paragraph if there is one, otherwise do not create one
1863: if (defined $p) {
1864: $p->appendChild($child);
1865: } else {
1866: push(@new_children, $child);
1867: }
1868: } elsif ($child->nodeType == XML_ELEMENT_NODE && string_in_array($all_block, $child->nodeName)) {
1869: # these children are blocks and should not be in a paragraph
1870: if (defined $p) {
1871: push(@new_children, $p);
1872: $p = undef;
1873: }
1874: push(@new_children, $child);
1875: } elsif ($child->nodeType == XML_TEXT_NODE && $child->nodeValue =~ /^[ \t\f\n\r]*$/) {
1876: # blank text: add to paragraph if there is one and there is a next node, otherwise keep out of the paragraph
1877: if (defined $p) {
1878: if (defined $next) {
1879: $p->appendChild($child);
1880: } else {
1881: push(@new_children, $p);
1882: $p = undef;
1883: push(@new_children, $child);
1884: }
1885: } else {
1886: push(@new_children, $child);
1887: }
1888: } elsif ($child->nodeType == XML_TEXT_NODE ||
1889: $child->nodeType == XML_ELEMENT_NODE || $child->nodeType == XML_CDATA_SECTION_NODE ||
1890: $child->nodeType == XML_ENTITY_NODE || $child->nodeType == XML_ENTITY_REF_NODE) {
1891: # these children require a paragraph
1892: if (!defined $p) {
1893: $p = $doc->createElement('p');
1894: }
1895: $p->appendChild($child);
1896: } else {
1897: # these children do not require a paragraph (XML comments, PI)
1898: # -> do not move them in a new paragraph
1899: if (defined $p) {
1900: push(@new_children, $p);
1901: $p = undef;
1902: }
1903: push(@new_children, $child);
1904: }
1905: }
1906: if (defined $p) {
1907: push(@new_children, $p);
1908: }
1909: foreach my $child (@new_children) {
1910: $node->appendChild($child);
1911: }
1912: }
1913: # now fix the paragraphs everywhere, so that all inline nodes are inside a paragraph, and block nodes are outside
1914: my $next;
1915: for (my $child=$node->firstChild; defined $child; $child=$next) {
1916: $next = $child->nextSibling;
1917: if ($child->nodeType == XML_ELEMENT_NODE && defined $child->firstChild) {
1918: if ($child->nodeName eq 'p') {
1919: fix_paragraph($child, $all_block);
1920: } else {
1921: fix_paragraphs_inside($child, $all_block);
1922: }
1923: }
1924: }
1925: }
1926:
1927: # returns 1 if a paragraph is needed inside this node (assuming the parent can have paragraphs)
1928: sub paragraph_needed {
1929: my ($node) = @_;
1930: for (my $child=$node->firstChild; defined $child; $child=$child->nextSibling) {
1931: if (($child->nodeType == XML_TEXT_NODE && $child->nodeValue !~ /^\s*$/) ||
1932: ($child->nodeType == XML_ELEMENT_NODE && !string_in_array(\@inline_like_block, $child->nodeName)) ||
1933: $child->nodeType == XML_CDATA_SECTION_NODE ||
1934: $child->nodeType == XML_ENTITY_NODE || $child->nodeType == XML_ENTITY_REF_NODE) {
1935: return(1);
1936: }
1937: }
1938: return(0);
1939: }
1940:
1941: # returns 1 if there is a paragraph or br in a child of this node, or inside an inline child
1942: sub paragraph_inside {
1943: my ($node) = @_;
1944: # inline elements that can be split in half if there is a paragraph inside (currently all HTML):
1945: # (also used in first_block below)
1946: my @splitable_inline = ('span', 'a', 'strong', 'em' , 'b', 'i', 'sup', 'sub', 'code', 'kbd', 'samp', 'tt', 'ins', 'del', 'var', 'small', 'big', 'font', 'u');
1947: for (my $child=$node->firstChild; defined $child; $child=$child->nextSibling) {
1948: if ($child->nodeType == XML_ELEMENT_NODE) {
1949: my $name = $child->nodeName;
1950: if ($name eq 'p' || $name eq 'br') {
1951: return(1);
1952: } elsif (string_in_array(\@splitable_inline, $name)) {
1953: if (paragraph_inside($child)) {
1954: return(1);
1955: }
1956: }
1957: }
1958: }
1959: return(0);
1960: }
1961:
1962: # fixes paragraphs inside paragraphs (without a block in-between)
1963: sub fix_paragraph {
1964: my ($p, $all_block) = @_;
1965: my $loop_right = 1; # this loops is to avoid out of memory errors with recurse, see below
1966: while ($loop_right) {
1967: $loop_right = 0;
1968: my $block = find_first_block($p, $all_block);
1969: if (defined $block) {
1970: my $trees = clone_ancestor_around_node($p, $block);
1971: my $doc = $p->ownerDocument;
1972: my $replacement = $doc->createDocumentFragment();
1973: my $left = $trees->{'left'};
1974: my $middle = $trees->{'middle'};
1975: my $right = $trees->{'right'};
1976: my $left_needs_p = 0; # 1 if it needs a paragraph (used to replace br later)
1977:
1978: if (defined $left) {
1979: # fix paragraphs inside, in case one of the descendants can have paragraphs inside (like numericalresponse/hintgroup):
1980: for (my $child=$left->firstChild; defined $child; $child=$child->nextSibling) {
1981: if ($child->nodeType == XML_ELEMENT_NODE) {
1982: fix_paragraphs_inside($child, $all_block);
1983: }
1984: }
1985: if (!paragraph_needed($left)) {
1986: # this was just blank text, comments or inline responses, it should not create a new paragraph
1987: my $next;
1988: for (my $child=$left->firstChild; defined $child; $child=$next) {
1989: $next = $child->nextSibling;
1990: $left->removeChild($child);
1991: $replacement->appendChild($child);
1992: }
1993: } else {
1994: $left_needs_p = 1;
1995: $replacement->appendChild($left);
1996: }
1997: }
1998:
1999: my $n = $middle->firstChild;
2000: while (defined $n) {
2001: if ($n->nodeType == XML_ELEMENT_NODE && (string_in_array($all_block, $n->nodeName) || $n->nodeName eq 'br')) {
2002: if ($n->nodeName eq 'p') {
2003: my $parent = $n->parentNode;
2004: # first apply recursion
2005: fix_paragraph($n, $all_block);
2006: # now the p might have been replaced by several nodes, which should replace the initial p
2007: my $next_block;
2008: for (my $block=$parent->firstChild; defined $block; $block=$next_block) {
2009: $next_block = $block->nextSibling;
2010: if ($block->nodeName eq 'p') {
2011: $parent->removeChild($block);
2012: # for each parent before $middle, clone in-between the p and its children (to preserve the styles)
2013: if (defined $block->firstChild) {
2014: for (my $p=$parent; $p!=$middle; $p=$p->parentNode) {
2015: my $newp = $p->cloneNode(0);
2016: my $next;
2017: for (my $child=$block->firstChild; defined $child; $child=$next) {
2018: $next = $child->nextSibling;
2019: $block->removeChild($child);
2020: $newp->appendChild($child);
2021: }
2022: $block->appendChild($newp);
2023: }
2024: }
2025: }
2026: $replacement->appendChild($block);
2027: }
2028: } else {
2029: # replace the whole p by this block, forgetting about intermediate inline elements
2030: $n->parentNode->removeChild($n);
2031: if ($n->nodeName eq 'br') {
2032: # replace a br by a paragraph if there was nothing before in the paragraph,
2033: # otherwise remove it because it already broke the paragraph in half
2034: if (!defined $left || !$left_needs_p) {
2035: $replacement->appendChild($middle);
2036: }
2037: } else {
2038: fix_paragraphs_inside($n, $all_block);
2039: $replacement->appendChild($n);
2040: }
2041: }
2042: last;
2043: }
2044: $n = $n->firstChild;
2045: if (defined $n && defined $n->nextSibling) {
2046: die "Error in post_xml.fix_paragraph: block not found";
2047: }
2048: }
2049:
2050: if (defined $right) {
2051: if ($block->nodeName eq 'p') {
2052: # remove attributes on the right paragraph
2053: my @attributelist = $right->attributes();
2054: foreach my $att (@attributelist) {
2055: $right->removeAttribute($att->nodeName);
2056: }
2057: }
2058: if ($right->firstChild->nodeType == XML_TEXT_NODE && $right->firstChild->nodeValue =~ /^[ \t\f\n\r]*$/) {
2059: # remove the first text node with whitespace only from the p, it should not trigger the creation of a p
2060: # (but take nbsp into account, so we should not use \s here)
2061: my $first = $right->firstChild;
2062: $right->removeChild($first);
2063: $replacement->appendChild($first);
2064: }
2065: if (defined $right->firstChild) {
2066: if (paragraph_needed($right)) {
2067: $replacement->appendChild($right);
2068: #fix_paragraph($right, $all_block); This is taking way too much memory for blocks with many children
2069: # -> loop instead of recurse
2070: $loop_right = 1;
2071: } else {
2072: # this was just blank text, comments or inline responses, it should not create a new paragraph
2073: my $next;
2074: for (my $child=$right->firstChild; defined $child; $child=$next) {
2075: $next = $child->nextSibling;
2076: $right->removeChild($child);
2077: $replacement->appendChild($child);
2078: # fix paragraphs inside, in case one of the descendants can have paragraphs inside (like numericalresponse/hintgroup):
2079: if ($child->nodeType == XML_ELEMENT_NODE) {
2080: fix_paragraphs_inside($child, $all_block);
2081: }
2082: }
2083: }
2084: }
2085: }
2086:
2087: $p->parentNode->replaceChild($replacement, $p);
2088:
2089: if ($loop_right) {
2090: $p = $right;
2091: }
2092:
2093: } else {
2094: # fix paragraphs inside, in case one of the descendants can have paragraphs inside (like numericalresponse/hintgroup):
2095: my $next;
2096: for (my $child=$p->firstChild; defined $child; $child=$next) {
2097: $next = $child->nextSibling;
2098: if ($child->nodeType == XML_ELEMENT_NODE) {
2099: fix_paragraphs_inside($child, $all_block);
2100: }
2101: }
2102: }
2103: }
2104: }
2105:
2106: sub find_first_block {
2107: my ($node, $all_block) = @_;
2108: # inline elements that can be split in half if there is a paragraph inside (currently all HTML):
2109: my @splitable_inline = ('span', 'a', 'strong', 'em' , 'b', 'i', 'sup', 'sub', 'code', 'kbd', 'samp', 'tt', 'ins', 'del', 'var', 'small', 'big', 'font', 'u');
2110: for (my $child=$node->firstChild; defined $child; $child=$child->nextSibling) {
2111: if ($child->nodeType == XML_ELEMENT_NODE) {
2112: if (string_in_array($all_block, $child->nodeName) || $child->nodeName eq 'br') {
2113: return($child);
2114: }
2115: if (string_in_array(\@splitable_inline, $child->nodeName)) {
2116: my $block = find_first_block($child, $all_block);
2117: if (defined $block) {
2118: return($block);
2119: }
2120: }
2121: }
2122: }
2123: return(undef);
2124: }
2125:
2126: # Creates clones of the ancestor containing the descendants before the node, at the node, and after the node.
2127: # returns a hash with: left, middle, right (left and right can be undef)
2128: sub clone_ancestor_around_node {
2129: my ($ancestor, $node) = @_;
2130: my $middle_node;
2131: my ($left, $middle, $right);
2132: for (my $child=$ancestor->firstChild; defined $child; $child=$child->nextSibling) {
2133: if ($child == $node || is_ancestor_of($child, $node)) {
2134: $middle_node = $child;
2135: last;
2136: }
2137: }
2138: if (!defined $middle_node) {
2139: die "error in split_ancestor_around_node: middle not found";
2140: }
2141: if (defined $middle_node->previousSibling) {
2142: $left = $ancestor->cloneNode(0);
2143: for (my $child=$ancestor->firstChild; $child != $middle_node; $child=$child->nextSibling) {
2144: $left->appendChild($child->cloneNode(1));
2145: }
2146: }
2147: $middle = $ancestor->cloneNode(0);
2148: if ($middle_node == $node) {
2149: $middle->appendChild($middle_node->cloneNode(1));
2150: } else {
2151: my $subres = clone_ancestor_around_node($middle_node, $node);
2152: my $subleft = $subres->{'left'};
2153: if (defined $subleft) {
2154: if (!defined $left) {
2155: $left = $ancestor->cloneNode(0);
2156: }
2157: $left->appendChild($subleft);
2158: }
2159: $middle->appendChild($subres->{'middle'});
2160: my $subright = $subres->{'right'};
2161: if (defined $subright) {
2162: $right = $ancestor->cloneNode(0);
2163: $right->appendChild($subright);
2164: }
2165: }
2166: if (defined $middle_node->nextSibling) {
2167: if (!defined $right) {
2168: $right = $ancestor->cloneNode(0);
2169: }
2170: for (my $child=$middle_node->nextSibling; defined $child; $child=$child->nextSibling) {
2171: $right->appendChild($child->cloneNode(1));
2172: }
2173: }
2174: my %result = ();
2175: $result{'left'} = $left;
2176: $result{'middle'} = $middle;
2177: $result{'right'} = $right;
2178: return(\%result);
2179: }
2180:
2181: sub is_ancestor_of {
2182: my ($n1, $n2) = @_;
2183: my $n = $n2->parentNode;
2184: while (defined $n) {
2185: if ($n == $n1) {
2186: return(1);
2187: }
2188: $n = $n->parentNode;
2189: }
2190: return(0);
2191: }
2192:
2193: # removes empty style elements and replaces the ones with only whitespaces inside by their content
2194: # also remove hints that have become empty after empty style removal.
2195: sub remove_empty_style {
2196: my ($root) = @_;
2197: # actually, preserve some elements like ins when they have whitespace, only remove if they are empty
2198: my @remove_if_empty = ('span', 'strong', 'em' , 'b', 'i', 'sup', 'sub', 'code', 'kbd', 'samp', 'tt', 'ins', 'del', 'var', 'small', 'big', 'font', 'u', 'hint');
2199: my @remove_if_blank = ('span', 'strong', 'em' , 'b', 'i', 'sup', 'sub', 'tt', 'var', 'small', 'big', 'font', 'u', 'hint');
2200: foreach my $name (@remove_if_empty) {
2201: my @nodes = $root->getElementsByTagName($name);
2202: while (scalar(@nodes) > 0) {
2203: my $node = pop(@nodes);
2204: if (!defined $node->firstChild) {
2205: my $parent = $node->parentNode;
2206: if (defined $node->previousSibling && $node->previousSibling->nodeType == XML_TEXT_NODE &&
2207: $node->previousSibling->nodeValue =~ /\$\S*$/) {
2208: # case $a<sup></sup>x
2209: my $value = $node->previousSibling->nodeValue;
2210: $value =~ s/\$(\S*)$/\$\{$1\}/;
2211: $node->previousSibling->setData($value);
2212: }
2213: $parent->removeChild($node);
2214: $parent->normalize();
2215: # now that we removed the node, check if the parent has become an empty style, and so on
2216: while (defined $parent && string_in_array(\@remove_if_empty, $parent->nodeName) && !defined $parent->firstChild) {
2217: my $grandparent = $parent->parentNode;
2218: $grandparent->removeChild($parent);
2219: remove_reference_from_array(\@nodes, $parent);
2220: $parent = $grandparent;
2221: }
2222: }
2223: }
2224: }
2225: foreach my $name (@remove_if_blank) {
2226: my @nodes = $root->getElementsByTagName($name);
2227: while (scalar(@nodes) > 0) {
2228: my $node = pop(@nodes);
2229: if (defined $node->firstChild && !defined $node->firstChild->nextSibling && $node->firstChild->nodeType == XML_TEXT_NODE) {
2230: # NOTE: careful, with UTF-8, \s matches non-breaking spaces and we want to preserve these
2231: if ($node->firstChild->nodeValue =~ /^[\t\n\f\r ]*$/) {
2232: my $parent = $node->parentNode;
2233: replace_by_children($node);
2234: $parent->normalize();
2235: # now that we removed the node, check if the parent has become a style with only whitespace, and so on
2236: while (defined $parent && string_in_array(\@remove_if_blank, $parent->nodeName) &&
2237: (!defined $parent->firstChild ||
2238: (!defined $parent->firstChild->nextSibling && $parent->firstChild->nodeType == XML_TEXT_NODE &&
2239: $parent->firstChild->nodeValue =~ /^^[\t\n\f\r ]*$/))) {
2240: my $grandparent = $parent->parentNode;
2241: replace_by_children($parent);
2242: remove_reference_from_array(\@nodes, $parent);
2243: $parent = $grandparent;
2244: }
2245: }
2246: }
2247: }
2248: }
2249: }
2250:
2251: # remove whitespace inside LON-CAPA elements that have an empty content-model (HTML ones are handled by html_to_xml)
2252: sub fix_empty_lc_elements {
2253: my ($node) = @_;
2254: my @lcempty = ('arc','axis','backgroundplot','drawoptionlist','drawvectorsum','fill','functionplotrule','functionplotvectorrule','functionplotvectorsumrule','hiddenline','hiddensubmission','key','line','location','organicstructure','parameter','plotobject','plotvector','responseparam','spline','textline');
2255: if (string_in_array(\@lcempty, $node->nodeName)) {
2256: if (defined $node->firstChild && !defined $node->firstChild->nextSibling &&
2257: $node->firstChild->nodeType == XML_TEXT_NODE && $node->firstChild->nodeValue =~ /^\s*$/) {
2258: $node->removeChild($node->firstChild);
2259: }
2260: if (defined $node->firstChild) {
2261: if ($warnings) {
2262: print "Warning: a ".$node->nodeName." has something inside\n";
2263: }
2264: }
2265: return;
2266: }
2267: for (my $child=$node->firstChild; defined $child; $child=$child->nextSibling) {
2268: if ($child->nodeType == XML_ELEMENT_NODE) {
2269: fix_empty_lc_elements($child);
2270: }
2271: }
2272: }
2273:
1.10 ! damieng 2274: # remove consecutive empty paragraphs (they will not show anyway)
! 2275: sub reduce_empty_p {
! 2276: my ($node) = @_;
! 2277: my $next;
! 2278: for (my $child=$node->firstChild; defined $child; $child=$next) {
! 2279: $next = $child->nextSibling;
! 2280: while (defined $next && $next->nodeType == XML_TEXT_NODE && $next->nodeValue =~ /^[ \t\f\n\r]*$/) {
! 2281: $next = $next->nextSibling;
! 2282: }
! 2283: if ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName eq 'p' && defined $next &&
! 2284: $next->nodeType == XML_ELEMENT_NODE && $next->nodeName eq 'p') {
! 2285: my $first = $child->firstChild;
! 2286: if (!defined $first || (!defined $first->nextSibling &&
! 2287: $first->nodeType == XML_TEXT_NODE && $first->nodeValue =~ /^[ \t\f\n\r]*$/)) {
! 2288: $first = $next->firstChild;
! 2289: if (!defined $first || (!defined $first->nextSibling &&
! 2290: $first->nodeType == XML_TEXT_NODE && $first->nodeValue =~ /^[ \t\f\n\r]*$/)) {
! 2291: $node->removeChild($child);
! 2292: }
! 2293: }
! 2294: }
! 2295: if ($child->nodeType == XML_ELEMENT_NODE) {
! 2296: reduce_empty_p($child);
! 2297: }
! 2298: }
! 2299: }
! 2300:
1.1 damieng 2301: # turn some attribute values into lowercase when they should be
2302: sub lowercase_attribute_values {
2303: my ($root) = @_;
2304: my @with_yesno = (['radiobuttonresponse', ['randomize']],
2305: ['optionresponse', ['randomize']],
2306: ['matchresponse', ['randomize']],
2307: ['itemgroup', ['randomize']],
2308: ['rankresponse', ['randomize']],
2309: ['functionplotresponse', ['xaxisvisible', 'yaxisvisible', 'gridvisible']],
2310: ['backgroundplot', ['fixed']],
2311: ['drawvectorsum', ['showvalue']],
2312: ['textline', ['readonly']],
2313: ['hint', ['showoncorrect']],
2314: ['body', ['dir']],
2315: ['img', ['encrypturl']],
2316: ['form', ['method']],
2317: ['input', ['type']]
2318: );
2319: foreach my $el_attributes (@with_yesno) {
2320: my $el_name = $el_attributes->[0];
2321: my @elements = $root->getElementsByTagName($el_name);
2322: foreach my $element (@elements) {
2323: my $att_list = $el_attributes->[1];
2324: foreach my $att_name (@$att_list) {
2325: my $att_value = $element->getAttribute($att_name);
2326: if (!defined $att_value) {
2327: next;
2328: }
2329: if ($att_value eq 'yes' || $att_value eq 'no') {
2330: next;
2331: }
2332: if ($att_value =~ /\s*yes\s*/i) {
2333: $element->setAttribute($att_name, 'yes');
2334: } elsif ($att_value =~ /\s*no\s*/i) {
2335: $element->setAttribute($att_name, 'no');
2336: }
2337: }
2338: }
2339: }
2340: }
2341:
2342: # fixes spelling mistakes for numericalresponse/@unit
2343: sub replace_numericalresponse_unit_attribute {
2344: my ($root) = @_;
2345: my @numericalresponses = $root->getElementsByTagName('numericalresponse');
2346: foreach my $numericalresponse (@numericalresponses) {
2347: if (defined $numericalresponse->getAttribute('units') && !defined $numericalresponse->getAttribute('unit')) {
2348: $numericalresponse->setAttribute('unit', $numericalresponse->getAttribute('units'));
2349: $numericalresponse->removeAttribute('units');
2350: }
2351: }
2352:
2353: }
2354:
2355: # Replaces &format and &prettyprint by <num> whenever possible.
2356: # Also replaces &chemparse by <chem>.
2357: # If the function call is enclosed in <display>, the <display> element is removed.
2358: sub replace_functions_by_elements {
2359: my ($root) = @_;
2360: my $doc = $root->ownerDocument;
2361: my @preserve = ('script','answer','parse','m','tm','dtm','numericalhintscript'); # display is handled later
2362: my @all = $root->getElementsByTagName('*');
2363: foreach my $element (@all) {
2364: if (string_in_array(\@preserve, $element->nodeName)) {
2365: next;
2366: }
2367: my $changed = 0;
2368: my $next;
2369: for (my $child=$element->firstChild; defined $child; $child=$next) {
2370: $next = $child->nextSibling;
2371: if ($child->nodeType == XML_TEXT_NODE) {
2372: my $value = $child->nodeValue;
2373: if ($value =~ /^(.*)&(?:format|prettyprint)\((\$\{?[a-zA-Z0-9]*\}?(?:\[[^\]]*\])?|[0-9.]+)\s?,\s?(["'][,.\$]?[0-9][eEfFgGsS]["']|\$[a-zA-Z0-9]*)\)(.*)$/s) {
2374: # NOTE: we don't check for &prettyprint's 3rd argument (target), but it has not been seen outside of script elements.
2375: # NOTE: the format options ',' and '$' are not supported by &format in current LON-CAPA since rev 1.81 of default_homework.lcpm,
2376: # but are supported by &prettyprint;
2377: # if we use (like current LON-CAPA) &prettyprint for <num> implementation, it will change a few resulting documents
2378: # (by making them display something they were probably intended to display, but which did not).
2379: # Usage of <num> with &prettyprint instead of &format might also change the display when there is an exponent.
2380: my $before = $1;
2381: my $number = $2;
2382: my $format = $3;
2383: my $after = $4;
2384: $format =~ s/^['"]|['"]$//g;
2385: # do not change this if the parent is <display> and there are other things before or after &format
2386: if ($element->nodeName eq 'display' && (defined $child->previousSibling || defined $next ||
2387: $before !~ /^\s*$/ || $after !~ /^\s*$/)) {
2388: last;
2389: }
2390: my $replacement = $doc->createDocumentFragment();
2391: my $num = $doc->createElement('num');
2392: $num->setAttribute('format', $format);
2393: $num->appendChild($doc->createTextNode($number));
2394: if (length($before) > 0) {
2395: $replacement->appendChild($doc->createTextNode($before));
2396: }
2397: $replacement->appendChild($num);
2398: if (length($after) > 0) {
2399: $replacement->appendChild($doc->createTextNode($after));
2400: }
2401: $element->replaceChild($replacement, $child);
2402: $changed = 1;
2403: $next = $element->firstChild; # start over, there might be another &format in the same text node
2404: } elsif ($value =~ /^(.*)&chemparse\(([^'"()]*|'[^']*'|"[^"]*")\)(.*)$/s) {
2405: my $before = $1;
2406: my $reaction = $2;
2407: my $after = $3;
2408: $reaction =~ s/^'(.*)'$/$1/;
2409: $reaction =~ s/^"(.*)"$/$1/;
2410: if ($element->nodeName eq 'display' && (defined $child->previousSibling || defined $next ||
2411: $before !~ /^\s*$/ || $after !~ /^\s*$/)) {
2412: last;
2413: }
2414: my $replacement = $doc->createDocumentFragment();
2415: my $chem = $doc->createElement('chem');
2416: $chem->appendChild($doc->createTextNode($reaction));
2417: if (length($before) > 0) {
2418: $replacement->appendChild($doc->createTextNode($before));
2419: }
2420: $replacement->appendChild($chem);
2421: if (length($after) > 0) {
2422: $replacement->appendChild($doc->createTextNode($after));
2423: }
2424: $element->replaceChild($replacement, $child);
2425: $changed = 1;
2426: $next = $element->firstChild;
2427: }
2428: }
2429: }
2430: if ($changed && $element->nodeName eq 'display') {
2431: my $first = $element->firstChild;
2432: if ($first->nodeType == XML_ELEMENT_NODE && string_in_array(['num','chem'], $first->nodeName) &&
2433: !defined $first->nextSibling) {
2434: # remove useless display element
2435: replace_by_children($element);
2436: }
2437: }
2438: }
2439: }
2440:
2441: # pretty-print using im-memory DOM tree
2442: sub pretty {
2443: my ($node, $all_block, $indent_level) = @_;
2444: my $doc = $node->ownerDocument;
2445: $indent_level ||= 0;
2446: my $type = $node->nodeType;
2447: if ($type == XML_ELEMENT_NODE) {
2448: my $name = $node->nodeName;
1.6 damieng 2449: if (string_in_array(\@preserve_elements, $name)) {
2450: # remove newlines at the beginning and the end of preserve elements
2451: if (defined $node->firstChild && ($node->firstChild->nodeType == XML_TEXT_NODE ||
2452: $node->firstChild->nodeType == XML_CDATA_SECTION_NODE)) {
2453: my $text = $node->firstChild->nodeValue;
2454: $text =~ s/^\n+//;
2455: if ($text eq '') {
2456: $node->removeChild($node->firstChild);
2457: } else {
2458: $node->firstChild->setData($text);
2459: }
2460: }
2461: if (defined $node->lastChild && ($node->lastChild->nodeType == XML_TEXT_NODE ||
2462: $node->lastChild->nodeType == XML_CDATA_SECTION_NODE)) {
2463: my $text = $node->lastChild->nodeValue;
2464: $text =~ s/\n+$//;
2465: if ($text eq '') {
2466: $node->removeChild($node->lastChild);
2467: } else {
2468: $node->lastChild->setData($text);
2469: }
2470: }
2471: } elsif (string_in_array($all_block, $name) || string_in_array(\@inline_like_block, $name)) {
1.1 damieng 2472: # make sure there is a newline at the beginning and at the end if there is anything inside
2473: if (defined $node->firstChild && !string_in_array(\@no_newline_inside, $name)) {
2474: my $first = $node->firstChild;
2475: if ($first->nodeType == XML_TEXT_NODE) {
2476: my $text = $first->nodeValue;
2477: if ($text !~ /^ *\n/) {
2478: $first->setData("\n" . $text);
2479: }
2480: } else {
2481: $node->insertBefore($doc->createTextNode("\n"), $first);
2482: }
2483: my $last = $node->lastChild;
2484: if ($last->nodeType == XML_TEXT_NODE) {
2485: my $text = $last->nodeValue;
2486: if ($text !~ /\n *$/) {
2487: $last->setData($text . "\n");
2488: }
2489: } else {
2490: $node->appendChild($doc->createTextNode("\n"));
2491: }
2492: }
2493:
2494: # indent and make sure there is a newline before and after a block element
2495: my $newline_indent = "\n".(' ' x (2*($indent_level + 1)));
2496: my $newline_indent_last = "\n".(' ' x (2*$indent_level));
2497: my $next;
2498: for (my $child=$node->firstChild; defined $child; $child=$next) {
2499: $next = $child->nextSibling;
2500: if ($child->nodeType == XML_ELEMENT_NODE) {
2501: if (string_in_array($all_block, $child->nodeName) || string_in_array(\@inline_like_block, $child->nodeName)) {
2502: # make sure there is a newline before and after a block element
2503: if (defined $child->previousSibling && $child->previousSibling->nodeType == XML_TEXT_NODE) {
2504: my $prev = $child->previousSibling;
2505: my $text = $prev->nodeValue;
2506: if ($text !~ /\n *$/) {
2507: $prev->setData($text . $newline_indent);
2508: }
2509: } else {
2510: $node->insertBefore($doc->createTextNode($newline_indent), $child);
2511: }
2512: if (defined $next && $next->nodeType == XML_TEXT_NODE) {
2513: my $text = $next->nodeValue;
2514: if ($text !~ /^ *\n/) {
2515: $next->setData($newline_indent . $text);
2516: }
2517: } else {
2518: $node->insertAfter($doc->createTextNode($newline_indent), $child);
2519: }
2520: }
2521: pretty($child, $all_block, $indent_level+1);
2522: } elsif ($child->nodeType == XML_TEXT_NODE) {
2523: my $text = $child->nodeValue;
2524: # collapse newlines
2525: $text =~ s/\n([\t ]*\n)+/\n/g;
2526: # indent and remove spaces and tabs before newlines
2527: if (defined $next) {
2528: $text =~ s/[\t ]*\n[\t ]*/$newline_indent/ge;
2529: } else {
2530: $text =~ s/[\t ]*\n[\t ]*/$newline_indent/ge;
2531: $text =~ s/[\t ]*\n[\t ]*$/$newline_indent_last/e;
2532: }
2533: $child->setData($text);
2534: }
2535: }
2536:
2537: # removes whitespace at the beginning and end of p td, th and li (except for nbsp at the beginning)
2538: my @to_trim = ('p','td','th','li');
2539: if (string_in_array(\@to_trim, $name) && defined $node->firstChild && $node->firstChild->nodeType == XML_TEXT_NODE) {
2540: my $text = $node->firstChild->nodeValue;
2541: $text =~ s/^[ \t\f\n\r]*//;
2542: if ($text eq '') {
2543: $node->removeChild($node->firstChild);
2544: } else {
2545: $node->firstChild->setData($text);
2546: }
2547: }
2548: if (string_in_array(\@to_trim, $name) && defined $node->lastChild && $node->lastChild->nodeType == XML_TEXT_NODE) {
2549: my $text = $node->lastChild->nodeValue;
2550: $text =~ s/\s*$//;
2551: if ($text eq '') {
2552: $node->removeChild($node->lastChild);
2553: } else {
2554: $node->lastChild->setData($text);
2555: }
2556: }
2557: }
2558: }
2559: }
2560:
2561: sub replace_tm_dtm {
2562: my ($root) = @_;
2563: my $doc = $root->ownerDocument;
2564: my @elements = $root->getElementsByTagName('tm');
2565: push(@elements, $root->getElementsByTagName('dtm'));
2566: foreach my $element (@elements) {
2567: my $first = $element->firstChild;
2568: if (defined $first && $first->nodeType == XML_TEXT_NODE) {
2569: my $text = $first->nodeValue;
2570: if ($element->nodeName eq 'tm') {
2571: $first->setData('$'.$text.'$');
2572: } else {
2573: $first->setData('$$'.$text.'$$');
2574: }
2575: }
2576: $element->setNodeName('m');
2577: }
2578: }
2579:
2580:
2581: ######## utilities ########
2582:
2583: ##
2584: # Trims a string (really, this should be built-in in Perl, this is ridiculous, ugly and slow)
2585: # @param {string} s - the string to trim
2586: # @returns the trimmed string
2587: ##
2588: sub trim {
2589: my ($s) = @_;
2590: $s =~ s/^\s+//;
2591: $s =~ s/\s+$//;
2592: return($s);
2593: }
2594:
2595: ##
2596: # Tests if a string is in an array (using eq) (to avoid Smartmatch warnings with $value ~~ @array)
2597: # @param {Array<string>} array - reference to the array of strings
2598: # @param {string} value - the string to look for
2599: # @returns 1 if found, 0 otherwise
2600: ##
2601: sub string_in_array {
2602: my ($array, $value) = @_;
2603: # NOTE: would it be faster to use "any { $value eq $_ } @{$array}" from List::Util ?
2604: foreach my $v (@{$array}) {
2605: if ($v eq $value) {
2606: return 1;
2607: }
2608: }
2609: return 0;
2610: }
2611:
2612: ##
2613: # Tests if an object is in an array (using ==)
2614: # @param {Array<Object>} array - reference to the array of references
2615: # @param {Object} ref - the reference to look for
2616: # @returns 1 if found, 0 otherwise
2617: ##
2618: sub reference_in_array {
2619: my ($array, $ref) = @_;
2620: foreach my $v (@{$array}) {
2621: if ($v == $ref) {
2622: return 1;
2623: }
2624: }
2625: return 0;
2626: }
2627:
2628: ##
2629: # returns the index of a string in an array
2630: # @param {Array<Object>} array - reference to the array of strings
2631: # @param {string} s - the string to look for (using eq)
2632: # @returns the index if found, -1 otherwise
2633: ##
2634: sub index_of_string {
2635: my ($array, $s) = @_;
2636: for (my $i=0; $i<scalar(@{$array}); $i++) {
2637: if ($array->[$i] eq $s) {
2638: return $i;
2639: }
2640: }
2641: return -1;
2642: }
2643:
2644: ##
2645: # returns the index of a reference in an array
2646: # @param {Array<Object>} array - reference to the array of references
2647: # @param {Object} ref - the reference to look for
2648: # @returns the index if found, -1 otherwise
2649: ##
2650: sub index_of_reference {
2651: my ($array, $ref) = @_;
2652: for (my $i=0; $i<scalar(@{$array}); $i++) {
2653: if ($array->[$i] == $ref) {
2654: return $i;
2655: }
2656: }
2657: return -1;
2658: }
2659:
2660: ##
2661: # if found, removes a string from an array, otherwise do nothing
2662: # @param {Array<string>} array - reference to the array of string
2663: # @param {string} s - the string to look for (using eq)
2664: ##
2665: sub remove_string_from_array {
2666: my ($array, $s) = @_;
2667: my $index = index_of_string($array, $s);
2668: if ($index != -1) {
2669: splice(@$array, $index, 1);
2670: }
2671: }
2672:
2673: ##
2674: # if found, removes a reference from an array, otherwise do nothing
2675: # @param {Array<Object>} array - reference to the array of references
2676: # @param {Object} ref - the reference to look for
2677: ##
2678: sub remove_reference_from_array {
2679: my ($array, $ref) = @_;
2680: my $index = index_of_reference($array, $ref);
2681: if ($index != -1) {
2682: splice(@$array, $index, 1);
2683: }
2684: }
2685:
2686: ##
2687: # replaces a node by its children
2688: # @param {Node} node - the DOM node
2689: ##
2690: sub replace_by_children {
2691: my ($node) = @_;
2692: my $parent = $node->parentNode;
2693: my $next;
2694: my $previous;
2695: for (my $child=$node->firstChild; defined $child; $child=$next) {
2696: $next = $child->nextSibling;
2697: if ((!defined $previous || !defined $next) &&
2698: $child->nodeType == XML_TEXT_NODE && $child->nodeValue =~ /^\s*$/) {
2699: next; # do not keep first and last whitespace nodes
2700: } else {
2701: if (!defined $previous && $child->nodeType == XML_TEXT_NODE) {
2702: # remove whitespace at the beginning
2703: my $value = $child->nodeValue;
2704: $value =~ s/^\s+//;
2705: $child->setData($value);
2706: }
2707: if (!defined $next && $child->nodeType == XML_TEXT_NODE) {
2708: # and at the end
2709: my $value = $child->nodeValue;
2710: $value =~ s/\s+$//;
2711: $child->setData($value);
2712: }
2713: }
2714: $node->removeChild($child);
2715: $parent->insertBefore($child, $node);
2716: $previous = $child;
2717: }
2718: $parent->removeChild($node);
2719: }
2720:
2721: ##
2722: # returns the trimmed attribute value if the attribute exists and is not blank, undef otherwise
2723: # @param {Node} node - the DOM node
2724: # @param {string} attribute_name - the attribute name
2725: ##
2726: sub get_non_empty_attribute {
2727: my ($node, $attribute_name) = @_;
2728: my $value = $node->getAttribute($attribute_name);
2729: if (defined $value && $value !~ /^\s*$/) {
2730: $value = trim($value);
2731: return($value);
2732: }
2733: return(undef);
2734: }
2735:
2736: ##
2737: # Returns a CSS property value from the style attribute of the element, or undef if not defined
2738: # @param {Element} el - the DOM element
2739: # @param {string} property_name - the CSS property name
2740: ##
2741: sub get_css_property {
2742: my ($el, $property_name) = @_;
2743: my $style = $el->getAttribute('style');
2744: if (defined $style) {
2745: $style =~ s/^\s*;\s*//;
2746: $style =~ s/\s*;\s*$//;
2747: } else {
2748: $style = '';
2749: }
2750: my @pairs = split(';', $style);
2751: foreach my $pair (@pairs) {
2752: my @name_value = split(':', $pair);
2753: if (scalar(@name_value) != 2) {
2754: next;
2755: }
2756: my $name = trim($name_value[0]);
2757: my $value = trim($name_value[1]);
2758: if (lc($name) eq $property_name) {
2759: return($value); # return the first one found
2760: }
2761: }
2762: return(undef);
2763: }
2764:
2765: ##
2766: # Returns the reference to a hash CSS property name => value from the style attribute of the element.
2767: # Returns an empty list if the style attribute is not defined,
2768: # @param {Element} el - the DOM element
2769: # @return {Hash<string, string>} reference to the hash property name => property value
2770: ##
2771: sub get_css_properties {
2772: my ($el) = @_;
2773: my $style = $el->getAttribute('style');
2774: if (defined $style) {
2775: $style =~ s/^\s*;\s*//;
2776: $style =~ s/\s*;\s*$//;
2777: } else {
2778: $style = '';
2779: }
2780: my @pairs = split(';', $style);
2781: tie (my %hash, 'Tie::IxHash', ());
2782: foreach my $pair (@pairs) {
2783: my @name_value = split(':', $pair);
2784: if (scalar(@name_value) != 2) {
2785: next;
2786: }
2787: my $name = trim($name_value[0]);
2788: my $value = trim($name_value[1]);
2789: if (defined $hash{$name}) {
2790: # duplicate property in the style attribute: keep only the last one
2791: delete $hash{$name};
2792: }
2793: $hash{$name} = $value;
2794: }
2795: return(\%hash);
2796: }
2797:
2798: ##
2799: # Sets a CSS property in the style attribute of an element
2800: # @param {Element} el - the DOM element
2801: # @param {string} property_name - the CSS property name
2802: # @param {string} property_value - the CSS property value
2803: ##
2804: sub set_css_property {
2805: my ($el, $property_name, $property_value) = @_;
2806: my $hash_ref = { $property_name => $property_value };
2807: set_css_properties($el, $hash_ref);
2808: }
2809:
2810: ##
2811: # Sets several CSS properties in the style attribute of an element
2812: # @param {Element} el - the DOM element
2813: # @param {Hash<string, string>} properties - reference to the hash property name => property value
2814: ##
2815: sub set_css_properties {
2816: my ($el, $properties) = @_;
2817: my $hash = get_css_properties($el);
2818: foreach my $property_name (keys %$properties) {
2819: my $property_value = $properties->{$property_name};
2820: if (defined $hash->{$property_name}) {
2821: delete $hash->{$property_name}; # to add the new one at the end
2822: }
2823: $hash->{$property_name} = $property_value;
2824: }
2825: my $style = '';
2826: foreach my $key (keys %$hash) {
2827: $style .= $key.':'.$hash->{$key}.'; ';
2828: }
2829: $style =~ s/; $//;
2830: $el->setAttribute('style', $style);
2831: }
2832:
2833: 1;
2834: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>