From 2f768cf5a745edf908b7b2e37f21b9e07660a52a Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 22 Nov 2019 17:02:59 -0500 Subject: [PATCH] Fix #6855: Non-RST translated text should be parsed by the appropriate parser --- sphinx/errors.py | 5 +++++ sphinx/io.py | 16 +--------------- sphinx/transforms/i18n.py | 5 +++-- sphinx/util/__init__.py | 14 +++++++++++++- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/sphinx/errors.py b/sphinx/errors.py index 7f8b1fbd50a..64036721fd9 100644 --- a/sphinx/errors.py +++ b/sphinx/errors.py @@ -126,3 +126,8 @@ def __str__(self): class NoUri(Exception): """Raised by builder.get_relative_uri() if there is no URI available.""" pass + + +class FiletypeNotFoundError(Exception): + "Raised by get_filetype() if a filename matches no source suffix." + pass diff --git a/sphinx/io.py b/sphinx/io.py index fa4437b3e47..fb389a4a319 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -27,7 +27,7 @@ PreserveTranslatableMessages, Locale, RemoveTranslatableInline, ) from sphinx.transforms.references import SphinxDomains -from sphinx.util import logging +from sphinx.util import logging, get_filetype from sphinx.util import UnicodeDecodeErrorHandler from sphinx.util.docutils import LoggingReporter from sphinx.util.rst import append_epilog, docinfo_re, prepend_prolog @@ -292,20 +292,6 @@ def count_docinfo_lines(self, content): return lineno -class FiletypeNotFoundError(Exception): - pass - - -def get_filetype(source_suffix, filename): - # type: (Dict[str, str], str) -> str - for suffix, filetype in source_suffix.items(): - if filename.endswith(suffix): - # If default filetype (None), considered as restructuredtext. - return filetype or 'restructuredtext' - else: - raise FiletypeNotFoundError - - def read_doc(app, env, filename): # type: (Sphinx, BuildEnvironment, str) -> nodes.document """Parse a document and convert to doctree.""" diff --git a/sphinx/transforms/i18n.py b/sphinx/transforms/i18n.py index 6512fd43d6b..d1c125a7eb2 100644 --- a/sphinx/transforms/i18n.py +++ b/sphinx/transforms/i18n.py @@ -22,7 +22,7 @@ from sphinx.domains.std import make_glossary_term, split_term_classifiers from sphinx.locale import __, init as init_locale from sphinx.transforms import SphinxTransform -from sphinx.util import split_index_msg, logging +from sphinx.util import split_index_msg, logging, get_filetype from sphinx.util.i18n import docname_to_domain from sphinx.util.nodes import ( LITERAL_TYPE_NODES, IMAGE_TYPE_NODES, NodeMatcher, @@ -61,7 +61,8 @@ def publish_msgstr(app: "Sphinx", source: str, source_path: str, source_line: in from sphinx.io import SphinxI18nReader reader = SphinxI18nReader() reader.setup(app) - parser = app.registry.create_source_parser(app, 'restructuredtext') + filetype = get_filetype(config.source_suffix, source_path) + parser = app.registry.create_source_parser(app, filetype) doc = reader.read( source=StringInput(source=source, source_path="%s:%s:" % (source_path, source_line)), diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index 19ffec63387..df9b5aafcc0 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -31,7 +31,9 @@ from docutils.utils import relative_path from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning -from sphinx.errors import PycodeError, SphinxParallelError, ExtensionError +from sphinx.errors import ( + PycodeError, SphinxParallelError, ExtensionError, FiletypeNotFoundError +) from sphinx.locale import __ from sphinx.util import logging from sphinx.util.console import strip_colors, colorize, bold, term_width_line # type: ignore @@ -120,6 +122,16 @@ def get_matching_docs(dirname: str, suffixes: List[str], break +def get_filetype(source_suffix, filename): + # type: (Dict[str, str], str) -> str + for suffix, filetype in source_suffix.items(): + if filename.endswith(suffix): + # If default filetype (None), considered as restructuredtext. + return filetype or 'restructuredtext' + else: + raise FiletypeNotFoundError + + class FilenameUniqDict(dict): """ A dictionary that automatically generates unique names for its keys,