lib/App/Bernard.pm

filter: Statementstmt 61.6% 45 / 73 Branchbran 27.7% 10 / 36 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 3 totaltotal 50.3% 64 / 127 CC 22 SCAR 31.7
CC
22
Cov
50%
parse_command_line
43.1
__ANON__
30.0
execute
10.7
CRAP 23.8
« lib/App/Bernard/Magic/Single.pm ↑ Summary blib/lib/App/Bernard/Magic/Fuzzreview.pm »
1 package App::Bernard;
2  
3 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Lingua::EN::Alphabet::Shaw;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = 0.01;
12  
13 ################################################################
14  
15 1 my %settings = (
16 output => undef,
17 script => 'shaw',
18 inplace => 0,
19 names => undef,
20 input => undef,
21 help => 0,
22 check => 0,
23 magic => 'single',
24 expression => [],
25 # "underscore" doesn't have an
26 # associated command-line option
27 underscore => 0,
28 definelist => [],
29 defines => {},
30 );
31  
32 1 ▶sub parse_command_line {
Subroutine parse_command_line: called CC=12 SCAR=37.6
Pod undocumented
33 GetOptions(
34 'o|output=s' => \$settings{'output'},
35 's|script=s' => \$settings{'script'},
36 'i|in-place' => \$settings{'inplace'},
37 'n|names=s' => \$settings{'names'},
38 'h|?|help' => \$settings{'help'},
39 'c|check' => \$settings{'check'},
40 'm|magic=s' => \$settings{'magic'},
41 1 'e|expression=s@' => \$settings{'expression'},
42 );
43  
44 1 ▶ if (scalar(@ARGV)==0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @ARGV == 0) { } 0 1 1
elsif (scalar @ARGV > 1) { } 0 1 1
45 0 ▶ $settings{'help'} = 1 unless $settings{'expression'};
Branch: 1 branch
BranchTrueFalseTotal
unless $settings{"expression"} 0 0 0
46 } elsif (scalar(@ARGV)>1) {
47 0 die "Can work on at most one file at once\n";
48 } else {
49 1 $settings{'input'} = $ARGV[0];
50 }
51  
52 1 ▶ if ($settings{'inplace'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{"inplace"}) 0 1 1
53 die "--in-place cannot be used with --output\n"
54 0 ▶ if $settings{'output'};
Branch: 1 branch
BranchTrueFalseTotal
if $settings{"output"} 0 0 0
55
56 die "--in-place cannot be used with standard input\n"
57 0 ▶ unless $settings{'input'};
Branch: 1 branch
BranchTrueFalseTotal
unless $settings{"input"} 0 0 0
58  
59 0 $settings{'output'} = $settings{'input'}.'.'.rand();
60 }
61  
62 1 my %known_alphabets = ( latn=>1, shaw=>1 );
63 1 $settings{'script'} = lc $settings{'script'};
64  
65 die "Alphabet $settings{'script'} is unknown\n"
66 1 ▶ unless defined $known_alphabets{$settings{'script'}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $known_alphabets{$settings{"script"}} 0 1 1
67  
68 die "Can't check standard output\n"
69 1 ▶ if $settings{'check'} && !defined($settings{'output'});
Branch: 1 branch
BranchTrueFalseTotal
if $settings{"check"} and not defined $settings{"output"} 0 1 1
Condition33%1/3
$settings{"check"} and not defined $settings{"output"}
!ll&&!rl&&r
100
MC/DC0%0/2
$settings{"check"} and not defined $settings{"output"}
$settings{"check"} not defined $settings{"output"}
Truth table33%1/3
A and not(B)
A= $settings{"check"}B= not defined $settings{"output"}
ABresult
0X0
100
111
70  
71 1 ▶ if ($settings{'help'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{"help"}) 0 1 1
72 0 ▶ if (system("man bernard")!=0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system("man bernard") != 0) 0 0 0
73 0 print "Sorry, I can't launch the help program.\n";
74 }
75 0 exit;
76 }
77 }
78  
79 ################################################################
80  
81 1 ▶sub print_expressions {
Subroutine print_expressions: called CC=3 SCAR=12.0
Pod undocumented
82 1 for my $expression (@{$settings{'expression'}}) {
83 0 print $settings{'transliterate'}->($expression), "\n";
84 }
85  
86 1 ▶ exit if scalar(@ARGV)==0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ARGV == 0 0 1 1
87 }
88  
89 ################################################################
90  
91  
92 1 ▶sub execute {
Subroutine execute: called CC=5 SCAR=23.7
Pod undocumented
93  
94 1 binmode STDOUT, ":utf8";
95  
96 1 parse_command_line();
97  
98 1 ▶ if ($settings{'script'} eq 'latn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{'script'} eq 'latn') { } 0 1 1
99 0 ▶ $settings{'transliterate'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 my ($text) = @_;
101 0 return $text;
102 0 };
103 } else {
104 1 my $leas = Lingua::EN::Alphabet::Shaw->new();
105  
106 0 ▶ $settings{'transliterate'} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
107 0 my ($text) = @_;
108  
109 0 ▶ $settings{'underscore'} = 0 if $text =~ /%\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /%\(/ 0 0 0
110  
111 0 ▶ if ($settings{'underscore'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{"underscore"}) 0 0 0
112 0 $text =~ s/_/_ /g;
113 0 $text =~ s/([a-z]+)_ ([a-z]+)/_ $1$2/gi;
114 }
115  
116 0 for my $lhs (keys(%{$settings{'defines'}})) {
117 0 my $rhs = $settings{'defines'}->{$lhs};
118 0 $text =~ s/\b$lhs\b/$rhs/gi;
119 }
120  
121 # split out Python interpolation
122 0 my @text = split /(%\([^)]*\)[^a-z]*[a-z])/, $text;
123  
124 0 $text = $leas->transliterate(@text);
125  
126 0 ▶ if ($settings{'underscore'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{"underscore"}) 0 0 0
127 0 $text =~ s/_ /_/g;
128 }
129  
130 0 return $text;
131 1 };
132 }
133  
134 1 print_expressions();
135  
136 # now do the magic
137  
138 1 my $magic = undef;
139 1 my $m = lc $settings{'magic'};
140 1 my $pkg = "App::Bernard::Magic::\u$m";
141  
142 1 eval {
143 1 my $filename = $pkg;
144 1 $filename =~ s!::!/!g;
145 1 require "$filename.pm";
146  
147 1 $magic = $pkg->new();
148 };
149  
150 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
151 0 ▶ die "Magic $settings{'magic'} is not known\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /Can't locate/ 0 0 0
152 if $@ =~ /Can't locate/;
153 0 die "Error in loading magic $settings{'magic'}: $@\n";
154 }
155  
156 1 ▶ $magic->handle(\%settings)
Branch: 1 branch
BranchTrueFalseTotal
if $magic 1 0 1
157 if $magic;
158 }
159  
160 1 1;
161  
162 =head1 NAME
163  
164 bernard - alphabet remix
165  
166 =head1 AUTHOR
167  
168 Thomas Thurman <thomas@thurman.org.uk>
169  
170 =head1 SYNOPSIS
171  
172 bernard <source> -o <target>
173  
174 =head1 DESCRIPTION
175  
176 bernard takes files written in the conventional alphabet and
177 returns them written in some other alphabet.
178  
179 At present, only the Shavian alphabet is supported.
180  
181 =head1 SWITCHES
182  
183 =head2 -o <filename>, --output <filename>
184  
185 Select output file. If this is not specified,
186 the output is written to the standard output.
187  
188 =head2 -s <alphabet>, --script <alphabet>
189  
190 Select alphabet. Use the ISO 15924 code.
191 This is not case-sensitive.
192 The only arguments currently accepted are "Shaw",
193 which represents the Shavian alphabet, and
194 "Latn", which causes no transformation to
195 the input text.
196  
197 =head2 -S <alphabet>, --source <alphabet>
198  
199 Specifies the alphabet of the source document.
200 The default is C<"Latn">. This is not automatically
201 detected, because the use-cases are so different.
202 This is not case-sensitive.
203 The only two values allowed are C<"Latn"> and C<"Shaw">.
204 Selecting C<"Shaw"> will allow you to transliterate
205 a document in Shavian into, for example, Deseret.
206  
207 If C<"Shaw"> is selected, this has the additional
208 effect of causing I<every> stanza in a .po file
209 to be transliterated, not only the fuzzy and empty
210 ones. It also disables the C<--in-place> switch.
211  
212 Selecting the same source and target alphabet is
213 a valid choice, but means that there will be
214 no change between input and output.
215  
216 It is currently an error to select C<"Shaw"> as
217 the source alphabet and C<"Latn"> as the target
218 alphabet. In other words, you can't yet undo
219 a transliteration into Shavian. This may be
220 added one day.
221  
222 This entire option is not yet implemented.
223  
224 =head2 -n <file>, --names <file>
225  
226 This switch only makes sense with gettext .po files.
227 It means that the msgids in the file are not English
228 strings, but identifiers, and that the English strings
229 are in the .po file whose name is supplied. This is
230 often found in Nokia catalogues.
231  
232 This is not yet implemented.
233  
234 =head2 -c, --check
235  
236 Runs the resulting file through C<"msgfmt -c"> to check
237 its validity.
238  
239 =head2 -i, --in-place
240  
241 This writes the output file over the top of the
242 input file.
243  
244 This switch is only useful with gettext .po files.
245 It is disabled for other filetypes because it would
246 be dangerous: you would lose the original text.
247  
248 =head2 -a, --armour
249  
250 This replaces Shavian letters with their traditional
251 ASCII equivalents. It is disabled for other alphabets.
252 This will cause obvious difficulties if the output
253 would ordinarily contain Latin-alphabet letters.
254 Latin-alphabet letters discovered in the text will be
255 retained.
256  
257 This is not currently implemented.
258  
259 The inverse operation is obtained by using
260 C<-m unarmour>.
261  
262 =head2 -D, --shift-down
263  
264 This is a nasty hack. It shifts the letters of the
265 output alphabet down so that they begin at codepoint
266 128. This is needed because of shortcomings in
267 the UTF-8 decoding of some programs, and when you
268 may be unable to use C<-a> because you need to include
269 characters from both alphabets. You will, of
270 course, need a special font with the relevant glyphs
271 at these non-standard positions.
272  
273 This is not currently implemented.
274  
275 =head2 -e <text>, --expression <text>
276  
277 Transliterates the given expression. This is
278 output before any other file.
279  
280 =head2 -U, --update
281  
282 Checks to see whether there's an updated version
283 of the Shavian set used for transliteration, and
284 downloads it if there is.
285  
286 This is not currently implemented.
287  
288 =head2 -m <magic>, --magic <magic>
289  
290 Selects an alternative mode of operation. The
291 defalt is C<single>, which behaves as described
292 above. Other values have other effects,
293 described in "Magic modes", below.
294  
295 =head2 -p, --apostrophe
296  
297 George Bernard Shaw believed that apostrophes,
298 which he called "uncouth bacilli", were redundant.
299 In honour of this opinion, the C<-p> option
300 strips apostrophes from the transliterated
301 output where they occur within words. The rare
302 apostrophes at the beginnings or endings of words
303 (as in C<'tis>) will not be stripped, in case
304 you use them for quotation marks.
305  
306 This is not currently implemented.
307  
308 =head2 -D, --define
309  
310 This allows you to define the Shavian spelling of
311 a word temporarily. Its argument is the
312 Latin-alphabet spelling, followed by an equals sign,
313 followed by the Shavian spelling. In case
314 you cannot type Shavian letters, you may use the
315 standard ASCII-armouring. For example, to cause
316 the word "of" to be written out in full, rather
317 than as a single-letter abbreviation, use C<-Dof=ov>.
318  
319 This is not currently implemented.
320  
321  
322 =head1 MAGIC MODES
323  
324 These are selected using the C<-m> or C<--magic>
325 switch.
326  
327 =head2 single
328  
329 This is the default, and behaves as described above.
330  
331 =head2 gnome
332  
333 In this mode, the sole non-option argument should be
334 the name of a Shavian .po file. The master template
335 for that package will be downloaded and merged with
336 the .po file, the transliterations will be updated,
337 and then run through C<msgfmt -c> to check them.
338  
339 Alternatively, the non-option argument may be the
340 name of a directory. Each subdirectory of this
341 directory should contain a GNOME package, which
342 contains a file C<po/en@shaw.po>. Each of these
343 files will be acted on as described in the previous
344 paragraph.
345  
346 =head2 unarmour
347  
348 This undoes the effect of the C<-a> or C<--armour>
349 switch. The single non-option argument is a file,
350 which is output verbatim except that characters from
351 the Latin alphabet will be replaced with their
352 corresponding values in the old Shavian-to-Latin
353 mapping.
354  
355 This is not currently implemented.
356  
357 =head1 BUGS
358  
359 Probably many.
360  
361 Code to update the Shavian transliteration of Firefox
362 exists, but has not yet been merged into C<bernard>.
363 It will be merged at some point.
364  
365 It will also be possible later to translate
366 Qt's C<.ts> files.
367  
368 Code to handle C<.srt> subtitle files exists,
369 but has not yet been merged.
370  
371 It doesn't handle any other alphabets than Shavian
372 and the conventional alphabet. At least Deseret
373 will be added.
374  
375 There are several other planned features which are
376 as yet unimplemented.
377  
378 =head1 COPYRIGHT
379  
380 This Perl module is copyright (C) Thomas Thurman, 2010.
381 This is free software, and can be used/modified under the same terms as
382 Perl itself.
383  
« lib/App/Bernard/Magic/Single.pm ↑ Summary blib/lib/App/Bernard/Magic/Fuzzreview.pm »