]> git.pond.sub.org Git - empserver/blob - info/mksubj.pl
Simplify how mksubj.pl keeps track of the subjects' topics
[empserver] / info / mksubj.pl
1 #!/usr/bin/perl
2 #
3 #   Empire - A multi-player, client/server Internet based war game.
4 #   Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
5 #                 Ken Stevens, Steve McClure, Markus Armbruster
6 #
7 #   Empire is free software: you can redistribute it and/or modify
8 #   it under the terms of the GNU General Public License as published by
9 #   the Free Software Foundation, either version 3 of the License, or
10 #   (at your option) any later version.
11 #
12 #   This program is distributed in the hope that it will be useful,
13 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #   GNU General Public License for more details.
16 #
17 #   You should have received a copy of the GNU General Public License
18 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 #   ---
21 #
22 #   See files README, COPYING and CREDITS in the root of the source
23 #   tree for related information and legal notices.  It is expected
24 #   that future projects/authors will amend these files as needed.
25 #
26 #   ---
27 #
28 #   mksubj.pl: Update the subject index pages
29 #
30 #   Known contributors to this file:
31 #      Ken Stevens (when it was still info.pl)
32 #      Markus Armbruster, 2006-2013
33 #
34 # Usage: mksubj.pl SUBJECT... INFO-FILE...
35 #
36 # Read the INFO-FILE..., update info/SUBJECT.t for each SUBJECT.
37
38 use strict;
39 use warnings;
40 use File::stat;
41
42 # The chapters, in order
43 my @Chapters = qw/Introduction Concept Command Server/;
44
45 my @Levels = qw/Basic Expert Obsolete/;
46
47 # $Subjects{SUBJECT} is a reference to an an anonymous array
48 # containing SUBJECT's topics
49 my %Subjects;
50
51 # $filename{TOPIC} is TOPIC's file name
52 my %filename;
53 # $long{TOPIC} is true when TOPIC's page is "long"
54 my %long;
55 # $chapter{TOPIC} is TOPIC's chapter (first arg to .TH)
56 my %chapter;
57 # $desc{TOPIC} is a one line description of TOPIC (second arg to .NA)
58 my %desc;
59 # $level{TOPIC} is TOPIC's difficulty level (arg to .LV)
60 my %level;
61
62 # current info file
63 my $filename;
64
65 while ($#ARGV >= 0 && $ARGV[0] !~ /\.t$/) {
66     $Subjects{shift @ARGV} = [];
67 }
68
69 for (@ARGV) {
70     $filename{fn2topic($_)} = $_;
71 }
72
73 for (@ARGV) {
74     parse_file($_);
75 }
76
77 for (keys %Subjects) {
78     update_subj($_);
79 }
80
81 sub fn2topic {
82     my ($fn) = @_;
83     $fn =~ s,.*/([^/]*)\.t$,$1,;
84     return $fn;
85 }
86
87 # Parse an info file
88 # Set $filename, $long{TOPIC}, $chapter{TOPIC}, $desc{TOPIC},
89 # $level{TOPIC}.
90 # Update %Subjects.
91 sub parse_file {
92     ($filename) = @_;
93     my $topic = fn2topic($filename);
94     my $st;
95
96     $st = stat $filename
97         or die "Can't stat $filename: $!";
98     $long{$topic} = $st->size > 9999;
99
100     open(F, "<$filename")
101         or die "Can't open $filename: $!";
102
103     $_ = <F>;
104     if (/^\.TH (\S+) (\S.+\S)$/) {
105         if (!grep(/^$1$/, @Chapters)) {
106             error("First argument to .TH was '$1', which is not a known chapter");
107         }
108         $chapter{$topic} = $1;
109         if ($1 eq "Command" && $2 ne "\U$topic") {
110             error("Second argument to .TH was '$2' but it should be '\U$topic'");
111         }
112     } else {
113         error("The first line in the file must be a .TH request");
114     }
115
116     $_ = <F>;
117     if (/^\.NA (\S+) "(\S.+\S)"$/) {
118         if ($topic ne $1) {
119             error("First argument to .NA was '$1' but it should be '$topic'");
120         }
121         $desc{$topic} = $2;
122     } else {
123         error("The second line in the file must be a .NA request");
124     }
125
126     $_ = <F>;
127     if (/^\.LV (\S+)$/) {
128         if (!grep(/^$1$/, @Levels)) {
129             error("The argument to .LV was '$1', which is not a known level");
130         }
131         $level{$topic} = $1;
132     } else {
133         error("The third line in the file must be a .LV request");
134     }
135
136     while (<F>) {
137         last if /^\.SA/;
138     }
139
140     if ($_) {
141         if (/^\.SA "([^\"]*)"/) {
142             parse_see_also($topic, $1);
143         } else {
144             error("Incorrect .SA Syntax.  Syntax should be '.SA \"item1, item2\"'");
145         }
146
147         while (<F>) {
148             error("Multiple .SA requests.  Each file may contain at most one.") if /^\.SA/;
149         }
150     } else {
151         error(".SA request is missing");
152     }
153
154     close F;
155 }
156
157 sub parse_see_also {
158     my ($topic, $sa) = @_;
159     my $wanted = $chapter{$topic};
160     my $found;                 # found a subject?
161
162     $wanted = undef if $wanted eq 'Concept' or $wanted eq 'Command';
163
164     for (split(/, /, $sa)) {
165         next if exists $filename{$_};
166         error("Unknown topic $_ in .SA") unless exists $Subjects{$_};
167         push @{$Subjects{$_}}, $topic;
168         $found = 1;
169         if ($wanted && $_ eq $wanted) {
170             $wanted = undef;
171         }
172     }
173
174     error("No subject listed in .SA") unless $found;
175     error("Chapter $wanted not listed in .SA") if $wanted;
176 }
177
178 # Update a Subject.t file
179 sub update_subj {
180     my ($subj) = @_;
181     my $fname = "info/$subj.t";
182     my @topics = @{$Subjects{$subj}};
183     my $out = "";
184     my ($any_topic, $any_basic, $any_obsolete, $any_long);
185
186     my $largest = "";
187     for my $topic (@topics) {
188         $largest = $topic if length $topic > length $largest;
189     }
190
191     $out .= '.\" DO NOT EDIT THIS FILE.  It was automatically generated by mksubj.pl'."\n";
192     $out .= ".TH Subject \U$subj\n";
193     $largest =~ s/-/M/g;
194     $out .= ".in \\w'$largest" . "XX\\0\\0\\0\\0'u\n";
195
196     for my $chap (@Chapters) {
197         my $empty = 1;
198         for my $topic (@topics) {
199             $any_topic = 1;
200             next if $chapter{$topic} ne $chap;
201             $out .= ".s1\n" if $empty;
202             $empty = 0;
203             my $flags = "";
204             if ($level{$topic} eq 'Basic') {
205                 $flags .= "*";
206                 $any_basic = 1;
207             }
208             if ($level{$topic} eq 'Obsolete') {
209                 $flags .= "+";
210                 $any_obsolete = 1;
211             }
212             if ($long{$topic}) {
213                 $flags .= "!";
214                 $any_long = 1;
215             }
216             $flags = sprintf("%-2s", $flags);
217             $out .= ".L \"$topic $flags\"\n";
218             $out .= "$desc{$topic}\n";
219         }
220     }
221     unless ($any_topic) {
222         print STDERR "$0: Subject $subj has no topics\n";
223         exit 1;
224     }
225     $out .= ".s1\n"
226         . ".in 0\n"
227         . "For info on a particular subject, type \"info <subject>\" where <subject> is\n"
228         . "one of the subjects listed above.\n";
229     $out .= "Subjects marked by * are the most important and should be read by new players.\n"
230         if $any_basic;
231     $out .= "Subjects marked by + are obsolete.\n"
232         if $any_obsolete;
233     $out .= "Unusually long subjects are marked with a !.\n"
234         if $any_long;
235
236     return if (same_contents($fname, $out));
237     open(SUBJ, ">$fname")
238         or die "Can't open $fname for writing: $!";
239     print SUBJ $out;
240     close SUBJ;
241 }
242
243 sub same_contents {
244     my ($fname, $contents) = @_;
245     local $/;
246
247     if (!open(SUBJ, "<$fname")) {
248         return 0 if ($!{ENOENT});
249         die "Can't open $fname for reading: $!";
250     }
251     my $old = <SUBJ>;
252     close SUBJ;
253     return $contents eq $old;
254 }
255
256 # Print an integrity error message and exit with code 1
257 sub error {
258     my ($error) = @_;
259
260     print STDERR "mksubj.pl:$filename:$.: $error\n";
261     exit 1;
262 }