summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/splitindex/splitindex.pl
blob: 378fb25e68c9aac61de3eeff7bd17263d070bf0d (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
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
#! /usr/bin/perl -w
eval 'exec perl -S $0 ${1+"$@"}'
    if 0; #$running_under_some_shell

# ======================================================================
# splitindex.pl
# Copyright (c) Markus Kohm, 2002
#
# $Id: splitindex.pl,v 1.5 2002/11/05 10:54:27 mjk Exp $
#
# This file is part of the SplitIndex package
#
# This file can be redistributed and/or modified under the conditions
# of the LaTeX Project Public License, either version 1.2 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.2 or later is part of all distributions of LaTeX
# version 1999/12/01 or later.
#
# You are not allowed to redistribute this file without all the
# other files of the SplitIndex package
# ======================================================================

use strict;
use Getopt::Long;

my $makeindex = "makeindex";
# my $identify = "^(.*)\\\\UseIndex *\\{([^\\}]*)\\}(.*)\$";
my $identify = "^(\\\\indexentry)\\[([^]]*)\\](.*)\$";
my $suffixis = "-\$2";
my $lineis = "\$1\$3";
my $verbose = 0;   # option verbose with default value 
my $result = GetOptions(
			'help' => sub { help() },
			'makeindex=s' => \$makeindex,
			'identify=s' => \$identify,
			'resultis=s' => \$lineis,
			'suffixis=s' => \$suffixis,
			'verbose|v+' => \$verbose,
			 'version' => sub { version(); exit 0; } 
			);

usage("missing raw index file") if ( $#ARGV < 0 );

my $indexinput = shift;
my $jobname    = ( $indexinput =~ /^(.*)\.idx$/ ) ? $1 : $indexinput;

my %idxfile;
my %linesatidxfile;

version() if ($verbose > 0);

if ( !( open (IDX,"<$indexinput") ) ) {
    if ( $indexinput ne "$jobname.idx" ) {
	open (IDX,"<$jobname.idx") ||
	    die "Cannot read raw index file $indexinput nor $jobname.idx";
	$indexinput = "$jobname.idx";
    } else {
	die "Cannot read raw index file $indexinput";
    }
}

while (<IDX>) {
    my $line;
    my $suffix;
    if ( /$identify/ ) {
	my $eval = "\$line = \"$lineis\n\"";
	eval $eval;
	$eval = "\$suffix = \"$suffixis\"";
	eval $eval;
    } else {
	$line = $_;
	$suffix = "";
	$suffix .= "$1" if ( $suffixis =~ /^(.*)\$/ );
	$suffix .= "idx";
	$suffix .= "$1" if ( $suffixis =~ /\$[123456789](.*)$/ );
    }
    while ( $suffix =~ /(^[^,]+)(.*)$/ ) {
	$suffix = $2;
	writetoidx ($1,$line);
    }
}

closeallind();

close(IDX);

generateallind(@ARGV);

exit 0;

sub generateallind {
    my $name;
    my $file;
    
    if ( $makeindex ne "" ) {
	while (($name,$file) = each %idxfile) {
	    system( "$makeindex @ARGV $jobname$name.idx" );
	}
    }
}

sub closeallind {
    my $name;
    my $file;
    my $lines;
    while (($name,$file) = each %idxfile) {
	print "Close $jobname$name.idx\n" 
	    if ( $verbose > 1 );
	close( $file );
	$idxfile{$name}=0;
    }
    if ( $verbose > 0 ) {
	print "\n";
	while (($name,$lines) = each %linesatidxfile) {
	    print "$jobname$name.idx with $lines lines\n";
	}
    }
}

sub writetoidx {
    my $suffix = $_[0];
    my $line = $_[1];
    my $file = $idxfile{$suffix};
    if ( ! $file ) {
	open ( $file, ">$jobname$suffix.idx" ) ||
	    die "Cannot write to file $jobname$suffix.idx";
	$idxfile{$suffix} = $file;
	$linesatidxfile{$suffix} = 0;
	print( "New index file $jobname$suffix.idx\n" )
	    if ( $verbose > 1 );
    }
    print ($file $line);
    $linesatidxfile{$suffix}++;
}

sub help {
    version();
    print "\n";
    usage();
    print  	
	"Split a single raw index file into multiple raw index files.\n".
	"Example: splitindex.pl foo.idx.\n".
	"\n".
	"Options:\n" .
	"  -h, --help    " .
	"\tshow this help and terminate\n" .
	"  -m, --makeindex PROGNAME\n" .
	"\t\t\tcall PROGNAME instead of default \`makeindex\'.\n" .
	"  -i, --identify EXPRESSION\n" .
	"\t\t\tuse regular EXPRESSION to match entries\n".
	"\t\t\t(see also option --resultis and --suffixis).\n".
	"\t\t\tDefault is \'$identify\'.\n".
	"  -r, --resultis PATTERN\n" .
	"\t\t\tcreate line to be written from PATTERN after matching\n".
	"\t\t\tlines (see also option --identify).\n".
	"\t\t\tDefault is \'$lineis\'.\n".
	"  -s, --suffixis PATTERN\n" .
	"\t\t\tcreate suffix to be used from PATTERN after matching\n".
	"\t\t\tlines (see also option --identify).\n".
	"\t\t\tDefault is \'$suffixis\'.\n".
	"  -v, --verbose " .
	"\tbe more verbose\n".
	"\t\t\t(can be used multiple to increase verbosity)\n" .
	"      --version " .
	"\tshow version and terminate\n";
    exit 0;
}

sub version {
    print "splitindex.pl 0.1\n" .
	"Copyright (c) 2002 Markus Kohm \<kohm\@gmx.de\>\n";
}

sub usage {
    my $text = "Usage: splitindex.pl [OPTION]... RAWINDEXFILE [MAKEINDEXOPTION]...\n";
    if ( $#_ >= 0 ) {
	print STDERR @_;
	print STDERR "\n$text";
	print STDERR "Try \`splitindex.pl --help\' for more information.\n";
	exit 1;
    } else {
	print $text;
    }
}