summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2018-01-13 15:18:09 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2018-01-13 20:25:49 +0900
commit4d040abafb9e0626dcf7a420bbe32cbfa0896df6 (patch)
tree0cfac8b8a6ded75da672a5f77217b3c14fd1be3d
parentf57634a8b8f6f656266b2a967cc3e5626ba70659 (diff)
downloadsphinx-git-4d040abafb9e0626dcf7a420bbe32cbfa0896df6.tar.gz
Fix #4079: Add notranslate class to let Google Translate know they are not translatable
-rw-r--r--CHANGES2
-rw-r--r--sphinx/ext/jsmath.py6
-rw-r--r--sphinx/ext/mathjax.py4
-rw-r--r--sphinx/writers/html.py6
-rw-r--r--sphinx/writers/html5.py6
-rw-r--r--tests/test_build_html.py16
-rw-r--r--tests/test_build_html5.py16
-rw-r--r--tests/test_ext_intersphinx.py3
-rw-r--r--tests/test_ext_math.py18
-rw-r--r--tests/test_markup.py11
10 files changed, 46 insertions, 42 deletions
diff --git a/CHANGES b/CHANGES
index 2c05a81fa..eb91109a5 100644
--- a/CHANGES
+++ b/CHANGES
@@ -77,6 +77,8 @@ Features added
* #3570: autodoc: Do not display 'typing.' module for type hints
* #4354: sphinx-build now emits finish message. Builders can modify it through
``Builder.epilog`` attribute
+* #4079: html: Add ``notranslate`` class to each code-blocks, literals and maths
+ to let Google Translate know they are not translatable
Features removed
----------------
diff --git a/sphinx/ext/jsmath.py b/sphinx/ext/jsmath.py
index a74f0641a..0858e4d5d 100644
--- a/sphinx/ext/jsmath.py
+++ b/sphinx/ext/jsmath.py
@@ -20,14 +20,14 @@ from sphinx.ext.mathbase import get_node_equation_number
def html_visit_math(self, node):
- self.body.append(self.starttag(node, 'span', '', CLASS='math'))
+ self.body.append(self.starttag(node, 'span', '', CLASS='math notranslate'))
self.body.append(self.encode(node['latex']) + '</span>')
raise nodes.SkipNode
def html_visit_displaymath(self, node):
if node['nowrap']:
- self.body.append(self.starttag(node, 'div', CLASS='math'))
+ self.body.append(self.starttag(node, 'div', CLASS='math notranslate'))
self.body.append(self.encode(node['latex']))
self.body.append('</div>')
raise nodes.SkipNode
@@ -40,7 +40,7 @@ def html_visit_displaymath(self, node):
self.body.append('<span class="eqno">(%s)' % number)
self.add_permalink_ref(node, _('Permalink to this equation'))
self.body.append('</span>')
- self.body.append(self.starttag(node, 'div', CLASS='math'))
+ self.body.append(self.starttag(node, 'div', CLASS='math notranslate'))
else:
# but only once!
self.body.append('<div class="math">')
diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py
index 8698e2801..bfbd34979 100644
--- a/sphinx/ext/mathjax.py
+++ b/sphinx/ext/mathjax.py
@@ -21,7 +21,7 @@ from sphinx.ext.mathbase import get_node_equation_number
def html_visit_math(self, node):
- self.body.append(self.starttag(node, 'span', '', CLASS='math'))
+ self.body.append(self.starttag(node, 'span', '', CLASS='math notranslate'))
self.body.append(self.builder.config.mathjax_inline[0] +
self.encode(node['latex']) +
self.builder.config.mathjax_inline[1] + '</span>')
@@ -29,7 +29,7 @@ def html_visit_math(self, node):
def html_visit_displaymath(self, node):
- self.body.append(self.starttag(node, 'div', CLASS='math'))
+ self.body.append(self.starttag(node, 'div', CLASS='math notranslate'))
if node['nowrap']:
self.body.append(self.encode(node['latex']))
self.body.append('</div>')
diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py
index 84e7bfbc9..16fc69bea 100644
--- a/sphinx/writers/html.py
+++ b/sphinx/writers/html.py
@@ -444,7 +444,7 @@ class HTMLTranslator(BaseTranslator):
location=(self.builder.current_docname, node.line), **highlight_args
)
starttag = self.starttag(node, 'div', suffix='',
- CLASS='highlight-%s' % lang)
+ CLASS='highlight-%s notranslate' % lang)
self.body.append(starttag + highlighted + '</div>\n')
raise nodes.SkipNode
@@ -494,10 +494,10 @@ class HTMLTranslator(BaseTranslator):
# type: (nodes.Node) -> None
if 'kbd' in node['classes']:
self.body.append(self.starttag(node, 'kbd', '',
- CLASS='docutils literal'))
+ CLASS='docutils literal notranslate'))
else:
self.body.append(self.starttag(node, 'code', '',
- CLASS='docutils literal'))
+ CLASS='docutils literal notranslate'))
self.protect_literal_text += 1
def depart_literal(self, node):
diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py
index 50bf2ea8c..c2810a898 100644
--- a/sphinx/writers/html5.py
+++ b/sphinx/writers/html5.py
@@ -390,7 +390,7 @@ class HTML5Translator(BaseTranslator):
location=(self.builder.current_docname, node.line), **highlight_args
)
starttag = self.starttag(node, 'div', suffix='',
- CLASS='highlight-%s' % lang)
+ CLASS='highlight-%s notranslate' % lang)
self.body.append(starttag + highlighted + '</div>\n')
raise nodes.SkipNode
@@ -440,10 +440,10 @@ class HTML5Translator(BaseTranslator):
# type: (nodes.Node) -> None
if 'kbd' in node['classes']:
self.body.append(self.starttag(node, 'kbd', '',
- CLASS='docutils literal'))
+ CLASS='docutils literal notranslate'))
else:
self.body.append(self.starttag(node, 'code', '',
- CLASS='docutils literal'))
+ CLASS='docutils literal notranslate'))
self.protect_literal_text += 1
def depart_literal(self, node):
diff --git a/tests/test_build_html.py b/tests/test_build_html.py
index 153ff5165..2388b06ec 100644
--- a/tests/test_build_html.py
+++ b/tests/test_build_html.py
@@ -163,21 +163,21 @@ def test_html_warnings(app, warning):
(".//pre/span", u'"quotes"'),
(".//pre/span", u"'included'"),
(".//pre/span[@class='s2']", u'üöä'),
- (".//div[@class='inc-pyobj1 highlight-text']//pre",
+ (".//div[@class='inc-pyobj1 highlight-text notranslate']//pre",
r'^class Foo:\n pass\n\s*$'),
- (".//div[@class='inc-pyobj2 highlight-text']//pre",
+ (".//div[@class='inc-pyobj2 highlight-text notranslate']//pre",
r'^ def baz\(\):\n pass\n\s*$'),
- (".//div[@class='inc-lines highlight-text']//pre",
+ (".//div[@class='inc-lines highlight-text notranslate']//pre",
r'^class Foo:\n pass\nclass Bar:\n$'),
- (".//div[@class='inc-startend highlight-text']//pre",
+ (".//div[@class='inc-startend highlight-text notranslate']//pre",
u'^foo = "Including Unicode characters: üöä"\\n$'),
- (".//div[@class='inc-preappend highlight-text']//pre",
+ (".//div[@class='inc-preappend highlight-text notranslate']//pre",
r'(?m)^START CODE$'),
- (".//div[@class='inc-pyobj-dedent highlight-python']//span",
+ (".//div[@class='inc-pyobj-dedent highlight-python notranslate']//span",
r'def'),
- (".//div[@class='inc-tab3 highlight-text']//pre",
+ (".//div[@class='inc-tab3 highlight-text notranslate']//pre",
r'-| |-'),
- (".//div[@class='inc-tab8 highlight-python']//pre/span",
+ (".//div[@class='inc-tab8 highlight-python notranslate']//pre/span",
r'-| |-'),
],
'autodoc.html': [
diff --git a/tests/test_build_html5.py b/tests/test_build_html5.py
index 4ac70be51..168e516cf 100644
--- a/tests/test_build_html5.py
+++ b/tests/test_build_html5.py
@@ -72,21 +72,21 @@ def cached_etree_parse():
(".//pre/span", u'"quotes"'),
(".//pre/span", u"'included'"),
(".//pre/span[@class='s2']", u'üöä'),
- (".//div[@class='inc-pyobj1 highlight-text']//pre",
+ (".//div[@class='inc-pyobj1 highlight-text notranslate']//pre",
r'^class Foo:\n pass\n\s*$'),
- (".//div[@class='inc-pyobj2 highlight-text']//pre",
+ (".//div[@class='inc-pyobj2 highlight-text notranslate']//pre",
r'^ def baz\(\):\n pass\n\s*$'),
- (".//div[@class='inc-lines highlight-text']//pre",
+ (".//div[@class='inc-lines highlight-text notranslate']//pre",
r'^class Foo:\n pass\nclass Bar:\n$'),
- (".//div[@class='inc-startend highlight-text']//pre",
+ (".//div[@class='inc-startend highlight-text notranslate']//pre",
u'^foo = "Including Unicode characters: üöä"\\n$'),
- (".//div[@class='inc-preappend highlight-text']//pre",
+ (".//div[@class='inc-preappend highlight-text notranslate']//pre",
r'(?m)^START CODE$'),
- (".//div[@class='inc-pyobj-dedent highlight-python']//span",
+ (".//div[@class='inc-pyobj-dedent highlight-python notranslate']//span",
r'def'),
- (".//div[@class='inc-tab3 highlight-text']//pre",
+ (".//div[@class='inc-tab3 highlight-text notranslate']//pre",
r'-| |-'),
- (".//div[@class='inc-tab8 highlight-python']//pre/span",
+ (".//div[@class='inc-tab8 highlight-python notranslate']//pre/span",
r'-| |-'),
],
'autodoc.html': [
diff --git a/tests/test_ext_intersphinx.py b/tests/test_ext_intersphinx.py
index 19f8613c6..aef495d30 100644
--- a/tests/test_ext_intersphinx.py
+++ b/tests/test_ext_intersphinx.py
@@ -236,7 +236,8 @@ def test_missing_reference_cppdomain(tempdir, app, status, warning):
html = (app.outdir / 'index.html').text()
assert ('<a class="reference external"'
' href="https://docs.python.org/index.html#cpp_foo_bar"'
- ' title="(in foo v2.0)"><code class="xref cpp cpp-class docutils literal">'
+ ' title="(in foo v2.0)">'
+ '<code class="xref cpp cpp-class docutils literal notranslate">'
'<span class="pre">Bar</span></code></a>' in html)
assert ('<a class="reference external"'
' href="https://docs.python.org/index.html#foons"'
diff --git a/tests/test_ext_math.py b/tests/test_ext_math.py
index 5bf4ebb15..ad12d4091 100644
--- a/tests/test_ext_math.py
+++ b/tests/test_ext_math.py
@@ -35,17 +35,17 @@ def test_jsmath(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'math.html').text()
- assert '<div class="math">\na^2 + b^2 = c^2</div>' in content
- assert '<div class="math">\n\\begin{split}a + 1 &lt; b\\end{split}</div>' in content
+ assert '<div class="math notranslate">\na^2 + b^2 = c^2</div>' in content
+ assert '<div class="math notranslate">\n\\begin{split}a + 1 &lt; b\\end{split}</div>' in content
assert (u'<span class="eqno">(1)<a class="headerlink" href="#equation-foo" '
u'title="Permalink to this equation">\xb6</a></span>'
- u'<div class="math" id="equation-foo">\ne^{i\\pi} = 1</div>' in content)
+ u'<div class="math notranslate" id="equation-foo">\ne^{i\\pi} = 1</div>' in content)
assert (u'<span class="eqno">(2)<a class="headerlink" href="#equation-math:0" '
u'title="Permalink to this equation">\xb6</a></span>'
- u'<div class="math" id="equation-math:0">\n'
+ u'<div class="math notranslate" id="equation-math:0">\n'
u'e^{ix} = \\cos x + i\\sin x</div>' in content)
- assert '<div class="math">\nn \\in \\mathbb N</div>' in content
- assert '<div class="math">\na + 1 &lt; b</div>' in content
+ assert '<div class="math notranslate">\nn \\in \\mathbb N</div>' in content
+ assert '<div class="math notranslate">\na + 1 &lt; b</div>' in content
@pytest.mark.skipif(not has_binary('dvipng'),
@@ -89,7 +89,7 @@ def test_mathjax_align(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'index.html').text()
- html = (r'<div class="math">\s*'
+ html = (r'<div class="math notranslate">\s*'
r'\\\[ \\begin\{align\}\\begin\{aligned\}S \&amp;= \\pi r\^2\\\\'
r'V \&amp;= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]</div>')
assert re.search(html, content, re.S)
@@ -102,7 +102,7 @@ def test_math_number_all_mathjax(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'index.html').text()
- html = (r'<div class="math" id="equation-index:0">\s*'
+ html = (r'<div class="math notranslate" id="equation-index:0">\s*'
r'<span class="eqno">\(1\)<a .*>\xb6</a></span>\\\[a\^2\+b\^2=c\^2\\\]</div>')
assert re.search(html, content, re.S)
@@ -167,7 +167,7 @@ def test_mathjax_numfig_html(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'math.html').text()
- html = ('<div class="math" id="equation-math:0">\n'
+ html = ('<div class="math notranslate" id="equation-math:0">\n'
'<span class="eqno">(1.2)')
assert html in content
html = ('<p>Referencing equation <a class="reference internal" '
diff --git a/tests/test_markup.py b/tests/test_markup.py
index c48096e34..1bc3ef8e6 100644
--- a/tests/test_markup.py
+++ b/tests/test_markup.py
@@ -133,7 +133,7 @@ def get_verifier(verify, verify_re):
# correct interpretation of code with whitespace
'verify_re',
'``code sample``',
- ('<p><code class="(samp )?docutils literal"><span class="pre">'
+ ('<p><code class="(samp )?docutils literal notranslate"><span class="pre">'
'code</span>&#160;&#160; <span class="pre">sample</span></code></p>'),
r'\\sphinxcode{\\sphinxupquote{code sample}}',
),
@@ -141,7 +141,7 @@ def get_verifier(verify, verify_re):
# correct interpretation of code with whitespace
'verify_re',
':samp:`code sample`',
- ('<p><code class="(samp )?docutils literal"><span class="pre">'
+ ('<p><code class="(samp )?docutils literal notranslate"><span class="pre">'
'code</span>&#160;&#160; <span class="pre">sample</span></code></p>'),
r'\\sphinxcode{\\sphinxupquote{code sample}}',
),
@@ -149,7 +149,8 @@ def get_verifier(verify, verify_re):
# interpolation of braces in samp and file roles (HTML only)
'verify',
':samp:`a{b}c`',
- ('<p><code class="samp docutils literal"><span class="pre">a</span>'
+ ('<p><code class="samp docutils literal notranslate">'
+ '<span class="pre">a</span>'
'<em><span class="pre">b</span></em>'
'<span class="pre">c</span></code></p>'),
'\\sphinxcode{\\sphinxupquote{a\\sphinxstyleemphasis{b}c}}',
@@ -173,7 +174,7 @@ def get_verifier(verify, verify_re):
# non-interpolation of dashes in option role
'verify_re',
':option:`--with-option`',
- ('<p><code( class="xref std std-option docutils literal")?>'
+ ('<p><code( class="xref std std-option docutils literal notranslate")?>'
'<span class="pre">--with-option</span></code></p>$'),
r'\\sphinxcode{\\sphinxupquote{-{-}with-option}}$',
),
@@ -188,7 +189,7 @@ def get_verifier(verify, verify_re):
# ... but not in literal text
'verify',
'``"John"``',
- ('<p><code class="docutils literal"><span class="pre">'
+ ('<p><code class="docutils literal notranslate"><span class="pre">'
'&quot;John&quot;</span></code></p>'),
'\\sphinxcode{\\sphinxupquote{"John"}}',
),