3 # Command line parameter self-documentation tool. Reads comments from
4 # the source code in the following form:
7 # // @arg <extra arguments>
8 # // @category Category
9 # // @platform <some platform that the parameter is specific to>
11 # // Long description of the parameter
14 # something_involving = M_CheckParm("-param");
16 # From this, a manpage can be automatically generated of the command
25 def __init__(self
, description
):
26 self
.description
= description
29 def add_param(self
, param
):
30 self
.params
.append(param
)
32 # Find the maximum width of a parameter in this category
34 def paramtext_width(self
):
50 def plaintext_output(self
):
51 result
= "=== %s ===\n\n" % self
.description
55 w
= self
.paramtext_width()
59 result
+= p
.plaintext_output(w
)
61 result
= result
.rstrip() + "\n"
65 def manpage_output(self
):
66 result
= ".SH " + self
.description
.upper() + "\n"
73 result
+= p
.manpage_output()
77 def wiki_output(self
):
78 result
= "=== %s ===\n" % self
.description
84 result
+= "; " + p
.wiki_output() + "\n"
86 # Escape special HTML characters
88 result
= result
.replace("&", "&")
89 result
= result
.replace("<", "<")
90 result
= result
.replace(">", ">")
95 None: Category("General options"),
96 "video": Category("Display options"),
97 "demo": Category("Demo options"),
98 "net": Category("Networking options"),
99 "mod": Category("Dehacked and WAD merging"),
100 "compat": Category("Compatibility"),
105 # Show options that are in Vanilla Doom? Or only new options?
107 show_vanilla_options
= True
110 def __cmp__(self
, other
):
111 if self
.name
< other
.name
:
122 self
.vanilla_option
= False
124 def should_show(self
):
125 return not self
.vanilla_option
or show_vanilla_options
127 def add_text(self
, text
):
131 match
= re
.match('@(\S+)\s*(.*)', text
)
134 raise "Malformed option line: %s" % text
136 option_type
= match
.group(1)
137 data
= match
.group(2)
139 if option_type
== "arg":
141 elif option_type
== "platform":
143 elif option_type
== "category":
145 elif option_type
== "vanilla":
146 self
.vanilla_option
= True
148 raise "Unknown option type '%s'" % option_type
151 self
.text
+= text
+ " "
153 def manpage_output(self
):
157 result
+= " " + self
.args
159 result
= '\\fB' + result
+ '\\fR'
164 result
+= "[%s only] " % self
.platform
166 escaped
= re
.sub('\\\\', '\\\\\\\\', self
.text
)
168 result
+= escaped
+ "\n"
172 def wiki_output(self
):
176 result
+= " " + self
.args
180 result
+= add_wiki_links(self
.text
)
183 result
+= "'''(%s only)'''" % self
.platform
187 def plaintext_output(self
, w
):
189 # Build the first line, with the argument on
191 line
= " " + self
.name
193 line
+= " " + self
.args
195 # pad up to the plaintext width
197 line
+= " " * (w
- len(line
))
199 # Build the description text
201 description
= self
.text
204 description
+= " (%s only)" % self
.platform
206 # Build the complete text for the argument
207 # Split the description into words and add a word at a time
210 for word
in re
.split('\s+', description
):
212 # Break onto the next line?
214 if len(line
) + len(word
) + 1 > 75:
215 result
+= line
+ "\n"
222 result
+= line
+ "\n\n"
226 # Read list of wiki pages
228 def read_wikipages():
229 f
= open("wikipages")
235 line
= re
.sub('\#.*$', '', line
)
237 if not re
.match('^\s*$', line
):
238 wikipages
.append(line
)
242 # Add wiki page links
244 def add_wiki_links(text
):
245 for pagename
in wikipages
:
246 page_re
= re
.compile('(%s)' % pagename
, re
.IGNORECASE
)
247 # text = page_re.sub("SHOES", text)
248 text
= page_re
.sub('[[\\1]]', text
)
252 def process_file(file):
257 waiting_for_checkparm
= False
262 # Currently reading a doc comment?
267 if not re
.match('\s*//', line
):
268 waiting_for_checkparm
= True
270 # Waiting for the M_CheckParm call that contains the
271 # name of the parameter we are documenting?
273 if waiting_for_checkparm
:
274 match
= re
.search('M_CheckParm\s*\(\s*"(.*?)"\s*\)', line
)
277 # Found the name! Finished documenting this
280 param
.name
= match
.group(1)
281 categories
[param
.category
].add_param(param
)
285 # More documentation text
287 munged_line
= re
.sub('\s*\/\/\s*', '', line
, 1)
288 munged_line
= re
.sub('\s*$', '', munged_line
)
289 param
.add_text(munged_line
)
291 # Check for start of a doc comment
293 if re
.search("//!", line
):
295 waiting_for_checkparm
= False
299 def process_files(dir):
300 # Process all C source files.
302 files
= glob
.glob(dir + "/*.c")
307 def print_file_contents(file):
317 def manpage_output(dir):
321 print_file_contents("header")
323 print categories
[None].manpage_output()
327 print categories
[c
].manpage_output()
329 print_file_contents("footer")
331 def wiki_output(dir):
335 print categories
[None].wiki_output()
339 print categories
[c
].wiki_output()
341 def plaintext_output(dir):
344 print "== Command line parameters =="
346 print "This is a list of the command line parameters supported by "
347 print "Chocolate Doom. A number of additional parameters are supported "
348 print "in addition to those present in Vanilla Doom. "
351 print categories
[None].plaintext_output()
355 print categories
[c
].plaintext_output()
358 print "Usage: %s [-V] ( -m | -w | -p ) <directory>" % sys
.argv
[0]
359 print " -m : Manpage output"
360 print " -w : Wikitext output"
361 print " -p : Plaintext output"
362 print " -V : Don't show Vanilla Doom options"
367 opts
, args
= getopt
.getopt(sys
.argv
[1:], "mwpV")
369 output_function
= None
373 output_function
= manpage_output
375 output_function
= wiki_output
377 output_function
= plaintext_output
379 show_vanilla_options
= False
381 if output_function
== None or len(args
) != 1:
384 output_function(args
[0])