Skip to content

Commit 66b8e63

Browse files
committed
Support default lexer, literal blocks, highlight directive and highlight_language config (#166)
1 parent 1a6a73a commit 66b8e63

30 files changed

+74
-15
lines changed

docs/src/about.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Caveats
1818
-------
1919
- **Only works with HTML documentation**, disabled otherwise. If the extension
2020
is off, it silently removes directives that would produce output.
21-
- **Only processes literal blocks, not inline code**. Sphinx has great tools
21+
- **Only processes blocks, not inline code**. Sphinx has great tools
2222
for linking definitions inline, and longer code should be in a block anyway.
2323
- **Doesn't run example code**. Therefore all possible resolvable types are not
2424
found, and the runtime correctness of code cannot be validated.

docs/src/examples.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@ Different import styles are supported, along with all Python syntax.
2323
Star imports might be particularly handy in code examples.
2424
`Doctest <https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html
2525
#doctest-blocks>`_ and console blocks using :code:`.. code:: pycon` work too.
26-
Including code via :rst:dir:`literalinclude` requires using one of the following parameters:
27-
:code:`:language: python`, :code:`:language: python3`,
28-
:code:`:language: py`, :code:`:language: py3`, :code:`:language: pyi`,
29-
:code:`:language: sage`, :code:`:language: bazel`, :code:`:language: starlark`
30-
(i.e., the list of
31-
`Pygments Lexers <https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer>`_).
32-
26+
Including code via :rst:dir:`literalinclude` requires using one of the
27+
`Python Lexers <https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer>`_.
3328

3429
.. code:: pycon
3530

docs/src/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Caveats
7070
For a more thorough explanation, see :ref:`about`.
7171

7272
- Only works with HTML documentation
73-
- Only processes literal blocks, not inline code
73+
- Only processes blocks, not inline code
7474
- Doesn't run example code
7575
- Parsing and type hint resolving is incomplete
7676

docs/src/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Directives
9696
9797
.. rst:directive:: .. autolink-concat:: [mode]
9898
99-
Toggle literal block concatenation.
99+
Toggle block concatenation.
100100
Concatenated code blocks are treated as a continuous source,
101101
so that imports and statements in previous blocks affect later blocks.
102102
Concatenation is begun at the directive, not applied retroactively.

docs/src/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ sphinx-codeautolink adheres to
1515
a corresponding tutorial or how-to (:issue:`161`)
1616
- Add more Pygments lexer aliases in code blocks (:issue:`160`)
1717
- Fix undocumented class attribute leading to a crash (:issue:`165`)
18+
- Support the ``default`` lexer, literal blocks, ``.. highlight::`` directive
19+
and ``highlight_language`` configuration (:issue:`166`)
1820

1921
0.16.2 (2025-01-16)
2022
-------------------

src/sphinx_codeautolink/extension/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self) -> None:
7575
self.warn_missing_inventory = None
7676
self.warn_failed_resolve = None
7777
self.warn_no_backreference = None
78+
self.highlight_lang = None
7879

7980
# Populated once
8081
self.outdated_docs: set[str] = set()
@@ -105,6 +106,7 @@ def build_inited(self, app) -> None:
105106
self.warn_missing_inventory = app.config.codeautolink_warn_on_missing_inventory
106107
self.warn_failed_resolve = app.config.codeautolink_warn_on_failed_resolve
107108
self.warn_no_backreference = app.config.codeautolink_warn_on_no_backreference
109+
self.highlight_lang = app.config.highlight_language
108110

109111
# Append static resources path so references in setup() are valid
110112
app.config.html_static_path.append(
@@ -138,6 +140,7 @@ def parse_blocks(self, app, doctree) -> None:
138140
global_preface=self.global_preface,
139141
custom_blocks=self.custom_blocks,
140142
concat_default=self.concat_default,
143+
default_highlight_lang=self.highlight_lang,
141144
)
142145
doctree.walkabout(visitor)
143146
self.cache.transforms[visitor.current_document] = visitor.source_transforms

src/sphinx_codeautolink/extension/block.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
# list from https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer
2121
BUILTIN_BLOCKS = {
22+
"default": None,
2223
"python": None,
2324
"python3": None,
2425
"py": None,
@@ -104,7 +105,7 @@ def clean_ipython(source: str) -> tuple[str, str]:
104105

105106

106107
class CodeBlockAnalyser(nodes.SparseNodeVisitor):
107-
"""Transform literal blocks of Python with links to reference documentation."""
108+
"""Transform blocks of Python with links to reference documentation."""
108109

109110
def __init__(
110111
self,
@@ -113,6 +114,7 @@ def __init__(
113114
global_preface: list[str],
114115
custom_blocks: dict[str, Callable[[str], str]],
115116
concat_default: bool,
117+
default_highlight_lang: str | None,
116118
**kwargs,
117119
) -> None:
118120
super().__init__(*args, **kwargs)
@@ -130,6 +132,7 @@ def __init__(
130132
self.concat_section = False
131133
self.concat_sources = []
132134
self.skip = None
135+
self.highlight_lang = default_highlight_lang
133136

134137
def unknown_visit(self, node) -> None:
135138
"""Handle and delete custom directives, ignore others."""
@@ -162,6 +165,10 @@ def unknown_visit(self, node) -> None:
162165
def unknown_departure(self, node) -> None:
163166
"""Ignore unknown nodes."""
164167

168+
def visit_highlightlang(self, node) -> None:
169+
"""Set expected highlight language."""
170+
self.highlight_lang = node["lang"]
171+
165172
def visit_title(self, node) -> None:
166173
"""Track section names and break concatenation and skipping."""
167174
self.title_stack.append(node.astext())
@@ -185,7 +192,7 @@ def visit_doctest_block(self, node):
185192

186193
def visit_literal_block(self, node: nodes.literal_block):
187194
"""Visit a generic literal block."""
188-
return self.parse_source(node, node.get("language", None))
195+
return self.parse_source(node, node.get("language", self.highlight_lang))
189196

190197
def parse_source(
191198
self, node: nodes.literal_block | nodes.doctest_block, language: str | None

src/sphinx_codeautolink/extension/directive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def copy(self):
6262

6363

6464
class Concat(Directive):
65-
"""Toggle and cut literal block concatenation in a document."""
65+
"""Toggle and cut block concatenation in a document."""
6666

6767
has_content = False
6868
required_arguments = 0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
test_project
2+
test_project.bar
3+
# split
4+
# split
5+
Test project
6+
============
7+
8+
.. code::
9+
10+
import test_project
11+
12+
test_project.bar()
13+
14+
.. automodule:: test_project

0 commit comments

Comments
 (0)