-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathpatch-replaceIDs.pl
More file actions
executable file
·59 lines (47 loc) · 1.66 KB
/
patch-replaceIDs.pl
File metadata and controls
executable file
·59 lines (47 loc) · 1.66 KB
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
#!/usr/bin/env perl
use warnings;
use strict;
use utf8;
use open ':utf8';
binmode(STDIN,'utf8');
binmode(STDOUT,'utf8');
binmode(STDERR,'utf8');
my ($help, $code, $ids_file);
sub usage
{
print STDERR ("Usage: patch-replaceIDs.pl -prefix <PrefixForIDs> -ids <ListOfIDsForReplacement>\n");
print STDERR (" read input from stdin and extend all references and xml:id\n");
print STDERR (" that are mentioned in <ListOfIDsForReplacement>\n");
print STDERR (" with prefix '<PrefixForIDs>-'\n");
print STDERR ("warning (known bug): tei prefixes are ignored when replacing references\n");
}
use Getopt::Long;
GetOptions
(
'help' => \$help,
'prefix=s' => \$code,
'ids=s' => \$ids_file,
);
if ($help || not($code && $ids_file)) {
&usage;
exit;
}
open my $fh, '<:utf8', $ids_file or die "Cannot open $ids_file: $!";
chomp(my @keys = <$fh>);
close $fh;
my $input = do { local $/; <STDIN> };
# fixing references
$input =~ s{
(\s(?:active|adj|adjFrom|adjTo|ana|calendar|change|children|class|code|copyOf|corresp|datcat|datingMethod|datingPoint|decls|domains|edRef|end|exclude|fVal|facs|feats|filter|follow|fromUnit|given|hand|inst|lemmaRef|location|mergedIn|mutual|new|next|nymRef|origin|parent|parts|passive|perf|period|prev|ref|rendition|require|resp|sameAs|scheme|scriptRef|select|since|source|spanTo|start|synch|target|targetEnd|toUnit|toWhom|unitRef|uri|url|valueDatcat|where|who|wit)="[^"]*")
}{
my $refs = $1;
foreach my $key (@keys) {
$refs =~ s/(?<=\s|")#\Q$key\E(?=\s|")/#$code-$key/g;
}
$refs;
}gex;
# fixing IDs
foreach my $key (@keys) {
$input =~ s/(\sxml:id=")(\Q$key\E")/$1$code-$2/;
}
print $input;