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"""
8 from __future__
import print_function
10 from qapi
.gen
import QAPIGenDoc
, QAPISchemaVisitor
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_if(ifcond
, prefix
='\n', suffix
='\n'):
131 """Format the #if condition"""
134 return '%s@b{If:} @code{%s}%s' % (prefix
, ', '.join(ifcond
), suffix
)
137 def texi_enum_value(value
, desc
, suffix
):
138 """Format a table of members item for an enumeration value"""
139 return '@item @code{%s}\n%s%s' % (
140 value
.name
, desc
, texi_if(value
.ifcond
, prefix
='@*'))
143 def texi_member(member
, desc
, suffix
):
144 """Format a table of members item for an object type member"""
145 typ
= member
.type.doc_type()
146 membertype
= ': ' + typ
if typ
else ''
147 return '@item @code{%s%s}%s%s\n%s%s' % (
148 member
.name
, membertype
,
149 ' (optional)' if member
.optional
else '',
150 suffix
, desc
, texi_if(member
.ifcond
, prefix
='@*'))
153 def texi_members(doc
, what
, base
, variants
, member_func
):
154 """Format the table of members"""
156 for section
in doc
.args
.values():
157 # TODO Drop fallbacks when undocumented members are outlawed
159 desc
= texi_format(section
.text
)
160 elif (variants
and variants
.tag_member
== section
.member
161 and not section
.member
.type.doc_type()):
162 values
= section
.member
.type.member_names()
163 members_text
= ', '.join(['@t{"%s"}' % v
for v
in values
])
164 desc
= 'One of ' + members_text
+ '\n'
166 desc
= 'Not documented\n'
167 items
+= member_func(section
.member
, desc
, suffix
='')
169 items
+= '@item The members of @code{%s}\n' % base
.doc_type()
171 for v
in variants
.variants
:
172 when
= ' when @code{%s} is @t{"%s"}%s' % (
173 variants
.tag_member
.name
, v
.name
, texi_if(v
.ifcond
, " (", ")"))
174 if v
.type.is_implicit():
175 assert not v
.type.base
and not v
.type.variants
176 for m
in v
.type.local_members
:
177 items
+= member_func(m
, desc
='', suffix
=when
)
179 items
+= '@item The members of @code{%s}%s\n' % (
180 v
.type.doc_type(), when
)
183 return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what
, items
)
186 def texi_features(doc
):
187 """Format the table of features"""
189 for section
in doc
.features
.values():
190 desc
= texi_format(section
.text
)
191 items
+= '@item @code{%s}\n%s' % (section
.name
, desc
)
194 return '\n@b{Features:}\n@table @asis\n%s@end table\n' % (items
)
197 def texi_sections(doc
, ifcond
):
198 """Format additional sections following arguments"""
200 for section
in doc
.sections
:
202 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
203 body
+= '\n@b{%s:}\n' % section
.name
204 if section
.name
and section
.name
.startswith('Example'):
205 body
+= texi_example(section
.text
)
207 body
+= texi_format(section
.text
)
208 body
+= texi_if(ifcond
, suffix
='')
212 def texi_entity(doc
, what
, ifcond
, base
=None, variants
=None,
213 member_func
=texi_member
):
214 return (texi_body(doc
)
215 + texi_members(doc
, what
, base
, variants
, member_func
)
217 + texi_sections(doc
, ifcond
))
220 class QAPISchemaGenDocVisitor(QAPISchemaVisitor
):
221 def __init__(self
, prefix
):
222 self
._prefix
= prefix
223 self
._gen
= QAPIGenDoc(self
._prefix
+ 'qapi-doc.texi')
226 def write(self
, output_dir
):
227 self
._gen
.write(output_dir
)
229 def visit_enum_type(self
, name
, info
, ifcond
, members
, prefix
):
231 self
._gen
.add(TYPE_FMT(type='Enum',
233 body
=texi_entity(doc
, 'Values', ifcond
,
234 member_func
=texi_enum_value
)))
236 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
,
239 if base
and base
.is_implicit():
241 self
._gen
.add(TYPE_FMT(type='Object',
243 body
=texi_entity(doc
, 'Members', ifcond
,
246 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
248 self
._gen
.add(TYPE_FMT(type='Alternate',
250 body
=texi_entity(doc
, 'Members', ifcond
)))
252 def visit_command(self
, name
, info
, ifcond
, arg_type
, ret_type
, gen
,
253 success_response
, boxed
, allow_oob
, allow_preconfig
):
256 body
= texi_body(doc
)
257 body
+= ('\n@b{Arguments:} the members of @code{%s}\n'
259 body
+= texi_sections(doc
, ifcond
)
261 body
= texi_entity(doc
, 'Arguments', ifcond
)
262 self
._gen
.add(MSG_FMT(type='Command',
266 def visit_event(self
, name
, info
, ifcond
, arg_type
, boxed
):
268 self
._gen
.add(MSG_FMT(type='Event',
270 body
=texi_entity(doc
, 'Arguments', ifcond
)))
272 def symbol(self
, doc
, entity
):
279 def freeform(self
, doc
):
283 self
._gen
.add(texi_body(doc
) + texi_sections(doc
, None))
286 def gen_doc(schema
, output_dir
, prefix
):
287 vis
= QAPISchemaGenDocVisitor(prefix
)
288 vis
.visit_begin(schema
)
289 for doc
in schema
.docs
:
291 vis
.symbol(doc
, schema
.lookup_entity(doc
.symbol
))
294 vis
.write(output_dir
)