From 553ed8e60f9d35b4584e2edeaba4647a04140f37 Mon Sep 17 00:00:00 2001 From: milde Date: Thu, 18 Oct 2012 23:13:45 +0000 Subject: [PATCH] Customizable MathJax URL (based on patch by Dmitry Shachnev). Also: No line break after opening inline math tag. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7532 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- HISTORY.txt | 2 + docs/user/config.txt | 8 ++++ docutils/writers/html4css1/__init__.py | 28 +++++++------ test/functional/expected/math_output_html.html | 48 ++++++++-------------- test/functional/expected/math_output_latex.html | 48 ++++++++-------------- test/functional/expected/math_output_mathjax.html | 48 ++++++++-------------- .../expected/standalone_rst_html4css1.html | 3 +- 7 files changed, 75 insertions(+), 110 deletions(-) diff --git a/HISTORY.txt b/HISTORY.txt index 53a4b5fc5..c114ec3d3 100644 --- a/HISTORY.txt +++ b/HISTORY.txt @@ -46,6 +46,8 @@ Changes Since 0.9.1 - Use ```` tag for inline "code", do not drop nested inline nodes (syntax highlight tokens). + - Customizable MathJax URL (based on patch by Dmitry Shachnev). + - No line break after opening inline math tag. * docutils/writers/manpage.py diff --git a/docs/user/config.txt b/docs/user/config.txt index a31634f89..2bbb9b74a 100644 --- a/docs/user/config.txt +++ b/docs/user/config.txt @@ -797,6 +797,14 @@ _`math_output` fonts for high-quality typesetting that is scalable and prints at full resolution. + A custom URL can be appended after whitespace, + for example a local install + ``MathJax file:/usr/share/javascript/mathjax/MathJax.js`` or + `access the MathJax CDN using a https secure connection`__ + ``mathjax https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=config=TeX-AMS-MML_HTMLorMML``. + + __ http://www.mathjax.org/resources/faqs/#problem-https + Pro: Works 'out of the box' across multiple browsers and platforms. diff --git a/docutils/writers/html4css1/__init__.py b/docutils/writers/html4css1/__init__.py index 569c5a211..acc11aa1d 100644 --- a/docutils/writers/html4css1/__init__.py +++ b/docutils/writers/html4css1/__init__.py @@ -263,12 +263,7 @@ class HTMLTranslator(nodes.NodeVisitor): # __http://www.mathjax.org/download/mathjax-cdn-terms-of-service/ mathjax_url = ('http://cdn.mathjax.org/mathjax/latest/MathJax.js?' 'config=TeX-AMS-MML_HTMLorMML') - # TODO: make this configurable: - # - # a) as extra option or - # b) appended to math-output="MathJax"? - # - # If b), which delimiter/delimter-set (':', ',', ' ')? + # may be overwritten by custom URL appended to "mathjax" stylesheet_link = '\n' embedded_stylesheet = '\n' @@ -302,8 +297,14 @@ class HTMLTranslator(nodes.NodeVisitor): self.body_suffix = ['\n\n'] self.section_level = 0 self.initial_header_level = int(settings.initial_header_level) - - self.math_output = settings.math_output.lower() + + self.math_output = settings.math_output.split(None, 1) + if len(self.math_output) == 2: + self.math_output_option = self.math_output[1] + else: + self.math_output_option = None + self.math_output = self.math_output[0].lower() + # A heterogenous stack used in conjunction with the tree traversal. # Make sure that the pops correspond to the pushes: self.context = [] @@ -1168,7 +1169,7 @@ class HTMLTranslator(nodes.NodeVisitor): def visit_math(self, node, math_env=''): # If the method is called from visit_math_block(), math_env != ''. - + # As there is no native HTML math support, we provide alternatives: # LaTeX and MathJax math_output modes simply wrap the content, # HTML and MathML math_output modes also convert the math_code. @@ -1176,7 +1177,7 @@ class HTMLTranslator(nodes.NodeVisitor): self.document.reporter.error( 'math-output format "%s" not supported ' 'falling back to "latex"'% self.math_output) - self.math_output = 'latex' + self.math_output = 'latex' # # HTML container tags = {# math_output: (block, inline, class-arguments) @@ -1205,7 +1206,8 @@ class HTMLTranslator(nodes.NodeVisitor): if self.math_output in ('latex', 'mathjax'): math_code = self.encode(math_code) if self.math_output == 'mathjax': - self.math_header = self.mathjax_script % self.mathjax_url + self.math_header = self.mathjax_script % ( + self.math_output_option or self.mathjax_url) elif self.math_output == 'html': math_code = math2html(math_code) elif self.math_output == 'mathml': @@ -1228,7 +1230,9 @@ class HTMLTranslator(nodes.NodeVisitor): raise nodes.SkipNode # append to document body if tag: - self.body.append(self.starttag(node, tag, CLASS=clsarg)) + self.body.append(self.starttag(node, tag, + suffix='\n'*bool(math_env), + CLASS=clsarg)) self.body.append(math_code) if math_env: self.body.append('\n') diff --git a/test/functional/expected/math_output_html.html b/test/functional/expected/math_output_html.html index 25f4f4e19..c77336787 100644 --- a/test/functional/expected/math_output_html.html +++ b/test/functional/expected/math_output_html.html @@ -13,10 +13,8 @@

Mathematics

Docutils supports inline math with the prefix or postfix :math: -role specificator, -n! + sin(x2n) - and -Ac = (π)/(4)d2 +role specificator, n! + sin(x2n) + and Ac = (π)/(4)d2 , as well as displayed math via the math directive:

@@ -59,8 +57,7 @@ See eq:M and
-

is -|M| = ad − bc +

is |M| = ad − bc .

More than one display math block can be put in one math directive. For example, the following sum and integral with limits:

@@ -76,8 +73,7 @@ directives:

i()/(t)Ψ = Ψ, 
-

with the wave function +

with the wave function Ψ , describes how the quantum state of a physical system changes in time.

@@ -89,44 +85,32 @@ physical system changes in time.

- - + \acute{a} - - + \dot{t} - -γ̂ +γ̂ \hat{\gamma} - - + \grave{a} - - + \ddot{t} - -α̃ +α̃ \tilde{\alpha} - - + \breve{x} - -t⃛ +t⃛ \dddot{t} - -ı⃗ +ı⃗ \vec{\imath} - - + \check{a} - -a +a \bar{a} - -R⃗ +R⃗ \vec{R} diff --git a/test/functional/expected/math_output_latex.html b/test/functional/expected/math_output_latex.html index c8605bab8..6a0d5cef1 100644 --- a/test/functional/expected/math_output_latex.html +++ b/test/functional/expected/math_output_latex.html @@ -12,10 +12,8 @@

Mathematics

Docutils supports inline math with the prefix or postfix :math: -role specificator, -n! + \sin(x_n^2) - and -A_\text{c} = +role specificator, n! + \sin(x_n^2) + and A_\text{c} = \frac{\pi}{4} d^2 , as well as displayed math via the math directive:

@@ -32,8 +30,7 @@ See
eq:M and \mathbf{M} = \left(\begin{matrix}a&b\\c&d\end{matrix}\right) -

is -|\mathbf{M}| = ad - bc +

is |\mathbf{M}| = ad - bc .

More than one display math block can be put in one math directive. For example, the following sum and integral with limits:

@@ -49,8 +46,7 @@ directives:

 i\hbar \frac{\partial }{\partial t}\Psi  = \hat{H}\Psi ,
 
-

with the wave function -\Psi +

with the wave function \Psi , describes how the quantum state of a physical system changes in time.

@@ -62,44 +58,32 @@ physical system changes in time.

- -\acute{a} +\acute{a} \acute{a} - -\dot{t} +\dot{t} \dot{t} - -\hat{\gamma} +\hat{\gamma} \hat{\gamma} - -\grave{a} +\grave{a} \grave{a} - -\ddot{t} +\ddot{t} \ddot{t} - -\tilde{\alpha} +\tilde{\alpha} \tilde{\alpha} - -\breve{x} +\breve{x} \breve{x} - -\dddot{t} +\dddot{t} \dddot{t} - -\vec{\imath} +\vec{\imath} \vec{\imath} - -\check{a} +\check{a} \check{a} - -\bar{a} +\bar{a} \bar{a} - -\vec{R} +\vec{R} \vec{R} diff --git a/test/functional/expected/math_output_mathjax.html b/test/functional/expected/math_output_mathjax.html index 2d8fb52f1..cf79bb3a3 100644 --- a/test/functional/expected/math_output_mathjax.html +++ b/test/functional/expected/math_output_mathjax.html @@ -13,10 +13,8 @@

Mathematics

Docutils supports inline math with the prefix or postfix :math: -role specificator, -\(n! + \sin(x_n^2)\) - and -\(A_\text{c} = +role specificator, \(n! + \sin(x_n^2)\) + and \(A_\text{c} = \frac{\pi}{4} d^2\) , as well as displayed math via the math directive:

@@ -39,8 +37,7 @@ See
eq:M and -\(|\mathbf{M}| = ad - bc\) +

is \(|\mathbf{M}| = ad - bc\) .

More than one display math block can be put in one math directive. For example, the following sum and integral with limits:

@@ -62,8 +59,7 @@ directives:

i\hbar \frac{\partial }{\partial t}\Psi = \hat{H}\Psi , \end{equation*} -

with the wave function -\(\Psi \) +

with the wave function \(\Psi \) , describes how the quantum state of a physical system changes in time.

@@ -75,44 +71,32 @@ physical system changes in time.

- -\(\acute{a}\) +\(\acute{a}\) \acute{a} - -\(\dot{t}\) +\(\dot{t}\) \dot{t} - -\(\hat{\gamma}\) +\(\hat{\gamma}\) \hat{\gamma} - -\(\grave{a}\) +\(\grave{a}\) \grave{a} - -\(\ddot{t}\) +\(\ddot{t}\) \ddot{t} - -\(\tilde{\alpha}\) +\(\tilde{\alpha}\) \tilde{\alpha} - -\(\breve{x}\) +\(\breve{x}\) \breve{x} - -\(\dddot{t}\) +\(\dddot{t}\) \dddot{t} - -\(\vec{\imath}\) +\(\vec{\imath}\) \vec{\imath} - -\(\check{a}\) +\(\check{a}\) \check{a} - -\(\bar{a}\) +\(\bar{a}\) \bar{a} - -\(\vec{R}\) +\(\vec{R}\) \vec{R} diff --git a/test/functional/expected/standalone_rst_html4css1.html b/test/functional/expected/standalone_rst_html4css1.html index 9e08674cc..87a5e91be 100644 --- a/test/functional/expected/standalone_rst_html4css1.html +++ b/test/functional/expected/standalone_rst_html4css1.html @@ -862,8 +862,7 @@ directly (the code will not be parsed/tagged, as the language is not known) or as base for special code roles, e.g. the LaTeX code in the next paragraph.

Docutils uses LaTeX syntax for math directives and roles: -\alpha = f(x) prints -\(\alpha = f(x)\) +\alpha = f(x) prints \(\alpha = f(x)\) .

The :code: option of the include directive sets the included content as a code block, here the rst file header_footer.txt with line numbers:

-- 2.11.4.GIT