commit b6ca2498b1dd64c821646d9462d377a5ab158de4
parent de3bbf6acdd3d5f181717ef8b57d752d07040cb8
Author: lumidify <nobody@lumidify.org>
Date: Wed, 1 Apr 2020 16:52:33 +0200
Add revert option to config
Diffstat:
1 file changed, 30 insertions(+), 16 deletions(-)
diff --git a/transliterate.pl b/transliterate.pl
@@ -508,7 +508,7 @@ sub parse_config {
# Load a file of replacement words into a hash table
sub load_table {
- my ($filename, $args, $config) = @_;
+ my ($filename, $args, $config, $revert) = @_;
my $fh;
# if the paths are relative, find their absolute location based
# on the location of the config file
@@ -532,16 +532,30 @@ sub load_table {
close $fh;
return undef;
}
- my $key = NFD($words[0]);
- if (exists $table{$key}) {
- if ($args->{"checkduplicates"}) {
- warn "WARNING: Duplicate word in file \"$filename\": " .
- "\"$key\", with replacement \"$words[1]\", " .
- "already exists with replacement \"$table{$key}\".\n";
- }
- $table{$key} = get_unique_words($table{$key} . $config->{choicesep} . $words[1], $config);
+ my $word;
+ my $replacement;
+ if ($revert) {
+ $word = NFD $words[1];
+ $replacement = $words[0];
} else {
- $table{$key} = $words[1];
+ $word = NFD $words[0];
+ $replacement = $words[1];
+ }
+ my @word_choices = split /\Q$config->{choicesep}\E/, $word;
+ foreach my $word_choice (@word_choices) {
+ if (exists $table{$word_choice}) {
+ if ($args->{"checkduplicates"}) {
+ warn "WARNING: Duplicate word in file \"$filename\": " .
+ "\"$word_choice\", with replacement \"$replacement\", " .
+ "already exists with replacement \"$table{$word_choice}\".\n";
+ }
+ $table{$word_choice} = get_unique_words(
+ $table{$word_choice} .
+ $config->{choicesep} .
+ $replacement, $config);
+ } else {
+ $table{$word_choice} = $replacement;
+ }
}
}
close $fh;
@@ -700,9 +714,9 @@ sub interpret_config {
}
if ($cmd->[0]->{"value"} eq "table") {
my $table_path = $cmd->[2]->{"value"};
- my $nodisplay = 0;
- if ($#$cmd >= 3 && $cmd->[3]->{"value"} eq "nodisplay") {
- $nodisplay = 1;
+ my %table_args;
+ foreach (3..$#$cmd) {
+ $table_args{$cmd->[$_]->{"value"}} = 1;
}
my $table;
# add to temporary path-to-table mapping so tables aren't
@@ -710,16 +724,16 @@ sub interpret_config {
if (exists $path_to_table{$table_path}) {
$table = $path_to_table{$table_path};
} else {
- $table = load_table $table_path, $args, \%config;
- $path_to_table{$table_path} = $table;
+ $table = load_table $table_path, $args, \%config, $table_args{"revert"};
return if !$table;
+ $path_to_table{$table_path} = $table;
}
my $table_id = $cmd->[1]->{"value"};
$tables{$table_id} = $table;
$table_id_to_path{$table_id} = $table_path;
# this is a hash to avoid duplicates if the same file
# is loaded multiple times
- $config{"display_tables"}->{$table_path} = 1 if !$nodisplay;
+ $config{"display_tables"}->{$table_path} = 1 if !exists $table_args{"nodisplay"};
} elsif ($cmd->[0]->{"value"} eq "expand") {
my $orig_table_id = $cmd->[1]->{"value"};
my $ending_table_id = $cmd->[2]->{"value"};