Fix #4079: Add notranslate class to let Google Translate know they are not translatable

This commit is contained in:
Takeshi KOMIYA 2018-01-13 15:18:09 +09:00
parent f57634a8b8
commit 4d040abafb
10 changed files with 46 additions and 42 deletions

View File

@ -77,6 +77,8 @@ Features added
* #3570: autodoc: Do not display 'typing.' module for type hints * #3570: autodoc: Do not display 'typing.' module for type hints
* #4354: sphinx-build now emits finish message. Builders can modify it through * #4354: sphinx-build now emits finish message. Builders can modify it through
``Builder.epilog`` attribute ``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 Features removed
---------------- ----------------

View File

@ -20,14 +20,14 @@ from sphinx.ext.mathbase import get_node_equation_number
def html_visit_math(self, node): 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>') self.body.append(self.encode(node['latex']) + '</span>')
raise nodes.SkipNode raise nodes.SkipNode
def html_visit_displaymath(self, node): def html_visit_displaymath(self, node):
if node['nowrap']: 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(self.encode(node['latex']))
self.body.append('</div>') self.body.append('</div>')
raise nodes.SkipNode raise nodes.SkipNode
@ -40,7 +40,7 @@ def html_visit_displaymath(self, node):
self.body.append('<span class="eqno">(%s)' % number) self.body.append('<span class="eqno">(%s)' % number)
self.add_permalink_ref(node, _('Permalink to this equation')) self.add_permalink_ref(node, _('Permalink to this equation'))
self.body.append('</span>') self.body.append('</span>')
self.body.append(self.starttag(node, 'div', CLASS='math')) self.body.append(self.starttag(node, 'div', CLASS='math notranslate'))
else: else:
# but only once! # but only once!
self.body.append('<div class="math">') self.body.append('<div class="math">')

View File

@ -21,7 +21,7 @@ from sphinx.ext.mathbase import get_node_equation_number
def html_visit_math(self, node): 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.body.append(self.builder.config.mathjax_inline[0] +
self.encode(node['latex']) + self.encode(node['latex']) +
self.builder.config.mathjax_inline[1] + '</span>') self.builder.config.mathjax_inline[1] + '</span>')
@ -29,7 +29,7 @@ def html_visit_math(self, node):
def html_visit_displaymath(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']: if node['nowrap']:
self.body.append(self.encode(node['latex'])) self.body.append(self.encode(node['latex']))
self.body.append('</div>') self.body.append('</div>')

View File

@ -444,7 +444,7 @@ class HTMLTranslator(BaseTranslator):
location=(self.builder.current_docname, node.line), **highlight_args location=(self.builder.current_docname, node.line), **highlight_args
) )
starttag = self.starttag(node, 'div', suffix='', starttag = self.starttag(node, 'div', suffix='',
CLASS='highlight-%s' % lang) CLASS='highlight-%s notranslate' % lang)
self.body.append(starttag + highlighted + '</div>\n') self.body.append(starttag + highlighted + '</div>\n')
raise nodes.SkipNode raise nodes.SkipNode
@ -494,10 +494,10 @@ class HTMLTranslator(BaseTranslator):
# type: (nodes.Node) -> None # type: (nodes.Node) -> None
if 'kbd' in node['classes']: if 'kbd' in node['classes']:
self.body.append(self.starttag(node, 'kbd', '', self.body.append(self.starttag(node, 'kbd', '',
CLASS='docutils literal')) CLASS='docutils literal notranslate'))
else: else:
self.body.append(self.starttag(node, 'code', '', self.body.append(self.starttag(node, 'code', '',
CLASS='docutils literal')) CLASS='docutils literal notranslate'))
self.protect_literal_text += 1 self.protect_literal_text += 1
def depart_literal(self, node): def depart_literal(self, node):

View File

@ -390,7 +390,7 @@ class HTML5Translator(BaseTranslator):
location=(self.builder.current_docname, node.line), **highlight_args location=(self.builder.current_docname, node.line), **highlight_args
) )
starttag = self.starttag(node, 'div', suffix='', starttag = self.starttag(node, 'div', suffix='',
CLASS='highlight-%s' % lang) CLASS='highlight-%s notranslate' % lang)
self.body.append(starttag + highlighted + '</div>\n') self.body.append(starttag + highlighted + '</div>\n')
raise nodes.SkipNode raise nodes.SkipNode
@ -440,10 +440,10 @@ class HTML5Translator(BaseTranslator):
# type: (nodes.Node) -> None # type: (nodes.Node) -> None
if 'kbd' in node['classes']: if 'kbd' in node['classes']:
self.body.append(self.starttag(node, 'kbd', '', self.body.append(self.starttag(node, 'kbd', '',
CLASS='docutils literal')) CLASS='docutils literal notranslate'))
else: else:
self.body.append(self.starttag(node, 'code', '', self.body.append(self.starttag(node, 'code', '',
CLASS='docutils literal')) CLASS='docutils literal notranslate'))
self.protect_literal_text += 1 self.protect_literal_text += 1
def depart_literal(self, node): def depart_literal(self, node):

View File

@ -163,21 +163,21 @@ def test_html_warnings(app, warning):
(".//pre/span", u'"quotes"'), (".//pre/span", u'"quotes"'),
(".//pre/span", u"'included'"), (".//pre/span", u"'included'"),
(".//pre/span[@class='s2']", u'üöä'), (".//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*$'), 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*$'), 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$'), 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$'), 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$'), r'(?m)^START CODE$'),
(".//div[@class='inc-pyobj-dedent highlight-python']//span", (".//div[@class='inc-pyobj-dedent highlight-python notranslate']//span",
r'def'), r'def'),
(".//div[@class='inc-tab3 highlight-text']//pre", (".//div[@class='inc-tab3 highlight-text notranslate']//pre",
r'-| |-'), r'-| |-'),
(".//div[@class='inc-tab8 highlight-python']//pre/span", (".//div[@class='inc-tab8 highlight-python notranslate']//pre/span",
r'-| |-'), r'-| |-'),
], ],
'autodoc.html': [ 'autodoc.html': [

View File

@ -72,21 +72,21 @@ def cached_etree_parse():
(".//pre/span", u'"quotes"'), (".//pre/span", u'"quotes"'),
(".//pre/span", u"'included'"), (".//pre/span", u"'included'"),
(".//pre/span[@class='s2']", u'üöä'), (".//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*$'), 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*$'), 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$'), 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$'), 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$'), r'(?m)^START CODE$'),
(".//div[@class='inc-pyobj-dedent highlight-python']//span", (".//div[@class='inc-pyobj-dedent highlight-python notranslate']//span",
r'def'), r'def'),
(".//div[@class='inc-tab3 highlight-text']//pre", (".//div[@class='inc-tab3 highlight-text notranslate']//pre",
r'-| |-'), r'-| |-'),
(".//div[@class='inc-tab8 highlight-python']//pre/span", (".//div[@class='inc-tab8 highlight-python notranslate']//pre/span",
r'-| |-'), r'-| |-'),
], ],
'autodoc.html': [ 'autodoc.html': [

View File

@ -236,7 +236,8 @@ def test_missing_reference_cppdomain(tempdir, app, status, warning):
html = (app.outdir / 'index.html').text() html = (app.outdir / 'index.html').text()
assert ('<a class="reference external"' assert ('<a class="reference external"'
' href="https://docs.python.org/index.html#cpp_foo_bar"' ' 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) '<span class="pre">Bar</span></code></a>' in html)
assert ('<a class="reference external"' assert ('<a class="reference external"'
' href="https://docs.python.org/index.html#foons"' ' href="https://docs.python.org/index.html#foons"'

View File

@ -35,17 +35,17 @@ def test_jsmath(app, status, warning):
app.builder.build_all() app.builder.build_all()
content = (app.outdir / 'math.html').text() content = (app.outdir / 'math.html').text()
assert '<div class="math">\na^2 + b^2 = c^2</div>' in content assert '<div class="math notranslate">\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">\n\\begin{split}a + 1 &lt; b\\end{split}</div>' in content
assert (u'<span class="eqno">(1)<a class="headerlink" href="#equation-foo" ' assert (u'<span class="eqno">(1)<a class="headerlink" href="#equation-foo" '
u'title="Permalink to this equation">\xb6</a></span>' 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" ' assert (u'<span class="eqno">(2)<a class="headerlink" href="#equation-math:0" '
u'title="Permalink to this equation">\xb6</a></span>' 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) 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 notranslate">\nn \\in \\mathbb N</div>' in content
assert '<div class="math">\na + 1 &lt; b</div>' in content assert '<div class="math notranslate">\na + 1 &lt; b</div>' in content
@pytest.mark.skipif(not has_binary('dvipng'), @pytest.mark.skipif(not has_binary('dvipng'),
@ -89,7 +89,7 @@ def test_mathjax_align(app, status, warning):
app.builder.build_all() app.builder.build_all()
content = (app.outdir / 'index.html').text() 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'\\\[ \\begin\{align\}\\begin\{aligned\}S \&amp;= \\pi r\^2\\\\'
r'V \&amp;= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]</div>') r'V \&amp;= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]</div>')
assert re.search(html, content, re.S) assert re.search(html, content, re.S)
@ -102,7 +102,7 @@ def test_math_number_all_mathjax(app, status, warning):
app.builder.build_all() app.builder.build_all()
content = (app.outdir / 'index.html').text() 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>') r'<span class="eqno">\(1\)<a .*>\xb6</a></span>\\\[a\^2\+b\^2=c\^2\\\]</div>')
assert re.search(html, content, re.S) assert re.search(html, content, re.S)
@ -167,7 +167,7 @@ def test_mathjax_numfig_html(app, status, warning):
app.builder.build_all() app.builder.build_all()
content = (app.outdir / 'math.html').text() 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)') '<span class="eqno">(1.2)')
assert html in content assert html in content
html = ('<p>Referencing equation <a class="reference internal" ' html = ('<p>Referencing equation <a class="reference internal" '

View File

@ -133,7 +133,7 @@ def get_verifier(verify, verify_re):
# correct interpretation of code with whitespace # correct interpretation of code with whitespace
'verify_re', 'verify_re',
'``code sample``', '``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>'), 'code</span>&#160;&#160; <span class="pre">sample</span></code></p>'),
r'\\sphinxcode{\\sphinxupquote{code sample}}', r'\\sphinxcode{\\sphinxupquote{code sample}}',
), ),
@ -141,7 +141,7 @@ def get_verifier(verify, verify_re):
# correct interpretation of code with whitespace # correct interpretation of code with whitespace
'verify_re', 'verify_re',
':samp:`code sample`', ':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>'), 'code</span>&#160;&#160; <span class="pre">sample</span></code></p>'),
r'\\sphinxcode{\\sphinxupquote{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) # interpolation of braces in samp and file roles (HTML only)
'verify', 'verify',
':samp:`a{b}c`', ':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>' '<em><span class="pre">b</span></em>'
'<span class="pre">c</span></code></p>'), '<span class="pre">c</span></code></p>'),
'\\sphinxcode{\\sphinxupquote{a\\sphinxstyleemphasis{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 # non-interpolation of dashes in option role
'verify_re', 'verify_re',
':option:`--with-option`', ':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>$'), '<span class="pre">--with-option</span></code></p>$'),
r'\\sphinxcode{\\sphinxupquote{-{-}with-option}}$', r'\\sphinxcode{\\sphinxupquote{-{-}with-option}}$',
), ),
@ -188,7 +189,7 @@ def get_verifier(verify, verify_re):
# ... but not in literal text # ... but not in literal text
'verify', 'verify',
'``"John"``', '``"John"``',
('<p><code class="docutils literal"><span class="pre">' ('<p><code class="docutils literal notranslate"><span class="pre">'
'&quot;John&quot;</span></code></p>'), '&quot;John&quot;</span></code></p>'),
'\\sphinxcode{\\sphinxupquote{"John"}}', '\\sphinxcode{\\sphinxupquote{"John"}}',
), ),