summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-04-06 01:52:04 (GMT)
committerDavid A. Madore <david+git@madore.org>2010-04-06 01:52:04 (GMT)
commitc3e737ab4574ba2569c7bcdc3c3e5fa7951c11ee (patch)
tree6c1677116e0232aa9d1f189fdbe4b46f3410265b
parentbd20d00b168c6b9fa06b9a2d156a2f674b4a6081 (diff)
downloadblogengine-c3e737ab4574ba2569c7bcdc3c3e5fa7951c11ee.zip
blogengine-c3e737ab4574ba2569c7bcdc3c3e5fa7951c11ee.tar.gz
blogengine-c3e737ab4574ba2569c7bcdc3c3e5fa7951c11ee.tar.bz2
Introduce a processing context as a recurring variable.
-rwxr-xr-xdaml2html.pl89
1 files changed, 62 insertions, 27 deletions
diff --git a/daml2html.pl b/daml2html.pl
index 67c0fc9..6822447 100755
--- a/daml2html.pl
+++ b/daml2html.pl
@@ -160,16 +160,21 @@ $xpc->registerNs('d', DAML_NS);
# undef, meaning handle a node as usual, otherwise it is a reference
# to a function that will be called with the rest of the array as
# arguments; the second argument ($node) is the node to examine; the
-# third ($optref) is a reference to a hash giving some extra context
-# on this node or why it is being examined now; the rest are optional
-# arguments to the handler function. The default behavior (when
-# $call_this is null) is to examine $node and, if a special handler is
-# registered for it, invoke that, otherwise invoke the default
-# handler: the latter simply lists all element children of the node
-# and shifts them at the beginning of @todo_stack.
+# third ($ctxref) is a reference to a hash giving some extra
+# information on the hereditary processing context, whereas the fourth
+# ($optref) is a reference to a hash giving some extra information on
+# the immediate processing context (such as why this particular node
+# is being examined now, or what parent it had); the difference
+# between these two contexts is that the default handler transmits
+# $ctxref unchanged whereas it passes a clean $optref; the rest are
+# optional arguments to the handler function. The default behavior
+# (when $call_this is null) is to examine $node and, if a special
+# handler is registered for it, invoke that, otherwise invoke the
+# default handler: the latter simply lists all element children of the
+# node and shifts them at the beginning of @todo_stack.
my @todo_stack;
-unshift @todo_stack, [undef, $doc->documentElement, {is_root=>1}];
+unshift @todo_stack, [undef, $doc->documentElement, {}, {is_root=>1}];
# Global variables for processing:
my $uri_to_top;
@@ -196,13 +201,14 @@ if ( open my $common_script_file, "<", "included.js" ) {
sub default_handler_nodeonly {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: default handler doesn't handle arguments\n" if @_;
# return unless $node->nodeType == XML_ELEMENT_NODE;
my @child_nodes = $node->childNodes;
my @to_process;
foreach my $child ( @child_nodes ) {
- push @to_process, [undef, $child, {}]
+ push @to_process, [undef, $child, $ctxref, {}]
if $child->nodeType == XML_ELEMENT_NODE;
}
unshift @todo_stack, @to_process;
@@ -210,12 +216,14 @@ sub default_handler_nodeonly {
sub default_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
default_handler_nodeonly $node, $optref;
}
sub do_title_or_subtitle_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
my $type = shift;
die "do_title_or_subtitle_handler called with type other than \"title\" or \"subtitle\""
@@ -239,25 +247,28 @@ sub do_title_or_subtitle_handler {
foreach my $child ( @child_nodes ) {
$new_t_node->appendChild($child->cloneNode(1));
}
- unshift @todo_stack, [undef, $new_t_node, {}];
+ unshift @todo_stack, [undef, $new_t_node, $ctxref, {}];
}
sub do_title_handler {
my $node = shift;
+ my $ctxref = 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;
+ do_title_or_subtitle_handler $node, $ctxref, $optref, "title", $src_title, $src_title_lang;
}
sub do_subtitle_handler {
my $node = shift;
+ my $ctxref = 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;
+ do_title_or_subtitle_handler $node, $ctxref, $optref, "subtitle", $src_subtitle, $src_subtitle_lang;
}
sub do_translations_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
my $parent = $node->parentNode;
foreach my $lang ( @translations ) {
@@ -301,6 +312,7 @@ sub do_translations_handler {
sub do_navbar_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: do-navbar handler doesn't handle arguments\n" if @_;
my $p = $doc->createElementNS(XHTML_NS, "p");
@@ -341,11 +353,12 @@ sub do_navbar_handler {
$a->setAttributeNS("", "href", ($uri_to_top//"")."weblog/");
$p->appendChild($a);
$a->appendChild($doc->createTextNode("WebLog"));
- unshift @todo_stack, [undef, $p, {}];
+ unshift @todo_stack, [undef, $p, $ctxref, {}];
}
sub do_footer_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: do-footer handler doesn't handle arguments\n" if @_;
my $parent = $node->parentNode;
@@ -374,8 +387,8 @@ sub do_footer_handler {
$node->nextSibling->unbindNode;
}
$node->unbindNode;
- unshift @todo_stack, [undef, $hr, {}]; # Pretty useless...
- unshift @todo_stack, [undef, $address, {}];
+ unshift @todo_stack, [undef, $hr, $ctxref, {}]; # Pretty useless...
+ unshift @todo_stack, [undef, $address, $ctxref, {}];
}
sub create_meta_element_helper {
@@ -398,6 +411,7 @@ sub create_meta_element_helper {
sub create_style_or_script_node {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
my $type = shift;
die "create_style_or_script_node called with type other than \"style\" or \"script\""
@@ -425,6 +439,7 @@ sub create_style_or_script_node {
sub daml_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: daml handler doesn't handle arguments\n" if @_;
die "daml element can only be root element" unless $$optref{is_root};
@@ -475,7 +490,7 @@ sub daml_handler {
$head_node->appendChild($child);
$head_node->appendChild($doc->createTextNode("\n"));
}
- push @to_process, [undef, $child, {is_daml_child=>1}];
+ push @to_process, [undef, $child, $ctxref, {is_daml_child=>1}];
} elsif ( $child->nodeType == XML_TEXT_NODE
|| $child->nodeType == XML_CDATA_SECTION_NODE ) {
die "daml element cannot contain text"
@@ -483,14 +498,17 @@ sub daml_handler {
}
}
unshift @todo_stack, @to_process;
- push @todo_stack, [\&create_style_or_script_node, $head_node, {special=>1},
+ push @todo_stack, [\&create_style_or_script_node, $head_node,
+ {special=>1}, {special=>1},
"style", \$style_content];
- push @todo_stack, [\&create_style_or_script_node, $head_node, {special=>1},
+ push @todo_stack, [\&create_style_or_script_node, $head_node,
+ {special=>1}, {special=>1},
"script", \$script_content];
}
sub body_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: body handler doesn't handle arguments\n" if @_;
my $body_node = $doc->createElementNS(XHTML_NS, "body");
@@ -539,7 +557,7 @@ sub body_handler {
unless $child->data =~ m/^\s*$/s;
}
$body_node->appendChild($child);
- push @to_process, [undef, $child, {}]
+ push @to_process, [undef, $child, $ctxref, {}]
if $child->nodeType == XML_ELEMENT_NODE;
}
unless ( $node->getAttributeNS("", "nofooter") ) {
@@ -553,6 +571,7 @@ sub body_handler {
sub document_title_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: title handler doesn't handle arguments\n" if @_;
my $title_text = $node->textContent;
@@ -580,6 +599,7 @@ sub document_title_handler {
sub subtitle_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: subtitle handler doesn't handle arguments\n" if @_;
$src_subtitle = $doc->createDocumentFragment;
@@ -596,6 +616,7 @@ sub subtitle_handler {
sub meta_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: meta handler doesn't handle arguments\n" if @_;
if ( $node->localName eq "meta-description" ) {
@@ -611,6 +632,7 @@ sub meta_handler {
sub translation_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: translation handler doesn't handle arguments\n" if @_;
my $lang = get_node_lang_norec $node;
@@ -631,6 +653,7 @@ sub translation_handler {
sub extra_style_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: extra-style handler doesn't handle arguments\n" if @_;
$style_content .= $node->textContent;
@@ -643,6 +666,7 @@ sub extra_style_handler {
sub email_at_or_dot_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
my $type = shift;
die "email_at_or_dot_handler called with type other than \"at\" or \"dot\""
@@ -686,20 +710,23 @@ sub email_at_or_dot_handler {
sub email_at_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: email-at handler doesn't handle arguments\n" if @_;
- email_at_or_dot_handler $node, $optref, "at";
+ email_at_or_dot_handler $node, $ctxref, $optref, "at";
}
sub email_dot_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: email-dot handler doesn't handle arguments\n" if @_;
- email_at_or_dot_handler $node, $optref, "dot";
+ email_at_or_dot_handler $node, $ctxref, $optref, "dot";
}
sub email_despammed_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: email-despammed handler doesn't handle arguments\n" if @_;
my $out0 = $doc->createElementNS(XHTML_NS, "code");
@@ -714,7 +741,7 @@ sub email_despammed_handler {
my @to_process;
foreach my $child ( @child_nodes ) {
$out1->appendChild($child);
- push @to_process, [undef, $child, {is_email_despammed_child=>1}]
+ push @to_process, [undef, $child, $ctxref, {is_email_despammed_child=>1}]
if $child->nodeType == XML_ELEMENT_NODE;
}
unshift @todo_stack, @to_process;
@@ -722,6 +749,7 @@ sub email_despammed_handler {
sub smiley_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: smiley handler doesn't handle arguments\n" if @_;
my ($emotion) = $node->localName =~ /^smiley-([a-z0-9\-]*)$/s
@@ -766,6 +794,7 @@ sub smiley_handler {
sub entry_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: entry handler doesn't handle arguments\n" if @_;
my $entry_number = $node->getAttributeNS("", "number");
@@ -779,6 +808,10 @@ sub entry_handler {
&& ( ($entry_year,$entry_month,$entry_day)
= ( $entry_date =~ /^(\d{4})\-(\d{2})\-(\d{2})$/s ) ) );
my $entry_dow = $node->getAttributeNS("", "day_of_week");
+ my %newctx = (%{$ctxref}, in_entry=>1, entry_number=>$entry_number,
+ entry_date=>$entry_date, entry_year=>$entry_year,
+ entry_month=>$entry_month, entry_day=>$entry_day,
+ entry_dow=>$entry_dow);
my $lang = get_node_lang_rec $node;
my $div = $doc->createElementNS(XHTML_NS, "div");
if ( defined(my $explicit_lang = get_node_lang_norec $node) ) {
@@ -802,13 +835,13 @@ sub entry_handler {
$header->appendChild($permalink);
$header->appendChild($doc->createTextNode(" (".$entry_dow.")"))
if defined($entry_dow);
+ $newctx{entry_header} = $header;
my @child_nodes = $node->childNodes;
my @to_process;
foreach my $child ( @child_nodes ) {
if ( $child->nodeType == XML_ELEMENT_NODE ) {
$div->appendChild($child);
- push @to_process, [undef, $child, {is_entry_child=>1,
- entry_header=>$header}];
+ push @to_process, [undef, $child, \%newctx, {is_entry_child=>1}];
} elsif ( $child->nodeType == XML_TEXT_NODE
|| $child->nodeType == XML_CDATA_SECTION_NODE ) {
die "entry element cannot contain text"
@@ -822,6 +855,7 @@ sub entry_handler {
sub entry_title_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: title handler doesn't handle arguments\n" if @_;
my $h2 = $doc->createElementNS(XHTML_NS, "h2");
@@ -834,7 +868,7 @@ sub entry_title_handler {
my @to_process;
foreach my $child ( @child_nodes ) {
$h2->appendChild($child);
- push @to_process, [undef, $child, {}]
+ push @to_process, [undef, $child, $ctxref, {}]
if $child->nodeType == XML_ELEMENT_NODE;
}
unshift @todo_stack, @to_process;
@@ -842,12 +876,13 @@ sub entry_title_handler {
sub title_handler {
my $node = shift;
+ my $ctxref = shift;
my $optref = shift;
print STDERR "warning: title handler doesn't handle arguments\n" if @_;
if ( $$optref{is_daml_child} ) {
- document_title_handler $node, $optref;
+ document_title_handler $node, $ctxref, $optref;
} elsif ( $$optref{is_entry_child} ) {
- entry_title_handler $node, $optref;
+ entry_title_handler $node, $ctxref, $optref;
}
}