summaryrefslogtreecommitdiffstats
path: root/daml2html.pl
blob: cf98ed58649e77afc5b3073130d8d09de30f5c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#! /usr/local/bin/perl -w
# -*- coding: utf-8 -*-

require 5.10.0;

use strict;
use warnings;
use utf8;

use Encode;

use XML::LibXML qw(:libxml :ns);

use DBI;

use Digest::SHA1 qw(sha1_hex);

use Getopt::Std;

use constant {
    XHTML_PUBID => "-//W3C//DTD XHTML 1.0 Strict//EN",
    XHTML_URI => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
    XHTML_NS => "http://www.w3.org/1999/xhtml",
    DAML_NS => "http://www.madore.org/~david/NS/daml/",
};

my %opts;

getopts('cd:', \%opts);

my $dbname = "weblog" // $opts{d};  # The PostgreSQL database to use
my $obtain_cdates = $opts{c};

my $parser = XML::LibXML->new();


# Functions to take care of the x-daml-magic URI scheme (this should go away)

sub match_daml_magic_uri {
    my $uri = shift;
    return $uri =~ /^x-daml-magic\:/;
}

my %daml_magic_values = (
    "x-daml-magic://daml/weblog/domaxentries.xml" => "INCLUDE",
    "x-daml-magic://daml/weblog/maxentries.xml" => "20",
    "x-daml-magic://daml/weblog/domonth.xml" => "IGNORE",
    "x-daml-magic://daml/weblog/month.xml" => "",
    "x-daml-magic://daml/weblog/docategory.xml" => "IGNORE",
    "x-daml-magic://daml/weblog/category-en.xml" => "",
    "x-daml-magic://daml/weblog/category-fr.xml" => "",
    );

sub open_daml_magic_uri {
    my $uri = shift;
    my $s = $daml_magic_values{$uri};
    if ( defined($s) ) {
	return \$s;
    } else {
	return undef;
    }
}

sub read_daml_magic_uri {
    my $handler = shift;
    my $length = shift;
    my $buf = substr($$handler, 0, $length, "");
    return $buf;
}

sub close_daml_magic_uri {
    my $handler = shift;
    return "0 but true";
}

my $input_callbacks = XML::LibXML::InputCallback->new();
$input_callbacks->register_callbacks([ \&match_daml_magic_uri,
				       \&open_daml_magic_uri,
				       \&read_daml_magic_uri,
				       \&close_daml_magic_uri ]);
$parser->input_callbacks($input_callbacks);

# Avoid accessing the W3C web site all the time.
$parser->load_catalog("/etc/xml/catalog");

my $doc;
if ( defined($ARGV[0]) ) {
    $doc = $parser->parse_file($ARGV[0]);
} else {
    $doc = $parser->parse_fh(\*STDIN);
}

$doc->setEncoding("utf-8");
$doc->removeExternalSubset();
$doc->removeInternalSubset();
$doc->createInternalSubset("html", XHTML_PUBID, XHTML_URI);

my $xpc = XML::LibXML::XPathContext->new($doc);
$xpc->registerNs('h', XHTML_NS);
$xpc->registerNs('d', DAML_NS);

my @node_stack;

unshift @node_stack, [$doc->documentElement, {is_root=>1}];

my $html_node;
my $head_node;
my $title_node;

sub default_handler_nodeonly {
    my $node = 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, [$child, {}] if $child->nodeType == XML_ELEMENT_NODE;
    }
    unshift @node_stack, @to_process;
}

sub default_handler {
    my $node = shift;
    my $optref = shift;
    default_handler_nodeonly $node, $optref;
}

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 $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);
    }
    $meta->setAttributeNS("", $name_or_http_equiv, $meta_name);
    $meta->setAttributeNS("", "content", $meta_content);
    if ( defined($src_node) ) {
	($doc->documentElement)->appendChild($meta);  # Work around libxml2 bug <URL: https://bugzilla.gnome.org/show_bug.cgi?id=614068 >
	$src_node->replaceNode($meta);
    } else {
	die "\$head_node should have been defined at this point" unless defined($head_node);
	$head_node->appendChild($meta);
	$head_node->appendChild($doc->createTextNode("\n"));
    }
}

sub daml_handler {
    my $node = shift;
    my $optref = shift;
    print STDERR "warning: title 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");
    my $lang;
    if ( $lang = $node->getAttributeNS(XML_XML_NS, "lang") ) {
	$html_node->setAttributeNS(XML_XML_NS, "lang", $lang);
    }
    $doc->setDocumentElement($html_node);
    $html_node->appendChild($doc->createTextNode("\n"));
    $html_node->appendChild($doc->createComment(" This file is automatically generated.  Do not edit! "));
    $html_node->appendChild($doc->createTextNode("\n"));
    die "\$head_node already defined: what magic is this?" if defined($head_node);
    $head_node = $doc->createElementNS(XHTML_NS, "head");
    $html_node->appendChild($head_node);
    $html_node->appendChild($doc->createTextNode("\n"));
    $head_node->appendChild($doc->createTextNode("\n"));
    create_meta_element_helper(undef, "http-equiv", "Content-Type", "text/html; charset=utf-8");
    create_meta_element_helper(undef, "http-equiv", "Content-Language", $lang) if $lang;
    my @child_nodes = $node->childNodes;
    my @to_process;
    foreach my $child ( @child_nodes ) {
	if ( $child->nodeType == XML_ELEMENT_NODE ) {
	    if ( $child->localName eq "body" ) {
		$html_node->appendChild($doc->createTextNode("\n"));
		$html_node->appendChild($child);
		$html_node->appendChild($doc->createTextNode("\n"));
	    } else {
		$head_node->appendChild($child);
		$head_node->appendChild($doc->createTextNode("\n"));
	    }
	    push @to_process, [$child, {is_daml_child=>1}];
	} elsif ( $child->nodeType == XML_TEXT_NODE
		  || $child->nodeType == XML_CDATA_SECTION_NODE ) {
	    die "daml element cannot contain text"
		unless $child->data =~ m/^\s*$/s;
	}
    }
    unshift @node_stack, @to_process;
}

sub body_handler {
    my $node = shift;
    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);
    }
    ($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;
    foreach my $child ( @child_nodes ) {
	if ( $child->nodeType == XML_TEXT_NODE
	     || $child->nodeType == XML_CDATA_SECTION_NODE ) {
	    die "body element cannot contain text"
		unless $child->data =~ m/^\s*$/s;
	}
	$body_node->appendChild($child);
	push @to_process, [$child, {}] if $child->nodeType == XML_ELEMENT_NODE;
    }
    unshift @node_stack, @to_process;
}

sub title_handler {
    my $node = shift;
    my $optref = shift;
    print STDERR "warning: title handler doesn't handle arguments\n" if @_;
    if ( $$optref{is_daml_child} ) {
	$title_node = $node;
	create_meta_element_helper $node, "name", "Title", $title_node->textContent;
    }
}

sub meta_handler {
    my $node = shift;
    my $optref = shift;
    print STDERR "warning: meta handler doesn't handle arguments\n" if @_;
    if ( $node->localName eq "meta-description" ) {
	create_meta_element_helper $node, "name", "Description", $node->textContent;
    } elsif ( $node->localName eq "meta-keywords" ) {
	create_meta_element_helper $node, "name", "Keywords", $node->textContent;
    }
}

my %daml_handler = (
    "daml" => \&daml_handler,
    "body" => \&body_handler,
    "title" => \&title_handler,
    "meta-description" => \&meta_handler,
    "meta-keywords" => \&meta_handler,
    );

NODELOOP:
while ( my $process = shift @node_stack ) {
    my $node = $$process[0];
    unless ( defined($node->namespaceURI) ) {
	printf STDERR "warning: skipping %s node with missing namespace\n", $node->nodeName;
	next NODELOOP;
    }
    if ( $node->namespaceURI eq DAML_NS && defined($daml_handler{$node->localName}) ) {
	&{$daml_handler{$node->localName}}(@$process);
    } else {
	default_handler(@$process);
    }
}

print $doc->toString;