diff --git a/CHANGES b/CHANGES index 13a0a3c93..6319fc8a3 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,9 @@ Incompatible changes Deprecated ---------- +* ``sphinx.io.FiletypeNotFoundError`` +* ``sphinx.io.get_filetype()`` + Features added -------------- diff --git a/doc/extdev/deprecated.rst b/doc/extdev/deprecated.rst index 482cb50bb..9fdaa2f96 100644 --- a/doc/extdev/deprecated.rst +++ b/doc/extdev/deprecated.rst @@ -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 diff --git a/sphinx/errors.py b/sphinx/errors.py index 7f8b1fbd5..64036721f 100644 --- a/sphinx/errors.py +++ b/sphinx/errors.py @@ -126,3 +126,8 @@ class PycodeError(Exception): 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 fa4437b3e..72762f46c 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -19,7 +19,10 @@ from docutils.statemachine import StringList, string2lines 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 ) @@ -27,7 +30,7 @@ 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 @@ -292,20 +295,6 @@ class SphinxRSTFileInput(SphinxBaseFileInput): 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.""" @@ -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) diff --git a/sphinx/transforms/i18n.py b/sphinx/transforms/i18n.py index 6512fd43d..d1c125a7e 100644 --- a/sphinx/transforms/i18n.py +++ b/sphinx/transforms/i18n.py @@ -22,7 +22,7 @@ from sphinx.config import Config 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 19ffec633..df9b5aafc 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -31,7 +31,9 @@ from urllib.parse import urlsplit, urlunsplit, quote_plus, parse_qsl, urlencode 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,