blob: 7083d0c0d011e1fe060b821f7e5533bb74161151 [file] [log] [blame]
Marc-André Lureau3313b612017-01-13 15:41:29 +01001#!/usr/bin/env python
2# QAPI texi generator
3#
4# This work is licensed under the terms of the GNU LGPL, version 2+.
5# See the COPYING file in the top-level directory.
6"""This script produces the documentation of a qapi schema in texinfo format"""
7import re
8import sys
9
10import qapi
11
Marc-André Lureau597494a2017-01-25 17:03:07 +040012MSG_FMT = """
Marc-André Lureau3313b612017-01-13 15:41:29 +010013@deftypefn {type} {{}} {name}
14
15{body}
16
17@end deftypefn
18
19""".format
20
Marc-André Lureau597494a2017-01-25 17:03:07 +040021TYPE_FMT = """
Marc-André Lureau3313b612017-01-13 15:41:29 +010022@deftp {{{type}}} {name}
23
24{body}
25
26@end deftp
27
28""".format
29
30EXAMPLE_FMT = """@example
31{code}
32@end example
33""".format
34
35
36def subst_strong(doc):
37 """Replaces *foo* by @strong{foo}"""
38 return re.sub(r'\*([^*\n]+)\*', r'@emph{\1}', doc)
39
40
41def subst_emph(doc):
42 """Replaces _foo_ by @emph{foo}"""
43 return re.sub(r'\b_([^_\n]+)_\b', r' @emph{\1} ', doc)
44
45
46def subst_vars(doc):
47 """Replaces @var by @code{var}"""
48 return re.sub(r'@([\w-]+)', r'@code{\1}', doc)
49
50
51def subst_braces(doc):
52 """Replaces {} with @{ @}"""
Markus Armbrusteref801a92017-03-15 13:57:08 +010053 return doc.replace('{', '@{').replace('}', '@}')
Marc-André Lureau3313b612017-01-13 15:41:29 +010054
55
56def texi_example(doc):
57 """Format @example"""
58 # TODO: Neglects to escape @ characters.
59 # We should probably escape them in subst_braces(), and rename the
60 # function to subst_special() or subs_texi_special(). If we do that, we
61 # need to delay it until after subst_vars() in texi_format().
62 doc = subst_braces(doc).strip('\n')
63 return EXAMPLE_FMT(code=doc)
64
65
66def texi_format(doc):
67 """
68 Format documentation
69
70 Lines starting with:
71 - |: generates an @example
72 - =: generates @section
73 - ==: generates @subsection
74 - 1. or 1): generates an @enumerate @item
75 - */-: generates an @itemize list
76 """
77 lines = []
78 doc = subst_braces(doc)
79 doc = subst_vars(doc)
80 doc = subst_emph(doc)
81 doc = subst_strong(doc)
Markus Armbrusteref801a92017-03-15 13:57:08 +010082 inlist = ''
Marc-André Lureau3313b612017-01-13 15:41:29 +010083 lastempty = False
84 for line in doc.split('\n'):
Markus Armbrusteref801a92017-03-15 13:57:08 +010085 empty = line == ''
Marc-André Lureau3313b612017-01-13 15:41:29 +010086
87 # FIXME: Doing this in a single if / elif chain is
88 # problematic. For instance, a line without markup terminates
89 # a list if it follows a blank line (reaches the final elif),
90 # but a line with some *other* markup, such as a = title
91 # doesn't.
92 #
93 # Make sure to update section "Documentation markup" in
94 # docs/qapi-code-gen.txt when fixing this.
Markus Armbrusteref801a92017-03-15 13:57:08 +010095 if line.startswith('| '):
Marc-André Lureau3313b612017-01-13 15:41:29 +010096 line = EXAMPLE_FMT(code=line[2:])
Markus Armbrusteref801a92017-03-15 13:57:08 +010097 elif line.startswith('= '):
98 line = '@section ' + line[2:]
99 elif line.startswith('== '):
100 line = '@subsection ' + line[3:]
Marc-André Lureau3313b612017-01-13 15:41:29 +0100101 elif re.match(r'^([0-9]*\.) ', line):
102 if not inlist:
Markus Armbrusteref801a92017-03-15 13:57:08 +0100103 lines.append('@enumerate')
104 inlist = 'enumerate'
105 line = line[line.find(' ')+1:]
106 lines.append('@item')
Marc-André Lureau3313b612017-01-13 15:41:29 +0100107 elif re.match(r'^[*-] ', line):
108 if not inlist:
Markus Armbrusteref801a92017-03-15 13:57:08 +0100109 lines.append('@itemize %s' % {'*': '@bullet',
110 '-': '@minus'}[line[0]])
111 inlist = 'itemize'
112 lines.append('@item')
Marc-André Lureau3313b612017-01-13 15:41:29 +0100113 line = line[2:]
114 elif lastempty and inlist:
Markus Armbrusteref801a92017-03-15 13:57:08 +0100115 lines.append('@end %s\n' % inlist)
116 inlist = ''
Marc-André Lureau3313b612017-01-13 15:41:29 +0100117
118 lastempty = empty
119 lines.append(line)
120
121 if inlist:
Markus Armbrusteref801a92017-03-15 13:57:08 +0100122 lines.append('@end %s\n' % inlist)
123 return '\n'.join(lines)
Marc-André Lureau3313b612017-01-13 15:41:29 +0100124
125
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100126def texi_body(doc):
127 """Format the main documentation body"""
128 return texi_format(str(doc.body)) + '\n'
Markus Armbruster860e8772017-03-15 13:57:04 +0100129
Marc-André Lureau3313b612017-01-13 15:41:29 +0100130
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100131def texi_enum_value(value):
132 """Format a table of members item for an enumeration value"""
Markus Armbruster71d918a2017-03-15 13:57:09 +0100133 return '@item @code{%s}\n' % value.name
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100134
135
136def texi_member(member):
137 """Format a table of members item for an object type member"""
Markus Armbruster691e0312017-03-15 13:57:14 +0100138 typ = member.type.doc_type()
139 return '@item @code{%s%s%s}%s\n' % (
140 member.name,
141 ': ' if typ else '',
142 typ if typ else '',
143 ' (optional)' if member.optional else '')
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100144
145
Markus Armbruster88f63462017-03-15 13:57:15 +0100146def texi_members(doc, what, base, member_func):
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100147 """Format the table of members"""
148 items = ''
149 for section in doc.args.itervalues():
Markus Armbruster5da19f12017-03-15 13:57:11 +0100150 if section.content:
151 desc = str(section)
152 else:
153 desc = 'Not documented'
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100154 items += member_func(section.member) + texi_format(desc) + '\n'
Markus Armbruster88f63462017-03-15 13:57:15 +0100155 if base:
156 items += '@item The members of @code{%s}\n' % base.doc_type()
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100157 if not items:
158 return ''
Markus Armbruster2a1183c2017-03-15 13:57:10 +0100159 return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items)
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100160
161
162def texi_sections(doc):
163 """Format additional sections following arguments"""
164 body = ''
Marc-André Lureau3313b612017-01-13 15:41:29 +0100165 for section in doc.sections:
166 name, doc = (section.name, str(section))
167 func = texi_format
Markus Armbrusteref801a92017-03-15 13:57:08 +0100168 if name.startswith('Example'):
Marc-André Lureau3313b612017-01-13 15:41:29 +0100169 func = texi_example
170
171 if name:
Marc-André Lureau1ede77d2017-02-17 13:34:16 +0400172 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
Markus Armbrusteref801a92017-03-15 13:57:08 +0100173 body += '\n\n@b{%s:}\n' % name
Marc-André Lureau1ede77d2017-02-17 13:34:16 +0400174
175 body += func(doc)
Marc-André Lureau3313b612017-01-13 15:41:29 +0100176 return body
177
178
Markus Armbruster88f63462017-03-15 13:57:15 +0100179def texi_entity(doc, what, base=None, member_func=texi_member):
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100180 return (texi_body(doc)
Markus Armbruster88f63462017-03-15 13:57:15 +0100181 + texi_members(doc, what, base, member_func)
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100182 + texi_sections(doc))
Marc-André Lureau3313b612017-01-13 15:41:29 +0100183
184
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100185class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
186 def __init__(self):
187 self.out = None
188 self.cur_doc = None
Marc-André Lureau3313b612017-01-13 15:41:29 +0100189
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100190 def visit_begin(self, schema):
191 self.out = ''
192
193 def visit_enum_type(self, name, info, values, prefix):
194 doc = self.cur_doc
195 if self.out:
196 self.out += '\n'
197 self.out += TYPE_FMT(type='Enum',
198 name=doc.symbol,
Markus Armbruster2a1183c2017-03-15 13:57:10 +0100199 body=texi_entity(doc, 'Values',
Markus Armbruster2c99f5f2017-03-15 13:57:12 +0100200 member_func=texi_enum_value))
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100201
202 def visit_object_type(self, name, info, base, members, variants):
203 doc = self.cur_doc
204 if not variants:
205 typ = 'Struct'
206 elif variants._tag_name: # TODO unclean member access
207 typ = 'Flat Union'
208 else:
209 typ = 'Simple Union'
Markus Armbruster88f63462017-03-15 13:57:15 +0100210 if base and base.is_implicit():
211 base = None
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100212 if self.out:
213 self.out += '\n'
214 self.out += TYPE_FMT(type=typ,
215 name=doc.symbol,
Markus Armbruster88f63462017-03-15 13:57:15 +0100216 body=texi_entity(doc, 'Members', base))
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100217
218 def visit_alternate_type(self, name, info, variants):
219 doc = self.cur_doc
220 if self.out:
221 self.out += '\n'
222 self.out += TYPE_FMT(type='Alternate',
223 name=doc.symbol,
Markus Armbruster2a1183c2017-03-15 13:57:10 +0100224 body=texi_entity(doc, 'Members'))
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100225
226 def visit_command(self, name, info, arg_type, ret_type,
227 gen, success_response, boxed):
228 doc = self.cur_doc
229 if self.out:
230 self.out += '\n'
Markus Armbrusterc2dd3112017-03-15 13:57:13 +0100231 if boxed:
232 body = texi_body(doc)
233 body += '\n@b{Arguments:} the members of @code{%s}' % arg_type.name
234 body += texi_sections(doc)
235 else:
236 body = texi_entity(doc, 'Arguments')
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100237 self.out += MSG_FMT(type='Command',
238 name=doc.symbol,
Markus Armbrusterc2dd3112017-03-15 13:57:13 +0100239 body=body)
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100240
241 def visit_event(self, name, info, arg_type, boxed):
242 doc = self.cur_doc
243 if self.out:
244 self.out += '\n'
245 self.out += MSG_FMT(type='Event',
246 name=doc.symbol,
Markus Armbruster2a1183c2017-03-15 13:57:10 +0100247 body=texi_entity(doc, 'Arguments'))
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100248
249 def symbol(self, doc, entity):
250 self.cur_doc = doc
251 entity.visit(self)
252 self.cur_doc = None
253
254 def freeform(self, doc):
255 assert not doc.args
256 if self.out:
257 self.out += '\n'
258 self.out += texi_body(doc) + texi_sections(doc)
Marc-André Lureau3313b612017-01-13 15:41:29 +0100259
260
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100261def texi_schema(schema):
262 """Convert QAPI schema documentation to Texinfo"""
263 gen = QAPISchemaGenDocVisitor()
264 gen.visit_begin(schema)
265 for doc in schema.docs:
266 if doc.symbol:
267 gen.symbol(doc, schema.lookup_entity(doc.symbol))
268 else:
269 gen.freeform(doc)
270 return gen.out
Marc-André Lureau3313b612017-01-13 15:41:29 +0100271
272
273def main(argv):
274 """Takes schema argument, prints result to stdout"""
275 if len(argv) != 2:
276 print >>sys.stderr, "%s: need exactly 1 argument: SCHEMA" % argv[0]
277 sys.exit(1)
278
279 schema = qapi.QAPISchema(argv[1])
Markus Armbrusterbc52d032017-03-15 13:56:51 +0100280 if not qapi.doc_required:
281 print >>sys.stderr, ("%s: need pragma 'doc-required' "
282 "to generate documentation" % argv[0])
Markus Armbrusteraa964b72017-03-15 13:57:05 +0100283 print texi_schema(schema)
Marc-André Lureau3313b612017-01-13 15:41:29 +0100284
285
Markus Armbrusteref801a92017-03-15 13:57:08 +0100286if __name__ == '__main__':
Marc-André Lureau3313b612017-01-13 15:41:29 +0100287 main(sys.argv)