summaryrefslogtreecommitdiff
path: root/sphinx/parsers.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/parsers.py')
-rw-r--r--sphinx/parsers.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/sphinx/parsers.py b/sphinx/parsers.py
index b58eefa23..48a155203 100644
--- a/sphinx/parsers.py
+++ b/sphinx/parsers.py
@@ -11,6 +11,8 @@
import docutils.parsers
import docutils.parsers.rst
+from docutils.parsers.rst import states
+from docutils.statemachine import StringList
from docutils.transforms.universal import SmartQuotes
from sphinx.transforms import SphinxSmartQuotes
@@ -18,6 +20,7 @@ from sphinx.transforms import SphinxSmartQuotes
if False:
# For type annotation
from typing import Any, Dict, List, Type # NOQA
+ from docutils import nodes # NOQA
from docutils.transforms import Transform # NOQA
from sphinx.application import Sphinx # NOQA
@@ -56,7 +59,7 @@ class Parser(docutils.parsers.Parser):
class RSTParser(docutils.parsers.rst.Parser):
- """A reST parser customized for Sphinx."""
+ """A reST parser for Sphinx."""
def get_transforms(self):
# type: () -> List[Type[Transform]]
@@ -66,6 +69,26 @@ class RSTParser(docutils.parsers.rst.Parser):
transforms.append(SphinxSmartQuotes)
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()
+ else:
+ # otherwise, inputstring might be a string. It will be handled by superclass.
+ docutils.parsers.rst.Parser.parse(self, inputstring, document)
+
def setup(app):
# type: (Sphinx) -> Dict[unicode, Any]