summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-03-14 02:30:35 (GMT)
committerDavid A. Madore <david+git@madore.org>2010-03-14 02:30:35 (GMT)
commit1eaafea741f93b3198541bd501c4488879f13004 (patch)
tree778357240fc0587edf3dcf3d7b54ad500e168b27
parentad480a34ed432e6989ccad46268483343bc3e8ef (diff)
downloadblogengine-1eaafea741f93b3198541bd501c4488879f13004.zip
blogengine-1eaafea741f93b3198541bd501c4488879f13004.tar.gz
blogengine-1eaafea741f93b3198541bd501c4488879f13004.tar.bz2
More annoying stuff in daml2html translation.
Navigation bar, meta and link elements, etc. The xml:lang handling mess is starting to rear its ugly head.
-rwxr-xr-xdaml2html.pl147
1 files changed, 139 insertions, 8 deletions
diff --git a/daml2html.pl b/daml2html.pl
index 774ee85..accd2ee 100755
--- a/daml2html.pl
+++ b/daml2html.pl
@@ -1,9 +1,11 @@
#! /usr/local/bin/perl -w
+# -*- coding: utf-8 -*-
require 5.10.0;
use strict;
use warnings;
+use utf8;
use Encode;
@@ -90,10 +92,50 @@ if ( defined($ARGV[0]) ) {
die "Root element should be daml"
unless $in_doc->documentElement->localname eq "daml";
+my $xpc = XML::LibXML::XPathContext->new($in_doc);
+#$xpc->registerNs('h', XHTML_NS);
+
+sub rec_lang {
+ my $node = shift;
+ my $lang;
+ while ( defined($node) && $node->nodeType == XML_ELEMENT_NODE ) {
+ $lang = $node->getAttributeNS(XML_XML_NS, "lang");
+ return $lang if defined($lang);
+ $node = $node->parentNode;
+ }
+ return undef;
+}
+
+sub set_rec_lang {
+ my $node = shift;
+ my $lang = shift;
+ return unless defined($lang);
+ my $parent = $node->parentNode;
+ my $parent_lang = defined($parent)?rec_lang($parent):undef;
+ if ( defined($parent_lang) && $lang eq $parent_lang ) {
+ $node->removeAttributeNS(XML_XML_NS, "lang");
+ } else {
+ $node->setAttributeNS(XML_XML_NS, "lang", $lang);
+ }
+}
+
+my $uri_to_top = $in_doc->documentElement->getAttributeNS("", "uri-to-top") // "";
+my $file_name = $in_doc->documentElement->getAttributeNS("", "file.name");
+if ( ! defined($file_name) ) {
+ print STDERR "Warning: file.name unspecified!\n";
+}
+my $top_lang = rec_lang($in_doc->documentElement);
+
+my $in_body = ($xpc->findnodes("/daml/body"))[0];
+my $body_lang = rec_lang($in_body) // $top_lang;
+
my $out_doc = XML::LibXML::Document->new("1.0","utf-8");
my $out_dtd = $out_doc->createInternalSubset("html", XHTML_PUBID, XHTML_URI);
my $out_html = $out_doc->createElementNS(XHTML_NS, "html");
$out_doc->setDocumentElement($out_html);
+if ( defined($top_lang) ) {
+ set_rec_lang($out_html, $top_lang);
+}
$out_html->appendChild($out_doc->createTextNode("\n"));
$out_html->appendChild($out_doc->createComment(" This file is automatically generated. Do not edit! "));
$out_html->appendChild($out_doc->createTextNode("\n"));
@@ -106,16 +148,11 @@ $out_head->appendChild($out_doc->createTextNode("\n"));
$out_html->appendChild($out_doc->createTextNode("\n"));
my $out_body = $out_doc->createElementNS(XHTML_NS, "body");
$out_html->appendChild($out_body);
+$out_body->setAttributeNS("","onload","onLoad()");
+set_rec_lang($out_body, $body_lang);
$out_body->appendChild($out_doc->createTextNode("\n"));
$out_html->appendChild($out_doc->createTextNode("\n"));
-my $top_lang = $in_doc->documentElement->getAttributeNS(XML_XML_NS, "lang");
-if ( defined($top_lang) ) {
- $out_html->setAttributeNS(XML_XML_NS, "lang", $top_lang);
-}
-
-my $xpc = XML::LibXML::XPathContext->new($in_doc);
-#$xpc->registerNs('h', XHTML_NS);
my $in_title = ($xpc->findnodes("/daml/title"))[0];
if ( defined($in_title) ) {
$out_title->appendChild($out_doc->createTextNode($in_title->textContent));
@@ -123,10 +160,12 @@ if ( defined($in_title) ) {
$out_meta_title->setAttributeNS("", "name", "Title");
$out_meta_title->setAttributeNS("", "content", $in_title->textContent);
$out_head->appendChild($out_meta_title);
+ set_rec_lang($out_meta_title, rec_lang $in_title);
$out_head->appendChild($out_doc->createTextNode("\n"));
my $out_h1 = $out_doc->createElementNS(XHTML_NS, "h1");
$out_h1->setAttributeNS("", "class", "title");
$out_body->appendChild($out_h1);
+ set_rec_lang($out_h1, rec_lang $in_title);
$out_body->appendChild($out_doc->createTextNode("\n"));
foreach my $child ( $in_title->childNodes ) {
$out_h1->appendChild($out_doc->importNode($child,1));
@@ -137,6 +176,7 @@ if ( defined($in_subtitle) ) {
my $out_p = $out_doc->createElementNS(XHTML_NS, "p");
$out_p->setAttributeNS("", "class", "subtitle");
$out_body->appendChild($out_p);
+ set_rec_lang($out_p, rec_lang $in_subtitle);
$out_body->appendChild($out_doc->createTextNode("\n"));
foreach my $child ( $in_subtitle->childNodes ) {
$out_p->appendChild($out_doc->importNode($child,1));
@@ -149,6 +189,7 @@ if ( defined($in_meta_description) ) {
$out_meta_description->setAttributeNS("", "content",
$in_meta_description->textContent);
$out_head->appendChild($out_meta_description);
+ set_rec_lang($out_meta_description, rec_lang $in_meta_description);
$out_head->appendChild($out_doc->createTextNode("\n"));
}
my $in_meta_keywords = ($xpc->findnodes("/daml/meta-keywords"))[0];
@@ -158,10 +199,95 @@ if ( defined($in_meta_keywords) ) {
$out_meta_keywords->setAttributeNS("", "content",
$in_meta_keywords->textContent);
$out_head->appendChild($out_meta_keywords);
+ set_rec_lang($out_meta_keywords, rec_lang $in_meta_keywords);
$out_head->appendChild($out_doc->createTextNode("\n"));
}
-my $in_body = ($xpc->findnodes("/daml/body"))[0];
+{
+ my $out_meta = $out_doc->createElementNS(XHTML_NS, "meta");
+ $out_meta->setAttributeNS("", "http-equiv", "Content-Type");
+ $out_meta->setAttributeNS("", "content", "text/html; charset=utf-8");
+ $out_head->appendChild($out_meta);
+ $out_head->appendChild($out_doc->createTextNode("\n"));
+}
+if ( defined($body_lang) && $body_lang ne "und" ) {
+ my $out_meta = $out_doc->createElementNS(XHTML_NS, "meta");
+ $out_meta->setAttributeNS("", "http-equiv", "Content-Language");
+ $out_meta->setAttributeNS("", "content", $body_lang);
+ $out_head->appendChild($out_meta);
+ $out_head->appendChild($out_doc->createTextNode("\n"));
+}
+
+{
+ my $out_link = $out_doc->createElementNS(XHTML_NS, "link");
+ $out_link->setAttributeNS("", "href", $uri_to_top."favicon.ico");
+ $out_link->setAttributeNS("", "rel", "Shortcut Icon");
+ $out_head->appendChild($out_link);
+ $out_head->appendChild($out_doc->createTextNode("\n"));
+}
+
+my @in_translations = $xpc->findnodes("/daml/translation");
+foreach my $in_translation ( @in_translations ) {
+ my $lang = $in_translation->getAttributeNS(XML_XML_NS, "lang");
+ die "xml:lang attribute missing on <translation>" unless defined($lang);
+ my $out_link = $out_doc->createElementNS(XHTML_NS, "link");
+ $out_link->setAttributeNS("", "href", $file_name.".".$lang);
+ $out_link->setAttributeNS("", "hreflang", $lang);
+ if ( $lang eq "en" ) {
+ $out_link->setAttributeNS("", "title", "English version");
+ } elsif ( $lang eq "fr" ) {
+ $out_link->setAttributeNS("", "title", "Version fran├žaise");
+ }
+ $out_link->setAttributeNS("", "rel", "Alternate");
+ $out_link->setAttributeNS(XML_XML_NS, "lang", $lang);
+ $out_head->appendChild($out_link);
+ $out_head->appendChild($out_doc->createTextNode("\n"));
+}
+
+my @in_metastuff = $xpc->findnodes("/daml/*[self::meta or self::link]");
+foreach my $in_mnode ( @in_metastuff ) {
+ $out_head->appendChild($out_doc->importNode($in_mnode,1));
+ $out_head->appendChild($out_doc->createTextNode("\n"));
+}
+
+{
+ my $out_navbar = $out_doc->createElementNS(XHTML_NS, "p");
+ $out_navbar->setAttributeNS("", "class", "navbar");
+ $out_body->appendChild($out_navbar);
+ $out_head->appendChild($out_doc->createTextNode("\n"));
+ my $lang;
+ if ( defined($body_lang) && $body_lang eq "fr" ) {
+ $lang = "fr";
+ } else {
+ $lang = "en";
+ }
+ my $out_node;
+ $out_node = $out_doc->createElementNS(XHTML_NS, "a");
+ $out_node->setAttributeNS("", "href", "http://www.madore.org/~david/");
+ $out_navbar->appendChild($out_node);
+ $out_node->appendChild($out_doc->createTextNode("David Madore"));
+ $out_navbar->appendChild($out_doc->createElementNS(XHTML_NS, "br"));
+ $out_node = $out_doc->createElementNS(XHTML_NS, "a");
+ $out_node->setAttributeNS("", "href", "http://perso.enst.fr/~madore/");
+ $out_navbar->appendChild($out_node);
+ $out_node->appendChild($out_doc->createTextNode($lang eq "fr"?"Site prof.":"Prof. site"));
+ $out_navbar->appendChild($out_doc->createElementNS(XHTML_NS, "br"));
+ $out_node = $out_doc->createElementNS(XHTML_NS, "a");
+ $out_node->setAttributeNS("", "href", $uri_to_top);
+ $out_navbar->appendChild($out_node);
+ $out_node->appendChild($out_doc->createTextNode($lang eq "fr"?"Racine du site":"Site Root"));
+ $out_navbar->appendChild($out_doc->createElementNS(XHTML_NS, "br"));
+ $out_node = $out_doc->createElementNS(XHTML_NS, "a");
+ $out_node->setAttributeNS("", "href", $uri_to_top."sitemap.html");
+ $out_navbar->appendChild($out_node);
+ $out_node->appendChild($out_doc->createTextNode($lang eq "fr"?"Plan du site":"Site Map"));
+ $out_navbar->appendChild($out_doc->createElementNS(XHTML_NS, "br"));
+ $out_node = $out_doc->createElementNS(XHTML_NS, "a");
+ $out_node->setAttributeNS("", "href", $uri_to_top."weblog/");
+ $out_navbar->appendChild($out_node);
+ $out_node->appendChild($out_doc->createTextNode("WebLog"));
+}
+
if ( defined($in_body) ) {
foreach my $child ( $in_body->childNodes ) {
$out_doc->adoptNode($child);
@@ -182,6 +308,11 @@ if ( open my $common_script_file, "<", "included.js" ) {
close $common_script_file;
}
+my @in_extra_style = $xpc->findnodes("/daml/extra-style");
+foreach my $in_extra_style ( @in_extra_style ) {
+ $pure_style_content .= $in_extra_style->textContent;
+}
+
my $out_style_section = $out_doc->createElementNS(XHTML_NS, "style");
$out_head->appendChild($out_style_section);
$out_head->appendChild($out_doc->createTextNode("\n"));