summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/hyperref/psdmapshortnames.pl
blob: 4af924c932dc3c2e701770d5e05fed2c8bd759f1 (plain)
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
#!/usr/bin/env perl
use strict;
$^W=1;

# File: psdmapshortnames.pl
# Date: 2012-07-12
# Copyright (c) 2012 by Heiko Oberdiek.
#
# This file is part of the `Hyperref Bundle'.
# -------------------------------------------
#
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license or (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# and version 1.3 or later is part of all distributions of LaTeX
# version 2005/12/01 or later.
#
# This work has the LPPL maintenance status `maintained'.
#
# The Current Maintainer of this work is Heiko Oberdiek.
#
# The list of all files belonging to the `Hyperref Bundle' is
# given in the file `manifest.txt'.

my $file_org = 'hyperref.dtx';
my $file_bak = 'hyperref.dtx.bak';
my $file_tmp = 'hyperref.dtx.tmp';

my @lines_map;
my $cmd_map = 'psdmapshortnames';
my $found_map_beg = 0;
my $found_map_end = 0;

my @lines_alias;
my $cmd_alias = 'psdaliasnames';
my $found_alias_beg = 0;
my $found_alias_end = 0;

open(IN, '<', $file_org) or die "!!! Error: Cannot open `$file_org'!\n";
binmode(IN);
unlink $file_tmp if -f $file_tmp;
open(OUT, '>', $file_tmp) or die "!!! Error: Cannot open `$file_org'!\n";
binmode(OUT);

while (<IN>) {
    print OUT;
    if (/^\\newcommand\*\{\\$cmd_map\}\{\%\s*$/) {
        $found_map_beg = 1;
        print OUT @lines_map;
        while (<IN>) {
            if (/^\}\% \\$cmd_map$/) {
                print OUT;
                $found_map_end = 1;
                last;
            }
            if (/^\}/) {
                print OUT;
                last;
            }
        }
    }
    if (/^\\DeclareTextCommand\{\\text(\w+)\}\{PU\}\{[\\\d\w]+\}\%\*/) {
        my $name = $1;
        push @lines_map, "  \\let\\$name\\text$name\n";
    }
    
    if (/^\\newcommand\*\{\\$cmd_alias\}\{\%\s*$/) {
        $found_alias_beg = 1;
        print OUT @lines_alias;
        while (<IN>) {
            if (/^\}\% \\$cmd_alias$/) {
                print OUT;
                $found_alias_end = 1;
                last;
            }
            if (/^\}/) {
                print OUT;
                last;
            }
        }
    }
    if (/^%\* \\([A-Za-z@]+)\s+->\s+\\(\w+)(\s|$)/) {
        my $name_old = $1;
        my $name_new = $2;
        push @lines_alias, "  \\let\\$name_new\\$name_old\n";
    }
}

close(IN);
close(OUT);

$found_map_beg or die "!!! Error: Definition for \\$cmd_map not found!\n";
$found_map_end or die "!!! Error: End of \\$cmd_map not found!\n";

$found_alias_beg or die "!!! Error: Definition for \\$cmd_alias not found!\n";
$found_alias_end or die "!!! Error: End of \\$cmd_alias not found!\n";

my $count_map = @lines_map;
print "* $count_map map entries found.\n";

my $count_alias = @lines_alias;
print "* $count_alias alias entries found.\n";

use Digest::MD5;

open(IN, '<', $file_org) or die "!!! Error: Cannot open `$file_org'!\n";
binmode(IN);
my $md5_org = Digest::MD5->new->addfile(*IN)->hexdigest;
close(IN);
print "* 0x$md5_org = md5($file_org)\n";

open(IN, '<', $file_tmp) or die "!!! Error: Cannot open `$file_tmp'!\n";
binmode(IN);
my $md5_tmp = Digest::MD5->new->addfile(*IN)->hexdigest;
close(IN);
print "* 0x$md5_tmp = md5($file_tmp)\n";

if ($md5_org eq $md5_tmp) {
    print "* Done, nothing to do.\n";
    exit(0);
}

unlink $file_bak if -f $file_bak;
rename $file_org, $file_bak or die "!!! Error: Moving `$file_org' to `$file_bak' failed!\n";
print "* $file_org -> $file_bak\n";
rename $file_tmp, $file_org or die "!!! Error: Moving `$file_tmp' to `$file_org' failed!\n";
print "* $file_tmp -> $file_org\n";

print "* Done.\n";

__END__