Merge remote-tracking branch 'remotes/gkurz/tags/for-upstream' into staging
[qemu.git] / scripts / qapi2texi.py
blobbf1c57b2e24f5f372b0dc30bc505262ad5347bbe
1 #!/usr/bin/env python
2 # QAPI texi generator
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"""
7 from __future__ import print_function
8 import re
9 import sys
11 import qapi
13 MSG_FMT = """
14 @deftypefn {type} {{}} {name}
16 {body}
17 @end deftypefn
19 """.format
21 TYPE_FMT = """
22 @deftp {{{type}}} {name}
24 {body}
25 @end deftp
27 """.format
29 EXAMPLE_FMT = """@example
30 {code}
31 @end example
32 """.format
35 def subst_strong(doc):
36 """Replaces *foo* by @strong{foo}"""
37 return re.sub(r'\*([^*\n]+)\*', r'@strong{\1}', doc)
40 def subst_emph(doc):
41 """Replaces _foo_ by @emph{foo}"""
42 return re.sub(r'\b_([^_\n]+)_\b', r'@emph{\1}', doc)
45 def subst_vars(doc):
46 """Replaces @var by @code{var}"""
47 return re.sub(r'@([\w-]+)', r'@code{\1}', doc)
50 def subst_braces(doc):
51 """Replaces {} with @{ @}"""
52 return doc.replace('{', '@{').replace('}', '@}')
55 def texi_example(doc):
56 """Format @example"""
57 # TODO: Neglects to escape @ characters.
58 # We should probably escape them in subst_braces(), and rename the
59 # function to subst_special() or subs_texi_special(). If we do that, we
60 # need to delay it until after subst_vars() in texi_format().
61 doc = subst_braces(doc).strip('\n')
62 return EXAMPLE_FMT(code=doc)
65 def texi_format(doc):
66 """
67 Format documentation
69 Lines starting with:
70 - |: generates an @example
71 - =: generates @section
72 - ==: generates @subsection
73 - 1. or 1): generates an @enumerate @item
74 - */-: generates an @itemize list
75 """
76 ret = ''
77 doc = subst_braces(doc)
78 doc = subst_vars(doc)
79 doc = subst_emph(doc)
80 doc = subst_strong(doc)
81 inlist = ''
82 lastempty = False
83 for line in doc.split('\n'):
84 empty = line == ''
86 # FIXME: Doing this in a single if / elif chain is
87 # problematic. For instance, a line without markup terminates
88 # a list if it follows a blank line (reaches the final elif),
89 # but a line with some *other* markup, such as a = title
90 # doesn't.
92 # Make sure to update section "Documentation markup" in
93 # docs/devel/qapi-code-gen.txt when fixing this.
94 if line.startswith('| '):
95 line = EXAMPLE_FMT(code=line[2:])
96 elif line.startswith('= '):
97 line = '@section ' + line[2:]
98 elif line.startswith('== '):
99 line = '@subsection ' + line[3:]
100 elif re.match(r'^([0-9]*\.) ', line):
101 if not inlist:
102 ret += '@enumerate\n'
103 inlist = 'enumerate'
104 ret += '@item\n'
105 line = line[line.find(' ')+1:]
106 elif re.match(r'^[*-] ', line):
107 if not inlist:
108 ret += '@itemize %s\n' % {'*': '@bullet',
109 '-': '@minus'}[line[0]]
110 inlist = 'itemize'
111 ret += '@item\n'
112 line = line[2:]
113 elif lastempty and inlist:
114 ret += '@end %s\n\n' % inlist
115 inlist = ''
117 lastempty = empty
118 ret += line + '\n'
120 if inlist:
121 ret += '@end %s\n\n' % inlist
122 return ret
125 def texi_body(doc):
126 """Format the main documentation body"""
127 return texi_format(doc.body.text)
130 def texi_enum_value(value):
131 """Format a table of members item for an enumeration value"""
132 return '@item @code{%s}\n' % value.name
135 def texi_member(member, suffix=''):
136 """Format a table of members item for an object type member"""
137 typ = member.type.doc_type()
138 return '@item @code{%s%s%s}%s%s\n' % (
139 member.name,
140 ': ' if typ else '',
141 typ if typ else '',
142 ' (optional)' if member.optional else '',
143 suffix)
146 def texi_members(doc, what, base, variants, member_func):
147 """Format the table of members"""
148 items = ''
149 for section in doc.args.values():
150 # TODO Drop fallbacks when undocumented members are outlawed
151 if section.text:
152 desc = texi_format(section.text)
153 elif (variants and variants.tag_member == section.member
154 and not section.member.type.doc_type()):
155 values = section.member.type.member_names()
156 members_text = ', '.join(['@t{"%s"}' % v for v in values])
157 desc = 'One of ' + members_text + '\n'
158 else:
159 desc = 'Not documented\n'
160 items += member_func(section.member) + desc
161 if base:
162 items += '@item The members of @code{%s}\n' % base.doc_type()
163 if variants:
164 for v in variants.variants:
165 when = ' when @code{%s} is @t{"%s"}' % (
166 variants.tag_member.name, v.name)
167 if v.type.is_implicit():
168 assert not v.type.base and not v.type.variants
169 for m in v.type.local_members:
170 items += member_func(m, when)
171 else:
172 items += '@item The members of @code{%s}%s\n' % (
173 v.type.doc_type(), when)
174 if not items:
175 return ''
176 return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items)
179 def texi_sections(doc):
180 """Format additional sections following arguments"""
181 body = ''
182 for section in doc.sections:
183 if section.name:
184 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
185 body += '\n@b{%s:}\n' % section.name
186 if section.name and section.name.startswith('Example'):
187 body += texi_example(section.text)
188 else:
189 body += texi_format(section.text)
190 return body
193 def texi_entity(doc, what, base=None, variants=None,
194 member_func=texi_member):
195 return (texi_body(doc)
196 + texi_members(doc, what, base, variants, member_func)
197 + texi_sections(doc))
200 class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
201 def __init__(self):
202 self.out = None
203 self.cur_doc = None
205 def visit_begin(self, schema):
206 self.out = ''
208 def visit_enum_type(self, name, info, values, prefix):
209 doc = self.cur_doc
210 self.out += TYPE_FMT(type='Enum',
211 name=doc.symbol,
212 body=texi_entity(doc, 'Values',
213 member_func=texi_enum_value))
215 def visit_object_type(self, name, info, base, members, variants):
216 doc = self.cur_doc
217 if base and base.is_implicit():
218 base = None
219 self.out += TYPE_FMT(type='Object',
220 name=doc.symbol,
221 body=texi_entity(doc, 'Members', base, variants))
223 def visit_alternate_type(self, name, info, variants):
224 doc = self.cur_doc
225 self.out += TYPE_FMT(type='Alternate',
226 name=doc.symbol,
227 body=texi_entity(doc, 'Members'))
229 def visit_command(self, name, info, arg_type, ret_type,
230 gen, success_response, boxed):
231 doc = self.cur_doc
232 if boxed:
233 body = texi_body(doc)
234 body += ('\n@b{Arguments:} the members of @code{%s}\n'
235 % arg_type.name)
236 body += texi_sections(doc)
237 else:
238 body = texi_entity(doc, 'Arguments')
239 self.out += MSG_FMT(type='Command',
240 name=doc.symbol,
241 body=body)
243 def visit_event(self, name, info, arg_type, boxed):
244 doc = self.cur_doc
245 self.out += MSG_FMT(type='Event',
246 name=doc.symbol,
247 body=texi_entity(doc, 'Arguments'))
249 def symbol(self, doc, entity):
250 if self.out:
251 self.out += '\n'
252 self.cur_doc = doc
253 entity.visit(self)
254 self.cur_doc = None
256 def freeform(self, doc):
257 assert not doc.args
258 if self.out:
259 self.out += '\n'
260 self.out += texi_body(doc) + texi_sections(doc)
263 def texi_schema(schema):
264 """Convert QAPI schema documentation to Texinfo"""
265 gen = QAPISchemaGenDocVisitor()
266 gen.visit_begin(schema)
267 for doc in schema.docs:
268 if doc.symbol:
269 gen.symbol(doc, schema.lookup_entity(doc.symbol))
270 else:
271 gen.freeform(doc)
272 return gen.out
275 def main(argv):
276 """Takes schema argument, prints result to stdout"""
277 if len(argv) != 2:
278 print("%s: need exactly 1 argument: SCHEMA" % argv[0], file=sys.stderr)
279 sys.exit(1)
281 schema = qapi.QAPISchema(argv[1])
282 if not qapi.doc_required:
283 print("%s: need pragma 'doc-required' "
284 "to generate documentation" % argv[0], file=sys.stderr)
285 sys.exit(1)
286 print(texi_schema(schema))
289 if __name__ == '__main__':
290 main(sys.argv)