source: josm/trunk/presets_lang.pl@ 6556

Last change on this file since 6556 was 6337, checked in by stoecker, 11 years ago

fix #9229 - proper coding of chines links

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1#! /usr/bin/perl -w
2# short tool to update language links in presets file
3
4use XML::Parser;
5use Data::Dumper;
6use URI::Escape;
7
8my $skip = 0;
9my $xml = new XML::Parser(Handlers => {Start => \&handle_start});
10undef $/;
11open FILE,"<","data/defaultpresets.xml" or die;
12my $file = <FILE>;
13close FILE;
14$xml->parsefile('data/defaultpresets.xml');
15
16sub 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 if($h eq "http://wiki.openstreetmap.org/wiki/Proposed_features/House_numbers/Karlsruhe_Schema")
40 {
41 $datan{"de"} = "/wiki/Proposed_features/De:Hausnummern";
42 }
43 while($main =~ /<span lang="([a-z_A-Z-]+)"(?: style="unicode-bidi:embed")?><a href="(\/wiki\/.*?)"/g)
44 {
45 my $lang = lc($1);
46 my $val = uri_unescape($2);
47 $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
48 $lang = "zh_CN" if $lang eq "zh-hans";
49 $lang = "zh_TW" if $lang eq "zh-hant";
50 $datan{$lang} = $val;
51 }
52 while($main =~ /<a href="(\/wiki\/([a-zA-Z-_]):.*?)".*?&#160;&#8226;&#160;/g)
53 {
54 my $lang = lc($1);
55 my $val = uri_unescape($2);
56 $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
57 $datan{$lang} = $val;
58 }
59 foreach my $l (sort keys %datan)
60 {
61 $v .= "\n" . (" " x 18) . "$l.href=\"http://wiki.openstreetmap.org$datan{$l}\"";
62 }
63 print "$v\n";
64 print "Replace failed for $data{href}.\n" if !($file =~ s/(<link )href="\Q$data{href}\E.*?( ?\/>)/$1$v$2/s);
65
66 $skip++;
67 }
68 }
69}
70open FILE,">","data/defaultpresets.xml" or die;
71print FILE $file;
72close FILE;
Note: See TracBrowser for help on using the repository browser.