Debian Codegraph @

texk/web2c/cwebdir/twill-refsort

raw
1 #!/usr/bin/env perl
2 # $Id: twill-refsort 69812 2024-02-11 23:09:24Z karl $
3 # Public domain. Originally written by Andreas Scherer, 2023.
4
5 use strict;
6 use warnings;
7
8 use File::Basename;
9 use Getopt::Long qw(:config no_ignore_case bundling);
10 use Pod::Usage;
11
12 my $progname;
13 my $collator;
14
15 BEGIN {
16 $progname = basename $0;
17 # Unicode::Collate has been around a long time,
18 # but it's not part of core Perl.
19 eval {
20 require Unicode::Collate;
21 $collator = Unicode::Collate->new();
22 };
23 }
24
25 my $usage = "Usage: $progname < foo.ref > foo.sref\n";
26
27 # Standard options for TeX Live.
28 Getopt::Long::GetOptions(
29 'help|?' => \&help_handler,
30 'version' => sub { print version(); exit 0; }
31 ) or die $usage;
32 ## help_handler()
33 sub help_handler {
34 open(my $pipe, '|-', $ENV{PAGER} || 'less -e') or exit 1;
35 pod2usage(-message => version(), -output => $pipe,
36 -verbose => 99, -sections => "SHORT DESCRIPTION|COPYRIGHT");
37 close $pipe;
38 exit 0;
39 }
40 ## version()
41 sub version {
42 return $progname.' $Revision: 69812 $ $Date: 2024-02-12 00:09:24 +0100 (Mon, 12 Feb 2024) $'."\n";
43 }
44
45 # Read input lines from the console and look for blocks like
46 # !127
47 # [+ KEY LOCATION TEXPART]*
48 # and sort them alphabetically by the KEYs.
49 #
50 # Every KEY consists of a single PASCAL identifier (variable, type, function,
51 # etc.) after an initial '+ ' marker. This marker is replaced with the '\['
52 # macro (i.e., '\makeref') to help TeX format the mini-index.
53 #
54 # The page number in the '!\d+' indicator is appended as argument of the
55 # '\donewithpage' macro.
56 my %mini_index; # storage for index entries of a two-page spread
57 my $donewithpage; # recto page number of a two-page spread
58 foreach my $line (<STDIN>)
59 {
60 if ($line =~ m/\!(\d+)/) { # start of a new two-page spread
61 if ($donewithpage) { # skip over first indicator line
62 foreach my $key ($collator ?
63 $collator->sort(keys %mini_index) :
64 sort {"\L$a" cmp "\L$b"} keys %mini_index) {
65 print $mini_index{$key};
66 }
67 %mini_index = (); # reset mini-index storage
68 print "\\donewithpage$donewithpage\n"; # done with current spread
69 }
70 $donewithpage = $1; # start of next spread
71 } else { # mini-index entry
72 my (undef,$key) = split / /, $line; # 2nd column is the key
73 $line =~ s/^\+ /\\\[/; # add \makeref macro
74 $mini_index{$key} = $line; # store index entry
75 next; # print later
76 }
77 }
78
79 print "\\donewithpage$donewithpage\n"; # done with current spread
80
81 exit 0;
82
83 =pod
84
85 =encoding utf8
86
87 =head1 NAME
88
89 twill-refsort - Sort mini-indexes alphabetically
90
91 =head1 SHORT DESCRIPTION
92
93 This small Perl script 'twill-refsort' sorts the mini-indexes for each section
94 in the '.ref' file created by 'twill foo' (twice) and 'tex foo' (first run).
95 It reads its input from STDIN and writes its output to STDOUT.
96
97 =over
98
99 =item Run TWILL twice on your 'foo'.w, creating 'foo'.tex
100
101 =item Invoke "tex 'foo'.tex", creating 'foo'.ref
102
103 =item Invoke "twill-refsort < 'foo'.ref > 'foo'.sref"
104
105 =item Run TeX on 'foo'.tex a second time
106
107 =back
108
109 See also "man twill" for more information on how to use the TWILL system.
110
111 =head1 COPYRIGHT
112
113 Public domain. Originally written by Andreas Scherer, 2023.
114
115 Contemporary development on https://github.com/ascherer/cwebbin.
116
117 =cut