Skip to content

Commit e88142b

Browse files
committed
Implicitly use pycon (#168)
1 parent d754f3e commit e88142b

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

docs/src/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ 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+
- Detect and implicitly use ``pycon`` lexer for blocks that look like
23+
console code (:issue:`168`)
2224
- Fix skipping blocks with identical content to linked ones (:issue:`172`)
2325

2426
0.16.2 (2025-01-16)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ isort.split-on-trailing-comma = false
8989
"ANN", # annotations
9090
"S101", # assertions - necessary in tests
9191
"T201", # print - helpful in tests
92+
"PLR2004", # magic number
9293
]
9394
# TODO: support future annotated hints properly
9495
"tests/extension/src/test_project/__init__.py" = ["FA100"]

src/sphinx_codeautolink/extension/block.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ def parse_source( # noqa: C901
224224
if skip:
225225
return
226226

227+
# Sphinx uses a similar trick to use pycon implicitly (#168)
228+
pycon_candidates = ("py", "python", "py3", "python3", "default", "pycon3")
229+
if source.startswith(">>>") and language in pycon_candidates:
230+
language = "pycon"
231+
227232
transformer = self.transformers[language]
228233
if transformer:
229234
try:

tests/extension/fail/ref_invalid_block_type.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Test project
44

55
.. code:: python
66

7-
>>> import test_project
8-
>>> test_project.bar()
7+
In[1]: import test_project
8+
... test_project.bar()
99

1010
.. automodule:: test_project
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
test_project
2+
test_project.bar
3+
attr
4+
test_project.Foo
5+
test_project.Foo
6+
# split
7+
# split
8+
Test project
9+
============
10+
11+
.. code:: python
12+
13+
>>> import test_project
14+
>>> test_project.bar().attr
15+
>>> def foo():
16+
... test_project.Foo
17+
this is an output line
18+
test_project.Baz
19+
<BLANKLINE>
20+
... test_project.Baz
21+
>>> test_project.Foo
22+
<BLANKLINE>
23+
24+
.. automodule:: test_project

0 commit comments

Comments
 (0)