Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Add strip_math_delims option to sphinxify so that its current default
Browse files Browse the repository at this point in the history
behavior can be disabled.

If I understand correctly, the SageNB instead uses a MathJax extension
which applies math rendering to any <span> or <div> with the "math"
class.  However, this does not work by default with the MathJax settings
in the Jupyter Notebook, so it's better to keep the math delimiters.

Also updates the code that removes them to use a regular expression
instead.
  • Loading branch information
embray committed Sep 23, 2019
1 parent be66263 commit 3067b7f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/sage/misc/sphinxify.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from sphinx.application import Sphinx


def sphinxify(docstring, format='html'):
def sphinxify(docstring, format='html', strip_math_delims=True):
r"""
Runs Sphinx on a ``docstring``, and outputs the processed
documentation.
Expand All @@ -41,6 +41,13 @@ def sphinxify(docstring, format='html'):
- ``format`` -- string (optional, default 'html') -- either 'html' or
'text'
- ``strip_math_delims`` -- string (optional, default True) -- if True,
remove content like ``\( .. \)`` and ``\[ .. \]`` that are used by
default to delimit math content processed by MathJax. This should be
set to False when generating output to be displayed in the Jupyter
Notebook, as its MathJax is configured to look for these. The default
behavior is for legacy support of SageNB.
OUTPUT:
- string -- Sphinx-processed documentation, in either HTML or
Expand All @@ -55,6 +62,8 @@ def sphinxify(docstring, format='html'):
'<div class="docstring"...<strong>Testing</strong>\n<span class="math...</p>\n\n\n</div>'
sage: sphinxify('`x=y`')
'<div class="docstring">\n \n <p><span class="math notranslate nohighlight">x=y</span></p>\n\n\n</div>'
sage: sphinxify(':math:`x=y`', strip_math_delims=False)
'<div class="docstring">\n \n <p><span class="math notranslate nohighlight">\\(x=y\\)</span></p>\n\n\n</div>'
sage: sphinxify('`x=y`', format='text')
'x=y\n'
sage: sphinxify(':math:`x=y`', format='text')
Expand Down Expand Up @@ -141,7 +150,8 @@ def sphinxify(docstring, format='html'):
'src="/doc/static/reference/media/\\2"',
output)
# Remove spurious \(, \), \[, \].
output = output.replace(r'\(', '').replace(r'\)', '').replace(r'\[', '').replace(r'\]', '')
if strip_math_delims:
output = re.sub(r'\\[()[\]]', '', output)
else:
from warnings import warn
warn("Sphinx did not produce any output", Warning)
Expand Down

0 comments on commit 3067b7f

Please sign in to comment.