Debian Codegraph @

translit

raw
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 2007-2008 ...
5 # Alex Linke <[email protected]>
6 # Rona Linke <[email protected]>
7 # Copyright (C) 2009-2016 Lingua-Systems Software GmbH
8 # Copyright (C) 2016-2017 Netzum Sorglos, Lingua-Systems Software GmbH
9 # Copyright (C) 2017-2022 Netzum Sorglos Software GmbH
10 #
11
12 use strict;
13 use Getopt::Long;
14
15 require 5.008;
16
17 use Lingua::Translit;
18 use Lingua::Translit::Tables qw/:all/;
19
20 my $VERSION = '0.6';
21
22 =pod
23
24 =head1 NAME
25
26 translit - transliterate text between various writing systems
27
28 =head1 SYNOPSIS
29
30 translit B<-t> I<name> B<-r> B<-i> I<file> B<-o> I<file> -l -v -h
31
32 =head1 DESCRIPTION
33
34 B<translit> transliterates B<UTF-8> encoded natural language text from one
35 writing system to another.
36
37 It utilizes the Perl module L<Lingua::Translit> to accomplish this task and
38 follows the provided transliteration standards as strict as possible.
39
40 B<translit> regards the following commandline switches:
41
42 =over 4
43
44 =item B<--trans>, B<-t> I<name>
45
46 Use the transliteration standard named I<name>.
47
48 =item B<--reverse>, B<-r>
49
50 Transliterate in reverse direction.
51
52 B<NOTE>: Not every transliteration is capable of being reversed.
53 If unsure, have a look at the list of supported transliterations using the
54 B<--list> switch.
55
56 =item B<--infile>, B<-i> I<FILE>
57
58 Read text from I<FILE>. Defaults to STDIN.
59
60 =item B<--outfile>, B<-o> I<FILE>
61
62 Write the transliterated text to I<FILE>. Defaults to STDOUT.
63
64 =item B<--list>, B<-l>
65
66 Print a list of supported transliterations showing their
67
68 =over 4
69
70 =item *
71
72 name
73
74 =item *
75
76 reversibility
77
78 =item *
79
80 description
81
82 =back
83
84 =item B<--verbose>, B<-v>
85
86 Print verbose status messages to STDERR.
87
88 =item B<--help>, B<-h>
89
90 Show a short help text introducing command line switches only.
91
92 =back
93
94 =cut
95
96 # Set defaults where possible
97 my %opt = (
98 trans => "",
99 infile => "",
100 outfile => "",
101 reverse => 0,
102 list => 0,
103 verbose => 0, # off
104 help => 0
105 );
106
107 show_help(1)
108 unless GetOptions(
109 "trans|t=s" => \$opt{trans},
110 "infile|i=s" => \$opt{infile},
111 "outfile|o=s" => \$opt{outfile},
112 "reverse|r" => \$opt{reverse},
113 "list|l" => \$opt{list},
114 "verbose|v" => \$opt{verbose},
115 "help|h" => \$opt{help}
116 );
117 show_help(0) if $opt{help};
118 show_list() if $opt{list};
119 show_help(1) unless $opt{trans};
120
121 # Assure the requested transliteration is supported...
122 die "$opt{trans} is not supported.\n" unless translit_supported( $opt{trans} );
123
124 # ...and reverse transliteration is supported, too - if requested
125 die "$opt{trans} cannot be reversed.\n"
126 if ( $opt{reverse} && !translit_reverse_supported( $opt{trans} ) );
127
128 # If no input file was specified, use STDIN as a fallback. This way, translit
129 # may also be used in pipes!
130 my $in;
131
132 if ( $opt{infile} ) {
133 print STDERR "Reading input from $opt{infile}...\n" if $opt{verbose};
134
135 open IN, "$opt{infile}" or die "$opt{infile}: $!\n";
136
137 $in = *IN;
138 }
139 else {
140 print STDERR "Reading input from STDIN...\n" if $opt{verbose};
141
142 $in = *STDIN;
143 }
144
145 # If no output file was specified, use STDOUT as a fallback.
146 my $out;
147
148 if ( $opt{outfile} ) {
149 print STDERR "Writing output to $opt{outfile}...\n" if $opt{verbose};
150
151 open OUT, "> $opt{outfile}" or die "$opt{outfile}: $!\n";
152
153 $out = *OUT;
154 }
155 else {
156 print STDERR "Writing output to STDOUT...\n" if $opt{verbose};
157
158 $out = *STDOUT;
159 }
160
161 # Slurp in all the input and close filehandle
162 local $/;
163 my $text = <$in>;
164 close($in);
165
166 # Transliterate
167 my $tr = new Lingua::Translit( $opt{trans} );
168 my $text_tr;
169
170 unless ( $opt{reverse} ) {
171 print STDERR "Transliterating according to ", $tr->name(), "...\n"
172 if $opt{verbose};
173
174 $text_tr = $tr->translit($text);
175 }
176 else {
177 print STDERR "Transliterating according to ", $tr->name(), " (reverse)...\n"
178 if $opt{verbose};
179
180 $text_tr = $tr->translit_reverse($text);
181 }
182
183 # Write the transliterated text to the filehandle and close it
184 print $out $text_tr;
185 close($out);
186
187 sub show_help {
188 my $retval = shift();
189
190 print "translit v$VERSION -- ",
191 "(c) 2007-2022 Netzum Sorglos Software GmbH\n\n",
192 "usage: $0 -i FILE -o FILE -t NAME -r -l -v -h\n\n",
193 " --infile -i FILE read input from FILE\n",
194 " --outfile -o FILE write output to FILE\n",
195 " --trans -t NAME use transliteration NAME\n",
196 " --reverse -r transliterate in reverse direction\n",
197 " --list -l list all supported transliterations\n\n",
198 " --verbose -v print verbose status messages\n",
199 " --help -h show this help\n\n",
200 "Read translit(1) for details.\n";
201
202 exit($retval);
203 }
204
205 sub show_list {
206 print "Transliterations supported by Lingua::Translit v"
207 . $Lingua::Translit::VERSION . ":\n\n";
208
209 translit_list_supported();
210
211 exit(0);
212 }
213
214 =head1 RESTRICTIONS
215
216 The input has to be UTF-8 encoded.
217
218 =head1 BUGS
219
220 None known.
221
222 Please report bugs using CPAN's request tracker at
223 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Lingua-Translit>.
224
225 =head1 SEE ALSO
226
227 L<Lingua::Translit>, L<Lingua::Translit::Tables>
228
229 =head1 AUTHORS
230
231 Alex Linke <[email protected]>
232
233 Rona Linke <[email protected]>
234
235 =head1 LICENSE AND COPYRIGHT
236
237 Copyright (C) 2007-2008 Alex Linke and Rona Linke
238
239 Copyright (C) 2009-2016 Lingua-Systems Software GmbH
240
241 Copyright (C) 2016-2017 Netzum Sorglos, Lingua-Systems Software GmbH
242
243 Copyright (C) 2017-2022 Netzum Sorglos Software GmbH
244
245 This module is free software; you can redistribute it and/or modify it under
246 the same terms as Perl itself.
247
248 =cut
249
250 # vim: set ft=perl sts=4 sw=4 ts=4 ai et: