]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blobdiff - rpp/blocks/scripts/doc_parse.py
doc: Allow underscores (and markdown syntax) in parameter documentation
[pes-rpp/rpp-simulink.git] / rpp / blocks / scripts / doc_parse.py
index c20b35f2f76e0944747cfdc4ffcfcb68915ff95a..1ccb38c57deac00181ab364d3516788aaaa257b2 100755 (executable)
 #!/usr/bin/env python3
 
+# Copyright (C) 2013-2015 Czech Technical University in Prague
+#
+# Authors:
+#     - Michal Sojka <sojkam1@fel.cvut.cz>
+#
+# This document contains proprietary information belonging to Czech
+# Technical University in Prague. Passing on and copying of this
+# document, and communication of its contents is not permitted
+# without prior written authorization.
+#
+# File : doc_parse.py
+# Abstract:
+
 import yaml
 import argparse
 import Rx
 import sys
 import subprocess
 import os
+import os.path
 from string import Template
 
 parser = argparse.ArgumentParser()
 parser.add_argument("file", help="file to process", nargs='+')
-parser.add_argument("--html", help="output block description as HTML",
+parser.add_argument("--html", help="output block description or help as HTML",
                     action = 'store_true')
 parser.add_argument("--latex", help="output block description as LaTeX",
                     action = 'store_true')
+parser.add_argument("--latex-table", help="output block status table as LaTeX",
+                    action = 'store_true')
+parser.add_argument('--printdesc', help="output block description",
+                    action = 'store_true')
+parser.add_argument('--printhelp', help="output block help",
+                    action = 'store_true')
+parser.add_argument('--maskpromptstring', help="output prompt strings for Simulink mask",
+                    action = 'store_true')
+parser.add_argument('--masktype', help="output mask type for Simulink mask",
+                    action = 'store_true')
+parser.add_argument('--name', help="print block name",
+                    action = 'store_true')
 args = parser.parse_args()
 
 mydir = os.path.dirname(os.path.realpath(__file__))
 
-def print_markdown_as(format, text):
+def markdown_as(format, text):
     sys.stdout.flush()
-    proc = subprocess.Popen(['pandoc', '-f', 'markdown_strict', '-t', format], stdin = subprocess.PIPE)
+    proc = subprocess.Popen(['pandoc', '-f', 'markdown', '-t', format], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
     proc.stdin.write(bytes(text, 'UTF-8'))
-    proc.communicate()
+    (stdout, stderr) = proc.communicate()
     proc.stdin.close()
+    if proc.returncode:
+        raise Exception("pandoc failed: %d" % proc.returncode)
+    return stdout.decode('utf-8').rstrip()
+
+def print_markdown_as(format, text):
+    print(markdown_as(format, text))
 
-def print_latex_desc(doc):
+def print_latex_desc(doc, filename):
     def iodef_str(iodef):
         if iodef is None: return "None"
         str='%d\n\\begin{enumerate}\n' % len(iodef)
         for io in iodef:
             str += Template('\\item {\\bf $name} $type').substitute(io)
             if 'range' in io:
-                str += '  %s' % io['range']
+                str += '  %s' % markdown_as('latex', io['range'])
+            if 'note' in io:
+                str += '  %s' % markdown_as('latex', io['note'])
             str += '\n'
         return str +'\end{enumerate}'
 
     print("\\newpage\n")
-    print("\\subsubsection{%s}\n" % doc['Name'])
+    print("\\subsection{%s}" % doc['Name'])
+    print("\\label{sec:block:%s}\n" % os.path.basename(filename))
     print("\\begin{description}\n")
     print("\\item[Inputs:]     %s\n" % iodef_str(doc['Inputs']))
     print("\\item[Outputs:]    %s\n" % iodef_str(doc['Outputs']))
     print("\\item[Parameters:] %s\n" % iodef_str(doc['Parameters']))
     print("\\end{description}\n")
 
-    print_markdown_as('latex', doc['Description'])
-
-    print("\n\\textbf{Status:}")
-    print("\\begin{multicols}{3}")
-    if doc['Status']['Tested']:
-        print("\\begin{compactitem}")
-        print("\\item \\textbf{Tested}:")
-        print(" \\begin{compactitem}")
-        print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Status']['Tested']]))
-        print(" \\end{compactitem}")
-        print("\\end{compactitem}")
-    else:
-        print("\\ ")
-
-    print("\\vfill\\columnbreak")
+    print_markdown_as('latex', doc['Help'])
 
-    if doc['Status']['Untested']:
-        print("\\begin{compactitem}")
-        print("\\item \\textbf{Untested}:")
-        print(" \\begin{compactitem}")
-        print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Status']['Untested']]))
-        print(" \\end{compactitem}")
-        print("\\end{compactitem}")
-    else:
-        print("\\ ")
-
-    print("\\vfill\\columnbreak")
-
-    if doc['Status']['Not working']:
-        print("\\begin{compactitem}")
-        print("\\item \\textbf{Not working}:")
-        print(" \\begin{compactitem}")
-        print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Status']['Not working']]))
-        print(" \\end{compactitem}")
-        print("\\end{compactitem}")
-    else:
-        print("\\ ")
-
-    print("\\end{multicols}\n")
-
-    if 'RPP API functions used' in doc:
-        print("\\textbf{RPP API functions used:}")
+    if doc.get('RPP API functions used', None) is not None:
+        print("\\paragraph{RPP API functions used:}")
         print("\\begin{compactitem}")
         print("\n".join(["\\item \\texttt{%s}" % i.replace('_', '\\_') for i in doc['RPP API functions used']]))
         print("\\end{compactitem}")
 
-    if 'Relevant demos' in doc:
-        print("\\textbf{Relevant demos:}")
+    if doc.get('Relevant demos', None) is not None:
+        print("\\paragraph{Relevant demos:}")
         print("\\begin{compactitem}")
         print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Relevant demos']]))
         print("\\end{compactitem}")
 
-
 def process_file(f):
     yaml_doc = None
     for line in open(f, 'r'):
@@ -120,11 +116,33 @@ def process_file(f):
         raise Exception("Validation error in %s: %s" % (f, e))
         #raise
 
-    if args.html:
-        print_markdown_as('html', doc['Description'])
-    if args.latex:
-        print_latex_desc(doc)
-
+    fmt = 'html' if args.html else 'markdown'
 
+    if args.printdesc:
+        print_markdown_as(fmt, doc['Description'])
+    if args.printhelp:
+        print_markdown_as(fmt, doc['Help'])
+    if args.latex:
+        print_latex_desc(doc, f)
+    if args.latex_table:
+        global last_category
+        if last_category == doc['Category']: doc['Category']=''
+        doc['Header'] = doc['Header'].replace('_', '\\_')
+        print(Template("$Category & $Name & $Status & $Mnemonic & \\texttt{$Header} \\\\").substitute(doc))
+        if doc['Category']:
+            last_category = doc['Category']
+    if args.masktype:
+        print('RPP {name}'.format(name=doc['Name']))
+    if args.name:
+        print(doc['Name'])
+    if args.maskpromptstring and doc['Parameters'] != None:
+        print('|'.join(['{name}{range}{colon}'.format(name=par['name'],
+                                                      range=(' '+par['range'] if ('range' in par and (par['type'] in ['double'] or
+                                                                                                'int' in par['type']))
+                                                             else ''),
+                                                      colon=':' if par['type'] != 'bool' else '')
+                        for par in doc['Parameters']]))
+
+last_category = None
 for f in args.file:
     process_file(f)