forked from sagemath/sage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.py
More file actions
1105 lines (946 loc) · 43.1 KB
/
Copy pathconf.py
File metadata and controls
1105 lines (946 loc) · 43.1 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Sphinx build configuration
This file contains configuration needed to customize Sphinx input and output
behavior.
"""
# ****************************************************************************
# Copyright (C) 2022 Kwankyu Lee <ekwankyu@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
# Load configuration shared with sage.misc.sphinxify
from sage.misc.sagedoc_conf import *
import sys
import os
import re
import importlib
import dateutil.parser
import sphinx
import sphinx.ext.intersphinx as intersphinx
from sphinx import highlighting
from sphinx.transforms import SphinxTransform
from sphinx.util.docutils import SphinxDirective
from IPython.lib.lexers import IPythonConsoleLexer, IPyLexer
from sage.misc.sagedoc import extlinks
from sage.env import SAGE_DOC_SRC, SAGE_DOC, PPLPY_DOCS, MATHJAX_DIR
from sage.misc.latex_macros import sage_mathjax_macros
from sage.features.sphinx import JupyterSphinx
from sage.features.all import all_features
import sage.version
# ---------------------
# General configuration
# ---------------------
SAGE_LIVE_DOC = os.environ.get('SAGE_LIVE_DOC', 'no')
SAGE_PREPARSED_DOC = os.environ.get('SAGE_PREPARSED_DOC', 'yes')
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
'sage_docbuild.ext.inventory_builder',
'sage_docbuild.ext.multidocs',
'sage_docbuild.ext.sage_autodoc',
'sphinx.ext.todo',
'sphinx.ext.extlinks',
'sphinx.ext.mathjax',
'sphinx.ext.linkcode',
'sphinx_copybutton',
'sphinx_inline_tabs',
'IPython.sphinxext.ipython_directive',
'matplotlib.sphinxext.plot_directive',
]
if JupyterSphinx().is_present():
extensions.append('jupyter_sphinx')
jupyter_execute_default_kernel = 'sagemath'
if SAGE_LIVE_DOC == 'yes':
JupyterSphinx().require()
SAGE_JUPYTER_SERVER = os.environ.get('SAGE_JUPYTER_SERVER', 'binder')
if SAGE_JUPYTER_SERVER.startswith('binder'):
# format: "binder" or
# "binder:sagemath/sage-binder-env" or
# "binder:sagemath/sage-binder-env/dev"
if SAGE_JUPYTER_SERVER == 'binder':
binder_repo = "sagemath/sage-binder-env/master"
else:
binder_repo = SAGE_JUPYTER_SERVER[7:]
s = binder_repo.split('/', 2)
if len(s) > 2:
binder_options = {
'repo': s[0] + '/' + s[1],
'ref': s[2]
}
else:
binder_options = {
'repo': binder_repo
}
jupyter_sphinx_thebelab_config = {
'requestKernel': False,
'binderOptions': binder_options,
'kernelOptions': {
'name': "sagemath",
'kernelName': "sagemath",
'path': ".",
},
'selector': "div.live-doc"
}
else: # local jupyter server
SAGE_JUPYTER_SERVER_TOKEN = os.environ.get('SAGE_JUPYTER_SERVER_TOKEN', 'secret')
jupyter_sphinx_thebelab_config = {
'requestKernel': False,
'kernelOptions': {
'name': "sagemath",
'kernelName': "sagemath",
'path': ".",
'serverSettings': {
'baseUrl': SAGE_JUPYTER_SERVER,
'token': SAGE_JUPYTER_SERVER_TOKEN
},
},
'selector': "div.live-doc"
}
jupyter_sphinx_thebelab_config.update({
'codeMirrorConfig': {
'lineNumbers': True,
}
})
# This code is executed before each ".. PLOT::" directive in the Sphinx
# documentation. It defines a 'sphinx_plot' function that displays a Sage object
# through matplotlib, so that it will be displayed in the HTML doc
plot_html_show_source_link = False
plot_pre_code = r"""
# Set locale to prevent having commas in decimal numbers
# in tachyon input (see https://github.com/sagemath/sage/issues/28971)
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
def sphinx_plot(graphics, **kwds):
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from sage.misc.temporary_file import tmp_filename
from sage.plot.graphics import _parse_figsize
if os.environ.get('SAGE_SKIP_PLOT_DIRECTIVE', 'no') != 'yes':
## Option handling is taken from Graphics.save
options = dict()
if isinstance(graphics, sage.plot.graphics.Graphics):
options.update(sage.plot.graphics.Graphics.SHOW_OPTIONS)
options.update(graphics._extra_kwds)
options.update(kwds)
elif isinstance(graphics, sage.plot.multigraphics.MultiGraphics):
options.update(kwds)
else:
graphics = graphics.plot(**kwds)
dpi = options.pop('dpi', None)
transparent = options.pop('transparent', None)
fig_tight = options.pop('fig_tight', None)
figsize = options.pop('figsize', None)
if figsize is not None:
figsize = _parse_figsize(figsize)
plt.figure(figsize=figsize)
figure = plt.gcf()
if isinstance(graphics, (sage.plot.graphics.Graphics,
sage.plot.multigraphics.MultiGraphics)):
graphics.matplotlib(figure=figure, figsize=figsize, **options)
if isinstance(graphics, (sage.plot.graphics.Graphics,
sage.plot.multigraphics.GraphicsArray)):
# for Graphics and GraphicsArray, tight_layout adjusts the
# *subplot* parameters so ticks aren't cut off, etc.
figure.tight_layout()
else:
# 3d graphics via png
import matplotlib as mpl
mpl.rcParams['image.interpolation'] = 'bilinear'
mpl.rcParams['image.resample'] = False
mpl.rcParams['figure.figsize'] = [8.0, 6.0]
mpl.rcParams['figure.dpi'] = 80
mpl.rcParams['savefig.dpi'] = 100
fn = tmp_filename(ext=".png")
graphics.save(fn)
img = mpimg.imread(fn)
plt.imshow(img)
plt.axis("off")
plt.margins(0)
if not isinstance(graphics, sage.plot.multigraphics.MultiGraphics):
plt.tight_layout(pad=0)
from sage.all_cmdline import *
"""
plot_html_show_formats = False
plot_formats = ['svg', 'pdf', 'png']
# We do *not* fully initialize intersphinx since we call it by hand
# in find_sage_dangling_links.
#, 'sphinx.ext.intersphinx']
# Add any paths that contain templates here, relative to this directory.
templates_path = [os.path.join(SAGE_DOC_SRC, 'common', 'templates'), 'templates']
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = ""
copyright = "2005--{}, The Sage Development Team".format(dateutil.parser.parse(sage.version.date).year)
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
version = sage.version.version
release = sage.version.version
source_repository = 'https://github.com/sagemath/sage/'
source_branch = 'develop'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
# language = None
# The LaTeX engine to build the docs.
# https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-latex_engine
latex_engine = 'lualatex'
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'
# List of glob-style patterns that should be excluded when looking for
# source files. [1] They are matched against the source file names
# relative to the source directory, using slashes as directory
# separators on all platforms.
exclude_patterns = ['.build']
# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
show_authors = True
# Default lexer to use when highlighting code blocks, using the IPython
# console lexers. 'ipycon' is the IPython console, which is what we want
# for most code blocks: anything with "sage:" prompts. For other IPython,
# like blocks which might appear in a notebook cell, use 'ipython'.
highlighting.lexers['ipycon'] = IPythonConsoleLexer(in1_regex=r'(sage:|>>>)', in2_regex=r'([.][.][.][.]:|[.][.][.])')
highlighting.lexers['ipython'] = IPyLexer()
highlight_language = 'ipycon'
# Create table of contents entries for domain objects (e.g. functions, classes,
# attributes, etc.). Default is True.
toc_object_entries = True
# A string that determines how domain objects (e.g. functions, classes,
# attributes, etc.) are displayed in their table of contents entry.
#
# Use "domain" to allow the domain to determine the appropriate number of parents
# to show. For example, the Python domain would show Class.method() and
# function(), leaving out the module. level of parents. This is the default
# setting.
#
# Use "hide" to only show the name of the element without any parents (i.e. method()).
#
# Use "all" to show the fully-qualified name for the object (i.e. module.Class.method()),
# displaying all parents.
toc_object_entries_show_parents = 'hide'
# -----------------------
# Extension configuration
# -----------------------
# include the todos
todo_include_todos = True
#
# intersphinx: Cross-links to other projects' online or installed documentation.
#
SAGE_DOC_REMOTE_INVENTORIES = os.environ.get('SAGE_DOC_REMOTE_INVENTORIES', 'no') == 'yes'
_vendored_inventories_dir = os.path.join(SAGE_DOC_SRC, "common", "_vendor")
# Run "sage -python -m sage_docbuild.vendor" to update src/doc/common/_vendor/*.inv
_intersphinx_targets = {
'cvxopt': ['https://cvxopt.org/userguide/'],
'cvxpy': ['https://www.cvxpy.org/'],
'cypari2': ['https://cypari2.readthedocs.io/en/latest/'],
'cysignals': ['https://cysignals.readthedocs.io/en/latest/'],
'flint': ['https://flintlib.org/doc/'],
'fpylll': ['https://fpylll.readthedocs.io/en/latest/'],
'gmpy2': ['https://gmpy2.readthedocs.io/en/latest/'],
'ipywidgets': ['https://ipywidgets.readthedocs.io/en/stable/'],
'matplotlib': ['https://matplotlib.org/stable/'],
'mpmath': ['https://mpmath.org/doc/current/'],
'networkx': ['https://networkx.org/documentation/stable/'],
'numpy': ['https://numpy.org/doc/stable/'],
'pplpy': [PPLPY_DOCS, 'https://www.sagemath.org/pplpy/'],
'python': ['https://docs.python.org/'],
'rpy2': ['https://rpy2.github.io/doc/latest/html/'],
'scipy': ['https://docs.scipy.org/doc/scipy/'],
'sympy': ['https://docs.sympy.org/latest/'],
}
def _intersphinx_mapping(key):
inventories = []
link_target = None
for target in _intersphinx_targets[key]:
if not target:
pass
elif target.startswith('http'):
if not link_target:
link_target = target
if SAGE_DOC_REMOTE_INVENTORIES:
inventories.append(None) # Try downloading inventory from link_target
elif os.path.exists(target):
if not link_target:
link_target = target
inventory = os.path.join(target, 'objects.inv')
if os.path.exists(inventory):
inventories.append(inventory)
break
else:
vendored_inventory = os.path.join(_vendored_inventories_dir, key + '.inv')
if os.path.exists(vendored_inventory):
inventories.append(vendored_inventory)
else:
# To avoid docbuild failures when building Sage without internet
# connection, we use the local python inventory file as a fallback for other
# projects. Cross-references will not be resolved in that case, but the
# docbuild will still succeed.
python_inventory_file = os.path.join(_vendored_inventories_dir, "python.inv")
inventories.append(python_inventory_file)
assert link_target
if len(inventories) == 1:
return link_target, inventories[0]
return link_target, tuple(inventories)
def set_intersphinx_mappings(app, config):
"""
Add precompiled inventory (the objects.inv)
"""
app.config.intersphinx_mapping = {}
refpath = os.path.join(SAGE_DOC, "html", "en", "reference")
invpath = os.path.join(SAGE_DOC, "inventory", "en", "reference")
if app.config.multidoc_first_pass == 1 or \
not (os.path.exists(refpath) and os.path.exists(invpath)):
return
app.config.intersphinx_mapping = {key: _intersphinx_mapping(key)
for key in _intersphinx_targets}
# Add master intersphinx mapping
dst = os.path.join(invpath, 'objects.inv')
app.config.intersphinx_mapping['sagemath'] = (refpath, dst)
# Add intersphinx mapping for subdirectories
for directory in os.listdir(os.path.join(invpath)):
if directory == 'jupyter_execute':
# This directory is created by jupyter-sphinx extension for
# internal use and should be ignored here. See Issue #33507.
continue
if os.path.isdir(os.path.join(invpath, directory)):
src = os.path.join(refpath, directory)
dst = os.path.join(invpath, directory, 'objects.inv')
app.config.intersphinx_mapping[directory] = (src, dst)
intersphinx.normalize_intersphinx_mapping(app, config)
# By default document is master.
multidocs_is_master = True
# https://sphinx-copybutton.readthedocs.io/en/latest/use.html
copybutton_prompt_text = r"sage: |[.][.][.][.]: |>>> |[.][.][.] |\$ "
copybutton_prompt_is_regexp = True
copybutton_exclude = '.linenos, .c1' # exclude single comments (in particular, # optional!)
copybutton_only_copy_prompt_lines = True
# https://www.sphinx-doc.org/en/master/usage/extensions/linkcode.html
def linkcode_resolve(domain, info):
import inspect
from urllib.parse import quote
from sage.misc.sageinspect import sage_getsourcelines
if domain != 'py':
return None
if info['module']:
m = importlib.import_module(info['module'])
filename = quote(info['module'].replace('.', '/'))
if m.__file__.endswith('py'):
filename += '.py'
else:
filename += '.pyx'
if 'fullname' in info:
fullname = info['fullname']
obj = m
try:
for attr in fullname.split('.'):
obj = getattr(obj, attr)
lineno = sage_getsourcelines(obj)[-1]
except Exception: # catch all
return None
anchor = f'#L{lineno}'
else:
anchor = ''
return f"{source_repository}blob/develop/src/{filename}{anchor}"
return None
# -----------------------
# Options for HTML output
# -----------------------
# Add any paths that contain custom themes here, relative to this directory.
html_theme_path = [os.path.join(SAGE_DOC_SRC, "common", "themes")]
# Deprecated Sage classic theme:
#
# html_theme = "sage-classic"
# html_theme_options = {}
#
# See the directory doc/common/themes/sage-classic/ for theme files.
# Sphinx theme "furo" does not permit an extension. Do not attempt to make
# a "sage-furo" theme.
html_theme = "furo"
# Theme options are theme-specific and customize the look and feel of
# a theme further. For a list of options available for each theme,
# see the documentation.
html_theme_options = {
"light_css_variables": {
"color-brand-primary": "#0f0fff",
"color-brand-content": "#0f0fff",
},
"light_logo": "logo_sagemath_black.svg",
"dark_logo": "logo_sagemath_white.svg",
# Furo can add a small edit button to each document to allow visitors to
# easily propose changes to that document using the repository’s source
# control system.
# https://pradyunsg.me/furo/customisation/edit-button/#adding-an-edit-button
"source_repository": source_repository,
"source_branch": source_branch,
# "source_directory" is defined in conf.py customized for the doc
}
# Check the condition for announcement banner
github_ref = os.environ.get('GITHUB_REF', '')
if github_ref:
match = re.search(r'refs/pull/(\d+)/merge', github_ref)
if match:
pr_number = match.group(1)
is_for_develop = github_ref.startswith('refs/heads/develop')
is_for_github_pr = github_ref and match and pr_number
is_stable_release = version.split('.')[-1].isnumeric()
if is_for_develop or is_for_github_pr or not is_stable_release: # condition for announcement banner
# This URL is hardcoded in the file .github/workflows/doc-publish.yml.
# See NETLIFY_ALIAS of the "Deploy to Netlify" step.
ver = f'<a href="https://doc-develop--sagemath.netlify.app/html/en/index.html">{version}</a>'
if is_for_github_pr:
pr_url = f'https://github.com/sagemath/sage/pull/{pr_number}'
pr_sha = os.environ.get('PR_SHA', '')
pr_commit = pr_url + f'/commits/{pr_sha}'
ver += f' built with GitHub PR <a href="{pr_url}">#{pr_number}</a>' \
f' on <a href="{pr_commit}">{pr_sha[:7]}</a>' \
f' [<a href="/changes.html">changes</a>]'
banner = f'This is documentation for Sage version {ver} for development purpose.'
html_theme_options.update({ "announcement": banner })
# The name of the Pygments (syntax highlighting) style to use. This
# overrides a HTML theme's corresponding setting.
pygments_style = "sphinx"
pygments_dark_style = "monokai"
# Add siderbar/home.html to the default sidebar.
html_sidebars = {
"**": [
"sidebar/scroll-start.html",
"sidebar/brand.html",
"sidebar/version-selector.html",
"sidebar/search.html",
"sidebar/home.html",
"sidebar/navigation.html",
"sidebar/ethical-ads.html",
"sidebar/scroll-end.html",
"sidebar/variant-selector.html",
]
}
# These paths are either relative to html_static_path
# or fully qualified paths (eg. https://...)
html_css_files = [
'custom-furo.css',
'custom-jupyter-sphinx.css',
'custom-codemirror-monokai.css',
'custom-tabs.css',
]
html_js_files = [
'jupyter-sphinx-furo.js',
]
# A list of paths that contain extra templates (or templates that overwrite
# builtin/theme-specific templates). Relative paths are taken as relative
# to the configuration directory.
templates_path = [os.path.join(SAGE_DOC_SRC, 'common', 'templates-furo')] + templates_path
# HTML style sheet. This overrides a HTML theme's corresponding setting.
# html_style = 'default.css'
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
# html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
# html_short_title = None
# The name of an image file (within the static path) to place at the top of
# the sidebar.
# html_logo = 'sagelogo-word.ico'
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
html_favicon = 'favicon.ico'
# html_static_path defined here and imported in the actual configuration file
# conf.py read by Sphinx was the cause of subtle bugs in builders (see #30418 for
# instance). Hence now html_common_static_path contains the common paths to static
# files, and is combined to html_static_path in each conf.py file read by Sphinx.
html_common_static_path = [os.path.join(SAGE_DOC_SRC, 'common', 'static'), 'static']
# Configure MathJax
# https://docs.mathjax.org/en/latest/options/input/tex.html
mathjax3_config = {
"tex": {
# Add custom sage macros
# http://docs.mathjax.org/en/latest/input/tex/macros.html
"macros": sage_mathjax_macros(),
# Add $...$ as possible inline math
# https://docs.mathjax.org/en/latest/input/tex/delimiters.html#tex-and-latex-math-delimiters
"inlineMath": [["$", "$"], ["\\(", "\\)"]],
# Increase the limit the size of the string to be processed
# https://docs.mathjax.org/en/latest/options/input/tex.html#option-descriptions
"maxBuffer": 50 * 1024,
# Use colorv2 extension instead of built-in color extension
# https://docs.mathjax.org/en/latest/input/tex/extensions/autoload.html#tex-autoload-options
# https://docs.mathjax.org/en/latest/input/tex/extensions/colorv2.html#tex-colorv2
"autoload": {"color": [], "colorv2": ["color"]},
},
}
if os.environ.get('SAGE_USE_CDNS', 'no') == 'yes':
mathjax_path = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"
else:
mathjax_path = os.path.join(MATHJAX_DIR, 'tex-chtml.js')
# A list of glob-style patterns that should be excluded when looking for source
# files. They are matched against the source file names relative to the
# source directory, using slashes as directory separators on all platforms.
exclude_patterns = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
# html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
# html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}
# If false, no module index is generated.
# html_use_modindex = True
# A list of prefixes that are ignored for sorting the Python module index ( if
# this is set to ['foo.'], then foo.bar is shown under B, not F). Works only
# for the HTML builder currently.
modindex_common_prefix = ['sage.']
# If false, no index is generated.
# html_use_index = True
# If true, the index is split into individual pages for each letter.
html_split_index = True
# If true, the reST sources are included in the HTML build as _sources/<name>.
# html_copy_source = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''
# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = ''
# Output file base name for HTML help builder.
# htmlhelp_basename = ''
# ------------------------
# Options for LaTeX output
# ------------------------
# See http://sphinx-doc.org/config.html#confval-latex_elements
latex_elements = {}
# The paper size ('letterpaper' or 'a4paper').
#latex_elements['papersize'] = 'letterpaper'
# The font size ('10pt', '11pt' or '12pt').
#latex_elements['pointsize'] = '10pt'
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, document class [howto/manual]).
latex_documents = []
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = 'sagelogo-word.png'
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# Additional stuff for the LaTeX preamble.
latex_elements['preamble'] = r"""
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{textcomp}
\usepackage{mathrsfs}
\usepackage{iftex}
\let\textLaTeX\LaTeX
\AtBeginDocument{\renewcommand*{\LaTeX}{\hbox{\textLaTeX}}}
% Workaround for a LaTeX bug -- see Issue #31397 and
% https://tex.stackexchange.com/questions/583391/mactex-2020-error-with-report-hyperref-mathbf-in-chapter.
\makeatletter
\pdfstringdefDisableCommands{%
\let\mathbf\@firstofone
}
\makeatother
"""
# Documents to append as an appendix to all manuals.
# latex_appendices = []
# If false, no module index is generated.
# latex_use_modindex = True
# -------------------------
# add LaTeX macros for Sage
# -------------------------
from sage.misc.latex_macros import sage_latex_macros
try:
pngmath_latex_preamble # check whether this is already defined
except NameError:
pngmath_latex_preamble = ""
for macro in sage_latex_macros():
# used when building latex and pdf versions
latex_elements['preamble'] += macro + '\n'
# used when building html version
pngmath_latex_preamble += macro + '\n'
# ------------------------------------------
# add custom context variables for templates
# ------------------------------------------
def add_page_context(app, pagename, templatename, context, doctree):
# # The template function
# def template_function(arg):
# return "Your string is " + arg
# # Add it to the page's context
# context['template_function'] = template_function
path1 = os.path.dirname(app.builder.get_outfilename(pagename))
path2 = os.path.join(SAGE_DOC, 'html', 'en')
relpath = os.path.relpath(path2, path1)
context['release'] = release
context['documentation_title'] = f'Version {release} Documentation'
context['documentation_root'] = os.path.join(relpath, 'index.html')
if 'website' in path1:
context['title'] = 'Documentation'
context['website'] = True
context['documentation_root'] = 'index.html'
if 'reference' in path1 and not path1.endswith('reference'):
path2 = os.path.join(SAGE_DOC, 'html', 'en', 'reference')
relpath = os.path.relpath(path2, path1)
context['reference_title'] = f'Version {release} Reference Manual'
context['reference_root'] = os.path.join(relpath, 'index.html')
context['refsub'] = True
if pagename.startswith('sage/'):
# This is for adding small view/edit buttons using Furo's feature:
# https://pradyunsg.me/furo/customisation/top-of-page-buttons/
# This works well if the source file is '.rst' file. But the '.rst'
# files in the directory 'sage/' are generated by the Sphinx
# autodoc from the Python or Cython source files. Hence we tweak
# here template context variables so that links to the correct
# source files are generated.
suffix = '.py' if importlib.import_module(pagename.replace('/','.')).__file__.endswith('.py') else '.pyx'
context['page_source_suffix'] = suffix
context['theme_source_view_link'] = os.path.join(source_repository, f'blob/develop/src', '{filename}')
context['theme_source_edit_link'] = os.path.join(source_repository, f'edit/develop/src', '{filename}')
dangling_debug = False
def debug_inf(app, message):
if dangling_debug:
app.info(message)
def call_intersphinx(app, env, node, contnode):
r"""
Call intersphinx and make links between Sage manuals relative.
TESTS:
Check that the link from the thematic tutorials to the reference
manual is relative, see :issue:`20118`::
sage: from sage.env import SAGE_DOC
sage: thematic_index = os.path.join(SAGE_DOC, "html", "en", "thematic_tutorials", "index.html")
sage: for line in open(thematic_index).readlines(): # optional - sagemath_doc_html
....: if "padics" in line:
....: _ = sys.stdout.write(line)
<li><p><a class="reference external" href="../reference/padics/sage/rings/padics/tutorial.html#sage-rings-padics-tutorial" title="(in $p$-adics v...)"><span>Introduction to the p-adics</span></a></p></li>
"""
debug_inf(app, "???? Trying intersphinx for %s" % node['reftarget'])
builder = app.builder
res = intersphinx.missing_reference(
app, env, node, contnode)
if res:
# Replace absolute links to $SAGE_DOC by relative links: this
# allows to copy the whole documentation tree somewhere else
# without breaking links, see Issue #20118.
if res['refuri'].startswith(SAGE_DOC):
here = os.path.dirname(os.path.join(builder.outdir,
node['refdoc']))
res['refuri'] = os.path.relpath(res['refuri'], here)
debug_inf(app, "++++ Found at %s" % res['refuri'])
else:
debug_inf(app, "---- Intersphinx: %s not Found" % node['reftarget'])
return res
def find_sage_dangling_links(app, env, node, contnode):
r"""
Try to find dangling link in local module imports or all.py.
"""
debug_inf(app, "==================== find_sage_dangling_links ")
reftype = node['reftype']
reftarget = node['reftarget']
try:
doc = node['refdoc']
except KeyError:
debug_inf(app, "-- no refdoc in node %s" % node)
return None
debug_inf(app, "Searching %s from %s" % (reftarget, doc))
# Workaround: in Python's doc 'object', 'list', ... are documented as a
# function rather than a class
if reftarget in base_class_as_func and reftype == 'class':
node['reftype'] = 'func'
res = call_intersphinx(app, env, node, contnode)
if res:
debug_inf(app, "++ DONE %s" % (res['refuri']))
return res
if node.get('refdomain') != 'py': # not a python file
return None
try:
module = node['py:module']
cls = node['py:class']
except KeyError:
debug_inf(app, "-- no module or class for :%s:%s" % (reftype,
reftarget))
return None
basename = reftarget.split(".")[0]
try:
target_module = getattr(sys.modules['sage.all'], basename).__module__
debug_inf(app, "++ found %s using sage.all in %s" % (basename, target_module))
except AttributeError:
try:
target_module = getattr(sys.modules[node['py:module']], basename).__module__
debug_inf(app, "++ found %s in this module" % (basename,))
except AttributeError:
debug_inf(app, "-- %s not found in sage.all or this module" % (basename))
return None
except KeyError:
target_module = None
if target_module is None:
target_module = ""
debug_inf(app, "?? found in None !!!")
newtarget = target_module+'.'+reftarget
node['reftarget'] = newtarget
# adapted from sphinx/domains/python.py
builder = app.builder
searchmode = node.hasattr('refspecific') and 1 or 0
matches = builder.env.domains['py'].find_obj(
builder.env, module, cls, newtarget, reftype, searchmode)
if not matches:
debug_inf(app, "?? no matching doc for %s" % newtarget)
return call_intersphinx(app, env, node, contnode)
elif len(matches) > 1:
env.warn(target_module,
'more than one target found for cross-reference '
'%r: %s' % (newtarget,
', '.join(match[0] for match in matches)),
node.line)
name, obj = matches[0]
debug_inf(app, "++ match = %s %s" % (name, obj))
from docutils import nodes
newnode = nodes.reference('', '', internal=True)
if name == target_module:
newnode['refid'] = name
else:
newnode['refuri'] = builder.get_relative_uri(node['refdoc'], obj[0])
newnode['refuri'] += '#' + name
debug_inf(app, "++ DONE at URI %s" % (newnode['refuri']))
newnode['reftitle'] = name
newnode.append(contnode)
return newnode
# lists of basic Python class which are documented as functions
base_class_as_func = [
'bool', 'complex', 'dict', 'file', 'float',
'frozenset', 'int', 'list', 'long', 'object',
'set', 'slice', 'str', 'tuple', 'type', 'unicode', 'xrange']
# nitpicky option configuration: Put here broken links we want to ignore. For
# link to the Python documentation several links where broken because there
# where class listed as functions. Expand the list 'base_class_as_func' above
# instead of marking the link as broken.
nitpick_ignore = [
('py:class', 'twisted.web2.resource.Resource'),
('py:class', 'twisted.web2.resource.PostableResource')]
skip_picklability_check_modules = [
#'sage.misc.test_nested_class', # for test only
'sage.misc.latex',
'sage.misc.explain_pickle',
'__builtin__',
]
def check_nested_class_picklability(app, what, name, obj, skip, options):
"""
Print a warning if pickling is broken for nested classes.
"""
if hasattr(obj, '__dict__') and hasattr(obj, '__module__'):
# Check picklability of nested classes. Adapted from
# sage.misc.nested_class.modify_for_nested_pickle.
module = sys.modules[obj.__module__]
for (nm, v) in obj.__dict__.items():
if (isinstance(v, type) and
v.__name__ == nm and
v.__module__ == module.__name__ and
getattr(module, nm, None) is not v and
v.__module__ not in skip_picklability_check_modules):
# OK, probably this is an *unpicklable* nested class.
app.warn('Pickling of nested class %r is probably broken. '
'Please set the metaclass of the parent class to '
'sage.misc.nested_class.NestedClassMetaclass.' % (
v.__module__ + '.' + name + '.' + nm))
def skip_member(app, what, name, obj, skip, options):
"""
To suppress Sphinx warnings / errors, we
- Don't include [aliases of] builtins.
- Don't include the docstring for any nested class which has been
inserted into its module by
:class:`sage.misc.NestedClassMetaclass` only for pickling. The
class will be properly documented inside its surrounding class.
- Optionally, check whether pickling is broken for nested classes.
- Optionally, include objects whose name begins with an underscore
('_'), i.e., "private" or "hidden" attributes, methods, etc.
Otherwise, we abide by Sphinx's decision. Note: The object
``obj`` is excluded (included) if this handler returns True
(False).
"""
if 'SAGE_CHECK_NESTED' in os.environ:
check_nested_class_picklability(app, what, name, obj, skip, options)
if getattr(obj, '__module__', None) == '__builtin__':
return True
objname = getattr(obj, "__name__", None)
if objname is not None:
# check if name was inserted to the module by NestedClassMetaclass
if name.find('.') != -1 and objname.find('.') != -1:
if objname.split('.')[-1] == name.split('.')[-1]:
return True
if 'SAGE_DOC_UNDERSCORE' in os.environ:
if name.split('.')[-1].startswith('_'):
return False
return skip
class SagecodeTransform(SphinxTransform):
"""
Transform a code block to a live code block enabled by jupyter-sphinx.
Effectively a code block like::
EXAMPLE::
sage: 1 + 1
2
is transformed into::
EXAMPLE::
sage: 1 + 1
2
.. ONLY:: html
.. JUPYTER-EXECUTE::
:hide-code:
:hide-output:
:raises:
:stderr:
1 + 1
enabling live execution of the code.
"""
# lower than the priority of jupyer_sphinx.execute.ExecuteJupyterCells
default_priority = 170
def apply(self):
if self.app.builder.tags.has('html') or self.app.builder.tags.has('inventory'):
for node in list(self.document.findall(nodes.literal_block)):
if node.get('language') is None and node.astext().startswith('sage:'):
from docutils.nodes import container as Container, label as Label, literal_block as LiteralBlock, Text
from sphinx_inline_tabs._impl import TabContainer
parent = node.parent
index = parent.index(node)
prev_node = node.previous_sibling()
if isinstance(prev_node, TabContainer):
# Make sure not to merge inline tabs for adjacent literal blocks
parent.insert(index, nodes.paragraph())
prev_node = parent[index]
index += 1
parent.remove(node)
# Tab for Sage code
container = TabContainer("", type="tab", new_set=False)
textnodes = [Text('Sage')]
label = Label("", "", *textnodes)
container += label
content = Container("", is_div=True, classes=["tab-content"])
content += node
container += content
parent.insert(index, container)
index += 1
if isinstance(prev_node, nodes.paragraph):
prev_node['classes'].append('with-sage-tab')
if SAGE_PREPARSED_DOC == 'yes':
# Tab for preparsed version
from sage.repl.preparse import preparse
container = TabContainer("", type="tab", new_set=False)
textnodes = [Text('Python')]
label = Label("", "", *textnodes)
container += label
content = Container("", is_div=True, classes=["tab-content"])
example_lines = []
preparsed_lines = ['>>> from sage.all import *']
for line in node.rawsource.splitlines() + ['']: # one extra to process last example
newline = line.lstrip()
if newline.startswith('....: '):
example_lines.append(newline[6:])
else:
if example_lines:
preparsed_example = preparse('\n'.join(example_lines))
prompt = '>>> '