PennMUSH Community

root/1.8.3/trunk/game/txt/index-files.pl

Revision 441, 4.0 kB (checked in by pennmush, 2 years ago)

PennMUSH 1.7.7p21 Archival

Line 
1 #!/usr/bin/perl
2 #
3 # index-files.pl - make an & index topic for events/news/help
4 #
5 # Called by compose-tricky
6 #
7 # Take a MUSH help.txt format file on the stdin, and write a
8 # "& entries" entry or entries.
9 # Lines with entries to be indexed start with &'s.
10 # Write the resulting entries to the stdout, also in help.txt format,
11 # in columns and paged.
12 # Idea by Schmidt@Dune, perl version by Alan Schwartz (Javelin/Paul)
13 # with mods by Jeff Heinen. Modified further by Raevnos.
14 #
15 # Usage: index-files.pl [options] < news.txt > nws/index.nws
16 #
17 require 5; # Sorry, Talek.
18 use strict;
19 use Getopt::Long;
20 use locale;
21 my (@entries, @aentries);
22
23 # Have we got any options?
24 my($first,$longest) = ("","");
25 exit 1 unless GetOptions ('first' => \$first, 'longest' => \$longest);
26
27 # Collect all the topic names
28 my @set = ();
29 my @aset = ();
30 while (<STDIN>) {
31   chomp;
32   next if /^& &?Entries/o; # Skip index entries.
33   if ((@set or @aset) and $_ !~ /&\s+\S/) {
34     # We've got a set of entries now. Choose which to add.
35     if ($first) {
36        # Add the first one
37        push(@entries,$set[0]) if $set[0];
38        push(@aentries,$aset[0]) if $aset[0];
39     }
40     if ($longest) {
41        # Add the longest one
42        @set = sort { length($b) <=> length($a) } @set;
43        @aset = sort { length($b) <=> length($a) } @aset;
44        push(@entries,$set[0]) if $set[0];
45        push(@aentries,$aset[0]) if $aset[0];
46     }
47     if (!$first and !$longest) {
48        # Add all
49        push(@entries,@set) if @set;
50        push(@aentries,@aset) if @aset;
51     }
52     @set = (); @aset = ();
53   }
54   push @aset,$1 if /^&\s+(&.*)/o; # Catch admin-help entries
55   push @set, $1 if /^&\s+([^&].*)/o; # Catch normal entries.
56 }
57 # If anything's left in @set/@aset now, someone's screwed up
58
59 # Make 'em all lower-case and sort 'em.
60 @entries = map { lc $_ } @entries;
61 @aentries = map { lc $_ } @aentries;
62
63 sub withnumchecking;
64 my @sorted = ($#entries > 0) ? (sort withnumchecking @entries) : @entries;
65 my @asorted = ($#entries > 0) ? (sort withnumchecking @aentries) : @aentries;
66
67 my $maxlines = 14;
68 my $maxlen = 25;
69 my $separator ="-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
70 my $three_items = " %-25.25s %-25.25s %-25.25s\n"; # Format for three entries
71 my $bigone = " %-51.51s %-25.25s\n"; # Format for two entries, long first.
72 my $bigtwo = " %-25.25s %-51.51s\n"; # Format for two entries, long second.
73 my $admin = 0;
74 my $index;
75
76 foreach $index (\@sorted, \@asorted) {
77   my $i = 0;
78   my $title = $admin ? "&Entries" : "Entries";
79   $admin++;
80   my $titlecount = 1;
81   my $header = 0;
82   my ($entry1, $entry2, $entry3);
83   print "\n& $title\n", $separator;
84   while (@$index) {
85     if (${$index}[0] eq "help") {
86        shift @$index;
87        next;
88      }
89     if ($header) {
90       print "& $title-$titlecount\n", $separator;
91       $header = 0;
92     }
93     $entry1 = shift @$index;
94     $entry2 = shift @$index;
95     $entry2 = "" if !defined $entry2;
96     if (length($entry1) > $maxlen) {
97       if (length($entry2) > $maxlen) {
98         printf " %-76.76s\n", $entry1;
99         unshift @$index, $entry2;
100       } else {
101         printf $bigone, $entry1, $entry2;
102       }
103     } else {
104       if (length($entry2) > $maxlen) {
105         printf $bigtwo, $entry1, $entry2;
106       } else {
107         $entry3 = shift @$index;
108         $entry3 = "" if !defined $entry3;
109         if (length($entry3) > $maxlen) {
110           unshift @$index, $entry3;
111           $entry3 ="";
112         }
113         printf $three_items, $entry1, $entry2, $entry3;
114       }
115     }
116    if ($i++ > $maxlines)  {
117      $titlecount++;
118      print "\nFor more, see $title-$titlecount\n", $separator;
119      $header = 1;
120      $i = 0;
121    }
122  }
123 }
124 print $separator;
125
126 sub withnumchecking {
127   my($firsta, $firstb) = ($a,$b);
128   my($lasta, $lastb);
129   ($firsta, $lasta) = ($1,$2) if $a =~ /(.+)p(\d+)/o;
130   ($firstb, $lastb) = ($1,$2) if $b =~ /(.+)p(\d+)/o;
131   my $res = $firsta cmp $firstb;
132   return $res if $res;
133   if (!defined($lasta)) {
134     warn "Possible duplicate help topic: $a\n";
135     return $res;
136   }
137   return $lasta <=> $lastb;
138 }
139
Note: See TracBrowser for help on using the browser.