mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: Use simple Input class
This commit is contained in:
parent
0a199c08b8
commit
de49b991f6
1
CHANGES
1
CHANGES
@ -64,6 +64,7 @@ Deprecated
|
|||||||
* ``sphinx.ext.autosummary.Autosummary.result``
|
* ``sphinx.ext.autosummary.Autosummary.result``
|
||||||
* ``sphinx.ext.doctest.doctest_encode()``
|
* ``sphinx.ext.doctest.doctest_encode()``
|
||||||
* ``sphinx.io.SphinxRSTFileInput``
|
* ``sphinx.io.SphinxRSTFileInput``
|
||||||
|
* ``sphinx.registry.SphinxComponentRegistry.add_source_input()``
|
||||||
* ``sphinx.testing.util.remove_unicode_literal()``
|
* ``sphinx.testing.util.remove_unicode_literal()``
|
||||||
* ``sphinx.util.attrdict``
|
* ``sphinx.util.attrdict``
|
||||||
* ``sphinx.util.force_decode()``
|
* ``sphinx.util.force_decode()``
|
||||||
|
@ -273,6 +273,11 @@ The following is a list of deprecated interfaces.
|
|||||||
- 3.0
|
- 3.0
|
||||||
- N/A
|
- N/A
|
||||||
|
|
||||||
|
* - ``sphinx.registry.SphinxComponentRegistry.add_source_input()``
|
||||||
|
- 2.0
|
||||||
|
- 3.0
|
||||||
|
- N/A
|
||||||
|
|
||||||
* - ``sphinx.writers.latex.LaTeXTranslator._make_visit_admonition()``
|
* - ``sphinx.writers.latex.LaTeXTranslator._make_visit_admonition()``
|
||||||
- 2.0
|
- 2.0
|
||||||
- 3.0
|
- 3.0
|
||||||
|
@ -92,7 +92,6 @@ builtin_extensions = (
|
|||||||
'sphinx.directives.other',
|
'sphinx.directives.other',
|
||||||
'sphinx.directives.patches',
|
'sphinx.directives.patches',
|
||||||
'sphinx.extension',
|
'sphinx.extension',
|
||||||
'sphinx.io',
|
|
||||||
'sphinx.parsers',
|
'sphinx.parsers',
|
||||||
'sphinx.registry',
|
'sphinx.registry',
|
||||||
'sphinx.roles',
|
'sphinx.roles',
|
||||||
|
37
sphinx/io.py
37
sphinx/io.py
@ -197,10 +197,15 @@ class SphinxBaseFileInput(FileInput):
|
|||||||
return UnicodeDecodeErrorHandler(self.env.docname)(error)
|
return UnicodeDecodeErrorHandler(self.env.docname)(error)
|
||||||
|
|
||||||
|
|
||||||
class SphinxFileInput(SphinxBaseFileInput):
|
class SphinxFileInput(FileInput):
|
||||||
"""A basic FileInput for Sphinx."""
|
"""A basic FileInput for Sphinx."""
|
||||||
supported = ('*',) # special source input
|
supported = ('*',) # special source input
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
# type: (Any, Any) -> None
|
||||||
|
kwargs['error_handler'] = 'sphinx'
|
||||||
|
super(SphinxFileInput, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class SphinxRSTFileInput(SphinxBaseFileInput):
|
class SphinxRSTFileInput(SphinxBaseFileInput):
|
||||||
"""A reST FileInput for Sphinx.
|
"""A reST FileInput for Sphinx.
|
||||||
@ -287,11 +292,8 @@ def read_doc(app, env, filename):
|
|||||||
error_handler = UnicodeDecodeErrorHandler(env.docname)
|
error_handler = UnicodeDecodeErrorHandler(env.docname)
|
||||||
codecs.register_error('sphinx', error_handler) # type: ignore
|
codecs.register_error('sphinx', error_handler) # type: ignore
|
||||||
|
|
||||||
filetype = get_filetype(app.config.source_suffix, filename)
|
|
||||||
input_class = app.registry.get_source_input(filetype)
|
|
||||||
reader = SphinxStandaloneReader(app)
|
reader = SphinxStandaloneReader(app)
|
||||||
source = input_class(app, env, source=None, source_path=filename, # type: ignore
|
filetype = get_filetype(app.config.source_suffix, filename)
|
||||||
encoding=env.config.source_encoding)
|
|
||||||
parser = app.registry.create_source_parser(app, filetype)
|
parser = app.registry.create_source_parser(app, filetype)
|
||||||
if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == ():
|
if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == ():
|
||||||
# a workaround for recommonmark
|
# a workaround for recommonmark
|
||||||
@ -301,6 +303,11 @@ def read_doc(app, env, filename):
|
|||||||
# CommonMarkParser.
|
# CommonMarkParser.
|
||||||
parser.settings_spec = RSTParser.settings_spec
|
parser.settings_spec = RSTParser.settings_spec
|
||||||
|
|
||||||
|
input_class = app.registry.get_source_input(filetype)
|
||||||
|
if input_class:
|
||||||
|
# Sphinx-1.8 style
|
||||||
|
source = input_class(app, env, source=None, source_path=filename, # type: ignore
|
||||||
|
encoding=env.config.source_encoding)
|
||||||
pub = Publisher(reader=reader, # type: ignore
|
pub = Publisher(reader=reader, # type: ignore
|
||||||
parser=parser,
|
parser=parser,
|
||||||
writer=SphinxDummyWriter(),
|
writer=SphinxDummyWriter(),
|
||||||
@ -308,16 +315,14 @@ def read_doc(app, env, filename):
|
|||||||
destination=NullOutput())
|
destination=NullOutput())
|
||||||
pub.process_programmatic_settings(None, env.settings, None)
|
pub.process_programmatic_settings(None, env.settings, None)
|
||||||
pub.set_source(source, filename)
|
pub.set_source(source, filename)
|
||||||
|
else:
|
||||||
|
# Sphinx-2.0 style
|
||||||
|
pub = Publisher(reader=reader,
|
||||||
|
parser=parser,
|
||||||
|
writer=SphinxDummyWriter(),
|
||||||
|
source_class=SphinxFileInput)
|
||||||
|
pub.process_programmatic_settings(None, env.settings, None)
|
||||||
|
pub.set_source(source_path=filename)
|
||||||
|
|
||||||
pub.publish()
|
pub.publish()
|
||||||
return pub.document
|
return pub.document
|
||||||
|
|
||||||
|
|
||||||
def setup(app):
|
|
||||||
# type: (Sphinx) -> Dict[str, Any]
|
|
||||||
app.registry.add_source_input(SphinxFileInput)
|
|
||||||
|
|
||||||
return {
|
|
||||||
'version': 'builtin',
|
|
||||||
'parallel_read_safe': True,
|
|
||||||
'parallel_write_safe': True,
|
|
||||||
}
|
|
||||||
|
@ -337,6 +337,8 @@ class SphinxComponentRegistry:
|
|||||||
|
|
||||||
def add_source_input(self, input_class, override=False):
|
def add_source_input(self, input_class, override=False):
|
||||||
# type: (Type[SphinxFileInput], bool) -> None
|
# type: (Type[SphinxFileInput], bool) -> None
|
||||||
|
warnings.warn('registry.source_input() is deprecated.',
|
||||||
|
RemovedInSphinx30Warning, stacklevel=2)
|
||||||
for filetype in input_class.supported:
|
for filetype in input_class.supported:
|
||||||
if filetype in self.source_inputs and not override:
|
if filetype in self.source_inputs and not override:
|
||||||
raise ExtensionError(__('source_input for %r is already registered') %
|
raise ExtensionError(__('source_input for %r is already registered') %
|
||||||
@ -352,7 +354,7 @@ class SphinxComponentRegistry:
|
|||||||
# use special source_input for unknown filetype
|
# use special source_input for unknown filetype
|
||||||
return self.source_inputs['*']
|
return self.source_inputs['*']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise SphinxError(__('source_input for %s not registered') % filetype)
|
return None
|
||||||
|
|
||||||
def add_translator(self, name, translator, override=False):
|
def add_translator(self, name, translator, override=False):
|
||||||
# type: (str, Type[nodes.NodeVisitor], bool) -> None
|
# type: (str, Type[nodes.NodeVisitor], bool) -> None
|
||||||
|
Loading…
Reference in New Issue
Block a user