summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/stex/bin/convert-paths
blob: 55ee72a237064fbcd978f495bd93a40b32c9f10c (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
#!/usr/bin/perl -w
# 
# A script that converts paths in sTeX files from the old \Foosnipppath{bar}
# to \snippets{foo/bar}, according to the definition of those path macros in
# paths.sty.
#
# Syntax: convert-paths path/to/paths.sty FILES
#
# © 2008 Christoph Lange, KWARC, Jacobs University

use File::Copy;

my %map;

my $SNIPPATHRE = '\\\\[[:alnum:]]+(?i:snip|pic)path';

if (open(P, shift(@ARGV))) {
    print STDERR "Mapping:\n";
    while (<P>) {
	if (/^\\def($SNIPPATHRE)#1\{(\\KWARCslides\{[^#]+)#1\}/o) {
	    printf STDERR "\t%s -> %s\n", $1, $2;
	    $map{$1} = $2;
	}
    }
    close(P);
}
else {
    die "Could not open paths.sty";
}

print STDERR "Processing files:\n";
while ($#ARGV >= 0) {
    my $filename = shift(@ARGV);
    print STDERR "\t$filename\n";
    copy($filename, "$filename~") || print STDERR "\t\tWARN: could not create backup\n";
    if (open(T, $filename)) {
	# slurp the file into a string
	local $/;
	$data = <T>;
	$data =~ s/($SNIPPATHRE)\{([^}]+)\}/
	    $map{$1} ? "$map{$1}$2\}"
		: ((print STDERR "\t\tWARN: could not replace $1\n"), "$1\{$2}")
	/oxge;
	close T;
	if (open(T, ">$filename")) {
	    print T $data;
	    close T;
	}
	else {
	    print STDERR "\t\tWARN: could not save replacement\n";
	}
    }
    else {
	print STDERR "\t\tWARN: could not open file\n";
    }
}