Skip to content

Commit b5ec4d5

Browse files
committed
Update doc conf file for python 3
1 parent 2678100 commit b5ec4d5

File tree

1 file changed

+136
-42
lines changed

1 file changed

+136
-42
lines changed

docs/source/conf.py

Lines changed: 136 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
package_folder = "../../sage_sample"
2020
authors = u"Matthias Koeppe, Sébastien Labbé, Viviane Pons, Nicolas M. Thiéry, ... with inspiration from many"
2121

22+
import six
2223
import sys
2324
import os
24-
2525
from sage.env import SAGE_DOC_SRC, SAGE_DOC, SAGE_SRC
2626

2727
try:
@@ -31,11 +31,15 @@
3131

3232

3333

34+
3435
# If extensions (or modules to document with autodoc) are in another directory,
3536
# add these directories to sys.path here. If the directory is relative to the
3637
# documentation root, use os.path.abspath to make it absolute, like shown here.
37-
sys.path.append(os.path.abspath(package_folder))
38-
sys.path.append(os.path.join(SAGE_SRC, "sage_setup", "docbuild", "ext"))
38+
sys.path.insert(0, os.path.abspath(package_folder))
39+
#sys.path.append(os.path.join(SAGE_SRC, "sage_setup", "docbuild", "ext"))
40+
41+
42+
print("Using sys.path = {}".format(sys.path))
3943

4044
# -- General configuration ------------------------------------------------
4145

@@ -46,13 +50,90 @@
4650
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
4751
# ones.
4852
extensions = [
49-
#'sphinx.ext.autodoc',
50-
'sage_autodoc',
51-
'sage_package.sphinx',
53+
'sphinx.ext.autodoc',
54+
#'sage_autodoc', ## Not available on conda-forge sage!
55+
#'sage_package.sphinx',
5256
'sphinx.ext.doctest',
5357
'sphinx.ext.coverage',
58+
'sphinx.ext.extlinks',
59+
'matplotlib.sphinxext.plot_directive',
60+
#'sphinxcontrib.bibtex'
5461
]
5562

63+
### from Sage src/doc/common/conf.py
64+
# This code is executed before each ".. PLOT::" directive in the Sphinx
65+
# documentation. It defines a 'sphinx_plot' function that displays a Sage object
66+
# through matplotlib, so that it will be displayed in the HTML doc.
67+
plot_html_show_source_link = False
68+
plot_pre_code = """
69+
def sphinx_plot(graphics, **kwds):
70+
import matplotlib.image as mpimg
71+
from sage.misc.temporary_file import tmp_filename
72+
import matplotlib.pyplot as plt
73+
## Option handling is taken from Graphics.save
74+
try:
75+
from sage.plot.multigraphics import GraphicsArray
76+
except ImportError:
77+
from sage.plot.graphics import GraphicsArray
78+
options = dict()
79+
if not isinstance(graphics, GraphicsArray):
80+
options.update(graphics.SHOW_OPTIONS)
81+
options.update(graphics._extra_kwds)
82+
options.update(kwds)
83+
dpi = options.pop('dpi', None)
84+
transparent = options.pop('transparent', None)
85+
fig_tight = options.pop('fig_tight', None)
86+
figsize = options.pop('figsize', None)
87+
## figsize handling is taken from Graphics.matplotlib()
88+
if figsize is not None and not isinstance(figsize, (list, tuple)):
89+
# in this case, figsize is a number and should be positive
90+
try:
91+
figsize = float(figsize) # to pass to mpl
92+
except TypeError:
93+
raise TypeError("figsize should be a positive number, not {0}".format(figsize))
94+
if figsize > 0:
95+
default_width, default_height=rcParams['figure.figsize']
96+
figsize=(figsize, default_height*figsize/default_width)
97+
else:
98+
raise ValueError("figsize should be positive, not {0}".format(figsize))
99+
100+
if figsize is not None:
101+
# then the figsize should be two positive numbers
102+
if len(figsize) != 2:
103+
raise ValueError("figsize should be a positive number "
104+
"or a list of two positive numbers, not {0}".format(figsize))
105+
figsize = (float(figsize[0]),float(figsize[1])) # floats for mpl
106+
if not (figsize[0] > 0 and figsize[1] > 0):
107+
raise ValueError("figsize should be positive numbers, "
108+
"not {0} and {1}".format(figsize[0],figsize[1]))
109+
110+
plt.figure(figsize=figsize)
111+
if isinstance(graphics, GraphicsArray):
112+
## from GraphicsArray.save
113+
figure = plt.gcf()
114+
rows = graphics.nrows()
115+
cols = graphics.ncols()
116+
for i, g in enumerate(graphics):
117+
subplot = figure.add_subplot(rows, cols, i + 1)
118+
g_options = copy(options)
119+
g_options.update(g.SHOW_OPTIONS)
120+
g_options.update(g._extra_kwds)
121+
g_options.pop('dpi', None)
122+
g_options.pop('transparent', None)
123+
g_options.pop('fig_tight', None)
124+
g.matplotlib(figure=figure, sub=subplot, **g_options)
125+
else:
126+
figure = graphics.matplotlib(figure=plt.gcf(), figsize=figsize, **options)
127+
plt.tight_layout(pad=0)
128+
plt.margins(0)
129+
plt.show()
130+
131+
from sage.all_cmdline import *
132+
"""
133+
134+
plot_html_show_formats = False
135+
plot_formats = ['svg', 'pdf', 'png']
136+
56137
# Add any paths that contain templates here, relative to this directory.
57138
# templates_path = ['_templates']
58139
templates_path = [os.path.join(SAGE_DOC_SRC, 'common', 'templates'), '_templates']
@@ -72,10 +153,15 @@
72153
# |version| and |release|, also used in various other places throughout the
73154
# built documents.
74155
#
75-
# The short X.Y version.
76-
version = open("../../VERSION").read().strip()
156+
from pkg_resources import get_distribution, DistributionNotFound
77157
# The full version, including alpha/beta/rc tags.
78-
release = version
158+
try:
159+
release = get_distribution('sage-numerical-interactive-mip').version
160+
except DistributionNotFound:
161+
release = "0.2"
162+
#print("############# release reported: {} ##################".format(release))
163+
# The short X.Y version.
164+
version = '.'.join(release.split('.')[:2])
79165

80166
# The language for content autogenerated by Sphinx. Refer to documentation
81167
# for a list of supported languages.
@@ -115,35 +201,18 @@
115201
# If true, keep warnings as "system message" paragraphs in the built documents.
116202
#keep_warnings = False
117203

118-
pythonversion = sys.version.split(' ')[0]
119-
# Python and Sage trac ticket shortcuts. For example, :trac:`7549` .
120-
extlinks = {
121-
'python': ('https://docs.python.org/release/'+pythonversion+'/%s', ''),
122-
'trac': ('http://trac.sagemath.org/%s', 'trac ticket #'),
123-
'wikipedia': ('https://en.wikipedia.org/wiki/%s', 'Wikipedia article '),
124-
'arxiv': ('http://arxiv.org/abs/%s', 'Arxiv '),
125-
'oeis': ('https://oeis.org/%s', 'OEIS sequence '),
126-
'doi': ('https://dx.doi.org/%s', 'doi:'),
127-
'mathscinet': ('http://www.ams.org/mathscinet-getitem?mr=%s', 'MathSciNet ')
128-
}
129-
130204
# -- Options for HTML output ----------------------------------------------
131205

132206
# The theme to use for HTML and HTML Help pages. See the documentation for
133207
# a list of builtin themes.
134-
html_theme = 'default'
208+
html_theme = 'sage'
209+
html_theme_path = ['../themes']
135210

136211
# Theme options are theme-specific and customize the look and feel of a theme
137212
# further. For a list of options available for each theme, see the
138213
# documentation.
139214
html_theme_options = {}
140215

141-
142-
# Add any paths that contain custom themes here, relative to this directory.
143-
#html_theme_path = []
144-
#html_theme_path = [os.path.join(SAGE_DOC_SRC, 'common', 'themes')]
145-
html_theme_path = [os.path.join(SAGE_DOC_SRC, 'common', 'themes', 'sage')]
146-
147216
# The name for this set of Sphinx documents. If None, it defaults to
148217
# "<project> v<release> documentation".
149218

@@ -162,8 +231,7 @@
162231
# Add any paths that contain custom static files (such as style sheets) here,
163232
# relative to this directory. They are copied after the builtin static files,
164233
# so a file named "default.css" will overwrite the builtin "default.css".
165-
#html_static_path = ['_static']
166-
html_static_path = []
234+
html_static_path = [] #['_static']
167235

168236
# Add any extra paths that contain custom files (such as robots.txt or
169237
# .htaccess) here, relative to this directory. These files are copied
@@ -232,7 +300,7 @@
232300
# (source start file, target name, title,
233301
# author, documentclass [howto, manual, or own class]).
234302
latex_documents = [
235-
('index', package_name + '.tex', u'Documentation of ' + unicode(package_name),
303+
('index', package_name + '.tex', u'Documentation of ' + six.text_type(package_name),
236304
authors, 'manual'),
237305
]
238306

@@ -262,7 +330,7 @@
262330
# One entry per manual page. List of tuples
263331
# (source start file, name, description, authors, manual section).
264332
man_pages = [
265-
('index', package_name, unicode(package_name) + u" documentation",
333+
('index', package_name, six.text_type(package_name) + u" documentation",
266334
[authors], 1)
267335
]
268336

@@ -276,7 +344,7 @@
276344
# (source start file, target name, title, author,
277345
# dir menu entry, description, category)
278346
texinfo_documents = [
279-
('index', package_name, unicode(package_name) + u" documentation",
347+
('index', package_name, six.text_type(package_name) + u" documentation",
280348
authors, package_name, project,
281349
'Miscellaneous'),
282350
]
@@ -310,17 +378,20 @@
310378
# this is broken for now
311379
# html_theme_options['mathjax_macros'] = sage_mathjax_macros()
312380

313-
from pkg_resources import Requirement, working_set
314-
sagenb_path = working_set.find(Requirement.parse('sagenb')).location
315-
mathjax_relative = os.path.join('sagenb','data','mathjax')
381+
## from pkg_resources import Requirement, working_set
382+
## sagenb_path = working_set.find(Requirement.parse('sagenb')).location
383+
## mathjax_relative = os.path.join('sagenb','data','mathjax')
316384

317-
# It would be really nice if sphinx would copy the entire mathjax directory,
318-
# (so we could have a _static/mathjax directory), rather than the contents of the directory
385+
## # It would be really nice if sphinx would copy the entire mathjax directory,
386+
## # (so we could have a _static/mathjax directory), rather than the contents of the directory
319387

320-
mathjax_static = os.path.join(sagenb_path, mathjax_relative)
321-
html_static_path.append(mathjax_static)
322-
exclude_patterns=['**/'+os.path.join(mathjax_relative, i) for i in ('docs', 'README*', 'test',
323-
'unpacked', 'LICENSE')]
388+
## mathjax_static = os.path.join(sagenb_path, mathjax_relative)
389+
## html_static_path.append(mathjax_static)
390+
## exclude_patterns=['**/'+os.path.join(mathjax_relative, i) for i in ('docs', 'README*', 'test',
391+
## 'unpacked', 'LICENSE')]
392+
from sage.env import SAGE_LOCAL, SAGE_SHARE
393+
html_static_path.append(SAGE_LOCAL + "/lib/mathjax") # conda
394+
html_static_path.append(SAGE_SHARE + "/mathjax") # sage distribution
324395
else:
325396
extensions.append('sphinx.ext.pngmath')
326397

@@ -349,3 +420,26 @@ def __init__(self, **options):
349420
\makeatother
350421
\renewcommand{\ttdefault}{txtt}
351422
'''
423+
424+
#####################################################
425+
# add LaTeX macros for Sage
426+
427+
from sage.misc.latex_macros import sage_latex_macros
428+
429+
try:
430+
pngmath_latex_preamble # check whether this is already defined
431+
except NameError:
432+
pngmath_latex_preamble = ""
433+
434+
for macro in sage_latex_macros():
435+
# used when building latex and pdf versions
436+
latex_elements['preamble'] += macro + '\n'
437+
# used when building html version
438+
pngmath_latex_preamble += macro + '\n'
439+
440+
441+
## The following is needed on conda-forge sagemath
442+
from sage.repl.user_globals import initialize_globals
443+
import sage.all
444+
my_globs = dict()
445+
initialize_globals(sage.all, my_globs)

0 commit comments

Comments
 (0)