Debian Codegraph @

texk/web2c/cwebdir/ctwill-proofsort

raw
1 #!/usr/bin/env perl
2 # $Id: ctwill-proofsort 73408 2025-01-11 11:42:54Z ascherer $
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 < input_file > output_file\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|EXAMPLE|COPYRIGHT");
37 close $pipe;
38 exit 0;
39 }
40 ## version()
41 sub version {
42 return $progname.' $Revision: 73408 $ $Date: 2025-01-11 12:42:54 +0100 (Sat, 11 Jan 2025) $'."\n";
43 }
44
45 # Read input from the console and look for blocks like
46 # \mini
47 # [LOCATION KEY TEXPART]*
48 # }\FI
49 # and sort them alphabetically by the KEYs.
50 #
51 # Every KEY contains a single C identifier (variable, type, function, etc.),
52 # either as a single brace-enclosed argument of a TeX control sequence,
53 # or as a single identifier in $math_mode$; the latter come from '@f'ormat
54 # definitions. All internal backslashes are removed from the KEY before it
55 # is used in the storage.
56 my %mini_index; # storage for index entries of a section
57 my $print_index = 0; # do we collect and print a mini-index?
58 foreach my $line (<STDIN>)
59 {
60 if ("\\mini\n" eq $line) { # start of mini-index
61 $print_index = 1;
62 %mini_index = (); # reset mini-index storage
63 } elsif ("}\\FI\n" eq $line) { # end of mini-index
64 foreach my $key ($collator ? # (I) sort by second column
65 $collator->sort(keys %mini_index) :
66 sort {"\L$a" cmp "\L$b"} keys %mini_index) {
67 print sort { # (II) sort by third and first column
68 my ($a_loc,undef,$a_id) = split / /, $a, 3;
69 my ($b_loc,undef,$b_id) = split / /, $b, 3;
70 $a_id cmp $b_id || $a_loc cmp $b_loc }
71 @{ $mini_index{$key} };
72 }
73 $print_index = 0;
74 } elsif ($print_index) { # mini-index entry
75 my (undef,$key) = split / /, $line; # 2nd column is the key
76 $key =~ s/\\//g; # strip internal TeX escape(s)
77 $key =~ m/\w*\{(\w+)\}/; # extract plain key from control seq.
78 $key =~ m/\$(\w+)\$/ unless defined $1; # extract @f TeX key
79 push @{ $mini_index{$1} }, $line; # store index entry
80 next; # print later
81 }
82
83 print $line;
84 }
85
86 exit 0;
87
88 =pod
89
90 =encoding utf8
91
92 =head1 NAME
93
94 ctwill-proofsort - Sort mini-indexes alphabetically
95
96 =head1 SHORT DESCRIPTION
97
98 This small Perl script 'ctwill-proofsort' sorts the mini-indexes for each
99 section in the TeX file created by 'ctwill +P', i.e., prior to
100 formatting with the 'ctproofmac.tex' macros.
101 It reads its input from <STDIN> and writes its output to <STDOUT>.
102
103 =over
104
105 =item Run CTWILL twice on your 'input_file'.w, creating 'input_file'.tex
106
107 =item Invoke "( ctwill-proofsort < 'input_file'.tex ) 1<> 'input_file'.tex"
108
109 =item Run TeX on 'input_file'.tex once
110
111 =back
112
113 See also "man ctwill" for more information on how to use the CTWILL system,
114 both with or without the '+P' option.
115
116 =head1 EXAMPLE
117
118 Example from MMIX (https://github.com/ascherer/mmix, 'local' branch):
119
120 =over
121
122 =item $ tie -c mmotype-twill.ch mmotype.{w,ch} mmotype-mini.ch
123
124 =item $ ctwill +P -x mmotype mmotype-twill (run this two times)
125
126 =item $ ( ctwill-proofsort < mmotype.tex ) 1<> mmotype.tex
127
128 =item $ tex mmotype
129
130 =back
131
132 =head1 COPYRIGHT
133
134 Public domain. Originally written by Andreas Scherer, 2023.
135
136 Contemporary development on https://github.com/ascherer/cwebbin.
137
138 =cut