summaryrefslogtreecommitdiff
path: root/tools/langtool.pl
diff options
context:
space:
mode:
Diffstat (limited to 'tools/langtool.pl')
-rwxr-xr-xtools/langtool.pl169
1 files changed, 97 insertions, 72 deletions
diff --git a/tools/langtool.pl b/tools/langtool.pl
index 79fedf5a0e..fe0c25bd39 100755
--- a/tools/langtool.pl
+++ b/tools/langtool.pl
@@ -17,7 +17,7 @@
17 17
18sub usage { 18sub usage {
19 print <<MOO 19 print <<MOO
20Usage langtool --options langfile 20Usage langtool [--inplace] --options langfile1 [langfile2 ...]
21 21
22 For all actions, the modified langfile will be output on stdout. When doing 22 For all actions, the modified langfile will be output on stdout. When doing
23 stuff to english.lang, you should almost always apply the same change to all 23 stuff to english.lang, you should almost always apply the same change to all
@@ -52,6 +52,13 @@ Usage langtool --options langfile
52 Change the target for the specified id from one value to another 52 Change the target for the specified id from one value to another
53 Example: 53 Example:
54 langtool --changetarget --from e200 --to e200,c200 --id LANG_ON dansk.lang 54 langtool --changetarget --from e200 --to e200,c200 --id LANG_ON dansk.lang
55
56 --inplace
57
58 Perform file operations in-place, instead of outputting the result to
59 stdout. With this option set, you can specify multiple langfiles for
60 all commands.
61 Example: langtool --deprecate --id LANG_ASK --inplace *.lang
55MOO 62MOO
56} 63}
57 64
@@ -64,6 +71,7 @@ my $changesource = '';
64my $changeid = ''; 71my $changeid = '';
65my $changetarget = ''; 72my $changetarget = '';
66my $changedesc = ''; 73my $changedesc = '';
74my $inplace = '';
67my $help = ''; 75my $help = '';
68# Parameters 76# Parameters
69my @ids = (); 77my @ids = ();
@@ -78,6 +86,7 @@ GetOptions(
78 'changetarget' => \$changetarget, 86 'changetarget' => \$changetarget,
79 'changedesc' => \$changedesc, 87 'changedesc' => \$changedesc,
80 'help' => \$help, 88 'help' => \$help,
89 'inplace' => \$inplace,
81 90
82 'ids=s' => \@ids, 91 'ids=s' => \@ids,
83 'from=s' => \$from, 92 'from=s' => \$from,
@@ -104,6 +113,8 @@ if (
104 ($deprecate and $numids < 1) 113 ($deprecate and $numids < 1)
105 or # Do changesource, but either target or to not set 114 or # Do changesource, but either target or to not set
106 ($changesource and ($s_target eq "" or $to eq "")) 115 ($changesource and ($s_target eq "" or $to eq ""))
116 or # More than one file passed, but inplace isn't set
117 ($numfiles > 1 and not $inplace)
107 ) { 118 ) {
108 usage(); 119 usage();
109 exit(1); 120 exit(1);
@@ -121,87 +132,101 @@ if ($changesource and not $to =~ /none|deprecated/) {
121 $to = sprintf('"%s"', $to); 132 $to = sprintf('"%s"', $to);
122} 133}
123 134
124open(LANGFILE, $ARGV[0]); 135foreach my $file (@ARGV) {
125my $id = ""; 136 print(STDERR "$file\n");
126my $desc = ""; 137 open(LANGFILE, $file) or die(sprintf("Couldn't open file for reading: %s", $file));
127my $location = ""; 138 my $id = "";
128my $target = ""; 139 my $desc = "";
129my $string = ""; 140 my $location = "";
130my $open = 0; 141 my $target = "";
131 142 my $string = "";
132for (<LANGFILE>) { 143 my $open = 0;
133 my $line = $_; 144 my $output = "";
134 145
135 if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) { 146 for (<LANGFILE>) {
136 my $tag = $2; 147 my $line = $_;
137 $open = $1 eq "/" ? 0 : 1; 148
138 if ($open) { 149 ### Set up values when a tag starts or ends ###
139 $location = $tag; 150 if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) {
140 ($target, $string) = ("", ""); 151 my $tag = $2;
141 } 152 $open = $1 eq "/" ? 0 : 1;
142 if ($open and $tag eq "phrase") { 153 if ($open) {
143 $id = ""; 154 $location = $tag;
144 } 155 ($target, $string) = ("", "");
145 if (not $open) { 156 }
146 $location = ""; 157 if ($open and $tag eq "phrase") {
158 $id = "";
159 }
160 if (not $open) {
161 $location = "";
162 }
147 } 163 }
148 } 164 ### Set up values when a key: value pair is found ###
149 elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) { 165 elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) {
150 my ($key, $val) = ($1, $2); 166 my ($key, $val) = ($1, $2);
151 if ($location =~ /source|dest|voice/) { 167 if ($location =~ /source|dest|voice/) {
152 ($target, $string) = ($key, $val); 168 ($target, $string) = ($key, $val);
169 }
170 if ($key eq "id") {
171 $id = $val;
172 }
173 elsif ($key eq "desc") {
174 $desc = $val;
175 }
153 } 176 }
154 if ($key eq "id") { 177
155 $id = $val; 178 if ($deprecate) {
179 if ($id ne "" and grep(/$id/, @ids)) {
180 # Set desc
181 $line =~ s/\s*desc:.*/ desc: deprecated/;
182 # Set user
183 $line =~ s/\s*user:.*/ user:/;
184 # Print an empty target line after opening tag (target isn't set)
185 if ($location =~ /source|dest|voice/ and $target eq "") {
186 $line .= " *: none\n";
187 }
188 # Do not print target: string lines
189 elsif ($location =~ /source|dest|voice/ and $target ne "") {
190 $line = "";
191 }
192 }
156 } 193 }
157 elsif ($key eq "desc") { 194 elsif ($changetarget) {
158 $desc = $val; 195 # Change target if set and it's the same as $from
196 if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
197 $line =~ s/$from/$to/;
198 }
159 } 199 }
160 } 200 elsif ($changesource) {
161 201 # Change string if $target is set and matches $s_target
162 if ($deprecate) { 202 if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") {
163 if ($id ne "" and grep(/$id/, @ids)) { 203 $line =~ s/$string/$to/;
164 # Set desc
165 $line =~ s/\s*desc:.*/ desc: deprecated/;
166 # Set user
167 $line =~ s/\s*user:.*/ user:/;
168 # Print an empty target line after opening tag (target isn't set)
169 if ($location =~ /source|dest|voice/ and $target eq "") {
170 $line .= " *: none\n";
171 } 204 }
172 # Do not print target: string lines 205 }
173 elsif ($location =~ /source|dest|voice/ and $target ne "") { 206 elsif ($changedesc) {
174 $line = ""; 207 # Simply change the desc line if the id matches
208 if ($id ne "" and grep(/$id/, @ids)) {
209 $line =~ s/\s*desc:.*/ desc: $to/;
175 } 210 }
176 } 211 }
177 print($line); 212 elsif ($changeid) {
178 } 213 $line =~ s/^\s*id:\s*$from.*$/ id: $to/;
179 elsif ($changetarget) {
180 # Change target if set and it's the same as $from
181 if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
182 $line =~ s/$from/$to/;
183 } 214 }
184 print($line); 215 else {
185 } 216 print("This should never happen.\n");
186 elsif ($changesource) { 217 exit(3);
187 # Change string if $target is set and matches $s_target
188 if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") {
189 $line =~ s/$string/$to/;
190 } 218 }
191 print($line); 219 if ($inplace) {
192 } 220 $output .= $line;
193 elsif ($changedesc) { 221 }
194 # Simply change the desc line if the id matches 222 else {
195 if ($id ne "" and grep(/$id/, @ids)) { 223 print($line);
196 $line =~ s/\s*desc:.*/ desc: $to/;
197 } 224 }
198 print($line);
199 }
200 elsif ($changeid) {
201 $line =~ s/^\s*id:\s*$from.*$/ id: $to/;
202 print($line);
203 } 225 }
204 else { 226 close(LANGFILE);
205 print("wut wut\n"); 227 if ($inplace) {
228 open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file));
229 print(LANGFILE $output);
230 close(LANGFILE);
206 } 231 }
207} 232}