summaryrefslogtreecommitdiff
path: root/sphinx/io.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/io.py')
-rw-r--r--sphinx/io.py38
1 files changed, 30 insertions, 8 deletions
diff --git a/sphinx/io.py b/sphinx/io.py
index 8f1da22bd..b9fa3f002 100644
--- a/sphinx/io.py
+++ b/sphinx/io.py
@@ -16,22 +16,25 @@ from docutils.io import FileInput, NullOutput
from docutils.readers import standalone
from docutils.statemachine import StringList, string2lines
from docutils.writers import UnfilteredWriter
-from six import text_type
+from six import text_type, iteritems
from typing import Any, Union # NOQA
+from sphinx.locale import __
from sphinx.transforms import (
ApplySourceWorkaround, ExtraTranslatableNodes, CitationReferences,
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks, SortIds,
AutoNumbering, AutoIndexUpgrader, FilterSystemMessages,
- UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink
+ UnreferencedFootnotesDetector, SphinxSmartQuotes, DoctreeReadEvent, ManpageLink
)
from sphinx.transforms import SphinxTransformer
from sphinx.transforms.compact_bullet_list import RefOnlyBulletListTransform
from sphinx.transforms.i18n import (
PreserveTranslatableMessages, Locale, RemoveTranslatableInline,
)
+from sphinx.transforms.references import SphinxDomains, SubstitutionDefinitionsRemover
from sphinx.util import logging
from sphinx.util.docutils import LoggingReporter
+from sphinx.versioning import UIDTransform
if False:
# For type annotation
@@ -92,7 +95,9 @@ class SphinxStandaloneReader(SphinxBaseReader):
Locale, CitationReferences, DefaultSubstitutions, MoveModuleTargets,
HandleCodeBlocks, AutoNumbering, AutoIndexUpgrader, SortIds,
RemoveTranslatableInline, FilterSystemMessages, RefOnlyBulletListTransform,
- UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink
+ UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink,
+ SphinxDomains, SubstitutionDefinitionsRemover, DoctreeReadEvent,
+ UIDTransform,
] # type: List[Transform]
def __init__(self, app, *args, **kwargs):
@@ -115,7 +120,8 @@ class SphinxI18nReader(SphinxBaseReader):
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks,
AutoNumbering, SortIds, RemoveTranslatableInline,
FilterSystemMessages, RefOnlyBulletListTransform,
- UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink]
+ UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink,
+ SubstitutionDefinitionsRemover]
def set_lineno_for_reporter(self, lineno):
# type: (int) -> None
@@ -149,6 +155,7 @@ class SphinxDummyWriter(UnfilteredWriter):
def SphinxDummySourceClass(source, *args, **kwargs):
+ # type: (Any, Any, Any) -> Any
"""Bypass source object as is to cheat Publisher."""
return source
@@ -157,7 +164,7 @@ class SphinxBaseFileInput(FileInput):
"""A base class of SphinxFileInput.
It supports to replace unknown Unicode characters to '?'. And it also emits
- Sphinx events ``source-read`` on reading.
+ Sphinx events :event:`source-read` on reading.
"""
def __init__(self, app, env, *args, **kwds):
@@ -198,7 +205,7 @@ class SphinxBaseFileInput(FileInput):
if lineend == -1:
lineend = len(error.object)
lineno = error.object.count(b'\n', 0, error.start) + 1
- logger.warning('undecodable source characters, replacing with "?": %r',
+ logger.warning(__('undecodable source characters, replacing with "?": %r'),
(error.object[linestart + 1:error.start] + b'>>>' +
error.object[error.start:error.end] + b'<<<' +
error.object[error.end:lineend]),
@@ -274,14 +281,29 @@ class SphinxRSTFileInput(SphinxBaseFileInput):
return lineno
+class FiletypeNotFoundError(Exception):
+ pass
+
+
+def get_filetype(source_suffix, filename):
+ # type: (Dict[unicode, unicode], unicode) -> unicode
+ for suffix, filetype in iteritems(source_suffix):
+ 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, unicode) -> nodes.document
"""Parse a document and convert to doctree."""
- input_class = app.registry.get_source_input(filename)
+ filetype = get_filetype(app.config.source_suffix, filename)
+ input_class = app.registry.get_source_input(filetype)
reader = SphinxStandaloneReader(app)
source = input_class(app, env, source=None, source_path=filename,
encoding=env.config.source_encoding)
- parser = app.registry.create_source_parser(app, filename)
+ parser = app.registry.create_source_parser(app, filetype)
pub = Publisher(reader=reader,
parser=parser,