]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/scripts/doc_parse.py
First step of inlining block documentation into S-Functions source
[pes-rpp/rpp-simulink.git] / rpp / blocks / scripts / doc_parse.py
1 #!/usr/bin/env python3
2
3 import yaml
4 import argparse
5 import Rx
6 import sys
7 import subprocess
8 import os
9 from string import Template
10
11 parser = argparse.ArgumentParser()
12 parser.add_argument("file", help="file to process", nargs='+')
13 parser.add_argument("--html", help="output block description as HTML",
14                     action = 'store_true')
15 parser.add_argument("--latex", help="output block description as LaTeX",
16                     action = 'store_true')
17 args = parser.parse_args()
18
19 mydir = os.path.dirname(os.path.realpath(__file__))
20
21 def print_markdown_as(format, text):
22     sys.stdout.flush()
23     proc = subprocess.Popen(['pandoc', '-f', 'markdown_strict', '-t', format], stdin = subprocess.PIPE)
24     proc.stdin.write(bytes(text, 'UTF-8'))
25     proc.communicate()
26     proc.stdin.close()
27
28 def print_latex_desc(doc):
29     def iodef_str(iodef):
30         if iodef is None: return "None"
31         str='%d\n\\begin{enumerate}\n' % len(iodef)
32         for io in iodef:
33             str += Template('\\item {\\bf $name} $type').substitute(io)
34             if 'range' in io:
35                 str += '  %s' % io['range']
36             str += '\n'
37         return str +'\end{enumerate}'
38
39     print("\\newpage\n")
40     print("\\subsubsection{%s}\n" % doc['Name'])
41     print("\\begin{description}\n")
42     print("\\item[Inputs:]     %s\n" % iodef_str(doc['Inputs']))
43     print("\\item[Outputs:]    %s\n" % iodef_str(doc['Outputs']))
44     print("\\item[Parameters:] %s\n" % iodef_str(doc['Parameters']))
45     print("\\end{description}\n")
46
47     print_markdown_as('latex', doc['Description'])
48
49     print("\n\\textbf{Status:}")
50     print("\\begin{multicols}{3}")
51     if doc['Status']['Tested']:
52         print("\\begin{compactitem}")
53         print("\\item \\textbf{Tested}:")
54         print(" \\begin{compactitem}")
55         print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Status']['Tested']]))
56         print(" \\end{compactitem}")
57         print("\\end{compactitem}")
58     else:
59         print("\\ ")
60
61     print("\\vfill\\columnbreak")
62
63     if doc['Status']['Untested']:
64         print("\\begin{compactitem}")
65         print("\\item \\textbf{Untested}:")
66         print(" \\begin{compactitem}")
67         print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Status']['Untested']]))
68         print(" \\end{compactitem}")
69         print("\\end{compactitem}")
70     else:
71         print("\\ ")
72
73     print("\\vfill\\columnbreak")
74
75     if doc['Status']['Not working']:
76         print("\\begin{compactitem}")
77         print("\\item \\textbf{Not working}:")
78         print(" \\begin{compactitem}")
79         print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Status']['Not working']]))
80         print(" \\end{compactitem}")
81         print("\\end{compactitem}")
82     else:
83         print("\\ ")
84
85     print("\\end{multicols}\n")
86
87     if 'RPP API functions used' in doc:
88         print("\\textbf{RPP API functions used:}")
89         print("\\begin{compactitem}")
90         print("\n".join(["\\item \\texttt{%s}" % i.replace('_', '\\_') for i in doc['RPP API functions used']]))
91         print("\\end{compactitem}")
92
93     if 'Relevant demos' in doc:
94         print("\\textbf{Relevant demos:}")
95         print("\\begin{compactitem}")
96         print("\n".join(["\\item %s" % i.replace('_', '\\_') for i in doc['Relevant demos']]))
97         print("\\end{compactitem}")
98
99
100 def process_file(f):
101     yaml_doc = None
102     for line in open(f, 'r'):
103         if yaml_doc:
104             yaml_doc += line
105         elif line.startswith("%YAML"):
106             yaml_doc = line
107         if line == '...\n':
108             break
109     if yaml_doc is None:
110         raise Exception("No line starting with '%%YAML' found in %s" % f)
111
112     doc = yaml.load(yaml_doc)
113     schema = yaml.load(open(mydir+'/schema.yaml', 'r'))
114
115     rx = Rx.Factory({ "register_core_types": True })
116     rpp_block_doc_schema = rx.make_schema(schema)
117     try:
118         rpp_block_doc_schema.check(doc)
119     except Rx.ValidationError as e:
120         raise Exception("Validation error in %s: %s" % (f, e))
121         #raise
122
123     if args.html:
124         print_markdown_as('html', doc['Description'])
125     if args.latex:
126         print_latex_desc(doc)
127
128
129 for f in args.file:
130     process_file(f)