Skip to content

Commit 81b70b2

Browse files
committed
Merge branch 'master' of github.com:adamtheturtle/sphinx-substitution-extensions
2 parents 6c3b58b + a97ce69 commit 81b70b2

File tree

2 files changed

+121
-10
lines changed

2 files changed

+121
-10
lines changed

src/sphinx_substitution_extensions/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def run(self) -> List:
5353
existing_content = self.content
5454
substitution_defs = self.state.document.substitution_defs
5555
for item in existing_content:
56-
for name in self.state.document.substitution_names:
56+
for name, value in substitution_defs.items():
5757
if _SUBSTITUTION_OPTION_NAME in self.options:
58-
replacement = substitution_defs[name].astext()
58+
replacement = value.astext()
5959
item = item.replace(
6060
'|{original}|'.format(original=name),
6161
replacement,
@@ -87,9 +87,9 @@ def run(self) -> List:
8787
existing_content = self.content
8888
substitution_defs = self.state.document.substitution_defs
8989
for item in existing_content:
90-
for name in self.state.document.substitution_names:
90+
for name, value in substitution_defs.items():
9191
if _SUBSTITUTION_OPTION_NAME in self.options:
92-
replacement = substitution_defs[name].astext()
92+
replacement = value.astext()
9393
item = item.replace(
9494
'|{original}|'.format(original=name),
9595
replacement,

tests/test_substitution_extensions.py

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import subprocess
6+
import sys
67
from pathlib import Path
78
from textwrap import dedent
89

@@ -26,7 +27,9 @@ def test_prompt_specified_late(tmp_path: Path) -> None:
2627
conf_py.write_text(conf_py_content)
2728
destination_directory = tmp_path / 'destination'
2829
args = [
29-
'sphinx-build',
30+
sys.executable,
31+
'-m',
32+
'sphinx',
3033
'-b',
3134
'html',
3235
'-W',
@@ -83,7 +86,58 @@ def test_substitution_prompt(tmp_path: Path) -> None:
8386
source_file.write_text(source_file_content)
8487
destination_directory = tmp_path / 'destination'
8588
args = [
86-
'sphinx-build',
89+
sys.executable,
90+
'-m',
91+
'sphinx',
92+
'-b',
93+
'html',
94+
'-W',
95+
# Directory containing source and configuration files.
96+
str(source_directory),
97+
# Directory containing build files.
98+
str(destination_directory),
99+
# Source file to process.
100+
str(source_file),
101+
]
102+
subprocess.check_output(args=args)
103+
expected = 'PRE-example_substitution-POST'
104+
content_html = Path(str(destination_directory)) / 'index.html'
105+
assert expected in content_html.read_text()
106+
107+
108+
def test_substitution_prompt_is_case_preserving(tmp_path: Path) -> None:
109+
"""
110+
The ``prompt`` directive respects the original case of replacements.
111+
"""
112+
source_directory = tmp_path / 'source'
113+
source_directory.mkdir()
114+
source_file = source_directory / 'index.rst'
115+
conf_py = source_directory / 'conf.py'
116+
conf_py.touch()
117+
source_file.touch()
118+
conf_py_content = dedent(
119+
"""\
120+
extensions = ['sphinx-prompt', 'sphinx_substitution_extensions']
121+
rst_prolog = '''
122+
.. |aBcD_eFgH| replace:: example_substitution
123+
'''
124+
""",
125+
)
126+
conf_py.write_text(conf_py_content)
127+
source_file_content = dedent(
128+
"""\
129+
.. prompt:: bash $
130+
:substitutions:
131+
132+
$ PRE-|aBcD_eFgH|-POST
133+
""",
134+
)
135+
source_file.write_text(source_file_content)
136+
destination_directory = tmp_path / 'destination'
137+
args = [
138+
sys.executable,
139+
'-m',
140+
'sphinx',
87141
'-b',
88142
'html',
89143
'-W',
@@ -130,7 +184,9 @@ def test_no_substitution_prompt(tmp_path: Path) -> None:
130184
source_file.write_text(source_file_content)
131185
destination_directory = tmp_path / 'destination'
132186
args = [
133-
'sphinx-build',
187+
sys.executable,
188+
'-m',
189+
'sphinx',
134190
'-b',
135191
'html',
136192
'-W',
@@ -177,7 +233,9 @@ def test_no_substitution_code_block(tmp_path: Path) -> None:
177233
source_file.write_text(source_file_content)
178234
destination_directory = tmp_path / 'destination'
179235
args = [
180-
'sphinx-build',
236+
sys.executable,
237+
'-m',
238+
'sphinx',
181239
'-b',
182240
'html',
183241
'-W',
@@ -225,7 +283,58 @@ def test_substitution_code_block(tmp_path: Path) -> None:
225283
source_file.write_text(source_file_content)
226284
destination_directory = tmp_path / 'destination'
227285
args = [
228-
'sphinx-build',
286+
sys.executable,
287+
'-m',
288+
'sphinx',
289+
'-b',
290+
'html',
291+
'-W',
292+
# Directory containing source and configuration files.
293+
str(source_directory),
294+
# Directory containing build files.
295+
str(destination_directory),
296+
# Source file to process.
297+
str(source_file),
298+
]
299+
subprocess.check_output(args=args)
300+
expected = 'PRE-example_substitution-POST'
301+
content_html = Path(str(destination_directory)) / 'index.html'
302+
assert expected in content_html.read_text()
303+
304+
305+
def test_substitution_code_block_case_preserving(tmp_path: Path) -> None:
306+
"""
307+
The ``code-block`` directive respects the original case of replacements.
308+
"""
309+
source_directory = tmp_path / 'source'
310+
source_directory.mkdir()
311+
source_file = source_directory / 'index.rst'
312+
conf_py = source_directory / 'conf.py'
313+
conf_py.touch()
314+
source_file.touch()
315+
conf_py_content = dedent(
316+
"""\
317+
extensions = ['sphinx-prompt', 'sphinx_substitution_extensions']
318+
rst_prolog = '''
319+
.. |aBcD_eFgH| replace:: example_substitution
320+
'''
321+
""",
322+
)
323+
conf_py.write_text(conf_py_content)
324+
source_file_content = dedent(
325+
"""\
326+
.. code-block:: bash
327+
:substitutions:
328+
329+
$ PRE-|aBcD_eFgH|-POST
330+
""",
331+
)
332+
source_file.write_text(source_file_content)
333+
destination_directory = tmp_path / 'destination'
334+
args = [
335+
sys.executable,
336+
'-m',
337+
'sphinx',
229338
'-b',
230339
'html',
231340
'-W',
@@ -270,7 +379,9 @@ def test_substitution_inline(tmp_path: Path) -> None:
270379
source_file.write_text(source_file_content)
271380
destination_directory = tmp_path / 'destination'
272381
args = [
273-
'sphinx-build',
382+
sys.executable,
383+
'-m',
384+
'sphinx',
274385
'-b',
275386
'html',
276387
'-W',

0 commit comments

Comments
 (0)