Skip to content

Commit d754f3e

Browse files
committed
Fix skipping with identical code to linked blocks (#172)
1 parent 57c7f36 commit d754f3e

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

docs/src/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sphinx-codeautolink adheres to
1919
and ``highlight_language`` configuration (:issue:`166`)
2020
- Add :confval:`codeautolink_warn_on_default_parse_fail` to warn about
2121
failing to link code blocks without a language parameter (:issue:`166`)
22+
- Fix skipping blocks with identical content to linked ones (:issue:`172`)
2223

2324
0.16.2 (2025-01-16)
2425
-------------------

src/sphinx_codeautolink/extension/block.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def visit_literal_block(self, node: nodes.literal_block):
196196
"""Visit a generic literal block."""
197197
return self.parse_source(node, node.get("language", self.highlight_lang))
198198

199-
def parse_source(
199+
def parse_source( # noqa: C901
200200
self, node: nodes.literal_block | nodes.doctest_block, language: str | None
201201
) -> None:
202202
"""Analyse Python code blocks."""
@@ -208,14 +208,22 @@ def parse_source(
208208
self.skip = None
209209

210210
if (
211-
skip
212-
or len(node.children) != 1
211+
len(node.children) != 1
213212
or not isinstance(node.children[0], nodes.Text)
214213
or language not in self.valid_blocks
215214
):
216215
return
217216

218217
source = node.children[0].astext()
218+
example = CodeExample(
219+
self.current_document, self.current_refid, list(self.title_stack)
220+
)
221+
transform = SourceTransform(source, [], example, node.line)
222+
self.source_transforms.append(transform)
223+
224+
if skip:
225+
return
226+
219227
transformer = self.transformers[language]
220228
if transformer:
221229
try:
@@ -228,11 +236,7 @@ def parse_source(
228236
return
229237
else:
230238
clean_source = source
231-
example = CodeExample(
232-
self.current_document, self.current_refid, list(self.title_stack)
233-
)
234-
transform = SourceTransform(source, [], example, node.line)
235-
self.source_transforms.append(transform)
239+
transform.source = source
236240

237241
modified_source = "\n".join(
238242
self.global_preface + self.concat_sources + prefaces + [clean_source]
@@ -244,7 +248,7 @@ def parse_source(
244248
return
245249

246250
show_source = self._format_source_for_error(
247-
self.global_preface, self.concat_sources, prefaces, source
251+
self.global_preface, self.concat_sources, prefaces, transform.source
248252
)
249253
msg = self._parsing_error_msg(e, language, show_source)
250254
logger.warning(msg, type=warn_type, subtype="parse_block", location=node)

tests/extension/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,37 @@ def test_parallel_build(tmp_path: Path):
280280
assert check_link_targets(result_dir) == n_subfiles * len(links)
281281

282282

283+
def test_skip_identical_code(tmp_path: Path):
284+
"""Code skipped and then linked in an identical block after."""
285+
index = """
286+
Test project
287+
============
288+
289+
.. autolink-skip::
290+
.. code:: python
291+
292+
import test_project
293+
test_project.bar()
294+
295+
.. code:: python
296+
297+
import test_project
298+
test_project.bar()
299+
300+
.. automodule:: test_project
301+
"""
302+
files = {"conf.py": default_conf, "index.rst": index}
303+
result_dir = _sphinx_build(tmp_path, "html", files)
304+
305+
index_html = result_dir / "index.html"
306+
text = index_html.read_text("utf-8")
307+
soup = BeautifulSoup(text, "html.parser")
308+
blocks = list(soup.select(".highlight-python"))
309+
assert len(blocks) == 2
310+
assert "sphinx-codeautolink-a" not in str(blocks[0])
311+
assert "sphinx-codeautolink-a" in str(blocks[1])
312+
313+
283314
def _sphinx_build(
284315
folder: Path, builder: str, files: dict[str, str], n_processes: int | None = None
285316
) -> Path:

tests/extension/ref/skip_next_consumed_non_python.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Test project
66
============
77

88
.. autolink-skip::
9-
.. code::
9+
.. code:: c
1010

1111
import test_project
1212
test_project.bar()

0 commit comments

Comments
 (0)