39
39
# General configuration
40
40
# ---------------------
41
41
42
+ SAGE_LIVE_DOC = os .environ .get ('SAGE_LIVE_DOC' , 'no' )
43
+ SAGE_PREPARSED_DOC = os .environ .get ('SAGE_PREPARSED_DOC' , 'yes' )
44
+
42
45
# Add any Sphinx extension module names here, as strings. They can be extensions
43
46
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
44
47
extensions = [
57
60
58
61
jupyter_execute_default_kernel = 'sagemath'
59
62
60
- if os . environ . get ( ' SAGE_LIVE_DOC' , 'no' ) == 'yes' :
63
+ if SAGE_LIVE_DOC == 'yes' :
61
64
SAGE_JUPYTER_SERVER = os .environ .get ('SAGE_JUPYTER_SERVER' , 'binder' )
62
65
if SAGE_JUPYTER_SERVER .startswith ('binder' ):
63
66
# format: "binder" or
@@ -789,11 +792,6 @@ class will be properly documented inside its surrounding class.
789
792
return skip
790
793
791
794
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
-
797
795
class SagecodeTransform (SphinxTransform ):
798
796
"""
799
797
Transform a code block to a live code block enabled by jupyter-sphinx.
@@ -831,6 +829,8 @@ def apply(self):
831
829
if self .app .builder .tags .has ('html' ) or self .app .builder .tags .has ('inventory' ):
832
830
for node in self .document .traverse (nodes .literal_block ):
833
831
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
834
834
parent = node .parent
835
835
index = parent .index (node )
836
836
parent .remove (node )
@@ -843,37 +843,40 @@ def apply(self):
843
843
content += node
844
844
container += content
845
845
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 ('....: ' ):
867
859
example_lines .append (newline [6 :])
868
860
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' :
876
878
# Tab for Jupyter-sphinx cell
879
+ from jupyter_sphinx .ast import JupyterCellNode , CellInputNode
877
880
source = node .rawsource
878
881
lines = []
879
882
for line in source .splitlines ():
@@ -915,7 +918,8 @@ def setup(app):
915
918
app .connect ('autodoc-process-docstring' , skip_TESTS_block )
916
919
app .connect ('autodoc-skip-member' , skip_member )
917
920
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 )
919
923
920
924
# When building the standard docs, app.srcdir is set to SAGE_DOC_SRC +
921
925
# 'LANGUAGE/DOCNAME'.
0 commit comments