-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean.pl
executable file
·353 lines (267 loc) · 10.9 KB
/
clean.pl
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/perl
# Author: Tom Laudeman, Daniel Pitti
# The Institute for Advanced Technology in the Humanities
# Copyright 2013 University of Virginia. Licensed under the Educational Community License, Version 2.0
# (the "License"); you may not use this file except in compliance with the License. You may obtain a
# copy of the License at
# http://www.osedu.org/licenses/ECL-2.0
# http://opensource.org/licenses/ECL-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing permissions and limitations under the
# License.
# Originally, this script was a multiline -pi which changed the file in place. However, I couldn't get the
# utf8 pragmas to work with the BEGIN, so all that was scrapped, and now the script reads the file into
# memory, modifies it, writes a temp file, closes the temp file, and renames temp to the original.
# Change file in place. Backup original files before running this script.
# clean.pl file.xml
# Only fix strange characters, \x2013 \x2014, and non-breaking space,
# clean.pl --chars file.xml
# report on what will be done
# clean.pl --debug file.xml
# > mkdir temp
# > find /data/source/findingAids/lds/ -type f | perl -ne 'print "../clean.pl $_";' > fix.sh
# > . fix.sh
# Enable/disable various fixes by setting the if() to 1 or 0.
# Below is an example of stuff we need to comment out in EAD files. We aren't using the DTD. We aren't doing
# includes.
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE ead PUBLIC "+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN" "http://www.lib.usf.edu/ldsu/dtd/ead.dtd">
# <ead audience="external" relatedencoding="MARC21">
# Need utf8 so that regex will match. Using the utf8 pragma with stdin solved all of the other problems.
# The use open qw(:std :utf8); is a powerful pragma required to make Perl read stdin as utf8.
# See http://stackoverflow.com/questions/519309/how-do-i-read-utf-8-with-diamond-operator
use strict;
use utf8;
use open qw(:std :utf8);
use Getopt::Long;
my $debug = 0;
my $full = 0;
my $chars_only = 0;
my $verbose = 0;
my @remove = ("\x200f");
# Used to fix non-standard character entities. Hash keys are case sensitive.
my %ent = ('eacute' => 'é',
'Eacute' => 'É',
'copy' => '©',
'and' => '&',
'Uuml' => 'Ü',
'uuml' => 'ü',
'aacute' => 'á',
'oacute' => 'ó',
'Oacute' => 'Ó',
'nbsp' => ' ',
'egrave' => 'è',
'beta' => 'β',
'ntilde' => 'ñ',
'ccedil' => 'ç'
);
main();
sub main
{
$| = 1; # unbuffer stdout
GetOptions( 'debug!' => \$debug, 'chars!' => \$chars_only, 'verbose!' => \$verbose);
if (! $chars_only)
{
$full = 1;
}
my $out;
my $tmp_fn = "tmp_$$.txt";
if (! $debug)
{
open($out, ">", $tmp_fn) || die "Cant' open $tmp_fn for write\n";
}
$_ = read_file($ARGV[0]);
work();
if (! $debug)
{
print $out $_;
close($out);
rename($tmp_fn, $ARGV[0]);
}
}
sub work
{
my $address; # = read_file("vt_address.xml");
my $contact; # = read_file("vt_contact.xml");
if ($verbose || $debug)
{
print "file: $ARGV[0]\n";
}
if ($debug)
{
# \x200F, right-to-left character
if ($_ =~ m/\x{200f}/sm)
{
print "clean: Will remove <U+200F> \x{200f} the right-to-left character.\n";
}
# Replace any unicode hex 2014 em dash, also hex 2013 en dash with a hyphen.
# xlf /data/source/findingAids/nwda/oregon_historical_society_research_library/OHYMSS1609.xml
# <persname role="subject" encodinganalog="600" rules="aacr2" source="local">Huber, Oskar—Correspondence</persname>
if ($_ =~ m/—|–/smg)
{
print "clean: Will change hex 2014 em dash to ascii hyphen\n";
}
if ($_ =~ m/–/smg)
{
print "clean: Will change hex 2013 en dash to ascii hyphen\n";
}
# We always want to remove the namespace in the <ead> element
if ($_ =~ /(<ead[^>]*xmlns=")(.*?)(")/smg)
{
print "clean: Will remove namespace in the <ead> element\n";
}
# fix any non-standard entities we encounter
if ($_ =~ m/\&([A-Za-z]+?)\;/sm && exists($ent{$1}))
{
print "clean: Will fix entity $1\n";
}
# 4th generation due to incomplete or buggy previous fixes. This code removes comments between
# <?xml..> and <ead...> and comments out the entire region.
# s/// is not greedy without /g since it only substitues one instance of a match. Trim whitespace
# before fixing $proci.
# This was run repeatedly on files and after the first fix makes no changes to already fixed files.
# $_ =~ s/(<?xml.*?>)(.*?)(<ead.*?>)/$1fix-data-here$3/sm;
# \s doesn't seem to make sense. We only do the XML header if there are non whitespace chars.
# May 12 2015 this didn't work to print the debug message. I'm pretty sure we are looking for a
# mixture of \S and \s, and regex are greedy, so we'll get as much of both as possible between ?> and
# <ead. if ($_ =~ m/(<\?xml.*?>)([\S]+?)(<ead.*?>)/sm)
if ($_ =~ m/(<\?xml.*?>)([\S\s]+?)(<ead.*?>)/sm)
{
# Intentionally put the unwanted header on a separate line. Some have a prefixed \n and some
# don't, but if we want to use | sort | uniq -c we want the message to be consistent.
print "clean: Will comment out unwanted XML headers:\n$2\n";
}
if ($_ =~ m/\240/sm)
{
print "clean: Will change non-breaking space (decimal 160 octal 240) to space\n";
}
exit();
}
if (1 && ! $debug && ($full || $chars_only))
{
# Remove the pesky non-printing right-to-left character
# jun 26 2015. This is tricky because the offending character doesn't print. Happily, linux "less"
# displays it as <U+200F>, and emacs will edit it in utf8 mode, although the character doesn't really
# display.
$_ =~ s/\x{200f}//smg;
}
if (1 && ! $debug && ($full || $chars_only))
{
# Remove any of those darned non-breaking spaces. Most of the downstream code won't handle them
# properly, even though they seem to match \s+
$_ =~ s/\240/ /smg;
}
if (1 && ! $debug && ($full || $chars_only))
{
# Replace any unicode hex 2014 em dash, also hex 2013 en dash with a hyphen.
# xlf /data/source/findingAids/nwda/oregon_historical_society_research_library/OHYMSS1609.xml
# <persname role="subject" encodinganalog="600" rules="aacr2" source="local">Huber, Oskar—Correspondence</persname>
$_ =~ s/—|–/-/smg;
}
if (0 && ! $debug)
{
# We always want to remove the namespace in the <ead> element
$_ =~ s/(<ead[^>]*xmlns=")(.*?)(")/$1$3/smg;
}
if (0 && ! $debug)
{
# Not a new generation of fixes to doctype, but instead fixing &address and &contact in VT aka vah xml
# files.
$_ =~ s/(\&address;)/$address/;
$_ =~ s/\&contact;/$contact/;
}
if (0 && ! $debug)
{
# fix any non-standard entities we encounter
if ($_ =~ m/\&([A-Za-z]+?)\;/sm && exists($ent{$1}))
{
$_ =~ s/\&([A-Za-z]+?)\;/$ent{$1}/smg;
}
}
if (0 && ! $debug)
{
# Special cleaning because someone created a URL "ead.lib.virginia.edu:" (notice the trailing colon)
# which should be "ead.lib.virginia.edu".
$_ =~ s/ead.lib.virginia.edu:/ead.lib.virginia.edu/smg;
}
if (1 && ! $debug)
{
# 4th generation due to incomplete or buggy previous fixes. This code removes comments between
# <?xml..> and <ead...> and comments out the entire region.
# s/// is not greedy without /g since it only substitues one instance of a match. Trim whitespace
# before fixing $proci.
# This was run repeatedly on files and after the first fix makes no changes to already fixed files.
# $_ =~ s/(<?xml.*?>)(.*?)(<ead.*?>)/$1fix-data-here$3/sm;
# May 12 2015 Comment out a non-greedy combination of \S and \s between ?> and <ead. I suspect .+? would work just
# as well.
if ($_ =~ m/<\?xml.*?>([\S\s]+?)<ead.*?>/sm)
{
my $proci = $1;
$proci =~ s/^\s+//smg;
$proci =~ s/\s+$//smg;
$proci =~ s/<\!--\s*//smg;
$proci =~ s/\s*-->//smg;
if ($proci)
{
$_ =~ s/(<\?xml.*?>)([\S\s]+?)(<ead.*?>)/$1<!-- $proci -->$3/sm;
# The old substitution which always added an XML comment
# $_ =~ s/fix-data-here/<!-- $proci -->/sm;
}
# Else after trimming whitespace and XML comments there was nothing left, so nothing to do, and we
# certainly don't want to add a pointless XML comment.
}
}
if (0 && ! $debug)
{
die "3 gen";
# <!-- DOCTYPE ead PUBLIC "+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN" "./dtds/ead.dtd" [<!ENTITY JSP_1.2_Tag_Library_Descriptor SYSTEM "file:/C:/Program%20Files/Oxygen%20XML%20Editor%2012/templates/JSP%201.2%20Tag%20Library%20Descriptor.xml" -->
# ]>
if ($_ =~ m/[<![^\]>]*?-->[^\]>]*?\]>/sm)
{
$_ =~ s/<!-- (DOCTYPE.*?) -->.*?]>/<!-- $1] -->/sm;
}
# else do nothing.
}
if (0 && ! $debug)
{
die "2 gen";
# Version 2 when I discovered 986 files like this:
# <?DOCTYPE ead PUBLIC "+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN" " "?>
$_ =~ s/<\?(DOCTYPE.*?)>/<!-- $1 -->/sm;
}
if (0 && ! $debug)
{
die "1 gen";
$_ =~ s/<!(DOCTYPE.*?)>/<!-- $1 -->/sm;
}
} # main
sub read_file
{
my @stat_array = stat($_[0]);
if ($#stat_array < 7)
{
die "read_file: File $_[0] not found\n";
}
my $buffer;
# It is possible that someone will ask us to open a file with a leading space.
# That requires separate args for the < and for the file name.
# It also works for files with trailing space.
if (! open(IN, "<", $_[0]))
{
die "Could not open $_[0] $!\n";
}
my $full = "";
while(sysread(IN, $buffer, 100000))
{
$full .= $buffer;
}
close(IN);
return $full;
}
# old, pre-pi version
# while(my $temp = <>)
# {
# $temp =~ s/<!(DOCTYPE.*?)>/<!-- $1 -->/;
# print $temp;
# }