summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtools/langtool.pl41
1 files changed, 31 insertions, 10 deletions
diff --git a/tools/langtool.pl b/tools/langtool.pl
index 2e959317c1..a0e147c4ad 100755
--- a/tools/langtool.pl
+++ b/tools/langtool.pl
@@ -53,6 +53,12 @@ Usage langtool [--inplace] --options langfile1 [langfile2 ...]
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 55
56 --delete --id ID_1,ID_2 --id ID_3 langfile
57
58 Delete a number of ids. THIS WILL BREAK BACKWARDS COMPATIBILITY. Use with
59 extreme caution.
60 Example: langtool --delete --id LANG_LEFT,LANG_RIGHT english.lang
61
56 --inplace 62 --inplace
57 63
58 Perform file operations in-place, instead of outputting the result to 64 Perform file operations in-place, instead of outputting the result to
@@ -77,6 +83,7 @@ my $changesource = '';
77my $changeid = ''; 83my $changeid = '';
78my $changetarget = ''; 84my $changetarget = '';
79my $changedesc = ''; 85my $changedesc = '';
86my $delete = '';
80my $inplace = ''; 87my $inplace = '';
81my $help = ''; 88my $help = '';
82# Parameters 89# Parameters
@@ -91,6 +98,7 @@ GetOptions(
91 'changeid' => \$changeid, 98 'changeid' => \$changeid,
92 'changetarget' => \$changetarget, 99 'changetarget' => \$changetarget,
93 'changedesc' => \$changedesc, 100 'changedesc' => \$changedesc,
101 'delete' => \$delete,
94 'help' => \$help, 102 'help' => \$help,
95 'inplace' => \$inplace, 103 'inplace' => \$inplace,
96 104
@@ -110,8 +118,8 @@ if ($help) {
110 exit(); 118 exit();
111} 119}
112# More than one option set (or none) 120# More than one option set (or none)
113elsif (($deprecate + $changesource + $changeid + $changetarget + $changedesc) != 1) { 121elsif (($deprecate + $changesource + $changeid + $changetarget + $changedesc +$delete) != 1) {
114 error("Exactly one of --deprecate, --changesource, --changeid, --changetarget,\n--changedesc must be used."); 122 error("Exactly one of --deprecate, --changesource, --changeid, --changetarget,\n--changedesc, --delete must be used.");
115} 123}
116# Do changeid, but either from or to is empty 124# Do changeid, but either from or to is empty
117elsif ($changeid and ($from eq "" or $to eq "")) { 125elsif ($changeid and ($from eq "" or $to eq "")) {
@@ -129,6 +137,10 @@ elsif ($changetarget and ($from eq "" or $to eq "")) {
129elsif ($deprecate and $numids < 1) { 137elsif ($deprecate and $numids < 1) {
130 error("--deprecate used, but no IDs specified"); 138 error("--deprecate used, but no IDs specified");
131} 139}
140# Do delete, but no ids set
141elsif ($delete and $numids < 1) {
142 error("--delete used, but no IDs specified");
143}
132# Do changesource, but either target or to not set 144# Do changesource, but either target or to not set
133elsif ($changesource and ($s_target eq "" or $to eq "")) { 145elsif ($changesource and ($s_target eq "" or $to eq "")) {
134 error("--changesource used, but --target or --to not set"); 146 error("--changesource used, but --target or --to not set");
@@ -159,7 +171,7 @@ foreach my $file (@ARGV) {
159 my $target = ""; 171 my $target = "";
160 my $string = ""; 172 my $string = "";
161 my $open = 0; 173 my $open = 0;
162 my $output = ""; 174 my @output;
163 175
164 for (<LANGFILE>) { 176 for (<LANGFILE>) {
165 my $line = $_; 177 my $line = $_;
@@ -209,6 +221,16 @@ foreach my $file (@ARGV) {
209 } 221 }
210 } 222 }
211 } 223 }
224 elsif ($delete) {
225 if ($id ne "" and grep(/^$id$/, @ids)) {
226 if ($location eq "phrase" and $line =~ /id:/) {
227 # Kluge to nuke the <phrase> line
228 pop(@output);
229 }
230 # Set the whole phrase to empty string.
231 $line = "";
232 }
233 }
212 elsif ($changetarget) { 234 elsif ($changetarget) {
213 # Change target if set and it's the same as $from 235 # Change target if set and it's the same as $from
214 if ($id ne "" and grep(/^$id$/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) { 236 if ($id ne "" and grep(/^$id$/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
@@ -234,17 +256,16 @@ foreach my $file (@ARGV) {
234 print("This should never happen.\n"); 256 print("This should never happen.\n");
235 exit(3); 257 exit(3);
236 } 258 }
237 if ($inplace) { 259
238 $output .= $line; 260 push(@output, $line);
239 }
240 else {
241 print($line);
242 }
243 } 261 }
244 close(LANGFILE); 262 close(LANGFILE);
245 if ($inplace) { 263 if ($inplace) {
246 open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file)); 264 open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file));
247 print(LANGFILE $output); 265 print(LANGFILE @output);
248 close(LANGFILE); 266 close(LANGFILE);
249 } 267 }
268 else {
269 print(@output);
270 }
250} 271}