-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccents
executable file
·159 lines (137 loc) · 2.74 KB
/
accents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl -w
# Fre Feb 20 19:57:25 CET 2015
(my $email='XXX%YYY,ch')=~ tr/%,/@./;
use strict; use warnings FATAL => 'uninitialized';
use utf8;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname ..
Convert combinations of accents and characters (as input when a
keyboard doesn't support 'dead keys') into combined characters,
e.g. 'All^o Qu´ebec, ' into 'All, Québec, ê'.
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
use Getopt::Long;
our $verbose=0;
#our $opt_dry;
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
#"dry-run"=> \$opt_dry,
) or exit 1;
our $map= +{
# https://en.wikipedia.org/wiki/Cedilla
'¸'=> {
c=> 'ç',
C=> 'Ç',
},
# https://en.wikipedia.org/wiki/Circumflex
'^'=> {
a=> 'â',
A=> 'Â',
e=> 'ê',
E=> 'Ê',
i=> 'î',
I=> 'Î',
o=> 'ô',
O=> 'Ô',
u=> 'û',
U=> 'Û',
},
# https://en.wikipedia.org=> 'wiki=> 'Umlaut_(diacritic)
'¨'=> {
a=> 'ä',
A=> 'Ä',
e=> 'ë',
E=> 'Ë',
i=> 'ï',
I=> 'Ï',
o=> 'ö',
O=> 'Ö',
u=> 'ü',
U=> 'Ü',
},
# https://en.wikipedia.org=> 'wiki=> 'Grave_accent
'`'=> {
a=> 'à',
A=> 'À',
e=> 'è',
E=> 'È',
i=> 'ì',
I=> 'Ì',
o=> 'ò',
O=> 'Ò',
u=> 'ù',
U=> 'Ù',
# ẁ ỳ ..
},
# https://en.wikipedia.org=> 'wiki=> 'Acute_accent
'´'=> {
a=> 'á',
A=> 'Á',
e=> 'é',
E=> 'É',
i=> 'í',
I=> 'Í',
o=> 'ó',
O=> 'Ó',
u=> 'ú',
U=> 'Ú',
# ń ḿ ..
},
};
sub _conv {
s{([\\¸^¨`´])(.?)}{
my ($cmd, $c)= ($1,$2);
($cmd eq '\\') ?
# ok to simply drop it at eof ?
$c :
do {
my $m= $$map{$cmd} // die "bug";
$$m{$c} //
($cmd eq $c ?
"$cmd$c" # lines of ^^^ or so, ok?
: do {
warn "unknown sequence '$cmd$c'";
"$cmd$c"
})
}
}sge;
}
sub conv ($) {
local ($_)=@_;
_conv;
$_
}
sub convfh ($) {
my ($fh)=@_;
binmode $fh, "encoding(utf-8)" or die;
while (<$fh>) {
_conv;
print or die $!;
}
close $fh or die $!;
}
use Chj::TEST;
TEST { conv q{Der ¨Ubermut} } q{Der Übermut};
TEST { conv q{Der \\¨Ubermut} } q{Der ¨Ubermut};
TEST { conv q{Der \\\\¨Ubermut} } q{Der \\Übermut};
TEST { conv q{Der ^^^^} } q{Der ^^^^}; # and no warning
TEST { conv q{Der ^^^^^} } q{Der ^^^^^}; # with warning, hum..XXX?
if ($ENV{RUN_TESTS}) {
#use Chj::Backtrace; use Chj::repl; repl;
Chj::TEST::run_tests;
} else {
binmode STDOUT, "encoding(utf-8)" or die;
if (@ARGV) {
for (@ARGV) {
open my $in, "<", $_ or die "'$_': $!";
convfh $in;
}
} else {
convfh *STDIN{IO};
}
}