|
10 | 10 | from docutils.parsers.rst.roles import code_role
|
11 | 11 | from docutils.parsers.rst.states import Inliner
|
12 | 12 | from docutils.statemachine import StringList
|
| 13 | +from myst_parser.mocking import MockInliner |
13 | 14 | from sphinx import addnodes
|
14 | 15 | from sphinx.application import Sphinx
|
15 | 16 | from sphinx.directives.code import CodeBlock
|
@@ -101,21 +102,31 @@ def __call__( # pylint: disable=dangerous-default-value
|
101 | 102 | rawtext: str,
|
102 | 103 | text: str,
|
103 | 104 | lineno: int,
|
104 |
| - inliner: Inliner, |
| 105 | + inliner: Inliner | MockInliner, |
105 | 106 | # We allow mutable defaults as the Sphinx implementation requires it.
|
106 | 107 | options: dict[Any, Any] = {}, # noqa: B006
|
107 | 108 | content: list[str] = [], # noqa: B006
|
108 | 109 | ) -> tuple[list[Node], list[system_message]]:
|
109 | 110 | """
|
110 | 111 | Replace placeholders with given variables.
|
111 | 112 | """
|
112 |
| - inliner_document = inliner.document |
113 |
| - for name, value in inliner_document.substitution_defs.items(): |
| 113 | + for name, value in inliner.document.substitution_defs.items(): |
114 | 114 | replacement = value.astext()
|
115 | 115 | text = text.replace(f"|{name}|", replacement)
|
116 | 116 | rawtext = text.replace(f"|{name}|", replacement)
|
117 | 117 | rawtext = rawtext.replace(name, replacement)
|
118 | 118 |
|
| 119 | + # ``types-docutils`` says that ``code_role`` requires an ``Inliner`` |
| 120 | + # for ``inliner``. |
| 121 | + # |
| 122 | + # We can remove this when |
| 123 | + # https://github.com/executablebooks/MyST-Parser/issues/1017 |
| 124 | + # is resolved by typing ``inliner`` as ``Inliner``. |
| 125 | + if isinstance(inliner, MockInliner): |
| 126 | + new_inliner = Inliner() |
| 127 | + new_inliner.document = inliner.document |
| 128 | + inliner = new_inliner |
| 129 | + |
119 | 130 | return code_role(
|
120 | 131 | role=typ,
|
121 | 132 | rawtext=rawtext,
|
|
0 commit comments