Debian Codegraph @

scripts/tmxgrep

raw
1 #!/usr/bin/perl -w -s
2
3 # PODNAME: tmxgrep
4 # ABSTRACT: grep translation units in a TMX file
5
6 ## use utf8::all;
7 use Data::Dumper;
8
9 our ($all,$w,$max,$pdf, $o, $n, $a,$debug, $i, $html);
10
11 $a //= "p" if $pdf || $html;
12
13 my ($ab,$ae); ## emphatise marker
14 my $browser="";
15 $browser = "=$html" if ($html and $html ne "1");
16
17 if($a){
18 if($a eq "1"){
19 $ab="=(";
20 $ae=")=";
21 }
22 elsif($a){
23 $ab="<$a>";
24 $ae="</$a>";
25 }
26 }
27
28 if($html){
29 if($html eq "1"){
30 $ab="<b>";
31 $ae="</b>";
32 }
33 elsif($html){
34 $ab="<$html>";
35 $ae="</$html>";
36 }
37 }
38
39 $max //= 500;
40 $max = 1000000000 if ($max == 0 or $all);
41
42 use strict;
43 use warnings;
44
45 my $p = shift or die("Usage: $0 [options] pattern f.tmx\n");
46 if ($p =~ m{(\w+):(.+?);(\w+):(.+)}){
47 if($i and $w){ $p = +{$1=> qr{(?i)\b$2\b}, $3 => qr{(?i)\b$4\b}} }
48 elsif($i) { $p = +{$1=> qr{(?i)$2}, $3 => qr{(?i)$4}} }
49 elsif($w) { $p = +{$1=> qr{\b$2\b}, $3 => qr{\b$4\b}} }
50 else { $p = +{$1=> $2, $3 => $4} }
51 }
52 elsif($p =~ m{(\w+):(.+)} ){
53 if($i and $w){ $p = +{$1=> qr{(?i)\b$2\b}} }
54 elsif($i) { $p = +{$1=> qr{(?i)$2}} }
55 elsif($w) { $p = +{$1=> qr{\b$2\b}} }
56 else { $p = +{$1=> $2} }
57 }
58 else{
59 if($i and $w){ $p = qr{(?i)\b$p\b} }
60 elsif($i) { $p = qr{(?i)$p} }
61 elsif($w) { $p = qr{\b$p\b} }
62 }
63
64 use XML::TMX;
65 use XML::TMX::Reader;
66
67 $o //= "__.tmx" if $pdf || $html;
68
69 our ($icons);
70
71 my $tmx = shift;
72 my $tmx_obj = XML::TMX::Reader->new($tmx);
73
74 print STDERR Dumper($p) if $main::debug;
75
76 $tmx_obj->for_tu(
77 { patt => $p,
78 gen_tu=> $max,
79 n => $n || 0,
80 output => $o || undef }, #### "__tmxgrep.tmx"
81 sub { my($tu,$at) = @_;
82 print STDERR Dumper($tu,$at,$p) if $main::debug;
83 # print STDERR ".";
84 if($main::a ){
85 for my $li(keys %$tu){
86 next if $li =~ /^-/;
87 for(keys %{$tu->{$li}}){
88 if(ref($p)){
89 my $pli = $p->{lc($li)} or next;
90 $tu->{$li}{$_} =~ s!($pli)!$ab$1$ae!g;
91 }
92 else{
93 $tu->{$li}{$_} =~ s!($p)!$ab$1$ae!g;
94 }
95 }
96 }
97 }
98
99 return $tu;
100 }
101 );
102
103 if($pdf){ system("tmx2pdf-prince -v $o");}
104 if($html){ system("tmx2html -v$browser $o");}
105
106 __END__
107
108 =pod
109
110 =encoding UTF-8
111
112 =head1 NAME
113
114 tmxgrep - grep translation units in a TMX file
115
116 =head1 VERSION
117
118 version 0.39
119
120 =head1 SYNOPSIS
121
122 tmxgrep patt file.tmx
123 options:
124 -max=300 -- extract up to 300 TU (def=500)
125 -max=0 -- all matches
126 -all -- all matches
127 -a -- annotate matches with '=(...)='
128 -a=xx -- annotate matches with '<xx>...</xx>'
129 -o=out.tmx -- define output file (defaut= STDOUT)
130 -n -- print original TU number (eg: <tu n="35">)
131 -i -- ignore case
132 -w -- whole words
133 -pdf -- output is transformed in a PDF file and show
134 -html -- output is transformed in a HTML file and show
135 -html=chromium -- idem and show it with chromium (def:firefox)
136
137 Where patt can be:
138
139 RegExp
140 l1:RegExp
141 l1:RegExp;l2:RegExp
142
143 =head1 DESCRIPTION
144
145 Creates a TMX file with the translation units that macth the provided
146 regular expression.
147
148 =head1 AUTHORS
149
150 =over 4
151
152 =item *
153
154 Alberto Simões <[email protected]>
155
156 =item *
157
158 José João Almeida <[email protected]>
159
160 =back
161
162 =head1 COPYRIGHT AND LICENSE
163
164 This software is copyright (c) 2010-2017 by Projeto Natura <[email protected]>.
165
166 This is free software; you can redistribute it and/or modify it under
167 the same terms as the Perl 5 programming language system itself.
168
169 =cut