diff mbox series

[5/7] sphinx: add a lexer for pydevshell sessions

Message ID 20261125154535.3486104-6-twoerner@gmail.com
State New
Headers show
Series sphinx: carry lexers for the languages the manuals show | expand

Commit Message

Trevor Woerner Nov. 25, 2026, 3:45 p.m. UTC
The development manual shows a session in BitBake's Python development
shell, and no lexer reads it: console wants a prompt ending in $, #, or
%, pycon wants >>>, and both mark the whole block as program output,
which the theme greys. The prompt cannot be changed to suit them either,
since devshell.bbclass is what sets it.

Teach a session lexer the prompt instead, with Python inside it, so the
expressions the reader types are highlighted as Python and the results
are not.

AI-Generated: codex/claude-opus 5.5 (xhigh)
Signed-off-by: Trevor Woerner <twoerner@gmail.com>
---
 documentation/conf.py                    |  3 +-
 documentation/sphinx/pydevshell-lexer.py | 48 ++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)
 create mode 100644 documentation/sphinx/pydevshell-lexer.py
diff mbox series

Patch

diff --git a/documentation/conf.py b/documentation/conf.py
index f89926549d20..3a7e3c920fc4 100644
--- a/documentation/conf.py
+++ b/documentation/conf.py
@@ -83,7 +83,8 @@  extensions = [
     'bitbake-lexer',
     'bitbake-placeholder',
     'scc-lexer',
-    'wks-lexer'
+    'wks-lexer',
+    'pydevshell-lexer'
 ]
 
 # current_version = "dev"
diff --git a/documentation/sphinx/pydevshell-lexer.py b/documentation/sphinx/pydevshell-lexer.py
new file mode 100644
index 000000000000..a0dd7c9fa684
--- /dev/null
+++ b/documentation/sphinx/pydevshell-lexer.py
@@ -0,0 +1,48 @@ 
+"""Lexer for ``bitbake -c pydevshell`` sessions.
+
+The development manual shows a session in BitBake's Python development
+shell: expressions typed at a ``pydevshell> `` prompt with their results
+beneath. No existing lexer reads it. ``console`` is a shell session and
+wants a prompt ending in ``$``, ``#``, or ``%``; ``pycon`` wants ``>>>``.
+Both mark the whole block as program output, which the theme renders in
+grey, so the block ends up dimmer than the prose around it and no better
+told apart.
+
+The prompt is not ours to change: ``devshell.bbclass`` sets
+``ps1 = "pydevshell> "``, so what the manual shows is what the reader
+sees. That leaves teaching a lexer the prompt as the only way to render
+one of these correctly.
+
+``ShellSessionBaseLexer`` does the work once ``_ps1rgx`` matches: it
+splits the typed line from its output and hands the typed part to an inner
+lexer, which here is Python.
+"""
+import re
+
+from pygments.lexers.python import PythonLexer
+from pygments.lexers.shell import ShellSessionBaseLexer
+
+__version__ = '1.0'
+
+
+class PyDevShellSessionLexer(ShellSessionBaseLexer):
+    """A session in BitBake's Python development shell."""
+
+    name = 'BitBake pydevshell session'
+    aliases = ['pydevshell']
+    filenames = []
+    mimetypes = []
+    url = 'https://docs.yoctoproject.org/dev-manual/python-development-shell.html'
+
+    _innerLexerCls = PythonLexer
+    _ps1rgx = re.compile(r'^(pydevshell> )(.*\n?)')
+    _ps2 = '... '
+
+
+def setup(app):
+    app.add_lexer('pydevshell', PyDevShellSessionLexer)
+    return {
+        'version': __version__,
+        'parallel_read_safe': True,
+        'parallel_write_safe': True,
+    }