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']) + '')
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('')
raise nodes.SkipNode
@@ -40,7 +40,7 @@ def html_visit_displaymath(self, node):
self.body.append('(%s)' % number)
self.add_permalink_ref(node, _('Permalink to this equation'))
self.body.append('')
- 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('
')
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] + '')
@@ -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('
')
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 + '\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 + '\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 (''
+ ' title="(in foo v2.0)">'
+ ''
'Bar
' in html)
assert ('\na^2 + b^2 = c^2' in content
- assert '\n\\begin{split}a + 1 < b\\end{split}
' in content
+ assert '\na^2 + b^2 = c^2
' in content
+ assert '\n\\begin{split}a + 1 < b\\end{split}
' in content
assert (u'(1)'
- u'\ne^{i\\pi} = 1
' in content)
+ u'\ne^{i\\pi} = 1
' in content)
assert (u'(2)'
- u'\n'
+ u'
\n'
u'e^{ix} = \\cos x + i\\sin x
' in content)
- assert '
\nn \\in \\mathbb N
' in content
- assert '
\na + 1 < b
' in content
+ assert '
\nn \\in \\mathbb N
' in content
+ assert '
\na + 1 < b
' 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'
\s*'
+ html = (r'
\s*'
r'\\\[ \\begin\{align\}\\begin\{aligned\}S \&= \\pi r\^2\\\\'
r'V \&= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]
')
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'
\s*'
+ html = (r'
\s*'
r'
\(1\)\xb6\\\[a\^2\+b\^2=c\^2\\\]
')
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 = ('
\n'
+ html = ('
\n'
'
(1.2)')
assert html in content
html = ('Referencing equation '
+ (''
'code sample
'),
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`',
- (''
+ (''
'code sample
'),
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`',
- ('a'
+ (''
+ 'a'
'b'
'c
'),
'\\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`',
- (''
+ (''
'--with-option
$'),
r'\\sphinxcode{\\sphinxupquote{-{-}with-option}}$',
),
@@ -188,7 +189,7 @@ def get_verifier(verify, verify_re):
# ... but not in literal text
'verify',
'``"John"``',
- (''
+ (''
'"John"
'),
'\\sphinxcode{\\sphinxupquote{"John"}}',
),