forked from OPM/opm-python-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphinx_ext_docstrings.py
More file actions
60 lines (50 loc) · 2.19 KB
/
sphinx_ext_docstrings.py
File metadata and controls
60 lines (50 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
from sphinx.util.nodes import nested_parse_with_titles
from docutils.statemachine import ViewList
from sphinx.util.docutils import SphinxDirective
from docutils import nodes
def read_doc_strings(directive, docstrings_path):
print(docstrings_path)
with open(docstrings_path, 'r') as file:
docstrings = json.load(file)
result = []
for name, item in docstrings.items():
# Create a ViewList instance for the function signature and docstring
rst = ViewList()
# Check if signature exists and prepend it to the docstring
signature = item.get('signature', '')
item_type = item.get('type', 'method')
signature_line = f".. py:{item_type}:: {signature}" if signature else f".. py:{item_type}:: {name}()"
rst.append(signature_line, source="")
rst.append("", source="")
# Add the docstring text if it exists
docstring = item.get('doc', '')
if docstring:
for line in docstring.split('\n'):
rst.append(f" {line}", source="")
# Create a node that will be populated by nested_parse_with_titles
node = nodes.section()
node.document = directive.state.document
# Parse the rst content
nested_parse_with_titles(directive.state, rst, node)
result.extend(node.children)
return result
class SimulatorsDirective(SphinxDirective):
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {}
def run(self):
return read_doc_strings(self, self.state.document.settings.env.app.config.opm_simulators_docstrings_path)
class CommonDirective(SphinxDirective):
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {}
def run(self):
return read_doc_strings(self, self.state.document.settings.env.app.config.opm_common_docstrings_path)
def setup(app):
app.add_config_value('opm_simulators_docstrings_path', None, 'env')
app.add_config_value('opm_common_docstrings_path', None, 'env')
app.add_directive("opm_simulators_docstrings", SimulatorsDirective)
app.add_directive("opm_common_docstrings", CommonDirective)