--- loncom/homework/cleanxml/xml_to_loncapa.pm 2016/01/05 18:51:08 1.4 +++ loncom/homework/cleanxml/xml_to_loncapa.pm 2016/01/20 00:41:31 1.7 @@ -1,7 +1,7 @@ # The LearningOnline Network # convert_file takes a well-formed XML file content and converts it to LON-CAPA syntax. # -# $Id: xml_to_loncapa.pm,v 1.4 2016/01/05 18:51:08 damieng Exp $ +# $Id: xml_to_loncapa.pm,v 1.7 2016/01/20 00:41:31 damieng Exp $ # # Copyright Michigan State University Board of Trustees # @@ -60,7 +60,10 @@ sub convert_file { my ($contents) = @_; my $dom_doc = XML::LibXML->load_xml(string => $contents); - add_outtext($dom_doc); + my $root = $dom_doc->documentElement(); + if (defined $root && $root->nodeName ne 'html') { + add_outtext($dom_doc); + } return node_to_string($dom_doc); } @@ -78,7 +81,7 @@ sub node_to_string { if (defined $parent->parentNode) { $grandparent_name = $parent->parentNode->nodeName; } - my @no_escape = ('m', 'script', 'display', 'parse', 'answer'); + my @no_escape = ('m', 'script', 'style', 'display', 'parse', 'answer'); if (string_in_array(\@no_escape, $parent_name) && ($parent_name ne 'answer' || (defined $grandparent_name && @@ -252,6 +255,21 @@ sub add_endouttext { } else { $parent->appendChild($endouttext); } + # replace spaces afterwards by a \n + indentation + my $next = $endouttext->nextSibling; + if (defined $next && $next->nodeType == XML_TEXT_NODE) { + my $v = $next->nodeValue; + if ($v =~ /^ /) { + $v =~ s/^ +//; + if ($parent->firstChild->nodeType == XML_TEXT_NODE && + $parent->firstChild->nodeValue =~ /^\n +$/) { + $v = $parent->firstChild->nodeValue.$v; + } else { + $v = "\n".$v; + } + $next->setData($v); + } + } } # Convert paragraph children when one contains an inline response into content +