Shortkeys/Ripper.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 175 CC 15 SCAR 28.0
CC
15
Cov
0%
start
42.0
get_rtl
20.0
end
12.0
CRAP 16.4
1 #!/usr/bin/perl
2
3 package Win32::Shortkeys::Ripper;
4
5 use strict;
6 use warnings;
7 use Config::YAML::Tiny;
8 #use IO::File;
9 use Encode;
10 #use locale;
11 use XML::Parser;
12 use Data::Dumper;
13 #use File::Copy;
14 #use Getopt::Long;
15
16 #use File::BOM qw( :all );
17 my $shk_path = "";
18 my $shkold_path = "";
19 my $tmp_path = "";
20
21 #GetOptions('fpath=s'=> \$shk_path, 'foldpath=s' => \$shkold_path, 'tmpath=s'=> \$tmp_path);
22 my $erasesignal;
23 my $comchain;
24 my $TF=undef;
25
26 my $keepsit;
27 my $afterstarttag;
28 my $beforestarttag;
29
30 =head1 SYNOPSIS
31
32 Win32::Shortkeys::Ripper::catch(
33 shk_file => "shortkey_utf8.xml",
34 tmp_file => "mytmp.txt",
35 shk_old => "shortkeys_utf8.xml_old.txt",
36 encoding => "UTF-8",
37 properties => "ripper.properties"
38 );
39
40 Make a backup of shortkeys_utf8.xml in shortkeys_utf8.xml_old.txt and remove the content of the data elememts in shortkeys_utf8.xml.
41 Other parameters are given in a properties file described below.
42
43 Default values
44
45 =over
46
47 =item *
48
49 for encoding : UTF-8
50
51 =item *
52
53 for tmp_file : tmp.txt
54
55 =item *
56
57 for properties file name : ripper.properties
58
59 =back
60
61 =cut
62
63 sub catch {
64 my %defaults = (tmp_file => "tmp.txt", encoding=> "UTF-8", properties=> "ripper.properties");
65 my %args = (%defaults, @_);
66 my $config = Config::YAML::Tiny->new( config => $args{properties} );
67 $keepsit = $config->get_tag_to_keep;
68
69 $afterstarttag = $config->get_after_start_tag;
70 $beforestarttag = $config->get_before_start_tag;
71
72 #die Dumper $keepsit;
73
74 #open( my $FH, "<:raw:utf8", $args{shk_file} )
75 open( my $FH, "<:encoding($args{encoding})", $args{shk_file} )
76 or die "Unable to open xml file: $!";
77
78 # http://blogs.msdn.com/brettsh/archive/2006/06/07/620986.aspx
79 #open (my $TF, ">:raw:encoding(UTF16-LE):crlf:utf8", $tmp_path) or die "Unable to make new temporary file: $!";
80 #with utf-8 this would suffice open( $TF, ">:encoding($args{encoding})", $args{tmp_file} )
81 # http://grokbase.com/t/perl/unicode/111hf8z9as/encoding-utf16-le-on-windows
82 # Files opened on Windows already have the :crlf layer pushed by default,
83 #so you somehow need to get the :encoding layer *below* it. If you have it on top,
84 #then the crlf substitution happens *after* the encoding, leading to incorrect data.
85 #this open( $TF, ">:encoding($args{encoding})", $args{tmp_file} ) or die "Unable to make new temporary file: $!";
86 #insert "blank" between each character, I suspect this comes from the explanation above
87 open( $TF, ">:raw:encoding($args{encoding}):crlf", $args{tmp_file} ) or die "Unable to make new temporary file: $!";
88
89
90 print $TF "\N{BOM}" if ($args{encoding} eq "UTF-8" || $args{encoding} eq "UTF-16BE");
91
92 #commentaire vide : <!-----> trois lignes sucessives font que les données ne sont plus
93 #produites. Le prochain commentaire non vide remet le compteur à zéro
94
95 $erasesignal = 2;
96 $comchain = 0;
97 # ProtocolEncoding => $args{encoding}, #with this accetended char in shortkey's name are preserved
98 my $p = XML::Parser->new(
99 ErrorContext => 2,
100 ProtocolEncoding => $args{encoding}
101 );
102
103 $p->setHandlers(
104 'Start' => \&Win32::Shortkeys::Ripper::MySubs::start,
105 'Char' => \&Win32::Shortkeys::Ripper::MySubs::char,
106 'End' => \&Win32::Shortkeys::Ripper::MySubs::end,
107 'Comment' => \&Win32::Shortkeys::Ripper::MySubs::com,
108 'Default' => \&Win32::Shortkeys::Ripper::MySubs::def,
109
110 );
111
112 $p->parse($FH);
113
114 close $FH;
115 close $TF;
116
117 #rename ("../../shortkeys.xml", "../../shortkeys.xml_old.txt") or die "can't rename $!";
118 #rename ("../../tmp.txt", "../../shortkeys.xml") or die "can't rename $!";
119
120 rename( $args{shk_file}, $args{old_file} ) or die "can't copy $!";
121 rename( $args{tmp_file}, $args{shk_file} ) or die "can't rename $!";
122
123 }
124
125 package Win32::Shortkeys::Ripper::MySubs;
126
127 #use Data::Dumper;
128
129 my $current;
130
131 sub start {
132 my ( $p, $el, %atts ) = @_;
133 my $k = $atts{k};
134 $comchain = 0 unless $comchain > 2;
135
136 my $rtl = get_rtl( $k, $beforestarttag->{$el} );
137
138 if ( $el eq "dataref" ) { $k = "dataref"; }
139
140 if ( $k && $keepsit->{$k} ) {
141 $current = $k;
142 }
143
144 #print "comments : $comchain ";
145 return if $comchain > $erasesignal;
146
147 # print "rtl : " . ($rtl eq "" ? " vide" : " retour ligne") . " el: $el\n";
148 print $TF $rtl . "<" . $el;
149 my $at;
150 foreach my $v ( keys %atts ) {
151
152 $at .= " " . $v . "= '" . $atts{$v} . "'";
153 }
154
155 print $TF $at if ($at);
156
157 #print $TF ">$end", "$afterstarttag{$el}";
158 print $TF ">" . get_rtl( $atts{k}, $afterstarttag->{$el} );
159
160 }
161
162 sub def {
163 my ( $p, $el ) = @_;
164 print $TF "$el";
165
166 }
167
168 sub end {
169 my ( $p, $el ) = @_;
170 return if $comchain > $erasesignal;
171 if ( $current && $current ne "dataref" ) {
172 $current = undef;
173 }
174 print $TF "</" . $el . ">";
175
176 }
177
178 sub char {
179 my ( $p, $s ) = @_;
180 return if $comchain > $erasesignal;
181
182 if ( $current && $keepsit->{$current} ) {
183 print $TF "$s";
184 }
185
186 }
187
188 sub com {
189 my ( $p, $s ) = @_;
190 if ( $s eq "" ) {
191 $comchain++;
192 }
193 else {
194 $comchain = 0;
195 }
196
197 print $TF "\n<!--" . $s . "-->";
198
199 }
200
201 sub get_rtl {
202 my ( $at, $mhref ) = @_;
203 my $rtl = "";
204
205 # print "el: $at ";
206 if ( $mhref && ref($mhref) eq "HASH" ) {
207
208 #my $href = $beforestarttag{$el};
209 #print "get_rtl: " . $href->{$at}. "\n" ;
210 $rtl = $mhref->{$at} if ( $at && exists ${$mhref}{$at} );
211
212 }
213 else {
214 $rtl = $mhref if ($mhref);
215 }
216
217 return $rtl;
218 }
219
220 =head1 DESCRIPTION
221
222 =head2 Properties file
223
224 It's name default to C<ripper.properties>. Use the C<properties => $file_name> to change that.
225
226 The file must follow YANL::Tiny::Simple syntax and contain three elements C<before_start_tag after_start_tag tag_to_keep>.
227
228 after_start_tag:
229 data:
230 a: "\n\n"
231 d: "\n"
232 t: "\n\n"
233 ....
234
235 dataref: ''
236 shortkey: ''
237 before_start_tag:
238 data:
239 a: "\n"
240 d: "\n"
241 t: "\n"
242 ....
243 dataref: ''
244 shortkey: ''
245 tag_to_keep:
246 a: 0
247 d: 0
248 dataref: 1
249 f: 1
250 hg: 1
251 hj: 1
252 j: 0
253 k: 0
254 l: 1
255 y: 0
256 z: 1
257
258
259 See the files in the example folder: ripper.properties is used clear shorkeys_utf8.xml.
260
261 =over
262
263 =item * before_start_tag after_start_tag
264
265 contains the sub elements C<data dataref shortkey>. The data element enumerates, using the k attribute, the element's start tag that are preceded (C<before_start_tag> or followed C<after_start_tag> by line break(s).
266
267 =item * tag_to_keep
268
269 Enumerate the data element using the value of the k attribute that you want to clear C<attribute:0> or keep unchanged C<attribute:1>. C<dataref> is also listed.
270
271 =back
272
273 =head1 SUPPORT
274
275 Any questions or problems can be posted to me (rappazf) on my gmail account.
276
277 The current state of the source can be extract using Mercurial from
278
279 L<http://sourceforge.net/projects/win32-shortkeys/>.
280
281 =head1 AUTHOR
282
283 FranE<ccedil>ois Rappaz <rappazf@gmail.com>
284
285
286 =head1 COPYRIGHT
287
288 This program is free software; you can redistribute
289 it and/or modify it under the same terms as Perl itself.
290
291 The full text of the license can be found in the
292 LICENSE file included with this module.
293
294 =cut
295
296 1;