summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david@procyon.(none)>2010-04-05 16:52:50 (GMT)
committerDavid A. Madore <david@procyon.(none)>2010-04-05 16:52:50 (GMT)
commitcbd82c214bad5ba1d2936a3e8ee599dde6b560bc (patch)
treebfdaeb14f012b40e861c15859d1305d3634a931a
parentdd14e78fef672acf2361416ac9eb17857ec55df7 (diff)
downloadblogengine-cbd82c214bad5ba1d2936a3e8ee599dde6b560bc.zip
blogengine-cbd82c214bad5ba1d2936a3e8ee599dde6b560bc.tar.gz
blogengine-cbd82c214bad5ba1d2936a3e8ee599dde6b560bc.tar.bz2
Navbar handling. Also fix/clarify lang handling, and process uri-to-top and file.name attributes on daml.
-rwxr-xr-xdaml2html.pl82
1 files changed, 76 insertions, 6 deletions
diff --git a/daml2html.pl b/daml2html.pl
index a672101..58196fd 100755
--- a/daml2html.pl
+++ b/daml2html.pl
@@ -35,11 +35,15 @@ my $parser = XML::LibXML->new();
sub get_node_lang_norec {
+ # Return the xml:lang attribute for $node, or undef if there is none.
my $node = shift;
return $node->getAttributeNS(XML_XML_NS, "lang");
}
sub get_node_lang_rec {
+ # Return the xml:lang attribute for $node, or that if its parent
+ # recursively (can return undef if no parent ever has an xml:lang
+ # attribute). $node may be undef here, in which case return undef.
my $node = shift;
my $lang;
while ( defined($node) && $node->nodeType == XML_ELEMENT_NODE ) {
@@ -51,17 +55,32 @@ sub get_node_lang_rec {
}
sub set_node_lang_norec {
+ # Explicitly set the xml:lang attribute to a given value (even if
+ # parent already has this).
my $node = shift;
my $lang = shift;
+ die "set_node_lang_norec called with no lang" unless $lang;
$node->setAttributeNS(XML_XML_NS, "lang", $lang);
}
+sub unset_node_lang_norec {
+ # Explicitly unset the xml:lang attribute (this is purposefully a
+ # different function from the previous one to catch programming
+ # errors).
+ my $node = shift;
+ $node->removeAttributeNS(XML_XML_NS, "lang");
+}
+
sub set_node_lang_rec {
+ # Set the xml:lang attribute if necessary to the given value, or
+ # unset if same lang as parent is requested.
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;
+ } else {
+ unset_node_lang_norec $node;
}
}
@@ -153,6 +172,8 @@ my @todo_stack;
unshift @todo_stack, [undef, $doc->documentElement, {is_root=>1}];
# Global variables for processing:
+my $uri_to_top;
+my $file_name;
my $html_node;
my $head_node;
my ($src_title, $src_title_lang);
@@ -210,7 +231,6 @@ sub do_title_or_subtitle_handler {
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;
}
@@ -255,7 +275,7 @@ sub do_translations_handler {
my $a = $doc->createElementNS(XHTML_NS, "a");
$p->appendChild($a);
$a->setAttributeNS("", "hreflang", $lang);
- $a->setAttributeNS("", "href", "FIXME.".$lang);
+ $a->setAttributeNS("", "href", ($file_name//"").".".$lang);
$str = $lang;
if ( $lang eq "en" ) {
$str = "English version";
@@ -278,6 +298,50 @@ sub do_translations_handler {
$node->unbindNode;
}
+sub do_navbar_handler {
+ my $node = shift;
+ my $optref = shift;
+ print STDERR "warning: do-navbar handler doesn't handle arguments\n" if @_;
+ my $p = $doc->createElementNS(XHTML_NS, "p");
+ my $lang = get_node_lang_rec $node;
+ my $explicit_lang = get_node_lang_norec $node;
+ unless ( defined($lang) && ( $lang eq "en" || $lang eq "fr" ) ) {
+ print STDERR "warning: will use English navbar\n";
+ $lang = "en";
+ $explicit_lang = "en";
+ }
+ if ( defined($explicit_lang) ) {
+ set_node_lang_norec $p, $explicit_lang;
+ }
+ $p->setAttributeNS("", "class", "navbar");
+ $node->replaceNode($p);
+ my $a;
+ $a = $doc->createElementNS(XHTML_NS, "a");
+ $a->setAttributeNS("", "href", "http://www.madore.org/~david/");
+ $p->appendChild($a);
+ $a->appendChild($doc->createTextNode("David Madore"));
+ $p->appendChild($doc->createElementNS(XHTML_NS, "br"));
+ $a = $doc->createElementNS(XHTML_NS, "a");
+ $a->setAttributeNS("", "href", "http://perso.enst.fr/~madore/");
+ $p->appendChild($a);
+ $a->appendChild($doc->createTextNode($lang eq "fr" ? "Site prof." : "Prof. site"));
+ $p->appendChild($doc->createElementNS(XHTML_NS, "br"));
+ $a = $doc->createElementNS(XHTML_NS, "a");
+ $a->setAttributeNS("", "href", $uri_to_top//"./");
+ $p->appendChild($a);
+ $a->appendChild($doc->createTextNode($lang eq "fr" ? "Racine du site" : "Site Root"));
+ $p->appendChild($doc->createElementNS(XHTML_NS, "br"));
+ $a = $doc->createElementNS(XHTML_NS, "a");
+ $a->setAttributeNS("", "href", ($uri_to_top//"")."sitemap.html");
+ $p->appendChild($a);
+ $a->appendChild($doc->createTextNode($lang eq "fr" ? "Plan du site" : "Site Map"));
+ $p->appendChild($doc->createElementNS(XHTML_NS, "br"));
+ $a = $doc->createElementNS(XHTML_NS, "a");
+ $a->setAttributeNS("", "href", ($uri_to_top//"")."weblog/");
+ $p->appendChild($a);
+ $a->appendChild($doc->createTextNode("WebLog"));
+}
+
sub create_meta_element_helper {
my $src_node = shift; # May be undef
my $name_or_http_equiv = shift;
@@ -288,7 +352,7 @@ sub create_meta_element_helper {
if ( defined($src_node) && !defined($lang) ) {
$lang = get_node_lang_norec $src_node;
}
- if ( defined($lang) ) {
+ if ( $lang ) {
set_node_lang_norec $meta, $lang;
}
$meta->setAttributeNS("", $name_or_http_equiv, $meta_name);
@@ -334,7 +398,7 @@ sub create_style_or_script_node {
sub daml_handler {
my $node = shift;
my $optref = shift;
- print STDERR "warning: title handler doesn't handle arguments\n" if @_;
+ print STDERR "warning: daml handler doesn't handle arguments\n" if @_;
die "daml element can only be root element" unless $$optref{is_root};
die "\$html_node already defined: what magic is this?" if defined($html_node);
$html_node = $doc->createElementNS(XHTML_NS, "html");
@@ -378,6 +442,12 @@ sub daml_handler {
"style", \$style_content];
push @todo_stack, [\&create_style_or_script_node, $head_node, {special=>1},
"script", \$script_content];
+ if ( defined(my $attr = $node->getAttributeNS("", "uri-to-top")) ) {
+ $uri_to_top = $attr;
+ }
+ if ( defined(my $attr = $node->getAttributeNS("", "file.name")) ) {
+ $file_name = $attr;
+ }
}
sub body_handler {
@@ -413,7 +483,7 @@ sub body_handler {
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}];
+ unshift @to_process, [\&do_navbar_handler, $token, {implicit=>1}];
}
unless ( $node->getAttributeNS("", "notranslations") ) {
if ( @translations ) {
@@ -509,7 +579,7 @@ sub translation_handler {
$link->setAttributeNS("", "title", "Version fran├žaise");
}
$link->setAttributeNS("", "hreflang", $lang);
- $link->setAttributeNS("", "href", "FIXME.".$lang);
+ $link->setAttributeNS("", "href", ($file_name//"").".".$lang);
$node->replaceNode($link);
}