| 1 |
wakaba |
1.1 |
#!/usr/bin/perl |
| 2 |
|
|
use strict; |
| 3 |
|
|
|
| 4 |
|
|
use Getopt::Long; |
| 5 |
|
|
use Pod::Usage; |
| 6 |
|
|
|
| 7 |
|
|
my $lang; |
| 8 |
|
|
|
| 9 |
|
|
GetOptions ( |
| 10 |
|
|
'lang=s' => \$lang, |
| 11 |
|
|
'help' => sub { |
| 12 |
|
|
pod2usage (-exitval => 0, -verbose => 2); |
| 13 |
|
|
}, |
| 14 |
|
|
) or pod2usage (-exitval => 1, -verbose => 1); |
| 15 |
|
|
|
| 16 |
|
|
pod2usage (-exitval => 1, -verbose => 1, |
| 17 |
|
|
-msg => "Required argument --lang is not specified.\n") |
| 18 |
|
|
unless defined $lang; |
| 19 |
|
|
$lang =~ tr/A-Z/a-z/; ## ASCII case-insensitive. |
| 20 |
|
|
|
| 21 |
|
|
use Message::DOM::DOMImplementation; |
| 22 |
|
|
my $dom = Message::DOM::DOMImplementation->new; |
| 23 |
|
|
|
| 24 |
|
|
my $doc = $dom->create_document; |
| 25 |
|
|
$doc->manakai_is_html (1); |
| 26 |
|
|
|
| 27 |
|
|
{ |
| 28 |
|
|
binmode STDIN, ':encoding(utf8)'; |
| 29 |
|
|
local $/ = undef; |
| 30 |
|
|
$doc->inner_html (scalar <STDIN>); |
| 31 |
|
|
} |
| 32 |
|
|
|
| 33 |
|
|
my @remove; |
| 34 |
|
|
|
| 35 |
|
|
my $containers = $doc->query_selector_all |
| 36 |
|
|
('[data-lang-container], [data-lang-content]'); |
| 37 |
|
|
for my $el (@$containers) { |
| 38 |
|
|
next unless $el->parent_node; |
| 39 |
|
|
|
| 40 |
|
|
if ($el->has_attribute ('data-lang-container')) { |
| 41 |
|
|
my $content = get_content_element ($el); |
| 42 |
|
|
next unless $content; |
| 43 |
|
|
if ($el->get_attribute ('data-lang-container') eq 'replace') { |
| 44 |
|
|
$el->parent_node->replace_child ($content, $el); |
| 45 |
|
|
} else { |
| 46 |
|
|
$el->text_content (''); |
| 47 |
|
|
$el->append_child ($content); |
| 48 |
|
|
} |
| 49 |
|
|
} elsif ($el->has_attribute ('data-lang-content')) { |
| 50 |
|
|
my $idref = $el->get_attribute ('data-lang-content'); |
| 51 |
|
|
my $container = $doc->get_element_by_id ($idref); |
| 52 |
|
|
unless ($container) { |
| 53 |
|
|
warn "Element with ID $idref not found\n"; |
| 54 |
|
|
next; |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
my $content = get_content_element ($container); |
| 58 |
|
|
$el->text_content ($content->text_content); |
| 59 |
|
|
$el->manakai_html_language ($content->manakai_html_language); |
| 60 |
|
|
|
| 61 |
|
|
if ($container->has_attribute ('data-lang-declaration')) { |
| 62 |
|
|
push @remove, $container; |
| 63 |
|
|
} |
| 64 |
|
|
} |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
for (@remove) { |
| 68 |
|
|
next unless $_->parent_node; |
| 69 |
|
|
$_->parent_node->remove_child ($_); |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
$doc->document_element->manakai_html_language ($lang); |
| 73 |
|
|
|
| 74 |
|
|
binmode STDOUT, ':encoding(utf8)'; |
| 75 |
|
|
print STDOUT $doc->inner_html; |
| 76 |
|
|
|
| 77 |
|
|
sub get_content_element ($) { |
| 78 |
|
|
my $container = shift; |
| 79 |
|
|
|
| 80 |
|
|
my $c_el; |
| 81 |
|
|
for my $e (@{$container->child_nodes}) { |
| 82 |
|
|
next unless $e->node_type == $e->ELEMENT_NODE; |
| 83 |
|
|
my $e_lang = $e->manakai_html_language; |
| 84 |
|
|
$e_lang =~ tr/A-Z/a-z/; ## ASCII case-insensitive. |
| 85 |
|
|
if ($e_lang eq $lang) { |
| 86 |
|
|
$c_el = $e; |
| 87 |
|
|
last; |
| 88 |
|
|
} else { |
| 89 |
|
|
$c_el ||= $e; |
| 90 |
|
|
} |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
return $c_el; |
| 94 |
|
|
} # get_content_element |
| 95 |
|
|
|
| 96 |
|
|
## $Date:$ |