]>
git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi2texi.py
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"""
13 @deftypefn {type} {{}} {name}
22 @deftp {{{type}}} {name}
30 EXAMPLE_FMT
= """@example
36 def subst_strong(doc
):
37 """Replaces *foo* by @strong{foo}"""
38 return re
.sub(r
'\*([^*\n]+)\*', r
'@emph{\1}', doc
)
42 """Replaces _foo_ by @emph{foo}"""
43 return re
.sub(r
'\b_([^_\n]+)_\b', r
' @emph{\1} ', doc
)
47 """Replaces @var by @code{var}"""
48 return re
.sub(r
'@([\w-]+)', r
'@code{\1}', doc
)
51 def subst_braces(doc
):
52 """Replaces {} with @{ @}"""
53 return doc
.replace('{', '@{').replace('}', '@}')
56 def texi_example(doc
):
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
)
71 - |: generates an @example
72 - =: generates @section
73 - ==: generates @subsection
74 - 1. or 1): generates an @enumerate @item
75 - */-: generates an @itemize list
78 doc
= subst_braces(doc
)
81 doc
= subst_strong(doc
)
84 for line
in doc
.split('\n'):
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
93 # Make sure to update section "Documentation markup" in
94 # docs/qapi-code-gen.txt when fixing this.
95 if line
.startswith('| '):
96 line
= EXAMPLE_FMT(code
=line
[2:])
97 elif line
.startswith('= '):
98 line
= '@section ' + line
[2:]
99 elif line
.startswith('== '):
100 line
= '@subsection ' + line
[3:]
101 elif re
.match(r
'^([0-9]*\.) ', line
):
103 lines
.append('@enumerate')
105 line
= line
[line
.find(' ')+1:]
106 lines
.append('@item')
107 elif re
.match(r
'^[*-] ', line
):
109 lines
.append('@itemize %s' % {'*': '@bullet',
110 '-': '@minus'}[line
[0]])
112 lines
.append('@item')
114 elif lastempty
and inlist
:
115 lines
.append('@end %s\n' % inlist
)
122 lines
.append('@end %s\n' % inlist
)
123 return '\n'.join(lines
)
127 """Format the main documentation body"""
128 return texi_format(str(doc
.body
)) + '\n'
131 def texi_enum_value(value
):
132 """Format a table of members item for an enumeration value"""
133 return '@item @code{%s}\n' % value
.name
136 def texi_member(member
):
137 """Format a table of members item for an object type member"""
138 typ
= member
.type.doc_type()
139 return '@item @code{%s%s%s}%s\n' % (
143 ' (optional)' if member
.optional
else '')
146 def texi_members(doc
, what
, base
, member_func
):
147 """Format the table of members"""
149 for section
in doc
.args
.itervalues():
153 desc
= 'Not documented'
154 items
+= member_func(section
.member
) + texi_format(desc
) + '\n'
156 items
+= '@item The members of @code{%s}\n' % base
.doc_type()
159 return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what
, items
)
162 def texi_sections(doc
):
163 """Format additional sections following arguments"""
165 for section
in doc
.sections
:
166 name
, doc
= (section
.name
, str(section
))
168 if name
.startswith('Example'):
172 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
173 body
+= '\n\n@b{%s:}\n' % name
179 def texi_entity(doc
, what
, base
=None, member_func
=texi_member
):
180 return (texi_body(doc
)
181 + texi_members(doc
, what
, base
, member_func
)
182 + texi_sections(doc
))
185 class QAPISchemaGenDocVisitor(qapi
.QAPISchemaVisitor
):
190 def visit_begin(self
, schema
):
193 def visit_enum_type(self
, name
, info
, values
, prefix
):
197 self
.out
+= TYPE_FMT(type='Enum',
199 body
=texi_entity(doc
, 'Values',
200 member_func
=texi_enum_value
))
202 def visit_object_type(self
, name
, info
, base
, members
, variants
):
206 elif variants
._tag
_name
: # TODO unclean member access
210 if base
and base
.is_implicit():
214 self
.out
+= TYPE_FMT(type=typ
,
216 body
=texi_entity(doc
, 'Members', base
))
218 def visit_alternate_type(self
, name
, info
, variants
):
222 self
.out
+= TYPE_FMT(type='Alternate',
224 body
=texi_entity(doc
, 'Members'))
226 def visit_command(self
, name
, info
, arg_type
, ret_type
,
227 gen
, success_response
, boxed
):
232 body
= texi_body(doc
)
233 body
+= '\n@b{Arguments:} the members of @code{%s}' % arg_type
.name
234 body
+= texi_sections(doc
)
236 body
= texi_entity(doc
, 'Arguments')
237 self
.out
+= MSG_FMT(type='Command',
241 def visit_event(self
, name
, info
, arg_type
, boxed
):
245 self
.out
+= MSG_FMT(type='Event',
247 body
=texi_entity(doc
, 'Arguments'))
249 def symbol(self
, doc
, entity
):
254 def freeform(self
, doc
):
258 self
.out
+= texi_body(doc
) + texi_sections(doc
)
261 def texi_schema(schema
):
262 """Convert QAPI schema documentation to Texinfo"""
263 gen
= QAPISchemaGenDocVisitor()
264 gen
.visit_begin(schema
)
265 for doc
in schema
.docs
:
267 gen
.symbol(doc
, schema
.lookup_entity(doc
.symbol
))
274 """Takes schema argument, prints result to stdout"""
276 print >>sys
.stderr
, "%s: need exactly 1 argument: SCHEMA" % argv
[0]
279 schema
= qapi
.QAPISchema(argv
[1])
280 if not qapi
.doc_required
:
281 print >>sys
.stderr
, ("%s: need pragma 'doc-required' "
282 "to generate documentation" % argv
[0])
283 print texi_schema(schema
)
286 if __name__
== '__main__':