def run(self):
env = self.state.document.settings.env
relpath, abspath = env.relfn2path(directives.path(self.arguments[0]))
# Add OpenAPI spec as a dependency to the current document. That means
# the document will be rebuilt if the spec is changed.
env.note_dependency(relpath)
# Read the spec using encoding passed to the directive or fallback to
# the one specified in Sphinx's config.
encoding = self.options.get('encoding', env.config.source_encoding)
with io.open(abspath, 'rt', encoding=encoding) as stream:
spec = yaml.load(stream, _YamlOrderedLoader)
# URI parameter is crucial for resolving relative references. So
# we need to set this option properly as it's used later down the
# stack.
self.options.setdefault('uri', 'file://%s' % abspath)
# reStructuredText DOM manipulation is pretty tricky task. It requires
# passing dozen arguments which is not easy without well-documented
# internals. So the idea here is to represent OpenAPI spec as
# reStructuredText in-memory text and parse it in order to produce a
# real DOM.
viewlist = ViewList()
for line in openapi2httpdomain(spec, **self.options):
viewlist.append(line, '<openapi>')
# Parse reStructuredText contained in `viewlist` and return produced
# DOM nodes.
node = nodes.section()
node.document = self.state.document
nested_parse_with_titles(self.state, viewlist, node)
return node.children
评论列表
文章目录