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}
21 @deftp {{{type}}} {name}
28 EXAMPLE_FMT
= """@example
34 def subst_strong(doc
):
35 """Replaces *foo* by @strong{foo}"""
36 return re
.sub(r
'\*([^*\n]+)\*', r
'@strong{\1}', doc
)
40 """Replaces _foo_ by @emph{foo}"""
41 return re
.sub(r
'\b_([^_\n]+)_\b', r
'@emph{\1}', doc
)
45 """Replaces @var by @code{var}"""
46 return re
.sub(r
'@([\w-]+)', r
'@code{\1}', doc
)
49 def subst_braces(doc
):
50 """Replaces {} with @{ @}"""
51 return doc
.replace('{', '@{').replace('}', '@}')
54 def texi_example(doc
):
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
)
69 - |: generates an @example
70 - =: generates @section
71 - ==: generates @subsection
72 - 1. or 1): generates an @enumerate @item
73 - */-: generates an @itemize list
76 doc
= subst_braces(doc
)
79 doc
= subst_strong(doc
)
82 for line
in doc
.split('\n'):
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
91 # Make sure to update section "Documentation markup" in
92 # docs/devel/qapi-code-gen.txt when fixing this.
93 if line
.startswith('| '):
94 line
= EXAMPLE_FMT(code
=line
[2:])
95 elif line
.startswith('= '):
96 line
= '@section ' + line
[2:]
97 elif line
.startswith('== '):
98 line
= '@subsection ' + line
[3:]
99 elif re
.match(r
'^([0-9]*\.) ', line
):
101 ret
+= '@enumerate\n'
104 line
= line
[line
.find(' ')+1:]
105 elif re
.match(r
'^[*-] ', line
):
107 ret
+= '@itemize %s\n' % {'*': '@bullet',
108 '-': '@minus'}[line
[0]]
112 elif lastempty
and inlist
:
113 ret
+= '@end %s\n\n' % inlist
120 ret
+= '@end %s\n\n' % inlist
125 """Format the main documentation body"""
126 return texi_format(doc
.body
.text
)
129 def texi_enum_value(value
):
130 """Format a table of members item for an enumeration value"""
131 return '@item @code{%s}\n' % value
.name
134 def texi_member(member
, suffix
=''):
135 """Format a table of members item for an object type member"""
136 typ
= member
.type.doc_type()
137 return '@item @code{%s%s%s}%s%s\n' % (
141 ' (optional)' if member
.optional
else '',
145 def texi_members(doc
, what
, base
, variants
, member_func
):
146 """Format the table of members"""
148 for section
in doc
.args
.itervalues():
149 # TODO Drop fallbacks when undocumented members are outlawed
151 desc
= texi_format(section
.text
)
152 elif (variants
and variants
.tag_member
== section
.member
153 and not section
.member
.type.doc_type()):
154 values
= section
.member
.type.member_names()
155 members_text
= ', '.join(['@t{"%s"}' % v
for v
in values
])
156 desc
= 'One of ' + members_text
+ '\n'
158 desc
= 'Not documented\n'
159 items
+= member_func(section
.member
) + desc
161 items
+= '@item The members of @code{%s}\n' % base
.doc_type()
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
)
171 items
+= '@item The members of @code{%s}%s\n' % (
172 v
.type.doc_type(), when
)
175 return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what
, items
)
178 def texi_sections(doc
):
179 """Format additional sections following arguments"""
181 for section
in doc
.sections
:
183 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
184 body
+= '\n@b{%s:}\n' % section
.name
185 if section
.name
and section
.name
.startswith('Example'):
186 body
+= texi_example(section
.text
)
188 body
+= texi_format(section
.text
)
192 def texi_entity(doc
, what
, base
=None, variants
=None,
193 member_func
=texi_member
):
194 return (texi_body(doc
)
195 + texi_members(doc
, what
, base
, variants
, member_func
)
196 + texi_sections(doc
))
199 class QAPISchemaGenDocVisitor(qapi
.QAPISchemaVisitor
):
204 def visit_begin(self
, schema
):
207 def visit_enum_type(self
, name
, info
, values
, prefix
):
209 self
.out
+= TYPE_FMT(type='Enum',
211 body
=texi_entity(doc
, 'Values',
212 member_func
=texi_enum_value
))
214 def visit_object_type(self
, name
, info
, base
, members
, variants
):
216 if base
and base
.is_implicit():
218 self
.out
+= TYPE_FMT(type='Object',
220 body
=texi_entity(doc
, 'Members', base
, variants
))
222 def visit_alternate_type(self
, name
, info
, variants
):
224 self
.out
+= TYPE_FMT(type='Alternate',
226 body
=texi_entity(doc
, 'Members'))
228 def visit_command(self
, name
, info
, arg_type
, ret_type
,
229 gen
, success_response
, boxed
):
232 body
= texi_body(doc
)
233 body
+= ('\n@b{Arguments:} the members of @code{%s}\n'
235 body
+= texi_sections(doc
)
237 body
= texi_entity(doc
, 'Arguments')
238 self
.out
+= MSG_FMT(type='Command',
242 def visit_event(self
, name
, info
, arg_type
, boxed
):
244 self
.out
+= MSG_FMT(type='Event',
246 body
=texi_entity(doc
, 'Arguments'))
248 def symbol(self
, doc
, entity
):
255 def freeform(self
, doc
):
259 self
.out
+= texi_body(doc
) + texi_sections(doc
)
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
:
268 gen
.symbol(doc
, schema
.lookup_entity(doc
.symbol
))
275 """Takes schema argument, prints result to stdout"""
277 print >>sys
.stderr
, "%s: need exactly 1 argument: SCHEMA" % argv
[0]
280 schema
= qapi
.QAPISchema(argv
[1])
281 if not qapi
.doc_required
:
282 print >>sys
.stderr
, ("%s: need pragma 'doc-required' "
283 "to generate documentation" % argv
[0])
285 print texi_schema(schema
)
288 if __name__
== '__main__':