summaryrefslogtreecommitdiff
path: root/sphinx/parsers.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/parsers.py')
-rw-r--r--sphinx/parsers.py59
1 files changed, 34 insertions, 25 deletions
diff --git a/sphinx/parsers.py b/sphinx/parsers.py
index 780b2e970..438f18257 100644
--- a/sphinx/parsers.py
+++ b/sphinx/parsers.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
"""
sphinx.parsers
~~~~~~~~~~~~~~
@@ -15,9 +14,11 @@ from docutils.parsers.rst import states
from docutils.statemachine import StringList
from docutils.transforms.universal import SmartQuotes
+from sphinx.util.rst import append_epilog, prepend_prolog
+
if False:
# For type annotation
- from typing import Any, Dict, List, Type # NOQA
+ from typing import Any, Dict, List, Type, Union # NOQA
from docutils import nodes # NOQA
from docutils.transforms import Transform # NOQA
from sphinx.application import Sphinx # NOQA
@@ -55,45 +56,53 @@ class Parser(docutils.parsers.Parser):
self.app = app
self.config = app.config
self.env = app.env
- self.warn = app.warn
- self.info = app.info
-class RSTParser(docutils.parsers.rst.Parser):
+class RSTParser(docutils.parsers.rst.Parser, Parser):
"""A reST parser for Sphinx."""
def get_transforms(self):
# type: () -> List[Type[Transform]]
"""Sphinx's reST parser replaces a transform class for smart-quotes by own's
- refs: sphinx.io.SphinxStandaloneReader"""
- transforms = docutils.parsers.rst.Parser.get_transforms(self)
+ refs: sphinx.io.SphinxStandaloneReader
+ """
+ transforms = super().get_transforms()
transforms.remove(SmartQuotes)
return transforms
def parse(self, inputstring, document):
- # type: (Any, nodes.document) -> None
- """Parse text and generate a document tree.
-
- This accepts StringList as an inputstring parameter.
- It enables to handle mixed contents (cf. :confval:`rst_prolog`) correctly.
- """
- if isinstance(inputstring, StringList):
- self.setup_parse(inputstring, document)
- self.statemachine = states.RSTStateMachine(
- state_classes=self.state_classes,
- initial_state=self.initial_state,
- debug=document.reporter.debug_flag)
- # Give inputstring directly to statemachine.
- self.statemachine.run(inputstring, document, inliner=self.inliner)
- self.finish_parse()
+ # type: (Union[str, StringList], nodes.document) -> None
+ """Parse text and generate a document tree."""
+ self.setup_parse(inputstring, document) # type: ignore
+ self.statemachine = states.RSTStateMachine(
+ state_classes=self.state_classes,
+ initial_state=self.initial_state,
+ debug=document.reporter.debug_flag)
+
+ # preprocess inputstring
+ if isinstance(inputstring, str):
+ lines = docutils.statemachine.string2lines(
+ inputstring, tab_width=document.settings.tab_width,
+ convert_whitespace=True)
+
+ inputlines = StringList(lines, document.current_source)
else:
- # otherwise, inputstring might be a string. It will be handled by superclass.
- docutils.parsers.rst.Parser.parse(self, inputstring, document)
+ inputlines = inputstring
+
+ self.decorate(inputlines)
+ self.statemachine.run(inputlines, document, inliner=self.inliner)
+ self.finish_parse()
+
+ def decorate(self, content):
+ # type: (StringList) -> None
+ """Preprocess reST content before parsing."""
+ prepend_prolog(content, self.config.rst_prolog)
+ append_epilog(content, self.config.rst_epilog)
def setup(app):
- # type: (Sphinx) -> Dict[unicode, Any]
+ # type: (Sphinx) -> Dict[str, Any]
app.add_source_parser(RSTParser)
return {