Skip to content

Commit

Permalink
Merge pull request #6927 from tk0miya/6856_i18n_parser
Browse files Browse the repository at this point in the history
Fix #6855: Non-RST translated text should be parsed by the appropriate parser
  • Loading branch information
tk0miya authored Dec 15, 2019
2 parents 876fd40 + 00c3067 commit de6184e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Incompatible changes
Deprecated
----------

* ``sphinx.io.FiletypeNotFoundError``
* ``sphinx.io.get_filetype()``

Features added
--------------

Expand Down
10 changes: 10 additions & 0 deletions doc/extdev/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ The following is a list of deprecated interfaces.
- (will be) Removed
- Alternatives

* - ``sphinx.io.FiletypeNotFoundError``
- 2.4
- 4.0
- ``sphinx.errors.FiletypeNotFoundError``

* - ``sphinx.io.get_filetype()``
- 2.4
- 4.0
- ``sphinx.util.get_filetype()``

* - ``sphinx.builders.gettext.POHEADER``
- 2.3
- 4.0
Expand Down
5 changes: 5 additions & 0 deletions sphinx/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 13 additions & 16 deletions sphinx/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
from docutils.transforms.references import DanglingReferences
from docutils.writers import UnfilteredWriter

from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning
from sphinx.deprecation import (
RemovedInSphinx30Warning, RemovedInSphinx40Warning, deprecated_alias
)
from sphinx.errors import FiletypeNotFoundError
from sphinx.transforms import (
AutoIndexUpgrader, DoctreeReadEvent, FigureAligner, SphinxTransformer
)
from sphinx.transforms.i18n import (
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
Expand Down Expand Up @@ -292,20 +295,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."""
Expand Down Expand Up @@ -349,3 +338,11 @@ def read_doc(app, env, filename):

pub.publish()
return pub.document


deprecated_alias('sphinx.io',
{
'FiletypeNotFoundError': FiletypeNotFoundError,
'get_filetype': get_filetype,
},
RemovedInSphinx40Warning)
5 changes: 3 additions & 2 deletions sphinx/transforms/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:<translated>" % (source_path, source_line)),
Expand Down
14 changes: 13 additions & 1 deletion sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit de6184e

Please sign in to comment.