Skip to content

Commit 3c5a8a8

Browse files
authored
Merge pull request #1075 from sphinx-contrib/introduce-raw-mathjax-support
Introduce raw mathjax support
2 parents 16b2b97 + 340d6ea commit 3c5a8a8

File tree

6 files changed

+186
-8
lines changed

6 files changed

+186
-8
lines changed

doc/configuration.rst

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ Advanced processing configuration
20042004

20052005
See also:
20062006

2007+
- :lref:`confluence_mathjax`
20072008
- :ref:`LaTeX directives <latex-directives>`
20082009
- :ref:`LaTeX roles <latex-roles>`
20092010
- :doc:`guide-math`
@@ -2022,6 +2023,42 @@ Advanced processing configuration
20222023
20232024
confluence_link_suffix = '.conf'
20242025
2026+
.. _confluence_mathjax:
2027+
2028+
.. confval:: confluence_mathjax
2029+
2030+
.. important::
2031+
2032+
This option relies on additional configuration of a Confluence
2033+
instance or additional directives such as using an HTML macro.
2034+
Both of these approaches may not be available in a default
2035+
Confluence configuration. Confluence Cloud may require a custom
2036+
marketplace add-on. Confluence Data Center can require a system
2037+
administrator configuring site support for MathJax; or environments
2038+
that have a Confluence HTML macro enabled, users can attempt to
2039+
include MathJax library support via the use of a
2040+
:lref:`confluence_html` directive.
2041+
2042+
While this extension aims to support the capability of generating
2043+
raw content that MathJax could render, the complete solution of
2044+
using MathJax on a Confluence instance is considered unsupported.
2045+
2046+
Generate math content into a raw format that can be rendered on a
2047+
Confluence instance that has been configured to support `MathJax`_.
2048+
2049+
.. code-block:: python
2050+
2051+
confluence_mathjax = True
2052+
2053+
See also:
2054+
2055+
- :lref:`confluence_latex_macro`
2056+
- :ref:`LaTeX directives <latex-directives>`
2057+
- :ref:`LaTeX roles <latex-roles>`
2058+
- :doc:`guide-math`
2059+
2060+
.. versionadded:: 2.10
2061+
20252062
.. index:: Mentions; Configuration
20262063

20272064
.. _confluence_mentions:
@@ -2153,7 +2190,7 @@ Third-party related options
21532190
.. warning::
21542191

21552192
This option relies on an HTML macro which is not available in a
2156-
default Confluence configuration. Using this option is only useful
2193+
default Confluence configuration. Using this option is only useful
21572194
for users that have instances where a system administrator has
21582195
enabled their use.
21592196

@@ -2290,6 +2327,7 @@ Deprecated options
22902327
.. _Confluence editor: https://support.atlassian.com/confluence-cloud/docs/confluence-cloud-editor-roadmap/
22912328
.. _Confluence-supported syntax highlight languages: https://confluence.atlassian.com/confcloud/code-block-macro-724765175.html
22922329
.. _Key of the space: https://support.atlassian.com/confluence-cloud/docs/choose-a-space-key/
2330+
.. _MathJax: https://www.mathjax.org/
22932331
.. _Pygments documented language types: http://pygments.org/docs/lexers/
22942332
.. _Requests -- Authentication: https://requests.readthedocs.io/en/stable/user/authentication/
22952333
.. _Requests SSL Cert Verification: https://requests.readthedocs.io/en/stable/user/advanced/#ssl-cert-verification

doc/guide-math.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Math support
44
============
55

6+
.. versionchanged:: 2.10 Limited support for generating MathJax-raw content.
67
.. versionchanged:: 1.8 Support for using Confluence-supported LaTeX macros.
78
.. versionchanged:: 1.7 SVG-generated math images requires the
89
``sphinx.ext.imgmath`` extension to be explicitly
@@ -192,7 +193,50 @@ determine the name of the macro by:
192193
...
193194
</ac:structured-macro>
194195
196+
.. index:: Math; MathJax
197+
198+
MathJax integration
199+
-------------------
200+
201+
.. important::
202+
203+
While this extension aims to support the capability of generating raw
204+
content that MathJax could render, the complete solution of using
205+
MathJax on a Confluence instance is considered unsupported.
206+
207+
Stock Confluence instances do not include support to render `MathJax`_
208+
content. However, if the instance has been configured to support the
209+
JavaScript engine, the Confluence builder extension can be configured to
210+
generate output that is compatible with MathJax.
211+
212+
Users can use the :lref:`confluence_mathjax` option to output all math
213+
content into a MathJax-compatible format:
214+
215+
.. code-block:: python
216+
217+
confluence_mathjax = True
218+
219+
There are no system administrator guidelines on how to setup a Confluence
220+
instance to provide MathJax support.
221+
222+
Users on a Confluence Data Center instance that has enabled support for
223+
HTML macros may be able to use the :lref:`confluence_html` directive to
224+
enable MathJax support on their pages. For example, adding the following
225+
into a project's ``conf.py``:
226+
227+
.. code-block:: rst
228+
229+
rst_prolog = """
230+
231+
.. confluence_html::
232+
233+
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
234+
235+
"""
236+
237+
User experience may vary.
195238

196239
.. references ------------------------------------------------------------------
197240
241+
.. _MathJax: https://www.mathjax.org/
198242
.. _sphinx.ext.imgmath: https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.imgmath

sphinxcontrib/confluencebuilder/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ def setup(app):
238238
cm.add_conf('confluence_latex_macro', 'confluence')
239239
# Link suffix for generated files.
240240
cm.add_conf('confluence_link_suffix', 'confluence')
241+
# Enable raw math output for MathJax support
242+
cm.add_conf_bool('confluence_mathjax', 'confluence')
241243
# Mappings for documentation mentions to Confluence keys.
242244
cm.add_conf('confluence_mentions', 'confluence')
243245
# Inject navigational hints into the documentation.

sphinxcontrib/confluencebuilder/nodes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ class confluence_link_card_inline(nodes.Inline, ConfluenceParams):
165165
"""
166166

167167

168+
class confluence_mathjax_block(nodes.TextElement):
169+
"""
170+
confluence mathjax block node
171+
172+
A Confluence builder defined MathJax block node, used to help manage MathJax
173+
content designed for a block/section.
174+
"""
175+
176+
177+
class confluence_mathjax_inline(nodes.Inline, nodes.TextElement):
178+
"""
179+
confluence mathjax inline node
180+
181+
A Confluence builder defined MathJax inline node, used to help manage MathJax
182+
content designed for an inlined section of a paragraph.
183+
"""
184+
185+
168186
class confluence_mention_inline(nodes.Inline, nodes.TextElement):
169187
"""
170188
confluence mention inline node

sphinxcontrib/confluencebuilder/storage/translator.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,47 @@ def visit_confluence_latex_inline(self, node):
28012801
param = config.get('inline-macro-param')
28022802
self._visit_confluence_latex(node, macro, param=param)
28032803

2804+
# ---------------------------------------------
2805+
# confluence-builder -- enhancements -- mathjax
2806+
# ---------------------------------------------
2807+
2808+
def visit_confluence_mathjax_block(self, node):
2809+
# if this block is numbered, attempt to align in on the following block
2810+
if node.get('from_math') and node.get('number'):
2811+
if self.builder.config.math_numfig and self.builder.config.numfig:
2812+
figtype = 'displaymath'
2813+
if self.builder.name == 'singleconfluence':
2814+
key = f'{self._docnames[-1]}/{figtype}'
2815+
else:
2816+
key = figtype
2817+
2818+
id_ = node['ids'][0]
2819+
number = self.builder.fignumbers.get(key, {}).get(id_, ())
2820+
number = '.'.join(map(str, number))
2821+
else:
2822+
number = node['number']
2823+
2824+
self.body.append(self.start_tag(node, 'div',
2825+
**{'style': 'float: right'}))
2826+
self.body.append(f'({number})')
2827+
self.body.append(self.end_tag(node))
2828+
2829+
attribs = {}
2830+
alignment = self._fetch_alignment(node)
2831+
if alignment and alignment != 'left':
2832+
attribs['style'] = f'text-align: {alignment};'
2833+
2834+
self.body.append(self.start_tag(node, 'div', **attribs))
2835+
self.body.append(self.encode(node.rawsource))
2836+
self.body.append(self.end_tag(node, suffix=''))
2837+
raise nodes.SkipNode
2838+
2839+
def visit_confluence_mathjax_inline(self, node):
2840+
self.body.append(self.start_tag(node, 'span'))
2841+
self.body.append(self.encode(node.rawsource))
2842+
self.body.append(self.end_tag(node, suffix=''))
2843+
raise nodes.SkipNode
2844+
28042845
# ----------------------------------------------
28052846
# confluence-builder -- enhancements -- mentions
28062847
# ----------------------------------------------

sphinxcontrib/confluencebuilder/transmute/__init__.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from sphinxcontrib.confluencebuilder.logger import ConfluenceLogger
88
from sphinxcontrib.confluencebuilder.nodes import confluence_latex_block
99
from sphinxcontrib.confluencebuilder.nodes import confluence_latex_inline
10+
from sphinxcontrib.confluencebuilder.nodes import confluence_mathjax_block
11+
from sphinxcontrib.confluencebuilder.nodes import confluence_mathjax_inline
1012
from sphinxcontrib.confluencebuilder.svg import confluence_supported_svg
1113
from sphinxcontrib.confluencebuilder.svg import svg_initialize
1214
from sphinxcontrib.confluencebuilder.transmute.ext_jupyter_sphinx import replace_jupyter_sphinx_nodes
@@ -65,7 +67,7 @@ def doctree_transmute(builder, doctree):
6567
# replace graphviz nodes with images
6668
replace_graphviz_nodes(builder, doctree)
6769

68-
# replace math blocks with Confluence LaTeX blocks
70+
# replace math blocks with Confluence LaTeX blocks or raw MathJax content
6971
replace_math_blocks(builder, doctree)
7072

7173
# --------------------------
@@ -109,6 +111,11 @@ def prepare_math_images(builder, doctree):
109111
doctree: the doctree to replace blocks on
110112
"""
111113

114+
# allow users to disabled implemented extension changes
115+
restricted = builder.config.confluence_adv_restricted
116+
if 'ext-imgmath' in restricted:
117+
return
118+
112119
# disable automatic conversion of latex blocks to images if a latex
113120
# macro is configured
114121
if builder.config.confluence_latex_macro:
@@ -296,7 +303,7 @@ def __init__(self, builder):
296303

297304
def replace_math_blocks(builder, doctree):
298305
"""
299-
replace math blocks with images
306+
replace math blocks with Confluence LaTeX blocks
300307
301308
Math blocks are pre-processed and replaced with Confluence LaTeX blocks.
302309
This is to help prepare nodes that can later be used for user-configured
@@ -310,26 +317,54 @@ def replace_math_blocks(builder, doctree):
310317

311318
# allow users to disabled implemented extension changes
312319
restricted = builder.config.confluence_adv_restricted
313-
if 'ext-imgmath' in restricted:
320+
if 'ext-math_blocks' in restricted:
314321
return
315322

323+
# check if raw mathjax output is desired
324+
#
325+
# Note that this will only work in Confluence environments that included
326+
# support for rendering MathJax. This extension will only provide raw
327+
# page content that a page can use. Confluence Cloud may only support
328+
# adding MathJax scripts via a Marketplace addon. Cloud Data Center can
329+
# support loading a MathJax script, but can require HTML support or
330+
# registered some other means by a system administrator. This plugin
331+
# will not attempt to handle any of this; it will be up to the end user
332+
# to utilize this for their instance.
333+
mathjax_mode = builder.config.confluence_mathjax
334+
316335
# convert math blocks into Confluence LaTeX blocks
317336
for node in itertools.chain(findall(doctree, nodes.math),
318337
findall(doctree, nodes.math_block)):
319-
if not isinstance(node, nodes.math):
338+
339+
inlined_math = isinstance(node, nodes.math)
340+
341+
if not inlined_math:
320342
if node['nowrap']:
321343
latex = node.astext()
322344
else:
323345
latex = wrap_displaymath(node.astext(), None, numbering=False)
324-
new_node_type = confluence_latex_block
346+
347+
if mathjax_mode:
348+
new_node_type = confluence_mathjax_block
349+
else:
350+
new_node_type = confluence_latex_block
325351
else:
352+
latex = node.astext()
353+
354+
if mathjax_mode:
355+
new_node_type = confluence_mathjax_inline
356+
else:
357+
new_node_type = confluence_latex_inline
358+
359+
if mathjax_mode:
360+
latex = '\\(' + node.astext() + '\\)'
361+
elif inlined_math:
326362
latex = '$' + node.astext() + '$'
327-
new_node_type = confluence_latex_inline
328363

329364
new_node = new_node_type(latex, latex, **node.attributes)
330365
new_node['from_math'] = True
331366

332-
if not isinstance(node, nodes.math):
367+
if not inlined_math:
333368
new_node['align'] = 'center'
334369

335370
node.replace_self(new_node)

0 commit comments

Comments
 (0)