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
14 @deftypefn {type} {{}} {name}
22 @deftp {{{type}}} {name}
29 EXAMPLE_FMT
= """@example
35 def subst_strong(doc
):
36 """Replaces *foo* by @strong{foo}"""
37 return re
.sub(r
'\*([^*\n]+)\*', r
'@strong{\1}', doc
)
41 """Replaces _foo_ by @emph{foo}"""
42 return re
.sub(r
'\b_([^_\n]+)_\b', r
'@emph{\1}', 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
):
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
)
70 - |: generates an @example
71 - =: generates @section
72 - ==: generates @subsection
73 - 1. or 1): generates an @enumerate @item
74 - */-: generates an @itemize list
77 doc
= subst_braces(doc
)
80 doc
= subst_strong(doc
)
83 for line
in doc
.split('\n'):
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
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
):
102 ret
+= '@enumerate\n'
105 line
= line
[line
.find(' ')+1:]
106 elif re
.match(r
'^[*-] ', line
):
108 ret
+= '@itemize %s\n' % {'*': '@bullet',
109 '-': '@minus'}[line
[0]]
113 elif lastempty
and inlist
:
114 ret
+= '@end %s\n\n' % inlist
121 ret
+= '@end %s\n\n' % inlist
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' % (
142 ' (optional)' if member
.optional
else '',
146 def texi_members(doc
, what
, base
, variants
, member_func
):
147 """Format the table of members"""
149 for section
in doc
.args
.values():
150 # TODO Drop fallbacks when undocumented members are outlawed
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'
159 desc
= 'Not documented\n'
160 items
+= member_func(section
.member
) + desc
162 items
+= '@item The members of @code{%s}\n' % base
.doc_type()
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
)
172 items
+= '@item The members of @code{%s}%s\n' % (
173 v
.type.doc_type(), when
)
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"""
182 for section
in doc
.sections
:
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
)
189 body
+= texi_format(section
.text
)
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
):
205 def visit_begin(self
, schema
):
208 def visit_enum_type(self
, name
, info
, values
, prefix
):
210 self
.out
+= TYPE_FMT(type='Enum',
212 body
=texi_entity(doc
, 'Values',
213 member_func
=texi_enum_value
))
215 def visit_object_type(self
, name
, info
, base
, members
, variants
):
217 if base
and base
.is_implicit():
219 self
.out
+= TYPE_FMT(type='Object',
221 body
=texi_entity(doc
, 'Members', base
, variants
))
223 def visit_alternate_type(self
, name
, info
, variants
):
225 self
.out
+= TYPE_FMT(type='Alternate',
227 body
=texi_entity(doc
, 'Members'))
229 def visit_command(self
, name
, info
, arg_type
, ret_type
,
230 gen
, success_response
, boxed
):
233 body
= texi_body(doc
)
234 body
+= ('\n@b{Arguments:} the members of @code{%s}\n'
236 body
+= texi_sections(doc
)
238 body
= texi_entity(doc
, 'Arguments')
239 self
.out
+= MSG_FMT(type='Command',
243 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
):
256 def freeform(self
, doc
):
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
:
269 gen
.symbol(doc
, schema
.lookup_entity(doc
.symbol
))
276 """Takes schema argument, prints result to stdout"""
278 print("%s: need exactly 1 argument: SCHEMA" % argv
[0], file=sys
.stderr
)
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
)
286 print(texi_schema(schema
))
289 if __name__
== '__main__':