-
Notifications
You must be signed in to change notification settings - Fork 100
/
parser.py
32 lines (23 loc) · 948 Bytes
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Utilities for side-effect-free rST parsing."""
from __future__ import annotations
from typing import TYPE_CHECKING
from docutils.utils import new_document
from sphinx.parsers import RSTParser
from sphinx.util.docutils import sphinx_domains
if TYPE_CHECKING:
import optparse
from docutils import nodes
from docutils.frontend import Values
from docutils.statemachine import StringList
class _RstSnippetParser(RSTParser):
@staticmethod
def decorate(_content: StringList) -> None:
"""Override to skip processing rst_epilog/rst_prolog for typing."""
def parse(inputstr: str, settings: Values | optparse.Values) -> nodes.document:
"""Parse inputstr and return a docutils document."""
doc = new_document("", settings=settings)
with sphinx_domains(settings.env):
parser = _RstSnippetParser()
parser.set_application(settings.env.app)
parser.parse(inputstr, doc)
return doc