summaryrefslogtreecommitdiff
path: root/sphinx/transforms.py
diff options
context:
space:
mode:
authorshimizukawa <shimizukawa@gmail.com>2016-02-03 08:57:54 +0900
committershimizukawa <shimizukawa@gmail.com>2016-02-09 00:16:46 +0900
commitaefab514e2bbce775a00c1aaae057044a02033fb (patch)
treef9163a73be9706f247d35d7f656f429c5e2692e5 /sphinx/transforms.py
parent9ba7cc815c247205ded53824da99d74d411ca827 (diff)
downloadsphinx-git-aefab514e2bbce775a00c1aaae057044a02033fb.tar.gz
Fix #2287: `sphinx.transforms.Locale` always uses rst parser. Sphinx i18n feature should support parsers that specified source_parsers.
Diffstat (limited to 'sphinx/transforms.py')
-rw-r--r--sphinx/transforms.py60
1 files changed, 29 insertions, 31 deletions
diff --git a/sphinx/transforms.py b/sphinx/transforms.py
index 4619449e7..bfae7f812 100644
--- a/sphinx/transforms.py
+++ b/sphinx/transforms.py
@@ -12,8 +12,8 @@
from os import path
from docutils import nodes
-from docutils.utils import new_document, relative_path
-from docutils.parsers.rst import Parser as RSTParser
+from docutils.io import StringInput
+from docutils.utils import relative_path
from docutils.transforms import Transform
from docutils.transforms.parts import ContentsFilter
@@ -202,21 +202,33 @@ class ExtraTranslatableNodes(Transform):
node['translatable'] = True
-class CustomLocaleReporter(object):
- """
- Replacer for document.reporter.get_source_and_line method.
+def publish_msgstr(source, source_path, source_line, config, settings):
+ """Publish msgstr (single line) into docutils document
- reST text lines for translation do not have the original source line number.
- This class provides the correct line numbers when reporting.
+ :param unicode source: source text
+ :param unicode source_path: source path for warning indication
+ :param source_line: source line for warning indication
+ :param sphinx.config.Config config: sphinx config
+ :param docutils.frontend.Values settings: docutils settings
+ :return: document
+ :rtype: docutils.nodes.document
"""
- def __init__(self, source, line):
- self.source, self.line = source, line
-
- def set_reporter(self, document):
- document.reporter.get_source_and_line = self.get_source_and_line
-
- def get_source_and_line(self, lineno=None):
- return self.source, self.line
+ from sphinx.io import SphinxI18nReader
+ reader = SphinxI18nReader(
+ parsers=config.source_parsers,
+ parser_name='restructuredtext', # default parser
+ )
+ reader.set_lineno_for_reporter(source_line)
+ doc = reader.read(
+ source=StringInput(source=source, source_path=source_path),
+ parser=reader.parser,
+ settings=settings,
+ )
+ try:
+ doc = doc[0]
+ except IndexError: # empty node
+ pass
+ return doc
class Locale(Transform):
@@ -244,8 +256,6 @@ class Locale(Transform):
if not has_catalog:
return
- parser = RSTParser()
-
# phase1: replace reference ids with translated names
for node, msg in extract_messages(self.document):
msgstr = catalog.gettext(msg)
@@ -267,13 +277,7 @@ class Locale(Transform):
if isinstance(node, LITERAL_TYPE_NODES):
msgstr = '::\n\n' + indent(msgstr, ' '*3)
- patch = new_document(source, settings)
- CustomLocaleReporter(node.source, node.line).set_reporter(patch)
- parser.parse(msgstr, patch)
- try:
- patch = patch[0]
- except IndexError: # empty node
- pass
+ patch = publish_msgstr(msgstr, source, node.line, env.config, settings)
# XXX doctest and other block markup
if not isinstance(patch, nodes.paragraph):
continue # skip for now
@@ -390,13 +394,7 @@ class Locale(Transform):
if isinstance(node, LITERAL_TYPE_NODES):
msgstr = '::\n\n' + indent(msgstr, ' '*3)
- patch = new_document(source, settings)
- CustomLocaleReporter(node.source, node.line).set_reporter(patch)
- parser.parse(msgstr, patch)
- try:
- patch = patch[0]
- except IndexError: # empty node
- pass
+ patch = publish_msgstr(msgstr, source, node.line, env.config, settings)
# XXX doctest and other block markup
if not isinstance(
patch,