diff options
author | David A. Madore <david+git@madore.org> | 2010-03-27 16:33:36 (GMT) |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2010-03-27 16:33:36 (GMT) |
commit | 4aee8b086dfc9735f0530ffe220e7d9550a003d9 (patch) | |
tree | 64adf39990619caf142d24249650f3d54c30e8d3 | |
parent | 31b87b24ce3bc9b4cb5f9b6877a2101a922d463b (diff) | |
download | blogengine-4aee8b086dfc9735f0530ffe220e7d9550a003d9.zip blogengine-4aee8b086dfc9735f0530ffe220e7d9550a003d9.tar.gz blogengine-4aee8b086dfc9735f0530ffe220e7d9550a003d9.tar.bz2 |
Title and subtitle handling, better language handling, prepare for navbar insertion.
-rwxr-xr-x | daml2html.pl | 156 |
1 files changed, 143 insertions, 13 deletions
diff --git a/daml2html.pl b/daml2html.pl index e522966..2e40272 100755 --- a/daml2html.pl +++ b/daml2html.pl @@ -34,6 +34,38 @@ my $obtain_cdates = $opts{c}; my $parser = XML::LibXML->new(); +sub get_node_lang_norec { + my $node = shift; + return $node->getAttributeNS(XML_XML_NS, "lang"); +} + +sub get_node_lang_rec { + my $node = shift; + my $lang; + while ( defined($node) && $node->nodeType == XML_ELEMENT_NODE ) { + $lang = get_node_lang_norec $node; + return $lang if defined($lang); + $node = $node->parentNode; + } + return undef; +} + +sub set_node_lang_norec { + my $node = shift; + my $lang = shift; + $node->setAttributeNS(XML_XML_NS, "lang", $lang); +} + +sub set_node_lang_rec { + my $node = shift; + my $lang = shift; + my $parent_lang = get_node_lang_rec($node->parentNode); # Works if undef! + if ( ! defined($parent_lang) || $lang ne $parent_lang ) { + set_node_lang_norec $node, $lang; + } +} + + # Functions to take care of the x-daml-magic URI scheme (this should go away) sub match_daml_magic_uri { @@ -123,7 +155,8 @@ unshift @todo_stack, [undef, $doc->documentElement, {is_root=>1}]; # Global variables for processing: my $html_node; my $head_node; -my $src_title; +my ($src_title, $src_title_lang); +my ($src_subtitle, $src_subtitle_lang); my $style_content = ""; my $script_content = ""; @@ -159,14 +192,61 @@ sub default_handler { default_handler_nodeonly $node, $optref; } +sub do_title_or_subtitle_handler { + my $node = shift; + my $optref = shift; + my $type = shift; + die "do_title_or_subtitle_handler called with type other than \"title\" or \"subtitle\"" + unless defined($type) && ($type eq "title" || $type eq "subtitle"); + my $src_t = shift or die "missing src_t argument"; + my $src_t_lang = shift; + if ( $src_t_lang ) { + printf STDERR ("warning: ignoring xml:lang attribute on %s node\n", + $src_t->nodeName) + if get_node_lang_norec $node; + set_node_lang_rec $node, $src_t_lang; + } + my $new_t_node = $doc->createElementNS(XHTML_NS, + ($type eq "title" ? "h1" : "p")); + $new_t_node->setAttributeNS("", "class", $type); + set_node_lang_rec $node, $src_t_lang; + if ( my $lang = get_node_lang_norec $node ) { + set_node_lang_norec $new_t_node, $lang; + } + $node->replaceNode($new_t_node); + my @child_nodes = $src_t->childNodes; + foreach my $child ( @child_nodes ) { + $new_t_node->appendChild($child->cloneNode(1)); + } + unshift @todo_stack, [undef, $new_t_node, {}]; +} + +sub do_title_handler { + my $node = shift; + my $optref = shift; + print STDERR "warning: title handler doesn't handle arguments\n" if @_; + do_title_or_subtitle_handler $node, $optref, "title", $src_title, $src_title_lang; +} + +sub do_subtitle_handler { + my $node = shift; + my $optref = shift; + print STDERR "warning: subtitle handler doesn't handle arguments\n" if @_; + do_title_or_subtitle_handler $node, $optref, "subtitle", $src_subtitle, $src_subtitle_lang; +} + sub create_meta_element_helper { my $src_node = shift; # May be undef my $name_or_http_equiv = shift; my $meta_name = shift; my $meta_content = shift; + my $lang = shift; my $meta = $doc->createElementNS(XHTML_NS, "meta"); - if ( defined($src_node) && ( my $lang = $src_node->getAttributeNS(XML_XML_NS, "lang") ) ) { - $meta->setAttributeNS(XML_XML_NS, "lang", $lang); + if ( defined($src_node) && !defined($lang) ) { + $lang = get_node_lang_norec $src_node; + } + if ( defined($lang) ) { + set_node_lang_norec $meta, $lang; } $meta->setAttributeNS("", $name_or_http_equiv, $meta_name); $meta->setAttributeNS("", "content", $meta_content); @@ -185,7 +265,7 @@ sub create_style_or_script_node { my $node = shift; my $optref = shift; my $type = shift; - die "create_style_or_script_node with type other than \"style\" or \"script\"" + die "create_style_or_script_node called with type other than \"style\" or \"script\"" unless defined($type) && ($type eq "style" || $type eq "script"); my $valref = shift; die "\$head_node should have been defined at this point" @@ -216,8 +296,8 @@ sub daml_handler { die "\$html_node already defined: what magic is this?" if defined($html_node); $html_node = $doc->createElementNS(XHTML_NS, "html"); my $lang; - if ( $lang = $node->getAttributeNS(XML_XML_NS, "lang") ) { - $html_node->setAttributeNS(XML_XML_NS, "lang", $lang); + if ( $lang = get_node_lang_norec $node ) { + set_node_lang_norec $html_node, $lang; } $doc->setDocumentElement($html_node); $html_node->appendChild($doc->createTextNode("\n")); @@ -262,14 +342,36 @@ sub body_handler { my $optref = shift; print STDERR "warning: body handler doesn't handle arguments\n" if @_; my $body_node = $doc->createElementNS(XHTML_NS, "body"); - if ( my $lang = $node->getAttributeNS(XML_XML_NS, "lang") ) { - $body_node->setAttributeNS(XML_XML_NS, "lang", $lang); + if ( my $lang = get_node_lang_norec $node ) { + set_node_lang_norec $body_node, $lang; } - $body->setAttributeNS("", "onload", "onLoad()"); + $body_node->setAttributeNS("", "onload", "onLoad()"); ($doc->documentElement)->appendChild($body_node); # Work around libxml2 bug <URL: https://bugzilla.gnome.org/show_bug.cgi?id=614068 > $node->replaceNode($body_node); my @child_nodes = $node->childNodes; my @to_process; + unless ( $node->getAttributeNS("", "notitle") ) { + if ( defined($src_title) ) { + my $token = $doc->createElementNS(DAML_NS, "d:implicit-do-title"); + $body_node->appendChild($doc->createTextNode("\n")); + $body_node->appendChild($token); + unshift @to_process, [\&do_title_handler, $token, {implicit=>1}]; + } + } + unless ( $node->getAttributeNS("", "nosubtitle") ) { + if ( defined($src_subtitle) ) { + my $token = $doc->createElementNS(DAML_NS, "d:implicit-do-subtitle"); + $body_node->appendChild($doc->createTextNode("\n")); + $body_node->appendChild($token); + unshift @to_process, [\&do_subtitle_handler, $token, {implicit=>1}]; + } + } + unless ( $node->getAttributeNS("", "nonavbar") ) { + my $token = $doc->createElementNS(DAML_NS, "d:implicit-do-navbar"); + $body_node->appendChild($doc->createTextNode("\n")); + $body_node->appendChild($token); +# unshift @to_process, [\&do_navbar_handler, $token, {implicit=>1}]; + } foreach my $child ( @child_nodes ) { if ( $child->nodeType == XML_TEXT_NODE || $child->nodeType == XML_CDATA_SECTION_NODE ) { @@ -280,6 +382,12 @@ sub body_handler { push @to_process, [undef, $child, {}] if $child->nodeType == XML_ELEMENT_NODE; } + unless ( $node->getAttributeNS("", "nofooter") ) { + my $token = $doc->createElementNS(DAML_NS, "d:implicit-do-footer"); + $body_node->appendChild($token); + $body_node->appendChild($doc->createTextNode("\n")); +# unshift @to_process, [\&do_footer_handler, $token, {implicit=>1}]; + } unshift @todo_stack, @to_process; } @@ -288,19 +396,41 @@ sub title_handler { my $optref = shift; print STDERR "warning: title handler doesn't handle arguments\n" if @_; if ( $$optref{is_daml_child} ) { - $src_title = $node; my $title_text = $node->textContent; + $src_title = $doc->createDocumentFragment; + $src_title_lang = get_node_lang_rec $node; + my $src_title_explicit_lang = get_node_lang_norec $node; + foreach my $child ( $node->childNodes ) { + $src_title->appendChild($child); + } my $title_node = $doc->createElementNS(XHTML_NS, "title"); - if ( my $lang = $node->getAttributeNS(XML_XML_NS, "lang") ) { - $title_node->setAttributeNS(XML_XML_NS, "lang", $lang); + if ( $src_title_explicit_lang ) { + # Note here this is with norec, earlier with rec... + set_node_lang_norec $title_node, $src_title_explicit_lang; } ($doc->documentElement)->appendChild($title_node); # Work around libxml2 bug <URL: https://bugzilla.gnome.org/show_bug.cgi?id=614068 > $node->replaceNode($title_node); $title_node->appendChild($doc->createTextNode($title_text)); - create_meta_element_helper undef, "name", "Title", $title_text; + create_meta_element_helper undef, "name", "Title", $title_text, $src_title_explicit_lang; } } +sub subtitle_handler { + my $node = shift; + my $optref = shift; + print STDERR "warning: subtitle handler doesn't handle arguments\n" if @_; + $src_subtitle = $doc->createDocumentFragment; + $src_subtitle_lang = get_node_lang_norec $node; + foreach my $child ( $node->childNodes ) { + $src_subtitle->appendChild($child); + } + if ( ($node->nextSibling)->nodeType == XML_TEXT_NODE + && ($node->nextSibling)->data =~ m/^\s*$/s ) { + $node->nextSibling->unbindNode; + } + $node->unbindNode; +} + sub meta_handler { my $node = shift; my $optref = shift; |