Repository: felix-hilden/sphinx-codeautolink

Issue:
Support `python3` language identifier (Pygments lexer name) in `literalinclude`/`code-block`
<!--
Hi, thanks for submitting a bug report!
This is an example bug template to start with.
Please fill in information below.
-->

*(Thanks for this awesome extension, I was always looking for something like this, and now I found it ;))*

### Issue
`python3` as a language identifier/Pygments lexer name doesn't seem to be supported.

### Expected behavior
Both `python3` and `python` should work the same.

Actually, according to https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer, there are more names (`python, py, sage, python3, py3, bazel, starlark, pyi`), maybe they should all be supported (especially `py3`)?

### Steps to reproduce
I am using `.. literalinclude:: python3` or `.. code-block:: python3`, and I don't see sphinx-codeautolink taking effect.
However, if I change these lines to `.. literalinclude:: python` and `.. code-block:: python` respectively, it's working as expected.

Hints from issue comments:
@felix-hilden: Hi! Thanks for submitting the issue. If they are really just synonyms for the same lexer then there's no reason for us not supporting them! Contributions are welcome if you're up for it, but if not I should get to it within a couple of weeks 👍 

Solution patch:
diff --git a/docs/src/examples.rst b/docs/src/examples.rst
index 18c9d56..ac25e5a 100644
--- a/docs/src/examples.rst
+++ b/docs/src/examples.rst
@@ -23,8 +23,13 @@ Different import styles are supported, along with all Python syntax.
 Star imports might be particularly handy in code examples.
 `Doctest <https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html
 #doctest-blocks>`_ and console blocks using :code:`.. code:: pycon` work too.
-Including code via :rst:dir:`literalinclude` requires using a
-:code:`:language: py` parameter.
+Including code via :rst:dir:`literalinclude` requires using one of the following parameters:
+:code:`:language: python`, :code:`:language: python3`,
+:code:`:language: py`, :code:`:language: py3`, :code:`:language: pyi`,
+:code:`:language: sage`, :code:`:language: bazel`, :code:`:language: starlark`
+(i.e., the list of
+`Pygments Lexers <https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer>`_).
+
 
 .. code:: pycon
 
diff --git a/docs/src/release_notes.rst b/docs/src/release_notes.rst
index 8701185..fba8213 100644
--- a/docs/src/release_notes.rst
+++ b/docs/src/release_notes.rst
@@ -8,6 +8,10 @@ These release notes are based on
 sphinx-codeautolink adheres to
 `Semantic Versioning <https://semver.org>`_.
 
+0.16.3 (unreleased)
+-------------------
+- Add more Pygments lexer aliases in code blocks (:issue:`160`)
+
 0.16.2 (2025-01-16)
 -------------------
 - Fix regression in not handling malformed return types (:issue:`159`)
diff --git a/src/sphinx_codeautolink/extension/block.py b/src/sphinx_codeautolink/extension/block.py
index ce6ec48..3e0fe49 100644
--- a/src/sphinx_codeautolink/extension/block.py
+++ b/src/sphinx_codeautolink/extension/block.py
@@ -17,7 +17,17 @@ from sphinx_codeautolink.warn import logger, warn_type
 from .backref import CodeExample
 from .directive import ConcatMarker, PrefaceMarker, SkipMarker
 
-BUILTIN_BLOCKS = {"python": None, "py": None}
+# list from https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer
+BUILTIN_BLOCKS = {
+    "python": None,
+    "python3": None,
+    "py": None,
+    "py3": None,
+    "pyi": None,
+    "sage": None,
+    "bazel": None,
+    "starlark": None,
+}
 
 
 @dataclass


Test patch:
diff --git a/tests/extension/ref/pygments_lexer_code-block_bazel.txt b/tests/extension/ref/pygments_lexer_code-block_bazel.txt
new file mode 100644
index 0000000..c2d2e39
--- /dev/null
+++ b/tests/extension/ref/pygments_lexer_code-block_bazel.txt
@@ -0,0 +1,13 @@
+test_project
+test_project.bar
+# split
+# split
+Test project
+============
+
+.. code-block:: bazel
+
+   import test_project
+   test_project.bar()
+
+.. automodule:: test_project
diff --git a/tests/extension/ref/pygments_lexer_code-block_py3.txt b/tests/extension/ref/pygments_lexer_code-block_py3.txt
new file mode 100644
index 0000000..fab0646
--- /dev/null
+++ b/tests/extension/ref/pygments_lexer_code-block_py3.txt
@@ -0,0 +1,13 @@
+test_project
+test_project.bar
+# split
+# split
+Test project
+============
+
+.. code-block:: py3
+
+   import test_project
+   test_project.bar()
+
+.. automodule:: test_project
diff --git a/tests/extension/ref/pygments_lexer_code-block_pyi.txt b/tests/extension/ref/pygments_lexer_code-block_pyi.txt
new file mode 100644
index 0000000..453d15f
--- /dev/null
+++ b/tests/extension/ref/pygments_lexer_code-block_pyi.txt
@@ -0,0 +1,13 @@
+test_project
+test_project.bar
+# split
+# split
+Test project
+============
+
+.. code-block:: pyi
+
+   import test_project
+   test_project.bar()
+
+.. automodule:: test_project
diff --git a/tests/extension/ref/pygments_lexer_code-block_python3.txt b/tests/extension/ref/pygments_lexer_code-block_python3.txt
new file mode 100644
index 0000000..d51ea5e
--- /dev/null
+++ b/tests/extension/ref/pygments_lexer_code-block_python3.txt
@@ -0,0 +1,13 @@
+test_project
+test_project.bar
+# split
+# split
+Test project
+============
+
+.. code-block:: python3
+
+   import test_project
+   test_project.bar()
+
+.. automodule:: test_project
diff --git a/tests/extension/ref/pygments_lexer_code-block_sage.txt b/tests/extension/ref/pygments_lexer_code-block_sage.txt
new file mode 100644
index 0000000..d963d95
--- /dev/null
+++ b/tests/extension/ref/pygments_lexer_code-block_sage.txt
@@ -0,0 +1,13 @@
+test_project
+test_project.bar
+# split
+# split
+Test project
+============
+
+.. code-block:: sage
+
+   import test_project
+   test_project.bar()
+
+.. automodule:: test_project
diff --git a/tests/extension/ref/pygments_lexer_code-block_starlark.txt b/tests/extension/ref/pygments_lexer_code-block_starlark.txt
new file mode 100644
index 0000000..40927a9
--- /dev/null
+++ b/tests/extension/ref/pygments_lexer_code-block_starlark.txt
@@ -0,0 +1,13 @@
+test_project
+test_project.bar
+# split
+# split
+Test project
+============
+
+.. code-block:: starlark
+
+   import test_project
+   test_project.bar()
+
+.. automodule:: test_project