Skip to content

Commit 83fba8b

Browse files
committed
Add docstring transform
1 parent e417e22 commit 83fba8b

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/doc/common/static/custom-furo.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ a.pdf:hover {
2121
text-decoration: none;
2222
}
2323

24+
25+
/* For sections INPUT, OUTPUT, EXAMPLES, etc. */
26+
27+
abbr {
28+
font-weight: 500;
29+
}

src/sage/misc/sagedoc_conf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# The reST default role (used for this markup: `text`) to use for all documents.
2020
default_role = 'math'
2121

22+
2223
def process_docstring_aliases(app, what, name, obj, options, docstringlines):
2324
"""
2425
Change the docstrings for aliases to point to the original object.
@@ -27,6 +28,7 @@ def process_docstring_aliases(app, what, name, obj, options, docstringlines):
2728
if hasattr(obj, '__name__') and obj.__name__ != basename:
2829
docstringlines[:] = ['See :obj:`%s`.' % name]
2930

31+
3032
def process_directives(app, what, name, obj, options, docstringlines):
3133
"""
3234
Remove 'nodetex' and other directives from the first line of any
@@ -39,6 +41,7 @@ def process_directives(app, what, name, obj, options, docstringlines):
3941
if 'nodetex' in directives:
4042
docstringlines.pop(0)
4143

44+
4245
def process_docstring_cython(app, what, name, obj, options, docstringlines):
4346
"""
4447
Remove Cython's filename and location embedding.
@@ -52,6 +55,7 @@ def process_docstring_cython(app, what, name, obj, options, docstringlines):
5255
docstringlines.pop(0)
5356
docstringlines.pop(0)
5457

58+
5559
def process_docstring_module_title(app, what, name, obj, options, docstringlines):
5660
"""
5761
Removes the first line from the beginning of the module's docstring. This
@@ -74,6 +78,7 @@ def process_docstring_module_title(app, what, name, obj, options, docstringlines
7478
else:
7579
break
7680

81+
7782
def process_dollars(app, what, name, obj, options, docstringlines):
7883
r"""
7984
Replace dollar signs with backticks.
@@ -87,6 +92,7 @@ def process_dollars(app, what, name, obj, options, docstringlines):
8792
for i in range(len(lines)):
8893
docstringlines[i] = lines[i]
8994

95+
9096
def process_inherited(app, what, name, obj, options, docstringlines):
9197
"""
9298
If we're including inherited members, omit their docstrings.
@@ -110,6 +116,7 @@ def process_inherited(app, what, name, obj, options, docstringlines):
110116
for i in range(len(docstringlines)):
111117
docstringlines.pop()
112118

119+
113120
def skip_TESTS_block(app, what, name, obj, options, docstringlines):
114121
"""
115122
Skip blocks labeled "TESTS:".
@@ -127,6 +134,7 @@ def skip_TESTS_block(app, what, name, obj, options, docstringlines):
127134
while len(docstringlines) > len(lines):
128135
del docstringlines[len(lines)]
129136

137+
130138
class SagemathTransform(Transform):
131139
"""
132140
Transform for code-blocks.
@@ -146,6 +154,7 @@ def apply(self):
146154
node.rawsource = source
147155
node[:] = [nodes.Text(source)]
148156

157+
149158
# This is only used by sage.misc.sphinxify
150159
def setup(app):
151160
app.connect('autodoc-process-docstring', process_docstring_cython)

src/sage_docbuild/conf.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,85 @@ def apply(self):
853853
node.parent.insert(node.parent.index(node) + 1, cell_node)
854854

855855

856+
class DocstringTransform(SphinxTransform):
857+
r"""
858+
Transform sections in Sage docstrings for better rendering.
859+
860+
The following are tests.
861+
862+
AUTHORS:
863+
864+
- Alice
865+
- Bob
866+
867+
INPUT:
868+
869+
- one
870+
- two
871+
872+
OUTPUT:
873+
874+
three
875+
876+
OUTPUT: three
877+
878+
EXAMPLE::
879+
880+
sage: 1 + 2
881+
3
882+
883+
EXAMPLES::
884+
885+
sage: 1 + 2
886+
3
887+
888+
EXAMPLE:
889+
890+
We show that `1 + 2 = 3`::
891+
892+
sage: 1 + 2
893+
3
894+
895+
EXAMPLES:
896+
897+
We show that `1 + 2 = 3`::
898+
899+
sage: 1 + 2
900+
3
901+
902+
"""
903+
default_priority = 600
904+
905+
def apply(self):
906+
for node in self.document.traverse(nodes.paragraph):
907+
if isinstance(node.children[0], nodes.Text) and node.children[0].astext().strip() == 'AUTHORS:':
908+
list_node = node.next_node(siblings=True, descend=False)
909+
if isinstance(list_node, nodes.bullet_list):
910+
new_node = nodes.admonition(classes=['authors'], admonitionclass='authors')
911+
node.replace_self(new_node)
912+
node = new_node
913+
admonition_title = nodes.title()
914+
admonition_title.append(nodes.Text('Authors'))
915+
node.insert(0, admonition_title)
916+
node.append(list_node)
917+
node.parent.remove(list_node)
918+
if isinstance(node.children[0], nodes.Text):
919+
text = node.children[0].astext()
920+
for section in ['INPUT', 'OUTPUT', 'EXAMPLES', 'EXAMPLE', 'TESTS', 'TEST',
921+
'ALGORITHM', 'REFERENCE', 'REFERENCES']:
922+
if text.startswith(f'{section}:'):
923+
parent = node.parent
924+
index = parent.index(node)
925+
parent.remove(node)
926+
acronym_node = nodes.acronym()
927+
acronym_node += nodes.Text(section)
928+
para = nodes.paragraph()
929+
para += acronym_node
930+
para += nodes.Text(text[len(f'{section}:'):])
931+
parent.insert(index, para)
932+
break
933+
934+
856935
# This replaces the setup() in sage.misc.sagedoc_conf
857936
def setup(app):
858937
app.connect('autodoc-process-docstring', process_docstring_cython)
@@ -864,6 +943,7 @@ def setup(app):
864943
app.connect('autodoc-process-docstring', skip_TESTS_block)
865944
app.connect('autodoc-skip-member', skip_member)
866945
app.add_transform(SagemathTransform)
946+
app.add_transform(DocstringTransform)
867947
if os.environ.get('SAGE_LIVE_DOC', 'no') == 'yes':
868948
app.add_transform(SagecodeTransform)
869949

0 commit comments

Comments
 (0)