Make mksubj.pl touch subject files only when it needs to change
Avoids unnecessary reformatting of subject pages again.
This commit is contained in:
parent
890e88d149
commit
156930c515
1 changed files with 32 additions and 31 deletions
|
@ -25,7 +25,7 @@
|
|||
#
|
||||
# ---
|
||||
#
|
||||
# mksubj.pl: Create the subject index pages
|
||||
# mksubj.pl: Update the subject index pages
|
||||
#
|
||||
# Known contributors to this file:
|
||||
# Ken Stevens (when it was still info.pl)
|
||||
|
@ -33,14 +33,12 @@
|
|||
#
|
||||
# Usage: mksubj.pl SUBJECT... INFO-FILE...
|
||||
#
|
||||
# Read the INFO-FILE..., create info/SUBJECT.t for each SUBJECT.
|
||||
# Read the INFO-FILE..., update info/SUBJECT.t for each SUBJECT.
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use File::stat;
|
||||
|
||||
use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
|
||||
|
||||
# The chapters, in order
|
||||
my @Chapters = qw/Introduction Concept Command Server/;
|
||||
|
||||
|
@ -84,7 +82,9 @@ for my $t (sort keys %desc) {
|
|||
parse_see_also($t);
|
||||
}
|
||||
|
||||
create_subjects();
|
||||
for (keys %Subjects) {
|
||||
update_subj($_);
|
||||
}
|
||||
|
||||
# Parse an info file
|
||||
# Set $filename, $filename{TOPIC}, $long{TOPIC}, $chapter{TOPIC},
|
||||
|
@ -194,22 +194,20 @@ sub set_subject {
|
|||
$largest{$sub} = $topic if length $topic > length $largest{$sub};
|
||||
}
|
||||
|
||||
# Create a Subject.t file
|
||||
sub create_subj {
|
||||
# Update a Subject.t file
|
||||
sub update_subj {
|
||||
my ($subj) = @_;
|
||||
my $fname = "info/$subj.t";
|
||||
my $out = "";
|
||||
my ($any_topic, $any_basic, $any_obsolete, $any_long);
|
||||
|
||||
sysopen(SUBJ, $fname, O_WRONLY | O_EXCL | O_CREAT)
|
||||
or die "Unable to create $fname: $!\n";
|
||||
|
||||
print SUBJ '.\" DO NOT EDIT THIS FILE. It was automatically generated by mksubj.pl'."\n";
|
||||
print SUBJ ".TH Subject \U$subj\n";
|
||||
$out .= '.\" DO NOT EDIT THIS FILE. It was automatically generated by mksubj.pl'."\n";
|
||||
$out .= ".TH Subject \U$subj\n";
|
||||
$largest{$subj} =~ s/-/M/g;
|
||||
print SUBJ ".in \\w'$largest{$subj}XX\\0\\0\\0\\0'u\n";
|
||||
$out .= ".in \\w'$largest{$subj}XX\\0\\0\\0\\0'u\n";
|
||||
for my $chap (@Chapters) {
|
||||
next unless exists $subject{$subj}{$chap};
|
||||
print SUBJ ".s1\n";
|
||||
$out .= ".s1\n";
|
||||
for my $topic (split(/\n/, $subject{$subj}{$chap})) {
|
||||
$any_topic = 1;
|
||||
my $flags = "";
|
||||
|
@ -226,40 +224,43 @@ sub create_subj {
|
|||
$any_long = 1;
|
||||
}
|
||||
$flags = sprintf("%-2s", $flags);
|
||||
print SUBJ ".L \"$topic $flags\"\n";
|
||||
print SUBJ "$desc{$topic}\n";
|
||||
$out .= ".L \"$topic $flags\"\n";
|
||||
$out .= "$desc{$topic}\n";
|
||||
}
|
||||
}
|
||||
unless ($any_topic) {
|
||||
print STDERR "$0: Subject $subj has no topics\n";
|
||||
exit 1;
|
||||
}
|
||||
print SUBJ ".s1\n"
|
||||
$out .= ".s1\n"
|
||||
. ".in 0\n"
|
||||
. "For info on a particular subject, type \"info <subject>\" where <subject> is\n"
|
||||
. "one of the subjects listed above.\n";
|
||||
print SUBJ "Subjects marked by * are the most important and should be read by new players.\n"
|
||||
$out .= "Subjects marked by * are the most important and should be read by new players.\n"
|
||||
if $any_basic;
|
||||
print SUBJ "Subjects marked by + are obsolete.\n"
|
||||
$out .= "Subjects marked by + are obsolete.\n"
|
||||
if $any_obsolete;
|
||||
print SUBJ "Unusually long subjects are marked with a !.\n"
|
||||
$out .= "Unusually long subjects are marked with a !.\n"
|
||||
if $any_long;
|
||||
|
||||
return if (same_contents($fname, $out));
|
||||
open(SUBJ, ">$fname")
|
||||
or die "Can't open $fname for writing: $!";
|
||||
print SUBJ $out;
|
||||
close SUBJ;
|
||||
}
|
||||
|
||||
# Remove the old Subject.t files and create new ones
|
||||
sub create_subjects {
|
||||
my (@subj);
|
||||
sub same_contents {
|
||||
my ($fname, $contents) = @_;
|
||||
local $/;
|
||||
|
||||
for (keys %Subjects) {
|
||||
unlink "info/$_.t";
|
||||
}
|
||||
|
||||
@subj = sort keys %subject;
|
||||
|
||||
for my $subj (@subj) {
|
||||
create_subj($subj);
|
||||
if (!open(SUBJ, "<$fname")) {
|
||||
return 0 if ($!{ENOENT});
|
||||
die "Can't open $fname for reading: $!";
|
||||
}
|
||||
my $old = <SUBJ>;
|
||||
close SUBJ;
|
||||
return $contents eq $old;
|
||||
}
|
||||
|
||||
# Print an integrity error message and exit with code 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue