Merge pull request #2883 from tk0miya/translatable_meta

Translatable meta
This commit is contained in:
Takeshi KOMIYA 2016-09-02 00:08:58 +09:00 committed by GitHub
commit 9ea2ced065
8 changed files with 67 additions and 6 deletions

View File

@ -130,6 +130,7 @@ Bugs fixed
directives directives
* #2485: autosummary crashes with multiple source_suffix values * #2485: autosummary crashes with multiple source_suffix values
* #1734: Could not translate the caption of toctree directive * #1734: Could not translate the caption of toctree directive
* Could not translate the content of meta directive (ref: #1734)
Documentation Documentation
------------- -------------

View File

@ -9,7 +9,9 @@
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import directives from docutils.parsers.rst import directives
from docutils.parsers.rst.directives import images from docutils.parsers.rst.directives import images, html
from sphinx import addnodes
class Figure(images.Figure): class Figure(images.Figure):
@ -35,5 +37,24 @@ class Figure(images.Figure):
return [figure_node] return [figure_node]
class Meta(html.Meta):
def run(self):
env = self.state.document.settings.env
result = html.Meta.run(self)
for node in result:
if (isinstance(node, nodes.pending) and
isinstance(node.details['nodes'][0], html.MetaBody.meta)):
meta = node.details['nodes'][0]
meta.source = env.doc2path(env.docname)
meta.line = self.lineno
meta.rawcontent = meta['content']
# docutils' meta nodes aren't picklable because the class is nested
meta.__class__ = addnodes.meta
return result
def setup(app): def setup(app):
directives.register_directive('figure', Figure) directives.register_directive('figure', Figure)
directives.register_directive('meta', Meta)

View File

@ -31,7 +31,6 @@ from docutils.core import Publisher
from docutils.utils import Reporter, relative_path, get_source_line from docutils.utils import Reporter, relative_path, get_source_line
from docutils.parsers.rst import roles, directives from docutils.parsers.rst import roles, directives
from docutils.parsers.rst.languages import en as english from docutils.parsers.rst.languages import en as english
from docutils.parsers.rst.directives.html import MetaBody
from docutils.frontend import OptionParser from docutils.frontend import OptionParser
from sphinx import addnodes from sphinx import addnodes
@ -776,9 +775,6 @@ class BuildEnvironment:
doctree.settings.warning_stream = None doctree.settings.warning_stream = None
doctree.settings.env = None doctree.settings.env = None
doctree.settings.record_dependencies = None doctree.settings.record_dependencies = None
for metanode in doctree.traverse(MetaBody.meta):
# docutils' meta nodes aren't picklable because the class is nested
metanode.__class__ = addnodes.meta
# cleanup # cleanup
self.temp_data.clear() self.temp_data.clear()

View File

@ -22,7 +22,7 @@ from sphinx.locale import _, init as init_locale
from sphinx.util import split_index_msg from sphinx.util import split_index_msg
from sphinx.util.nodes import ( from sphinx.util.nodes import (
traverse_translatable_index, extract_messages, LITERAL_TYPE_NODES, IMAGE_TYPE_NODES, traverse_translatable_index, extract_messages, LITERAL_TYPE_NODES, IMAGE_TYPE_NODES,
apply_source_workaround, apply_source_workaround, is_pending_meta,
) )
from sphinx.util.i18n import find_catalog, format_date from sphinx.util.i18n import find_catalog, format_date
from sphinx.util.pycompat import indent from sphinx.util.pycompat import indent
@ -400,6 +400,11 @@ class Locale(Transform):
node.apply_translated_message(msg, msgstr) node.apply_translated_message(msg, msgstr)
continue continue
# update meta nodes
if is_pending_meta(node):
node.details['nodes'][0]['content'] = msgstr
continue
# Avoid "Literal block expected; none found." warnings. # Avoid "Literal block expected; none found." warnings.
# If msgstr ends with '::' then it cause warning message at # If msgstr ends with '::' then it cause warning message at
# parser.parse() processing. # parser.parse() processing.

View File

@ -85,6 +85,14 @@ IGNORED_NODES = (
) )
def is_pending_meta(node):
if (isinstance(node, nodes.pending) and
isinstance(node.details.get('nodes', [None])[0], addnodes.meta)):
return True
else:
return False
def is_translatable(node): def is_translatable(node):
if isinstance(node, addnodes.translatable): if isinstance(node, addnodes.translatable):
return True return True
@ -106,6 +114,11 @@ def is_translatable(node):
if isinstance(node, nodes.image) and node.get('translatable'): if isinstance(node, nodes.image) and node.get('translatable'):
return True return True
if isinstance(node, addnodes.meta):
return True
if is_pending_meta(node):
return True
return False return False
@ -117,6 +130,9 @@ LITERAL_TYPE_NODES = (
IMAGE_TYPE_NODES = ( IMAGE_TYPE_NODES = (
nodes.image, nodes.image,
) )
META_TYPE_NODES = (
addnodes.meta,
)
def extract_messages(doctree): def extract_messages(doctree):
@ -134,6 +150,10 @@ def extract_messages(doctree):
msg = '.. image:: %s' % node['uri'] msg = '.. image:: %s' % node['uri']
if node.get('alt'): if node.get('alt'):
msg += '\n :alt: %s' % node['alt'] msg += '\n :alt: %s' % node['alt']
elif isinstance(node, META_TYPE_NODES):
msg = node.rawcontent
elif is_pending_meta(node):
msg = node.details['nodes'][0].rawcontent
else: else:
msg = node.rawsource.replace('\n', ' ').strip() msg = node.rawsource.replace('\n', ' ').strip()

View File

@ -18,3 +18,9 @@ msgstr ""
msgid "Table of Contents" msgid "Table of Contents"
msgstr "TABLE OF CONTENTS" msgstr "TABLE OF CONTENTS"
msgid "testdata for i18n"
msgstr "TESTDATA FOR I18N"
msgid "i18n, sphinx, markup"
msgstr "I18N, SPHINX, MARKUP"

View File

@ -1,6 +1,10 @@
CONTENTS CONTENTS
======== ========
.. meta::
:description: testdata for i18n
:keywords: i18n, sphinx, markup
.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2
:numbered: :numbered:

View File

@ -360,6 +360,14 @@ def test_gettext_builder(app, status, warning):
def test_html_builder(app, status, warning): def test_html_builder(app, status, warning):
app.builder.build_all() app.builder.build_all()
# --- test for meta
result = (app.outdir / 'contents.html').text(encoding='utf-8')
expected_expr = '<meta content="TESTDATA FOR I18N" name="description" />'
yield assert_in, expected_expr, result
expected_expr = '<meta content="I18N, SPHINX, MARKUP" name="keywords" />'
yield assert_in, expected_expr, result
# --- test for #955 cant-build-html-with-footnotes-when-using # --- test for #955 cant-build-html-with-footnotes-when-using
# expect no error by build # expect no error by build