summaryrefslogtreecommitdiffstats
path: root/daml2html.pl
blob: 3ec1db659fb9470a15e2ccb1bb75a58cb7ffb57e (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#! /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 $in_doc;
if ( defined($ARGV[0]) ) {
    $in_doc = $parser->parse_file($ARGV[0]);
} else {
    $in_doc = $parser->parse_fh(\*STDIN);
}

die "Root element should be daml"
    unless ( $in_doc->documentElement->namespaceURI eq DAML_NS
	     && $in_doc->documentElement->localname eq "daml" );

my $xpc = XML::LibXML::XPathContext->new($in_doc);
$xpc->registerNs('h', XHTML_NS);
$xpc->registerNs('d', DAML_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("/d:daml/d: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"));
my $out_head = $out_doc->createElementNS(XHTML_NS, "head");
$out_html->appendChild($out_head);
$out_head->appendChild($out_doc->createTextNode("\n"));
my $out_title = $out_doc->createElementNS(XHTML_NS, "title");
$out_head->appendChild($out_title);
$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 $in_title = ($xpc->findnodes("/d:daml/d:title"))[0];
if ( defined($in_title) ) {
    $out_title->appendChild($out_doc->createTextNode($in_title->textContent));
    my $out_meta_title = $out_doc->createElementNS(XHTML_NS, "meta");
    $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));
    }
}
my $in_subtitle = ($xpc->findnodes("/d:daml/d:subtitle"))[0];
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));
    }
}
my $in_meta_description = ($xpc->findnodes("/d:daml/d:meta-description"))[0];
if ( defined($in_meta_description) ) {
    my $out_meta_description = $out_doc->createElementNS(XHTML_NS, "meta");
    $out_meta_description->setAttributeNS("", "name", "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];
if ( defined($in_meta_keywords) ) {
    my $out_meta_keywords = $out_doc->createElementNS(XHTML_NS, "meta");
    $out_meta_keywords->setAttributeNS("", "name", "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 $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("/d:daml/d: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("/d:daml/*[self::h:meta or self::h: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_body->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"));
}

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_p = $out_doc->createElementNS(XHTML_NS, "p");
    $out_p->setAttributeNS("", "class", "translation-offer");
    $out_p->setAttributeNS(XML_XML_NS, "lang", $lang);
    if ( $lang eq "fr" ) {
	$out_p->appendChild($out_doc->createTextNode("[Une "));
    } elsif ( $lang eq "en" ) {
	$out_p->appendChild($out_doc->createTextNode("[An "));
    } else {
	$out_p->appendChild($out_doc->createTextNode("["));
    }
    my $out_a = $out_doc->createElementNS(XHTML_NS, "a");
    $out_a->setAttributeNS("", "href", $file_name.".".$lang);
    $out_a->setAttributeNS("", "hreflang", $lang);
    if ( $lang eq "fr" ) {
	$out_a->appendChild($out_doc->createTextNode("version française"));
    } elsif ( $lang eq "en" ) {
	$out_a->appendChild($out_doc->createTextNode("English version"));
    } else {
	$out_a->appendChild($out_doc->createTextNode($lang));
    }
    $out_p->appendChild($out_a);
    if ( $lang eq "fr" ) {
	$out_p->appendChild($out_doc->createTextNode(" de cette page est également disponible.]"));
    } elsif ( $lang eq "en" ) {
	$out_p->appendChild($out_doc->createTextNode(" of this page is also available.]"));
    } else {
	$out_p->appendChild($out_doc->createTextNode("]"));
    }
    $out_body->appendChild($out_p);
    $out_body->appendChild($out_doc->createTextNode("\n"));
}

if ( defined($in_body) ) {
    foreach my $child ( $in_body->childNodes ) {
	$out_doc->adoptNode($child);
	$out_body->appendChild($child);
    }
}

{
    my $out_hr = $out_doc->createElementNS(XHTML_NS, "hr");
    $out_hr->setAttributeNS("", "class", "cleared");
    $out_body->appendChild($out_hr);
    $out_body->appendChild($out_doc->createTextNode("\n"));
    my $out_address = $out_doc->createElementNS(XHTML_NS, "address");
    my $out_a = $out_doc->createElementNS(XHTML_NS, "a");
    $out_a->setAttributeNS("", "href", $uri_to_top);
    $out_a->appendChild($out_doc->createTextNode("David Madore"));
    $out_address->appendChild($out_a);
    $out_address->appendChild($out_doc->createTextNode(" ("));
    # XXX - email address here
    $out_address->appendChild($out_doc->createTextNode(")"));
    $out_body->appendChild($out_address);
    $out_body->appendChild($out_doc->createTextNode("\n"));
}

my $in_last_modified = ($xpc->findnodes("/d:daml/d:last-modified"))[0];
if ( defined($in_last_modified) ) {
    my $out_last_modified = $out_doc->createElementNS(XHTML_NS, "p");
    $out_last_modified->setAttributeNS("", "class", "last-modified");
    $out_body->appendChild($out_last_modified);
    my $lang = rec_lang $in_last_modified;
    my $str = $in_last_modified->textContent;
    if ( defined($lang) && $lang eq "fr" ) {
	$str = "Dernière modification : " . $str;
    } elsif ( defined($lang) && $lang eq "en" ) {
	$str = "Last modified: " . $str;
    }
    $out_last_modified->appendChild($out_doc->createTextNode($str));
    set_rec_lang($out_last_modified, $lang);
    $out_body->appendChild($out_doc->createTextNode("\n"));
}

my $pure_style_content = "";
if ( open my $common_style_file, "<", "included.css" ) {
    local $/;
    $pure_style_content = <$common_style_file>;
    close $common_style_file;
}
my $pure_script_content = "";
if ( open my $common_script_file, "<", "included.js" ) {
    local $/;
    $pure_script_content = <$common_script_file>;
    close $common_script_file;
}

my @in_extra_style = $xpc->findnodes("/d:daml/d: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"));
$out_style_section->setAttributeNS("", "type", "text/css");
$out_style_section->appendChild($out_doc->createTextNode("\n/* "));
$out_style_section->appendChild($out_doc->createCDATASection(" */\n".$pure_style_content."/* "));
$out_style_section->appendChild($out_doc->createTextNode(" */\n"));
my $out_script_section = $out_doc->createElementNS(XHTML_NS, "script");
$out_head->appendChild($out_script_section);
$out_head->appendChild($out_doc->createTextNode("\n"));
$out_script_section->setAttributeNS("", "type", "text/javascript");
$out_script_section->setAttributeNS("", "defer", "defer");
$out_script_section->appendChild($out_doc->createTextNode("\n// "));
$out_script_section->appendChild($out_doc->createCDATASection("\n".$pure_script_content."// "));
$out_script_section->appendChild($out_doc->createTextNode("\n"));

# sub map_children {
#     my $fun = shift;
#     my $node = shift;
#     my $cref = shift;
#     for ( my $child=$node->firstChild ; $child ; $child=$child->nextSibling ) {
# 	&$fun($child, $cref) if $child->nodeType == XML_ELEMENT_NODE;
#     }
# }

# sub handle_body {
#     my $node = shift;
#     my $cref = shift;
#     # Extract children list first, because they will be removed!
#     my @child_nodes = $node->childNodes();
#     foreach my $child ( @child_nodes ) {
# 	$out_doc->adoptNode($child);
# 	$out_body->appendChild($child);
#     }
#     $cref = {%$cref, in_body=>1};
#     foreach my $child ( @child_nodes ) {
# 	apply_templates($child, $cref) if $child->nodeType == XML_ELEMENT_NODE;
#     }
# }

# sub apply_templates {
#     my $node = shift;
#     my $cref = shift;
#     my $node_name = $node->localname;
#     if ( $node_name eq "body" ) {
# 	handle_body $node, $cref;
# 	return;
#     }
#     map_children \&apply_templates, $node, $cref;
# }

# my %context = ();

# apply_templates $in_doc->documentElement, \%context;

print $out_doc->toString;