1 |
wakaba |
1.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/manakai/lib |
7 |
|
|
/home/wakaba/public_html/-temp/wiki/lib]; |
8 |
|
|
|
9 |
|
|
my $HTML_NS = q<http://www.w3.org/1999/xhtml>; |
10 |
|
|
my $SRC_NS = q<http://suika.fam.cx/~wakaba/archive/2007/wdcc-desc/>; |
11 |
|
|
my $XML_NS = q<http://www.w3.org/XML/1998/namespace>; |
12 |
|
|
|
13 |
|
|
require Message::DOM::DOMImplementation; |
14 |
|
|
my $dom = Message::DOM::DOMImplementation->____new; |
15 |
|
|
|
16 |
|
|
my $doc; |
17 |
|
|
{ |
18 |
|
|
my $source_file_name = shift or die "$0: No source file specified\n"; |
19 |
|
|
open my $source_file, '<', $source_file_name |
20 |
|
|
or die "$0: $source_file_name: $!"; |
21 |
|
|
require Message::DOM::XMLParserTemp; |
22 |
|
|
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
23 |
|
|
($source_file => $dom, undef, charset => 'utf-8'); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
my $target_lang = 'en'; |
27 |
|
|
my @node = (@{$doc->child_nodes}); |
28 |
|
|
while (@node) { |
29 |
|
|
my $node = shift @node; |
30 |
|
|
if ($node->node_type == $node->ELEMENT_NODE) { |
31 |
|
|
if ($node->namespace_uri eq $HTML_NS) { |
32 |
|
|
if ($node->manakai_local_name eq 'title') { |
33 |
|
|
unless ($node->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
34 |
|
|
$node->parent_node->remove_child ($node); |
35 |
|
|
} |
36 |
|
|
} else { |
37 |
|
|
push @node, @{$node->child_nodes}; |
38 |
|
|
} |
39 |
|
|
} elsif ($node->namespace_uri eq $SRC_NS) { |
40 |
|
|
if ($node->manakai_local_name eq 'item') { |
41 |
|
|
my $message; |
42 |
|
|
my $desc; |
43 |
|
|
for (@{$node->child_nodes}) { |
44 |
|
|
if ($_->node_type == $_->ELEMENT_NODE and |
45 |
|
|
$_->namespace_uri eq $SRC_NS) { |
46 |
|
|
if ($_->manakai_local_name eq 'desc') { |
47 |
|
|
if ($_->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
48 |
|
|
$desc = $_; |
49 |
|
|
next; |
50 |
|
|
} else { |
51 |
|
|
$desc ||= $_; |
52 |
|
|
} |
53 |
|
|
} elsif ($_->manakai_local_name eq 'message') { |
54 |
|
|
if ($_->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
55 |
|
|
$message = $_; |
56 |
|
|
next; |
57 |
|
|
} else { |
58 |
|
|
$message ||= $_; |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
my $name = $node->get_attribute_ns (undef, 'name'); |
65 |
|
|
$name =~ tr/ /-/; |
66 |
|
|
my $section = $doc->create_element_ns ($HTML_NS, 'div'); |
67 |
|
|
$section->set_attribute_ns |
68 |
|
|
(undef, class => 'section ' . |
69 |
|
|
$node->get_attribute_ns (undef, 'class')); |
70 |
|
|
$section->set_attribute_ns (undef, id => $name); |
71 |
|
|
|
72 |
|
|
my @message_child = @{$message->child_nodes}; |
73 |
|
|
my $msg = $section->append_child |
74 |
|
|
($doc->create_element_ns ($HTML_NS, 'h3')); |
75 |
|
|
$msg->append_child ($_) for @message_child; |
76 |
|
|
|
77 |
|
|
my @desc_child = @{$desc->child_nodes}; |
78 |
|
|
$section->append_child ($_) for @desc_child; |
79 |
|
|
|
80 |
|
|
$node->parent_node->insert_before ($section, $node); |
81 |
|
|
$node->parent_node->remove_child ($node); ## TODO: replace_child is not yet implemented |
82 |
|
|
} else { |
83 |
|
|
warn "$0: ", $node->manakai_local_name, " is not supported\n"; |
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
$doc->document_element->set_attribute_ns (undef, lang => $target_lang); |
89 |
|
|
|
90 |
|
|
require Whatpm::HTML; |
91 |
|
|
print ${ Whatpm::HTML->get_inner_html ($doc) }; |