shithub: choc

Download patch

ref: eccaa5aacec2d1dc15c44b3b4854dd8748b6d4b1
parent: aff22a2eaa4129441ad8123dc87de4ee929d22e2
author: Mike Swanson <[email protected]>
date: Sun Sep 16 00:30:25 EDT 2018

docgen: add Markdown output mode

--- a/man/docgen
+++ b/man/docgen
@@ -116,6 +116,19 @@
 
         return result
 
+    def markdown_output(self):
+        result = "## %s\n\n| Parameter | Description |\n| - | - |\n" % self.description
+
+        self.params.sort()
+
+        for p in self.params:
+            if p.should_show():
+                result += p.markdown_output()
+
+        result = result.rstrip() + "\n"
+
+        return result
+
     def completion_output(self):
         result = ""
 
@@ -262,6 +275,28 @@
 
         return result
 
+    def markdown_output(self):
+        if self.args:
+            name = "%s %s" % (self.name, self.args)
+        else:
+            name = "%s" % self.name
+
+        name = name.replace("|", "\\|")
+
+        text = self.text
+        if self.platform:
+            text += " (%s only)" % self.platform
+
+        text = text.replace("|", "\\|")
+
+        result = "| %s | %s |\n" % (name, text)
+
+        # html escape
+        result = result.replace("<", "&lt;")
+        result = result.replace(">", "&gt;")
+
+        return result
+
     def plaintext_output(self, indent):
         # Build the first line, with the argument on
         start = "  " + self.name
@@ -468,6 +503,14 @@
     for t in targets:
         stdout(t.wiki_output().encode('UTF-8') + b'\n')
 
+def markdown_output(targets, substs, template_file):
+    content = ""
+
+    for t in targets:
+        content += t.markdown_output() + "\n"
+
+    print_template(template_file, substs, content)
+
 def plaintext_output(targets, substs, template_file):
 
     content = ""
@@ -487,7 +530,7 @@
     print_template(template_file, substs, content)
 
 def usage():
-    print("Usage: %s [-V] [-c tag] [-g game] -n program_name -s package_name [ -z shortname ] ( -m | -w | -p ) <dir>..." \
+    print("Usage: %s [-V] [-c tag] [-g game] -n program_name -s package_name [ -z shortname ] ( -M | -m | -w | -p ) <dir>..." \
             % sys.argv[0])
     print("   -c :  Provide documentation for the specified configuration file")
     print("         (matches the given tag name in the source file)")
@@ -494,6 +537,7 @@
     print("   -s :  Package name, e.g. Chocolate Doom (for substitution)")
     print("   -z :  Package short-name, e.g. Chocolate (for substitution)")
     print("   -n :  Program name, e.g. chocolate (for substitution)")
+    print("   -M :  Markdown output")
     print("   -m :  Manpage output")
     print("   -w :  Wikitext output")
     print("   -p :  Plaintext output")
@@ -504,7 +548,7 @@
 
 # Parse command line
 
-opts, args = getopt.getopt(sys.argv[1:], "n:s:z:m:wp:b:c:g:V")
+opts, args = getopt.getopt(sys.argv[1:], "n:s:z:M:m:wp:b:c:g:V")
 
 output_function = None
 template = None
@@ -521,6 +565,9 @@
         substs["@PACKAGE_SHORTNAME@"] = opt[1]
     if opt[0] == "-m":
         output_function = manpage_output
+        template = opt[1]
+    elif opt[0] == "-M":
+        output_function = markdown_output
         template = opt[1]
     elif opt[0] == "-w":
         output_function = wiki_output