source: josm/trunk/scripts/presets_lang.pl@ 14273

Last change on this file since 14273 was 13480, checked in by stoecker, 7 years ago

update wiki links

  • Property svn:eol-style set to native
  • 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 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-_]):.*?)".*?&#160;&#8226;&#160;/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}
73open FILE,">","data/defaultpresets.xml" or die;
74print FILE $file;
75close FILE;
Note: See TracBrowser for help on using the repository browser.