| 1 |
#! /usr/bin/perl -w |
| 2 |
eval 'exec perl -S $0 ${1+"$@"}' |
| 3 |
if 0; #$running_under_some_shell |
| 4 |
|
| 5 |
# ====================================================================== |
| 6 |
# splitindex.pl |
| 7 |
# Copyright (c) Markus Kohm, 2002-2009 |
| 8 |
# |
| 9 |
# $Id: splitindex.pl,v 1.3 2009-03-20 14:55:11 mjk Exp $ |
| 10 |
# |
| 11 |
# This file is part of the SplitIndex bundle. |
| 12 |
# |
| 13 |
# This work may be distributed and/or modified under the conditions of |
| 14 |
# the LaTeX Project Public License, version 1.3c of the license. |
| 15 |
# The latest version of this license is in |
| 16 |
# http://www.latex-project.org/lppl.txt |
| 17 |
# and version 1.3c or later is part of all distributions of LaTeX |
| 18 |
# version 2005/12/01 or later and of this work. |
| 19 |
# |
| 20 |
# This work has the LPPL maintenance status "author-maintained". |
| 21 |
# |
| 22 |
# The Current Maintainer and author of this work is Markus Kohm. |
| 23 |
# |
| 24 |
# The list of all files belongig to the SplitIndex bundle is given in |
| 25 |
# in the file `manifest.txt'. Files generated by means of unpacking the |
| 26 |
# distribution (using, for example, the docstrip program) or by means |
| 27 |
# of compiling them from a source file, for example, from splitindex.c |
| 28 |
# or splitindex.java may be distributed at the distributor's discretion. |
| 29 |
# However if they are distributed then a copy of the SplitIndex bundle |
| 30 |
# must be distributed together with them. |
| 31 |
# |
| 32 |
# The list of derived (unpacked or compiled) files belongig to the |
| 33 |
# distribution and covered by LPPL is defined by the unpacking scripts |
| 34 |
# (with extension .ins) and the installation script (with name |
| 35 |
# install.sh) which are part of the distribution. |
| 36 |
# |
| 37 |
# Two often ignorred clauses from LPPL 1.3c you should not ignore: |
| 38 |
# ---------------------------------------------------------------- |
| 39 |
# 2. You may distribute a complete, unmodified copy of the Work as you |
| 40 |
# received it. Distribution of only part of the Work is considered |
| 41 |
# modification of the Work, and no right to distribute such a Derived |
| 42 |
# Work may be assumed under the terms of this clause. |
| 43 |
# 3. You may distribute a Compiled Work that has been generated from a |
| 44 |
# complete, unmodified copy of the Work as distributed under Clause 2 |
| 45 |
# above, as long as that Compiled Work is distributed in such a way that |
| 46 |
# the recipients may install the Compiled Work on their system exactly |
| 47 |
# as it would have been installed if they generated a Compiled Work |
| 48 |
# directly from the Work. |
| 49 |
# ====================================================================== |
| 50 |
|
| 51 |
use strict; |
| 52 |
use Getopt::Long; |
| 53 |
|
| 54 |
my $makeindex = "makeindex"; |
| 55 |
# my $identify = "^(.*)\\\\UseIndex *\\{([^\\}]*)\\}(.*)\$"; |
| 56 |
my $identify = "^(\\\\indexentry)\\[([^]]*)\\](.*)\$"; |
| 57 |
my $suffixis = "-\$2"; |
| 58 |
my $lineis = "\$1\$3"; |
| 59 |
my $verbose = 0; # option verbose with default value |
| 60 |
my $result = GetOptions( |
| 61 |
'help' => sub { help() }, |
| 62 |
'makeindex=s' => \$makeindex, |
| 63 |
'identify=s' => \$identify, |
| 64 |
'resultis=s' => \$lineis, |
| 65 |
'suffixis=s' => \$suffixis, |
| 66 |
'verbose|v+' => \$verbose, |
| 67 |
'version' => sub { version(); exit 0; } |
| 68 |
); |
| 69 |
|
| 70 |
usage("missing raw index file") if ( $#ARGV < 0 ); |
| 71 |
|
| 72 |
my $indexinput = shift; |
| 73 |
my $jobname = ( $indexinput =~ /^(.*)\.idx$/ ) ? $1 : $indexinput; |
| 74 |
|
| 75 |
my %idxfile; |
| 76 |
my %linesatidxfile; |
| 77 |
|
| 78 |
version() if ($verbose > 0); |
| 79 |
|
| 80 |
if ( !( open (IDX,"<$indexinput") ) ) { |
| 81 |
if ( $indexinput ne "$jobname.idx" ) { |
| 82 |
open (IDX,"<$jobname.idx") || |
| 83 |
die "Cannot read raw index file $indexinput nor $jobname.idx"; |
| 84 |
$indexinput = "$jobname.idx"; |
| 85 |
} else { |
| 86 |
die "Cannot read raw index file $indexinput"; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
while (<IDX>) { |
| 91 |
my $line; |
| 92 |
my $suffix; |
| 93 |
if ( /$identify/ ) { |
| 94 |
my $eval = "\$line = \"$lineis\n\""; |
| 95 |
eval $eval; |
| 96 |
$eval = "\$suffix = \"$suffixis\""; |
| 97 |
eval $eval; |
| 98 |
} else { |
| 99 |
$line = $_; |
| 100 |
$suffix = ""; |
| 101 |
$suffix .= "$1" if ( $suffixis =~ /^(.*)\$/ ); |
| 102 |
$suffix .= "idx"; |
| 103 |
$suffix .= "$1" if ( $suffixis =~ /\$[123456789](.*)$/ ); |
| 104 |
} |
| 105 |
while ( $suffix =~ /(^[^,]+)(.*)$/ ) { |
| 106 |
$suffix = $2; |
| 107 |
writetoidx ($1,$line); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
closeallind(); |
| 112 |
|
| 113 |
close(IDX); |
| 114 |
|
| 115 |
generateallind(@ARGV); |
| 116 |
|
| 117 |
exit 0; |
| 118 |
|
| 119 |
sub generateallind { |
| 120 |
my $name; |
| 121 |
my $file; |
| 122 |
|
| 123 |
if ( $makeindex ne "" ) { |
| 124 |
while (($name,$file) = each %idxfile) { |
| 125 |
system( "$makeindex @ARGV $jobname$name.idx" ); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
sub closeallind { |
| 131 |
my $name; |
| 132 |
my $file; |
| 133 |
my $lines; |
| 134 |
while (($name,$file) = each %idxfile) { |
| 135 |
print "Close $jobname$name.idx\n" |
| 136 |
if ( $verbose > 1 ); |
| 137 |
close( $file ); |
| 138 |
$idxfile{$name}=0; |
| 139 |
} |
| 140 |
if ( $verbose > 0 ) { |
| 141 |
print "\n"; |
| 142 |
while (($name,$lines) = each %linesatidxfile) { |
| 143 |
print "$jobname$name.idx with $lines lines\n"; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
sub writetoidx { |
| 149 |
my $suffix = $_[0]; |
| 150 |
my $line = $_[1]; |
| 151 |
my $file = $idxfile{$suffix}; |
| 152 |
if ( ! $file ) { |
| 153 |
open ( $file, ">$jobname$suffix.idx" ) || |
| 154 |
die "Cannot write to file $jobname$suffix.idx"; |
| 155 |
$idxfile{$suffix} = $file; |
| 156 |
$linesatidxfile{$suffix} = 0; |
| 157 |
print( "New index file $jobname$suffix.idx\n" ) |
| 158 |
if ( $verbose > 1 ); |
| 159 |
} |
| 160 |
print ($file $line); |
| 161 |
$linesatidxfile{$suffix}++; |
| 162 |
} |
| 163 |
|
| 164 |
sub help { |
| 165 |
version(); |
| 166 |
print "\n"; |
| 167 |
usage(); |
| 168 |
print |
| 169 |
"Split a single raw index file into multiple raw index files.\n". |
| 170 |
"Example: splitindex.pl foo.idx.\n". |
| 171 |
"\n". |
| 172 |
"Options:\n" . |
| 173 |
" -h, --help " . |
| 174 |
"\tshow this help and terminate\n" . |
| 175 |
" -m, --makeindex PROGNAME\n" . |
| 176 |
"\t\t\tcall PROGNAME instead of default \`makeindex\'.\n" . |
| 177 |
" -i, --identify EXPRESSION\n" . |
| 178 |
"\t\t\tuse regular EXPRESSION to match entries\n". |
| 179 |
"\t\t\t(see also option --resultis and --suffixis).\n". |
| 180 |
"\t\t\tDefault is \'$identify\'.\n". |
| 181 |
" -r, --resultis PATTERN\n" . |
| 182 |
"\t\t\tcreate line to be written from PATTERN after matching\n". |
| 183 |
"\t\t\tlines (see also option --identify).\n". |
| 184 |
"\t\t\tDefault is \'$lineis\'.\n". |
| 185 |
" -s, --suffixis PATTERN\n" . |
| 186 |
"\t\t\tcreate suffix to be used from PATTERN after matching\n". |
| 187 |
"\t\t\tlines (see also option --identify).\n". |
| 188 |
"\t\t\tDefault is \'$suffixis\'.\n". |
| 189 |
" -v, --verbose " . |
| 190 |
"\tbe more verbose\n". |
| 191 |
"\t\t\t(can be used multiple to increase verbosity)\n" . |
| 192 |
" --version " . |
| 193 |
"\tshow version and terminate\n"; |
| 194 |
exit 0; |
| 195 |
} |
| 196 |
|
| 197 |
sub version { |
| 198 |
print "splitindex.pl 0.1\n" . |
| 199 |
"Copyright (c) 2002 Markus Kohm \<kohm\@gmx.de\>\n"; |
| 200 |
} |
| 201 |
|
| 202 |
sub usage { |
| 203 |
my $text = "Usage: splitindex.pl [OPTION]... RAWINDEXFILE [MAKEINDEXOPTION]...\n"; |
| 204 |
if ( $#_ >= 0 ) { |
| 205 |
print STDERR @_; |
| 206 |
print STDERR "\n$text"; |
| 207 |
print STDERR "Try \`splitindex.pl --help\' for more information.\n"; |
| 208 |
exit 1; |
| 209 |
} else { |
| 210 |
print $text; |
| 211 |
} |
| 212 |
} |