Allow synonyms for localhost
[spider.git] / sgml / commands2sgml.pl
1 #!/usr/bin/perl -w
2
3 # cat Commands_en.hlp | ./commands2sgml.pl <level> > commands.sgml
4 # Level 0 is assumed by default.
5
6 # This Perl may not be nice but it seems to work :)
7 # This is supposed to take a spider command definition file and
8 # convert it to SGML format suitable for inclusion in the spider manual.
9 #
10 # It is cunningly written with no language in mind, and should work for all
11 # command files in whatever language.  
12 #
13 # I claim no suitability for purpose, and should this script mutate and eat
14 # your children I'm afraid I'm not responsible.  Wild herds of rampaging
15 # Taiwanese suicide squirrels attacking your rabbit are also not my fault.
16 #
17 # Ian (M0AZM) 20030210.
18 #
19 # $Id$
20 #
21
22 print STDERR localtime() ." ($$) $0 Starting\n";
23
24 use strict;
25
26 # Bewitched, debugged and bewildered?
27 my $DEBUG = 0 ;
28
29 # SGML headers - use for debugging your SGML output :)
30 my $HEADERS = 0 ;
31
32 # Definitions of things....
33 my $count = 0 ;
34 my ($cmd, $line) ;
35 my %help ;
36
37 # Default output level, take $ARGV[0] as being a level
38 my $level = shift || 0 ;
39
40 # Disable line buffering
41 $| = 1 ;
42
43 # SGML headers
44 if ($HEADERS) {
45     print("<!doctype linuxdoc system>\n") ;
46     print("<article>\n") ;
47     print("<sect>\n") ;
48 }
49
50 # Loop until EOF
51 while (<>) {
52
53     # Ignore comments
54     next if /^\s*\#/;
55
56         chomp $_;
57     
58         # Is this a command definition line?
59         # if(m/^=== ([\d])\^([\w,\W]*)\^([\w,\W]*)/)
60         if (/^=== ([\d])\^(.*)\^(.*)/) {
61                 $count++ ;
62         
63                 if ($DEBUG) {
64                         print("Level       $1\n") ;
65                         print("Command     $2\n") ;
66                         print("Description $3\n") ;
67                         next;
68                 }
69
70                 $cmd = $2 ;
71
72                 $help{$cmd}{level} = $1 ;
73                 $help{$cmd}{command} = $2 ;
74                 $help{$cmd}{description} = $3 ;
75         } else {
76                 # Not a command definition line - Carry On Appending(tm)....
77                 $help{$cmd}{comment} .= $_ . "\n" ;
78         }
79         # print("$_\n") ;
80 }
81
82 # Go through all of the records in the hash in order
83 foreach $cmd (sort(keys %help)) {
84
85         # Level checking goes here.
86         next if $help{$cmd}{level} > $level;
87     
88         # Need to change characters that SGML doesn't like at this point.
89         # Perhaps we should use a function for each of these variables?
90         # Deal with < and >
91         $help{$cmd}{command} =~ s/</&lt;/g ;
92         $help{$cmd}{command} =~ s/>/&gt;/g ;
93
94         # Deal with [ and ]
95         $help{$cmd}{command} =~ s/\[/&lsqb;/g ;
96         $help{$cmd}{command} =~ s/\]/&rsqb;/g ;
97
98         # Change to lower case
99         $help{$cmd}{command} = lc($help{$cmd}{command}) ;
100
101         # Deal with < and >
102         $help{$cmd}{description} =~ s/</&lt;/g ;
103         $help{$cmd}{description} =~ s/>/&gt;/g ;
104
105         # Deal with < and >
106         if ($help{$cmd}{comment}) {
107                 $help{$cmd}{comment} =~ s/</&lt;/g ;
108                 $help{$cmd}{comment} =~ s/>/&gt;/g ;
109         }
110
111         # Output the section details and command summary.
112         print("<sect1>$help{$cmd}{command}") ;
113         print(" ($help{$cmd}{level})") if $level > 0;
114         print("\n\n") ;
115         print("<P>\n") ;
116         print("<tt>\n") ;
117         print("<bf>$help{$cmd}{command}</bf> $help{$cmd}{description}\n") ;
118         print("</tt>\n") ;
119         print("\n") ;
120
121         # Output the command comments.
122         print("<P>\n") ;
123
124         # Loop through each line of the command comments.
125         # If the first character of the line is whitespace, then use tscreen
126         # Once a tscreen block has started, continue until the next blank line.
127         my $block = 0 ;
128
129         # Is the comment field blank?  Then trying to split will error - lets not.
130         next unless $help{$cmd}{comment};
131
132         # Work through the comments line by line
133         foreach $line (split('\n', $help{$cmd}{comment})) {
134                 # Leading whitespace or not?
135                 if ($line =~ /^\s+\S+/) {
136                         if (!$block) {
137                                 $block = 1 ;
138                                 print("<tscreen><verb>\n") ; 
139                         }
140                 } else {
141                         if ($block) {
142                                 $block = 0 ;
143                                 print("</verb></tscreen>\n") ;
144                         }
145                 }
146                 print("$line\n") ;
147         }
148     
149         # We fell out of the command comments still in a block - Ouch....
150         if ($block) {
151                 print("</verb></tscreen>\n\n") ;
152         }
153 }
154
155 print("</article>\n") ;
156
157 # Is it 'cos we is dun ?
158 print STDERR localtime()." ($$) $0 Exiting ($count read)\n" ;
159