Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: use Sphinx extension to generate CLI options
- Loading branch information
1 parent
41f9f12
commit e649cd0
Showing
6 changed files
with
105 additions
and
199 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
import json | ||
from textwrap import dedent | ||
from docutils import nodes | ||
from docutils.statemachine import ViewList | ||
|
||
from jinja2 import Template | ||
from sphinx.util.compat import Directive | ||
from sphinx.util.nodes import nested_parse_with_titles | ||
|
||
|
||
class CLIOptionsDirective(Directive): | ||
required_arguments = 1 | ||
|
||
def run(self): | ||
# Load options from given JSON file | ||
options_path = os.path.join( | ||
os.path.dirname(self.state.document['source']), | ||
self.arguments[0], | ||
) | ||
with open(options_path) as f: | ||
options = json.load(f) | ||
|
||
# Generate reStructuredText markup | ||
rst = '' | ||
for name, attrs in sorted(options.items()): | ||
data = { | ||
'name': name, | ||
'alias': attrs.get('alias'), | ||
'default': json.dumps(attrs['default']) if 'default' in attrs else None, | ||
'description': attrs['description'], | ||
} | ||
template = Template(dedent(''' | ||
.. _{{ name }}{% if alias %}-{{ alias }}{% endif %}: | ||
.. option:: --{{ name }}{% if alias %}, -{{ alias }}{% endif %} | ||
{{ description }} | ||
{% if default %}**Default value:** ``{{ default }}``{% endif %} | ||
''')) | ||
rst += template.render(**data) | ||
|
||
# Generate docutils nodes | ||
result = ViewList() | ||
for line in rst.splitlines(): | ||
result.append(line, f'<cli-options-directive>') | ||
node = nodes.section(document=self.state.document) | ||
nested_parse_with_titles(self.state, result, node) | ||
return node.children | ||
|
||
|
||
def setup(app): | ||
app.add_directive('cli-options', CLIOptionsDirective) | ||
return {'version': '1.0', 'parallel_read_safe': True} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters