3 # Script for producing the Index for the Exim manual from the output of the
4 # SGCAL run. This is copied from the script for the Exim book.
7 ##############################################################################
8 # Patterns for matching things to be removed from the sort keys
10 # This was copied from the Exim book processor, but we have now found a
11 # better way of doing this. Leave the code until I am quite sure...
13 # $pat[0] = qr/ \(\\\*see also\*\\[^)]+\)/;
14 # $pat[1] = qr/(?<!@)\/\//; # //
15 # $pat[2] = qr/(?<!@)\/\\/; # /\
16 # $pat[3] = qr/(?<!@)\\\//; # \/
17 # $pat[4] = qr/(?<!@) \\ # non-@ \, followed by one of
19 # [\.\/] | # dot or slash
24 # \([.\/]? | # ( and optional . or slash
25 # [[\$\\%?!-"] | # [ $ \ % ! " or -
27 # \^{1,2}\/? # ^ or ^^ and optional slash
29 # $pat[5] = qr/(?: []\$\\%)?!"] | # ] $ \ % ) ? " or ! )
30 # \*{1,2} | # * or ** ) optional
31 # \^{1,2})? # ^ or ^^ )
33 # $pat[6] = qr/(?<!@)::/;
34 # $pat[7] = qr/\sR[FS]\b/;
40 # $pat[13] = qr/\(e?s\)/;
45 # $keysplit = qr/^(.*?)(\|\|.*?)?\s(R[AZ])?\s?(\d+)$/;
47 $keysplit = qr/^(.*?)(\@\|\@\|.*?)?\s(R[AZ])?\s?(\d+)$/;
55 ############old#############
56 #foreach $pattern (@pat) # Remove strings by pattern
58 # $x =~ s/$pattern//g;
59 # $y =~ s/$pattern//g;
61 ##########################
69 # Remove all special characters, except those preceded by @
71 $x =~ s/(?<!\@)[^\w\@\s]//g;
72 $y =~ s/(?<!\@)[^\w\@\s]//g;
74 # Remove the escaping @s
81 ################old ########################
82 #$x =~ s/:(\w+):/$1/g; # :fail: etc => fail
83 #$y =~ s/:(\w+):/$1/g;
85 #$x =~ s/^\@[^a-z]+/\@/i; # Make keys starting with @
86 #$y =~ s/^\@[^a-z]+/\@/i; # sort on @ followed by the first letter
87 ##############################################3
90 $x =~ s/\@_/\x7f/g; # Make underscore sort late (option names)
93 # Split up to sort on individual parts
95 my($xp,$xs,$xr,$xn) = $x =~ /$keysplit/;
96 my($yp,$ys,$yr,$yn) = $y =~ /$keysplit/;
98 $xr = "" if !defined $xr;
99 $yr = "" if !defined $yr;
101 $xs = "" if !defined $xs;
102 $ys = "" if !defined $ys;
106 print "a=$a\n x=$x\n xp=$xp\n xs=$xs\n xr=$xr\n xn=$xn\n";
107 print "b=$b\n y=$y\n yp=$yp\n ys=$ys\n yr=$yr\n yn=$yn\n";
110 my ($c) = "\L$xp" cmp "\L$yp"; # Caseless, primary text only
111 $c = $xp cmp $yp if $c == 0; # Caseful, primary text only
112 $c = "\L$xs" cmp "\L$ys" if $c == 0; # Caseless, secondary text only
113 $c = $xs cmp $ys if $c == 0; # Caseful, secondary text only
114 $c = $xn <=> $yn if $c == 0; # Compare the numbers
115 $c = $xr cmp $yr if $c == 0; # Sort RA before RZ
121 ##############################################################################
122 # Function for getting the next line from the @lines vector, using the global
123 # index $1. If the next pair of lines specifies a range of pages, combine them.
124 # That's why $linenumber has to be global - so we can increment it. If there's
125 # a range error, return "".
128 my($line) = $lines[$linenumber];
129 my($aa,$zz,$tline,$nextline,$tnextline);
131 if ($line =~ / RA (\d+)/)
134 $nextline = $lines[++$linenumber];
135 if ($nextline =~ / RZ (\d+)/)
141 print STDERR "** Bad range data (1)\n";
142 print STDERR " $line\n";
143 print STDERR " $nextline\n";
148 $tnextline = $nextline;
150 $tline =~ s/ RA \d+//;
151 $tnextline =~ s/ RZ \d+//;
153 if ($tline ne $tnextline)
155 print STDERR "** Bad range data (2)\n";
156 print STDERR " $line\n";
157 print STDERR " $nextline\n";
161 $line = ($aa eq $zz)? "$tline $aa" : "$tline $aa--$zz";
164 elsif ($line =~ / RZ (\d+)/)
166 print STDERR "** Bad range data (RZ without RA)\n";
167 print STDERR " $line\n";
177 ##############################################################################
178 # Function for outputting a line, checking for the current primary
179 # and indenting a bit for secondaries. We also need a newpar
180 # before each item, because the main indent is set to a largish indent
181 # for long reference lists, but the parindent is set to counter this.
182 # This is where we handle the break between letters. We know that any non-
183 # alphamerics at the start of lines are markup, except for @. A reference
184 # value of 99999 is for the "see also" lines. Suppress it.
187 my($text,$ref) = ($_[0],$_[1]);
188 my ($letter) = $text =~ /^[^A-Za-z0-9\@]*(.)/;
190 return if $text =~ /^\s*$/;
192 if ($ref eq "99999") # dummy for see also
198 $ref = "#$ref"; # prepend space
201 if ($letter =~ /\d/) { $letter = "0"; } else { $letter = "\U$letter"; }
203 print OUT ".newpar\n";
205 if ($letter ne $currentletter && $letter ge "A")
207 print OUT ".newletter\n";
208 $currentletter = $letter;
211 $text =~ s/\@'/\$'/g; # Turns @' into $' so that it prints a non-curly quote
213 if ($text =~ /^(.+)\|\|(.*)$/)
215 my($primary,$secondary) = ($1,$2);
217 if ($primary ne $lastprimary)
219 print OUT ".primary $primary\n";
220 $lastprimary = $primary;
223 $primary =~ s/"/""/g;
224 $secondary =~ s/"/""/g;
226 my($contprim) = $primary;
227 $contprim =~ s/ \(\\\*see also\*\\[^)]+\)//;
229 print OUT ".secondary \"$primary\" \"$secondary$ref\" \"$contprim\"\n";
232 # Not a two-part item; insert @ if the first char is a dot
236 print OUT "@" if $text =~ /^\./;
237 print OUT "$text$ref\n";
238 $lastprimary = $text;
246 ##############################################################################
255 my($arg) = shift @ARGV;
256 if ($arg eq "-k") { $show_keys = 1; }
257 elsif ($arg eq "-s") { $save_sorted = 1; }
258 elsif ($arg eq "-t") { $test_index = $save_sorted = 1; }
259 else { die "Unknown option $arg\n"; }
264 open(IN, "z-testindex") || die "Can't open z-testindex\n";
268 open(IN, "z-rawindex") || die "Can't open z-rawindex\n";
271 open(OUT, ">z-index") || die "Can't open z-index\n";
273 # Extract index lines ($e lines are contents). Until we hit the first
274 # $e line, we are dealing with "see also" index lines, for which we want
275 # to turn the line number into 99999.
289 s/(\D)$/$1 99999/ if $prestuff; # No number in "see also"
292 $index_pagenumber = $1 if /^Index\$e(\d+)/;
296 # Sort, ignoring markup
298 print STDERR "Sorting ...\n";
299 @lines = sort cf @lines;
301 # Keep a copy of the sorted data, for reference
305 open(X, ">z-indexsorted") || die "Can't open z-indexsorted\n";
306 foreach $line (@lines)
313 # Heading for the index file
317 .linelength ~~sys.linelength + 16.0
327 \$c [~~sys.pagenumber]
334 .macro primary "text"
335 .if ~~sys.leftonpage < 2ld
342 .macro secondary "prim" "sec" "contprim"
343 .if ~~sys.leftonpage < 1ld
346 ~~3 \$it\{(continued)\}
353 .if ~~sys.leftonpage < 4ld
362 .page $index_pagenumber
369 # Process the lines and output the result.
370 # Note that $linenumber is global, and is changed by getnextentry() for
371 # pairs of lines that represent ranges.
375 $currenttext = $currentref = "";
379 print STDERR "Processing ...\n";
381 for ($linenumber = 0; $linenumber < @lines; $linenumber++)
383 $line = &getnextentry();
385 if ($line eq "") # Bad range data - but carry on to get all of it
391 # Split off the text and reference
393 ($text,$ref) = $line =~ /^(.*)\s+([\d-]+)$/;
395 # If same as current text, just add the new reference, unless its a duplicate
397 if ($text eq $currenttext)
399 if ($ref ne $lastref)
401 $currentref .= ", $ref";
407 # Not the same as the current text. Output the current text, then
408 # set up a new current.
410 &outline($currenttext, $currentref);
412 $currenttext = $text;
413 $currentref = $lastref = $ref;
416 # Output the final line and close the file
418 &outline($currenttext, $currentref);
421 die "** Aborted\n" if $badrange;
425 system("sgcal z-index -to zi-gcode -index /dev/null");
426 system("sgtops zi-gcode -to zi-ps");
427 print "PostScript in zi-ps\n";