Add sphinx.parsers.Parser class; a base class for new parsers

The class inherits ``docutils.parsers.Parser`` and implements ``set_application()`` in addition.
It enables subclasses to read configurations, to access environments and to logging.
This commit is contained in:
Takeshi KOMIYA 2016-01-01 01:31:59 +09:00
parent ed196a8adf
commit f1765c2576
2 changed files with 35 additions and 3 deletions

View File

@ -105,13 +105,16 @@ class SphinxStandaloneReader(standalone.Reader):
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks, DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks,
AutoNumbering, SortIds, RemoveTranslatableInline] AutoNumbering, SortIds, RemoveTranslatableInline]
def __init__(self, parsers={}, *args, **kwargs): def __init__(self, app, parsers={}, *args, **kwargs):
standalone.Reader.__init__(self, *args, **kwargs) standalone.Reader.__init__(self, *args, **kwargs)
self.parser_map = {} self.parser_map = {}
for suffix, parser_class in parsers.items(): for suffix, parser_class in parsers.items():
if isinstance(parser_class, string_types): if isinstance(parser_class, string_types):
parser_class = import_object(parser_class, 'source parser') parser_class = import_object(parser_class, 'source parser')
self.parser_map[suffix] = parser_class() parser = parser_class()
if hasattr(parser, 'set_application'):
parser.set_application(app)
self.parser_map[suffix] = parser
def read(self, source, parser, settings): def read(self, source, parser, settings):
self.source = source self.source = source
@ -776,7 +779,7 @@ class BuildEnvironment:
codecs.register_error('sphinx', self.warn_and_replace) codecs.register_error('sphinx', self.warn_and_replace)
# publish manually # publish manually
reader = SphinxStandaloneReader(parsers=self.config.source_parsers) reader = SphinxStandaloneReader(self.app, parsers=self.config.source_parsers)
pub = Publisher(reader=reader, pub = Publisher(reader=reader,
writer=SphinxDummyWriter(), writer=SphinxDummyWriter(),
destination_class=NullOutput) destination_class=NullOutput)

29
sphinx/parsers.py Normal file
View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
"""
sphinx.parsers
~~~~~~~~~~~~~~
A Base class for additional parsers.
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import docutils.parsers
class Parser(docutils.parsers.Parser):
"""
A base class of parsers.
"""
def set_application(self, app):
"""set_application will be called from Sphinx to set app and other instance variables
:param sphinx.application.Sphinx app: Sphinx application object
"""
self.app = app
self.config = app.config
self.env = app.env
self.warn = app.warn
self.info = app.info