Changes to manual namings to standardise. Added script from M0AZM to
[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 print STDERR localtime() ." ($$) $0 Starting\n";
20
21 use strict;
22
23 # Bewitched, debugged and bewildered?
24 my $DEBUG = 0 ;
25
26 # SGML headers - use for debugging your SGML output :)
27 my $HEADERS = 0 ;
28
29 # Definitions of things....
30 my $count = 0 ;
31 my ($cmd, $line) ;
32 my %help ;
33
34 # Default output level
35 my $level = 0 ;
36
37 # Command line parameters
38 if(my $var = shift(@ARGV))
39     {
40     $level = $var ;
41     }
42
43 # Disable line buffering
44 $| = 1 ;
45
46 # SGML headers
47 if($HEADERS)
48     {
49     print("<!doctype linuxdoc system>\n") ;
50     print("<article>\n") ;
51     print("<sect>\n") ;
52     }
53
54 # Loop until EOF
55 while(<>) 
56     {
57     # Ignore comments
58     if(m/^#/)
59         {
60         next;
61         }
62     
63     chomp $_;
64     
65     # Is this a command definition line?
66     # if(m/^=== ([\d])\^([\w,\W]*)\^([\w,\W]*)/)
67     if(m/^=== ([\d])\^(.*)\^(.*)/)
68         {
69         $count++ ;
70         
71         if($DEBUG)
72             {
73             print("Level       $1\n") ;
74             print("Command     $2\n") ;
75             print("Description $3\n") ;
76             next;
77             }
78
79         $cmd = $2 ;
80
81         $help{$cmd}{level} = $1 ;
82         $help{$cmd}{command} = $2 ;
83         $help{$cmd}{description} = $3 ;
84         }
85     # Not a command definition line - Carry On Appending(tm)....
86     else
87         {
88         $help{$cmd}{comment} .= $_ . "\n" ;
89         }
90     # print("$_\n") ;
91     }
92
93 # Go through all of the records in the hash in order
94 foreach $cmd (sort(keys %help))
95     {
96     # Level checking goes here.
97     if($help{$cmd}{level} > $level) { next ; }
98     
99     # Need to change characters that SGML doesn't like at this point.
100     # Perhaps we should use a function for each of these variables?
101     # Deal with < and >
102     $help{$cmd}{command} =~ s/</&lt;/g ;
103     $help{$cmd}{command} =~ s/>/&gt;/g ;
104     # Deal with [ and ]
105     $help{$cmd}{command} =~ s/\[/&lsqb;/g ;
106     $help{$cmd}{command} =~ s/\]/&rsqb;/g ;
107     # Change to lower case
108     $help{$cmd}{command} = lc($help{$cmd}{command}) ;
109
110     # Deal with < and >
111     $help{$cmd}{description} =~ s/</&lt;/g ;
112     $help{$cmd}{description} =~ s/>/&gt;/g ;
113
114     # Deal with < and >
115     if($help{$cmd}{comment})
116         {
117         $help{$cmd}{comment} =~ s/</&lt;/g ;
118         $help{$cmd}{comment} =~ s/>/&gt;/g ;
119         }
120
121     # Output the section details and command summary.
122     print("<sect1>$help{$cmd}{command}") ;
123     if($level > 0) { print(" ($help{$cmd}{level})") ; }
124     print("\n\n") ;
125     print("<P>\n") ;
126     print("<tt>\n") ;
127     print("<bf>$help{$cmd}{command}</bf> $help{$cmd}{description}\n") ;
128     print("</tt>\n") ;
129     print("\n") ;
130
131     # Output the command comments.
132     print("<P>\n") ;
133
134     # Loop through each line of the command comments.
135     # If the first character of the line is whitespace, then use tscreen
136     # Once a tscreen block has started, continue until the next blank line.
137     my $block = 0 ;
138
139     # Is the comment field blank?  Then trying to split will error - lets not.
140     if(!$help{$cmd}{comment})
141         {
142         next;
143         }
144
145     # Work through the comments line by line
146     foreach $line (split('\n', $help{$cmd}{comment}))
147         {
148         # Leading whitespace or not?
149         if($line =~ m/^\s+\S+/)
150             {
151             if(!$block) 
152                 {
153                 $block = 1 ;
154                 print("<tscreen><verb>\n") ; 
155                 }
156             }
157         else
158             {
159             if($block)
160                 {
161                 $block = 0 ;
162                 print("</verb></tscreen>\n") ;
163                 }
164             }
165         print("$line\n") ;
166         }
167     
168     # We fell out of the command comments still in a block - Ouch....
169     if($block)
170         {
171         print("</verb></tscreen>\n\n") ;
172         }
173     }
174
175 print("</article>\n") ;
176
177 # Is it 'cos we is dun ?
178 print STDERR localtime()." ($$) $0 Exiting ($count read)\n" ;
179