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 # prefer @b over @strong, so txt doesn't translate it to *Foo:*
163 body
+= "\n\n@b{%s:}\n" % name
170 def texi_alternate(expr
, doc
):
171 """Format an alternate to texi"""
172 body
= texi_body(doc
)
173 return STRUCT_FMT(type="Alternate",
178 def texi_union(expr
, doc
):
179 """Format a union to texi"""
180 discriminator
= expr
.get("discriminator")
184 union
= "Simple Union"
186 body
= texi_body(doc
)
187 return STRUCT_FMT(type=union
,
192 def texi_enum(expr
, doc
):
193 """Format an enum to texi"""
194 for i
in expr
['data']:
195 if i
not in doc
.args
:
197 body
= texi_body(doc
)
198 return ENUM_FMT(name
=doc
.symbol
,
202 def texi_struct(expr
, doc
):
203 """Format a struct to texi"""
204 body
= texi_body(doc
)
205 return STRUCT_FMT(type="Struct",
210 def texi_command(expr
, doc
):
211 """Format a command to texi"""
212 body
= texi_body(doc
)
213 return COMMAND_FMT(type="Command",
218 def texi_event(expr
, doc
):
219 """Format an event to texi"""
220 body
= texi_body(doc
)
221 return COMMAND_FMT(type="Event",
226 def texi_expr(expr
, doc
):
227 """Format an expr to texi"""
228 (kind
, _
) = expr
.items()[0]
230 fmt
= {"command": texi_command
,
231 "struct": texi_struct
,
234 "alternate": texi_alternate
,
235 "event": texi_event
}[kind
]
237 return fmt(expr
, doc
)
241 """Convert QAPI schema expressions to texi documentation"""
246 res
.append(texi_body(doc
))
249 doc
= texi_expr(expr
, doc
)
252 print >>sys
.stderr
, "error at @%s" % doc
.info
255 return '\n'.join(res
)
259 """Takes schema argument, prints result to stdout"""
261 print >>sys
.stderr
, "%s: need exactly 1 argument: SCHEMA" % argv
[0]
264 schema
= qapi
.QAPISchema(argv
[1])
265 print texi(schema
.docs
)
268 if __name__
== "__main__":