23 |
$doc->manakai_is_html (1); |
$doc->manakai_is_html (1); |
24 |
} |
} |
25 |
|
|
26 |
my $target_lang = 'en'; |
my $target_lang = shift || 'en'; |
27 |
|
|
28 |
my @node = (@{$doc->child_nodes}); |
my @node = (@{$doc->child_nodes}); |
29 |
|
my $title; |
30 |
|
my $title_parent; |
31 |
while (@node) { |
while (@node) { |
32 |
my $node = shift @node; |
my $node = shift @node; |
33 |
if ($node->node_type == $node->ELEMENT_NODE) { |
if ($node->node_type == $node->ELEMENT_NODE) { |
34 |
if ($node->namespace_uri eq $HTML_NS) { |
if ($node->namespace_uri eq $HTML_NS) { |
35 |
if ($node->manakai_local_name eq 'title') { |
if ($node->manakai_local_name eq 'title') { |
36 |
unless ($node->get_attribute_ns ($XML_NS, 'lang') eq $target_lang) { |
$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); |
$node->parent_node->remove_child ($node); |
42 |
} |
} |
43 |
} else { |
} else { |
44 |
unshift @node, @{$node->child_nodes}; |
unshift @node, @{$node->child_nodes}; |
45 |
} |
} |
46 |
} elsif ($node->namespace_uri eq $SRC_NS) { |
} elsif ($node->namespace_uri eq $SRC_NS) { |
47 |
if ($node->manakai_local_name eq 'item') { |
my $ln = $node->manakai_local_name; |
48 |
|
if ($ln eq 'item' or $ln eq 'cat') { |
49 |
my $message; |
my $message; |
50 |
my $desc; |
my $desc; |
51 |
|
my $text; |
52 |
for (@{$node->child_nodes}) { |
for (@{$node->child_nodes}) { |
53 |
if ($_->node_type == $_->ELEMENT_NODE and |
if ($_->node_type == $_->ELEMENT_NODE and |
54 |
$_->namespace_uri eq $SRC_NS) { |
$_->namespace_uri eq $SRC_NS) { |
66 |
} else { |
} else { |
67 |
$message ||= $_; |
$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 |
my $name = $node->get_attribute_ns (undef, 'name'); |
if ($ln eq 'item' or $desc) { |
81 |
$name =~ tr/ /-/; |
my $name = $node->get_attribute_ns (undef, 'name'); |
82 |
my $level = $node->get_attribute_ns (undef, 'level'); |
$name =~ tr/ /-/; |
83 |
$name = $level . ':' . $name if defined $level; |
|
84 |
my $section = $doc->create_element_ns ($HTML_NS, 'div'); |
my $section = $doc->create_element_ns ($HTML_NS, 'div'); |
85 |
$section->set_attribute_ns |
$section->set_attribute_ns (undef, class => 'section'); |
86 |
(undef, class => 'section ' . |
$section->set_attribute_ns (undef, id => $name); |
87 |
$node->get_attribute_ns (undef, 'class')); |
|
88 |
$section->set_attribute_ns (undef, id => $name); |
my $msg = $section->append_child |
89 |
|
($doc->create_element_ns ($HTML_NS, 'h3')); |
90 |
my @message_child = @{$message->child_nodes}; |
if ($ln eq 'item' and $message) { |
91 |
my $msg = $section->append_child |
my @message_child = @{$message->child_nodes}; |
92 |
($doc->create_element_ns ($HTML_NS, 'h3')); |
$msg->append_child ($_) for @message_child; |
93 |
$msg->append_child ($_) for @message_child; |
} elsif ($ln eq 'cat' and $text) { |
94 |
|
$msg->append_child ($_) for @{$text->child_nodes}; |
95 |
if ($desc) { |
} |
96 |
my @desc_child = @{$desc->child_nodes}; |
|
97 |
$section->append_child ($_) for @desc_child; |
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 |
} |
} |
|
|
|
|
$node->parent_node->insert_before ($section, $node); |
|
|
$node->parent_node->remove_child ($node); ## TODO: replace_child is not yet implemented |
|
|
} elsif ($node->manakai_local_name eq 'cat') { |
|
104 |
$node->parent_node->remove_child ($node); |
$node->parent_node->remove_child ($node); |
105 |
} else { |
} else { |
106 |
warn "$0: ", $node->manakai_local_name, " is not supported\n"; |
warn "$0: ", $node->manakai_local_name, " is not supported\n"; |
109 |
} |
} |
110 |
} |
} |
111 |
$doc->document_element->set_attribute_ns (undef, lang => $target_lang); |
$doc->document_element->set_attribute_ns (undef, lang => $target_lang); |
112 |
|
$title_parent->append_child ($title) if $title; |
113 |
|
|
114 |
print $doc->inner_html; |
print $doc->inner_html; |