1 | #! /usr/bin/perl -w
|
---|
2 | # short tool to update language links in presets file
|
---|
3 |
|
---|
4 | use XML::Parser;
|
---|
5 | use Data::Dumper;
|
---|
6 | use URI::Escape;
|
---|
7 |
|
---|
8 | my $skip = 0;
|
---|
9 | my $xml = new XML::Parser(Handlers => {Start => \&handle_start});
|
---|
10 | undef $/;
|
---|
11 | open FILE,"<","data/defaultpresets.xml" or die;
|
---|
12 | my $file = <FILE>;
|
---|
13 | close FILE;
|
---|
14 | $xml->parsefile('data/defaultpresets.xml');
|
---|
15 |
|
---|
16 | sub handle_start
|
---|
17 | {
|
---|
18 | my ($expat, $element, %data) = @_;
|
---|
19 |
|
---|
20 | if($element eq "link" && $skip < 50000)
|
---|
21 | {
|
---|
22 | my %datan;
|
---|
23 | foreach my $k (sort {$az=$a;$bz=$b;$az=~s/.?href//;;$bz=~s/.?href//;$az cmp $bz} keys %data)
|
---|
24 | {
|
---|
25 | my $z = $k;
|
---|
26 | $z =~ s/.?href//;
|
---|
27 | #printf("%-5s %s\n", $z,$data{$k});
|
---|
28 | }
|
---|
29 |
|
---|
30 | if(!$data{href})
|
---|
31 | {
|
---|
32 | print "Missing href: %s\n", join(" ", %data);
|
---|
33 | }
|
---|
34 | else
|
---|
35 | {
|
---|
36 | my $main = `wget "$data{href}" -O - -q`;
|
---|
37 | my $h = uri_unescape($data{href});
|
---|
38 | my $v = "href=\"$h\"";
|
---|
39 | while($main =~ /<a href="(\/wiki\/[^"]+)"[^>]+><b>·<\/b> <bdi lang="([a-z_A-Z-]+)"(?: style="unicode-bidi:embed;unicode-bidi:-webkit-isolate;unicode-bidi:isolate")?>/g)
|
---|
40 | {
|
---|
41 | my $lang = lc($2);
|
---|
42 | my $val = uri_unescape($1);
|
---|
43 | $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
|
---|
44 | $lang = "zh_CN" if $lang eq "zh-hans";
|
---|
45 | $lang = "zh_TW" if $lang eq "zh-hant";
|
---|
46 | if(-f "data/$lang.lang")
|
---|
47 | {
|
---|
48 | $datan{$lang} = $val;
|
---|
49 | }
|
---|
50 | else
|
---|
51 | {
|
---|
52 | print "Skip lang $lang\n";
|
---|
53 | }
|
---|
54 | }
|
---|
55 | while($main =~ /<a href="(\/wiki\/([a-zA-Z-_]):.*?)".*? • /g)
|
---|
56 | {
|
---|
57 | my $lang = lc($1);
|
---|
58 | my $val = uri_unescape($2);
|
---|
59 | $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
|
---|
60 | $datan{$lang} = $val;
|
---|
61 | }
|
---|
62 | foreach my $l (sort keys %datan)
|
---|
63 | {
|
---|
64 | $v .= "\n" . (" " x 18) . "$l.href=\"https://wiki.openstreetmap.org$datan{$l}\"";
|
---|
65 | }
|
---|
66 | print "$v\n";
|
---|
67 | print "Replace failed for $data{href}.\n" if !($file =~ s/(<link )href="\Q$data{href}\E".*?( ?\/>)/$1$v$2/s);
|
---|
68 |
|
---|
69 | $skip++;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | open FILE,">","data/defaultpresets.xml" or die;
|
---|
74 | print FILE $file;
|
---|
75 | close FILE;
|
---|