Skip to content

Commit 8974bcb

Browse files
author
Matthias Koeppe
committed
src/sage_docbuild/conf.py: Conditionalize on environment variable SAGE_PREPARSED_DOC (default yes)
1 parent 10acc0f commit 8974bcb

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

src/sage_docbuild/conf.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
# General configuration
4040
# ---------------------
4141

42+
SAGE_LIVE_DOC = os.environ.get('SAGE_LIVE_DOC', 'no')
43+
SAGE_PREPARSED_DOC = os.environ.get('SAGE_PREPARSED_DOC', 'yes')
44+
4245
# Add any Sphinx extension module names here, as strings. They can be extensions
4346
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
4447
extensions = [
@@ -57,7 +60,7 @@
5760

5861
jupyter_execute_default_kernel = 'sagemath'
5962

60-
if os.environ.get('SAGE_LIVE_DOC', 'no') == 'yes':
63+
if SAGE_LIVE_DOC == 'yes':
6164
SAGE_JUPYTER_SERVER = os.environ.get('SAGE_JUPYTER_SERVER', 'binder')
6265
if SAGE_JUPYTER_SERVER.startswith('binder'):
6366
# format: "binder" or
@@ -789,11 +792,6 @@ class will be properly documented inside its surrounding class.
789792
return skip
790793

791794

792-
from jupyter_sphinx.ast import JupyterCellNode, CellInputNode
793-
from docutils.nodes import container as Container, label as Label, literal_block as LiteralBlock, Text
794-
from sphinx_inline_tabs._impl import TabContainer
795-
from sage.repl.preparse import preparse
796-
797795
class SagecodeTransform(SphinxTransform):
798796
"""
799797
Transform a code block to a live code block enabled by jupyter-sphinx.
@@ -831,6 +829,8 @@ def apply(self):
831829
if self.app.builder.tags.has('html') or self.app.builder.tags.has('inventory'):
832830
for node in self.document.traverse(nodes.literal_block):
833831
if node.get('language') is None and node.astext().startswith('sage:'):
832+
from docutils.nodes import container as Container, label as Label, literal_block as LiteralBlock, Text
833+
from sphinx_inline_tabs._impl import TabContainer
834834
parent = node.parent
835835
index = parent.index(node)
836836
parent.remove(node)
@@ -843,37 +843,40 @@ def apply(self):
843843
content += node
844844
container += content
845845
parent.insert(index, container)
846-
# Tab for preparsed version
847-
container = TabContainer("", type="tab", new_set=False)
848-
textnodes = [Text('Python')]
849-
label = Label("", "", *textnodes)
850-
container += label
851-
content = Container("", is_div=True, classes=["tab-content"])
852-
example_lines = []
853-
preparsed_lines = ['>>> from sage.all import *']
854-
for line in node.rawsource.splitlines() + ['']: # one extra to process last example
855-
newline = line.lstrip()
856-
if newline.startswith('....: '):
857-
example_lines.append(newline[6:])
858-
else:
859-
if example_lines:
860-
preparsed_example = preparse('\n'.join(example_lines))
861-
prompt = '>>> '
862-
for preparsed_line in preparsed_example.splitlines():
863-
preparsed_lines.append(prompt + preparsed_line)
864-
prompt = '... '
865-
example_lines = []
866-
if newline.startswith('sage: '):
846+
if SAGE_PREPARSED_DOC == 'yes':
847+
# Tab for preparsed version
848+
from sage.repl.preparse import preparse
849+
container = TabContainer("", type="tab", new_set=False)
850+
textnodes = [Text('Python')]
851+
label = Label("", "", *textnodes)
852+
container += label
853+
content = Container("", is_div=True, classes=["tab-content"])
854+
example_lines = []
855+
preparsed_lines = ['>>> from sage.all import *']
856+
for line in node.rawsource.splitlines() + ['']: # one extra to process last example
857+
newline = line.lstrip()
858+
if newline.startswith('....: '):
867859
example_lines.append(newline[6:])
868860
else:
869-
preparsed_lines.append(line)
870-
preparsed = '\n'.join(preparsed_lines)
871-
preparsed_node = LiteralBlock(preparsed, preparsed, language='ipycon')
872-
content += preparsed_node
873-
container += content
874-
parent.insert(index + 1, container)
875-
if os.environ.get('SAGE_LIVE_DOC', 'no') == 'yes':
861+
if example_lines:
862+
preparsed_example = preparse('\n'.join(example_lines))
863+
prompt = '>>> '
864+
for preparsed_line in preparsed_example.splitlines():
865+
preparsed_lines.append(prompt + preparsed_line)
866+
prompt = '... '
867+
example_lines = []
868+
if newline.startswith('sage: '):
869+
example_lines.append(newline[6:])
870+
else:
871+
preparsed_lines.append(line)
872+
preparsed = '\n'.join(preparsed_lines)
873+
preparsed_node = LiteralBlock(preparsed, preparsed, language='ipycon')
874+
content += preparsed_node
875+
container += content
876+
parent.insert(index + 1, container)
877+
if SAGE_LIVE_DOC == 'yes':
876878
# Tab for Jupyter-sphinx cell
879+
from jupyter_sphinx.ast import JupyterCellNode, CellInputNode
877880
source = node.rawsource
878881
lines = []
879882
for line in source.splitlines():
@@ -915,7 +918,8 @@ def setup(app):
915918
app.connect('autodoc-process-docstring', skip_TESTS_block)
916919
app.connect('autodoc-skip-member', skip_member)
917920
app.add_transform(SagemathTransform)
918-
app.add_transform(SagecodeTransform)
921+
if SAGE_LIVE_DOC == 'yes' or SAGE_PREPARSED_DOC == 'yes':
922+
app.add_transform(SagecodeTransform)
919923

920924
# When building the standard docs, app.srcdir is set to SAGE_DOC_SRC +
921925
# 'LANGUAGE/DOCNAME'.

0 commit comments

Comments
 (0)