]> git.pond.sub.org Git - empserver/blob - info/mksubj.pl
Suppress explanation of '*' flag in subject pages when not used
[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: Create the index for a subject
29 #
30 #   Known contributors to this file:
31 #      Ken Stevens (when it was still info.pl)
32 #      Markus Armbruster, 2006
33 #
34
35 # Usage: mksubj.pl OUTFILE INFILE...
36 # The INFILE... contain all the topics belonging to a subject.  Read
37 # and check the information required for the index from them, write
38 # the index to OUTFILE.
39
40 use strict;
41 use warnings;
42 use File::stat;
43
44 # The chapters, in order
45 my @Chapters = qw/Introduction Concept Command Server/;
46
47 my $filename;
48 my (%subject, %level, %desc, %long, %cnt);
49 my $largest = "";
50
51 my $out = shift @ARGV;
52 $out =~ /([^\/]*)\.t$/
53     or die "Strange subject file name $out";
54 my $subj = $1;
55
56 for (@ARGV) {
57     my ($topic, $chap, $lvl, $desc, $long) = parse_file($_);
58     $largest = $topic if length $topic > length $largest;
59     $subject{$chap} .= "$topic\n";
60     $level{$topic} = $lvl;
61     $cnt{$lvl}++;
62     $desc{$topic} = $desc;
63     $long{$topic} = $long;
64     $cnt{'long'}++ if $long;
65 }
66
67 open(SUBJ, ">$out")
68     or die "Can't open $out for writing: $!";
69
70 print SUBJ '.\" DO NOT EDIT THIS FILE.  It was automatically generated by mksubj.pl'."\n";
71 print SUBJ ".TH Subject \U$subj\n";
72 $largest =~ s/-/M/g;
73 print SUBJ ".in \\w'$largest", "XX\\0\\0\\0\\0'u\n";
74
75 for my $chap (@Chapters) {
76     next unless exists $subject{$chap};
77     print SUBJ ".s1\n";
78     for (split(/\n/, $subject{$chap})) {
79         my $flags = "";
80         $flags .= "*" if $level{$_} eq 'Basic';
81         $flags .= "!" if $long{$_};
82         $flags = sprintf("%-2s", $flags);
83         print SUBJ ".L \"$_ $flags\"\n";
84         print SUBJ "$desc{$_}\n";
85     }
86 }
87
88 print SUBJ <<EOF;
89 .s1
90 .in 0
91 For info on a particular subject, type "info <subject>" where <subject> is
92 one of the subjects listed above.
93 EOF
94 print SUBJ <<EOF if $cnt{'Basic'};
95 Subjects marked by * are the most important and should be read by new
96 players.
97 EOF
98 print SUBJ <<EOF if $cnt{'long'};
99 Unusually long subjects are marked with a !.
100 EOF
101 close SUBJ;
102
103
104 sub parse_file {
105     ($filename) = @_;
106     my ($topic, $chap, $lvl, $desc, $long, $st);
107
108     $topic = $filename;
109     $topic =~ s,.*/([^/]*)\.t$,$1,;
110
111     $st = stat $filename
112         or die "Can't stat $filename: $!";
113     $long = $st->size > 9999;
114
115     open(F, "<$filename")
116         or die "Can't open $filename: $!";
117
118     $_ = <F>;
119     if (/^\.TH (\S+) (\S.+\S)$/) {
120         if (!grep(/^$1$/, @Chapters)) {
121             error("First argument to .TH was '$1', which is not a known chapter");
122         }
123         $chap = $1;
124         if ($1 eq "Command" && $2 ne "\U$topic") {
125             error("Second argument to .TH was '$2' but it should be '\U$topic'");
126         }
127     } else {
128         error("The first line in the file must be a .TH request");
129     }
130
131     $_ = <F>;
132     if (/^\.NA (\S+) "(\S.+\S)"$/) {
133         if ($topic ne $1) {
134             error("First argument to .NA was '$1' but it should be '$topic'");
135         }
136         $desc = $2;
137     } else {
138         error("The second line in the file must be a .NA request");
139     }
140
141     $_ = <F>;
142     if (/^\.LV (\S+)$/) {
143         if ($1 ne 'Basic' && $1 ne 'Expert' && $1 ne 'Obsolete') {
144             error("The argument to .LV was '$1' but it must be either 'Basic', 'Expert', or 'Obsolete'");
145         }
146         $lvl = $1;
147     } else {
148         error("The third line in the file must be a .LV request");
149     }
150
151     close F;
152
153     return ($topic, $chap, $lvl, $desc, $long);
154 }
155
156 # Print an integrity error message and exit with code 1
157 sub error {
158     my ($error) = @_;
159
160     print STDERR "mksubj.pl:$filename:$.: $error\n";
161     exit 1;
162 }