mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Move source_parsers manager to SphinxFactory
This commit is contained in:
1
CHANGES
1
CHANGES
@@ -45,6 +45,7 @@ Incompatible changes
|
||||
* ``Builder.env`` is not filled at instantiation
|
||||
* #3594: LaTeX: single raw directive has been considered as block level element
|
||||
* #3639: If ``html_experimental_html5_writer`` is available, epub builder use it by default.
|
||||
* ``Sphinx.add_source_parser()`` raises an error if duplicated
|
||||
|
||||
Features removed
|
||||
----------------
|
||||
|
||||
@@ -115,7 +115,6 @@ class Sphinx(object):
|
||||
# type: (unicode, unicode, unicode, unicode, unicode, Dict, IO, IO, bool, bool, List[unicode], int, int) -> None # NOQA
|
||||
self.verbosity = verbosity
|
||||
self.extensions = {} # type: Dict[unicode, Extension]
|
||||
self._additional_source_parsers = {} # type: Dict[unicode, Parser]
|
||||
self._setting_up_extension = ['?'] # type: List[unicode]
|
||||
self.builder = None # type: Builder
|
||||
self.env = None # type: BuildEnvironment
|
||||
@@ -264,11 +263,11 @@ class Sphinx(object):
|
||||
|
||||
def _init_source_parsers(self):
|
||||
# type: () -> None
|
||||
for suffix, parser in iteritems(self._additional_source_parsers):
|
||||
for suffix, parser in iteritems(self.config.source_parsers):
|
||||
self.add_source_parser(suffix, parser)
|
||||
for suffix, parser in iteritems(self.factory.get_source_parsers()):
|
||||
if suffix not in self.config.source_suffix:
|
||||
self.config.source_suffix.append(suffix)
|
||||
if suffix not in self.config.source_parsers:
|
||||
self.config.source_parsers[suffix] = parser
|
||||
|
||||
def _init_env(self, freshenv):
|
||||
# type: (bool) -> None
|
||||
@@ -738,12 +737,7 @@ class Sphinx(object):
|
||||
def add_source_parser(self, suffix, parser):
|
||||
# type: (unicode, Parser) -> None
|
||||
logger.debug('[app] adding search source_parser: %r, %r', suffix, parser)
|
||||
if suffix in self._additional_source_parsers:
|
||||
logger.warning(_('while setting up extension %s: source_parser for %r is '
|
||||
'already registered, it will be overridden'),
|
||||
self._setting_up_extension[-1], suffix,
|
||||
type='app', subtype='add_source_parser')
|
||||
self._additional_source_parsers[suffix] = parser
|
||||
self.factory.add_source_parser(suffix, parser)
|
||||
|
||||
def add_env_collector(self, collector):
|
||||
# type: (Type[EnvironmentCollector]) -> None
|
||||
|
||||
@@ -691,7 +691,8 @@ class BuildEnvironment(object):
|
||||
codecs.register_error('sphinx', self.warn_and_replace) # type: ignore
|
||||
|
||||
# publish manually
|
||||
reader = SphinxStandaloneReader(self.app, parsers=self.config.source_parsers)
|
||||
reader = SphinxStandaloneReader(self.app,
|
||||
parsers=self.app.factory.get_source_parsers())
|
||||
pub = Publisher(reader=reader,
|
||||
writer=SphinxDummyWriter(),
|
||||
destination_class=NullOutput)
|
||||
|
||||
@@ -573,7 +573,7 @@ def get_rst_suffix(app):
|
||||
# type: (Sphinx) -> unicode
|
||||
def get_supported_format(suffix):
|
||||
# type: (unicode) -> Tuple[unicode]
|
||||
parser_class = app.config.source_parsers.get(suffix)
|
||||
parser_class = app.factory.get_source_parsers().get(suffix)
|
||||
if parser_class is None:
|
||||
return ('restructuredtext',)
|
||||
if isinstance(parser_class, string_types):
|
||||
|
||||
@@ -27,6 +27,7 @@ if False:
|
||||
# For type annotation
|
||||
from typing import Any, Callable, Dict, Iterator, List, Type # NOQA
|
||||
from docutils import nodes # NOQA
|
||||
from docutils.parsers import Parser # NOQA
|
||||
from sphinx.application import Sphinx # NOQA
|
||||
from sphinx.builders import Builder # NOQA
|
||||
from sphinx.domains import Domain, Index # NOQA
|
||||
@@ -35,8 +36,9 @@ if False:
|
||||
|
||||
class SphinxFactory(object):
|
||||
def __init__(self):
|
||||
self.builders = {} # type: Dict[unicode, Type[Builder]]
|
||||
self.domains = {} # type: Dict[unicode, Type[Domain]]
|
||||
self.builders = {} # type: Dict[unicode, Type[Builder]]
|
||||
self.domains = {} # type: Dict[unicode, Type[Domain]]
|
||||
self.source_parsers = {} # type: Dict[unicode, Parser]
|
||||
|
||||
def add_builder(self, builder):
|
||||
# type: (Type[Builder]) -> None
|
||||
@@ -140,3 +142,13 @@ class SphinxFactory(object):
|
||||
stddomain.directives[directivename] = directive
|
||||
stddomain.roles[rolename] = XRefRole(innernodeclass=ref_nodeclass)
|
||||
stddomain.object_types[directivename] = ObjType(objname or directivename, rolename)
|
||||
|
||||
def add_source_parser(self, suffix, parser):
|
||||
# type: (unicode, Parser) -> None
|
||||
if suffix in self.source_parsers:
|
||||
raise ExtensionError(_('source_parser for %r is already registered') % suffix)
|
||||
self.source_parsers[suffix] = parser
|
||||
|
||||
def get_source_parsers(self):
|
||||
# type: () -> Dict[unicode, Parser]
|
||||
return self.source_parsers
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
from docutils.io import FileInput
|
||||
from docutils.readers import standalone
|
||||
from docutils.writers import UnfilteredWriter
|
||||
from six import string_types, text_type
|
||||
from six import string_types, text_type, iteritems
|
||||
from typing import Any, Union # NOQA
|
||||
|
||||
from sphinx.transforms import (
|
||||
@@ -158,9 +158,8 @@ class SphinxFileInput(FileInput):
|
||||
# type: () -> unicode
|
||||
def get_parser_type(source_path):
|
||||
# type: (unicode) -> Tuple[unicode]
|
||||
for suffix in self.env.config.source_parsers:
|
||||
for suffix, parser_class in iteritems(self.app.factory.get_source_parsers()):
|
||||
if source_path.endswith(suffix):
|
||||
parser_class = self.env.config.source_parsers[suffix]
|
||||
if isinstance(parser_class, string_types):
|
||||
parser_class = import_object(parser_class, 'source parser') # type: ignore # NOQA
|
||||
return parser_class.supported
|
||||
|
||||
@@ -52,7 +52,7 @@ def publish_msgstr(app, source, source_path, source_line, config, settings):
|
||||
from sphinx.io import SphinxI18nReader
|
||||
reader = SphinxI18nReader(
|
||||
app=app,
|
||||
parsers=config.source_parsers,
|
||||
parsers=app.factory.get_source_parsers(),
|
||||
parser_name='restructuredtext', # default parser
|
||||
)
|
||||
reader.set_lineno_for_reporter(source_line)
|
||||
|
||||
@@ -85,13 +85,6 @@ def test_domain_override(app, status, warning):
|
||||
@pytest.mark.sphinx(testroot='add_source_parser')
|
||||
def test_add_source_parser(app, status, warning):
|
||||
assert set(app.config.source_suffix) == set(['.rst', '.md', '.test'])
|
||||
assert set(app.config.source_parsers.keys()) == set(['.md', '.test'])
|
||||
assert app.config.source_parsers['.md'].__name__ == 'DummyMarkdownParser'
|
||||
assert app.config.source_parsers['.test'].__name__ == 'TestSourceParser'
|
||||
|
||||
|
||||
@pytest.mark.sphinx(testroot='add_source_parser-conflicts-with-users-setting')
|
||||
def test_add_source_parser_conflicts_with_users_setting(app, status, warning):
|
||||
assert set(app.config.source_suffix) == set(['.rst', '.test'])
|
||||
assert set(app.config.source_parsers.keys()) == set(['.test'])
|
||||
assert app.config.source_parsers['.test'].__name__ == 'DummyTestParser'
|
||||
assert set(app.factory.get_source_parsers().keys()) == set(['.md', '.test'])
|
||||
assert app.factory.get_source_parsers()['.md'].__name__ == 'DummyMarkdownParser'
|
||||
assert app.factory.get_source_parsers()['.test'].__name__ == 'TestSourceParser'
|
||||
|
||||
Reference in New Issue
Block a user