Skip to content

Commit

Permalink
Match with line numbers (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-hilden committed Mar 14, 2024
1 parent 21b68d1 commit e282dfd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ These release notes are based on
sphinx-codeautolink adheres to
`Semantic Versioning <https://semver.org>`_.

Unreleased
----------
- Fix linking blocks with line numbers (:issue:`137`)

0.15.0 (2023-02-05)
-------------------
- Fix handling of syntax errors in parsed blocks (:issue:`135`)
Expand Down
9 changes: 8 additions & 1 deletion src/sphinx_codeautolink/extension/block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Code block processing."""

import re
from copy import copy
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Union
Expand Down Expand Up @@ -305,7 +306,13 @@ def link_html(

for trans in transforms:
for ix in range(len(inners)):
if trans.source.rstrip() == "".join(inners[ix].strings).rstrip():
candidate = copy(inners[ix])

# remove line numbers for matching
for lineno in candidate.find_all("span", attrs={"class": "linenos"}):
lineno.extract()

if trans.source.rstrip() == "".join(candidate.strings).rstrip():
inner = inners.pop(ix)
break
else:
Expand Down
15 changes: 15 additions & 0 deletions tests/extension/ref/line_numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_project
test_project.bar
asdf
# split
# split
Test project
============

.. code-block:: python
:linenos:

import test_project
test_project.bar()

.. automodule:: test_project

0 comments on commit e282dfd

Please sign in to comment.