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