1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
use encoding 'us-ascii', STDOUT => 'utf-8'; |
4 |
|
5 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
6 |
/home/wakaba/work/manakai2/lib]; |
7 |
|
8 |
my $HTML_NS = q<http://www.w3.org/1999/xhtml>; |
9 |
my $SRC_NS = q<http://suika.fam.cx/~wakaba/archive/2007/wdcc-desc/>; |
10 |
my $XML_NS = q<http://www.w3.org/XML/1998/namespace>; |
11 |
|
12 |
require Message::DOM::DOMImplementation; |
13 |
my $dom = Message::DOM::DOMImplementation->new; |
14 |
|
15 |
my $doc; |
16 |
{ |
17 |
my $source_file_name = shift or die "$0: No source file specified\n"; |
18 |
open my $source_file, '<', $source_file_name |
19 |
or die "$0: $source_file_name: $!"; |
20 |
require Message::DOM::XMLParserTemp; |
21 |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
22 |
($source_file => $dom, undef, charset => 'utf-8'); |
23 |
$doc->manakai_is_html (1); |
24 |
} |
25 |
|
26 |
my $target_lang = shift || 'en'; |
27 |
|
28 |
my @node = (@{$doc->child_nodes}); |
29 |
my $title; |
30 |
my $title_parent; |
31 |
while (@node) { |
32 |
my $node = shift @node; |
33 |
if ($node->node_type == $node->ELEMENT_NODE) { |
34 |
if ($node->namespace_uri eq $HTML_NS) { |
35 |
if ($node->manakai_local_name eq 'title') { |
36 |
$title_parent = $node->parent_node; |
37 |
if ($node->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
38 |
$title = $node; |
39 |
} else { |
40 |
$title ||= $node; |
41 |
$node->parent_node->remove_child ($node); |
42 |
} |
43 |
} else { |
44 |
unshift @node, @{$node->child_nodes}; |
45 |
} |
46 |
} elsif ($node->namespace_uri eq $SRC_NS) { |
47 |
my $ln = $node->manakai_local_name; |
48 |
if ($ln eq 'item' or $ln eq 'cat') { |
49 |
my $message; |
50 |
my $desc; |
51 |
my $text; |
52 |
for (@{$node->child_nodes}) { |
53 |
if ($_->node_type == $_->ELEMENT_NODE and |
54 |
$_->namespace_uri eq $SRC_NS) { |
55 |
if ($_->manakai_local_name eq 'desc') { |
56 |
if ($_->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
57 |
$desc = $_; |
58 |
next; |
59 |
} else { |
60 |
$desc ||= $_; |
61 |
} |
62 |
} elsif ($_->manakai_local_name eq 'message') { |
63 |
if ($_->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
64 |
$message = $_; |
65 |
next; |
66 |
} else { |
67 |
$message ||= $_; |
68 |
} |
69 |
} elsif ($_->manakai_local_name eq 'text') { |
70 |
if ($_->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
71 |
$text = $_; |
72 |
next; |
73 |
} else { |
74 |
$text ||= $_; |
75 |
} |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
if ($ln eq 'item' or $desc) { |
81 |
my $name = $node->get_attribute_ns (undef, 'name'); |
82 |
$name =~ tr/ /-/; |
83 |
|
84 |
my $section = $doc->create_element_ns ($HTML_NS, 'div'); |
85 |
$section->set_attribute_ns (undef, class => 'section'); |
86 |
$section->set_attribute_ns (undef, id => $name); |
87 |
|
88 |
my $msg = $section->append_child |
89 |
($doc->create_element_ns ($HTML_NS, 'h3')); |
90 |
if ($ln eq 'item' and $message) { |
91 |
my @message_child = @{$message->child_nodes}; |
92 |
$msg->append_child ($_) for @message_child; |
93 |
} elsif ($ln eq 'cat' and $text) { |
94 |
$msg->append_child ($_) for @{$text->child_nodes}; |
95 |
} |
96 |
|
97 |
if ($desc) { |
98 |
my @desc_child = @{$desc->child_nodes}; |
99 |
$section->append_child ($_) for @desc_child; |
100 |
} |
101 |
|
102 |
$node->parent_node->insert_before ($section, $node); |
103 |
} |
104 |
$node->parent_node->remove_child ($node); |
105 |
} else { |
106 |
warn "$0: ", $node->manakai_local_name, " is not supported\n"; |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
$doc->document_element->set_attribute_ns (undef, lang => $target_lang); |
112 |
$title_parent->append_child ($title) if $title; |
113 |
|
114 |
print $doc->inner_html; |