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}
31 @deftp {{{type}}} {name}
39 EXAMPLE_FMT
= """@example
45 def subst_strong(doc
):
46 """Replaces *foo* by @strong{foo}"""
47 return re
.sub(r
'\*([^*\n]+)\*', r
'@emph{\1}', doc
)
51 """Replaces _foo_ by @emph{foo}"""
52 return re
.sub(r
'\b_([^_\n]+)_\b', r
' @emph{\1} ', doc
)
56 """Replaces @var by @code{var}"""
57 return re
.sub(r
'@([\w-]+)', r
'@code{\1}', doc
)
60 def subst_braces(doc
):
61 """Replaces {} with @{ @}"""
62 return doc
.replace("{", "@{").replace("}", "@}")
65 def texi_example(doc
):
67 # TODO: Neglects to escape @ characters.
68 # We should probably escape them in subst_braces(), and rename the
69 # function to subst_special() or subs_texi_special(). If we do that, we
70 # need to delay it until after subst_vars() in texi_format().
71 doc
= subst_braces(doc
).strip('\n')
72 return EXAMPLE_FMT(code
=doc
)
80 - |: generates an @example
81 - =: generates @section
82 - ==: generates @subsection
83 - 1. or 1): generates an @enumerate @item
84 - */-: generates an @itemize list
87 doc
= subst_braces(doc
)
90 doc
= subst_strong(doc
)
93 for line
in doc
.split('\n'):
96 # FIXME: Doing this in a single if / elif chain is
97 # problematic. For instance, a line without markup terminates
98 # a list if it follows a blank line (reaches the final elif),
99 # but a line with some *other* markup, such as a = title
102 # Make sure to update section "Documentation markup" in
103 # docs/qapi-code-gen.txt when fixing this.
104 if line
.startswith("| "):
105 line
= EXAMPLE_FMT(code
=line
[2:])
106 elif line
.startswith("= "):
107 line
= "@section " + line
[2:]
108 elif line
.startswith("== "):
109 line
= "@subsection " + line
[3:]
110 elif re
.match(r
'^([0-9]*\.) ', line
):
112 lines
.append("@enumerate")
114 line
= line
[line
.find(" ")+1:]
115 lines
.append("@item")
116 elif re
.match(r
'^[*-] ', line
):
118 lines
.append("@itemize %s" % {'*': "@bullet",
119 '-': "@minus"}[line
[0]])
121 lines
.append("@item")
123 elif lastempty
and inlist
:
124 lines
.append("@end %s\n" % inlist
)
131 lines
.append("@end %s\n" % inlist
)
132 return "\n".join(lines
)
137 Format the body of a symbol documentation:
140 - followed by "Returns/Notes/Since/Example" sections
142 body
= texi_format(str(doc
.body
)) + "\n"
144 body
+= "@table @asis\n"
145 for arg
, section
in doc
.args
.iteritems():
148 if "#optional" in desc
:
149 desc
= desc
.replace("#optional", "")
151 body
+= "@item @code{'%s'}%s\n%s\n" % (arg
, opt
,
153 body
+= "@end table\n"
155 for section
in doc
.sections
:
156 name
, doc
= (section
.name
, str(section
))
158 if name
.startswith("Example"):
162 # FIXME the indentation produced by @quotation in .txt and
163 # .html output is confusing
164 body
+= "\n@quotation %s\n%s\n@end quotation" % \
172 def texi_alternate(expr
, doc
):
173 """Format an alternate to texi"""
174 body
= texi_body(doc
)
175 return STRUCT_FMT(type="Alternate",
180 def texi_union(expr
, doc
):
181 """Format a union to texi"""
182 discriminator
= expr
.get("discriminator")
186 union
= "Simple Union"
188 body
= texi_body(doc
)
189 return STRUCT_FMT(type=union
,
194 def texi_enum(expr
, doc
):
195 """Format an enum to texi"""
196 for i
in expr
['data']:
197 if i
not in doc
.args
:
199 body
= texi_body(doc
)
200 return ENUM_FMT(name
=doc
.symbol
,
204 def texi_struct(expr
, doc
):
205 """Format a struct to texi"""
206 body
= texi_body(doc
)
207 return STRUCT_FMT(type="Struct",
212 def texi_command(expr
, doc
):
213 """Format a command to texi"""
214 body
= texi_body(doc
)
215 return COMMAND_FMT(type="Command",
220 def texi_event(expr
, doc
):
221 """Format an event to texi"""
222 body
= texi_body(doc
)
223 return COMMAND_FMT(type="Event",
228 def texi_expr(expr
, doc
):
229 """Format an expr to texi"""
230 (kind
, _
) = expr
.items()[0]
232 fmt
= {"command": texi_command
,
233 "struct": texi_struct
,
236 "alternate": texi_alternate
,
237 "event": texi_event
}[kind
]
239 return fmt(expr
, doc
)
243 """Convert QAPI schema expressions to texi documentation"""
248 res
.append(texi_body(doc
))
251 doc
= texi_expr(expr
, doc
)
254 print >>sys
.stderr
, "error at @%s" % doc
.info
257 return '\n'.join(res
)
261 """Takes schema argument, prints result to stdout"""
263 print >>sys
.stderr
, "%s: need exactly 1 argument: SCHEMA" % argv
[0]
266 schema
= qapi
.QAPISchema(argv
[1])
267 print texi(schema
.docs
)
270 if __name__
== "__main__":