diff mbox series

[6/7] sphinx: read a bare "#" in a console block as output

Message ID 20261125154535.3486104-7-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
Pygments treats "$", "#", and "%" alike as prompts and lets the preamble
before them be empty, so a line opening with "#" is taken for a root
prompt and the rest of it is lexed as the command it introduces. Across
these manuals that line is a comment or a line of program output every
time, never a prompt: a root prompt carries the host preamble saying
whose root it is.

Require that preamble before a "#". The line then falls through to
Generic.Output, which is what it was, and the theme stops showing
perf headers and generated-file comments as commands somebody typed.

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

Patch

diff --git a/documentation/conf.py b/documentation/conf.py
index 3a7e3c920fc4..5db20ff021fc 100644
--- a/documentation/conf.py
+++ b/documentation/conf.py
@@ -84,7 +84,8 @@  extensions = [
     'bitbake-placeholder',
     'scc-lexer',
     'wks-lexer',
-    'pydevshell-lexer'
+    'pydevshell-lexer',
+    'console-lexer'
 ]
 
 # current_version = "dev"
diff --git a/documentation/sphinx/console-lexer.py b/documentation/sphinx/console-lexer.py
new file mode 100644
index 000000000000..0f9eaa4ce33e
--- /dev/null
+++ b/documentation/sphinx/console-lexer.py
@@ -0,0 +1,53 @@ 
+"""A console session lexer that reads a bare ``#`` as output, not a prompt.
+
+Pygments treats ``$``, ``#``, and ``%`` alike and allows the preamble
+before them to be empty, so a line beginning ``#`` is a root prompt and
+the rest of the line is lexed as the command it introduces. In these
+manuals that is wrong far more often than it is right: a ``#`` opening a
+line is a comment or a line of program output, and a root prompt carries
+the ``user@host:path`` preamble that says whose root it is.
+
+Requiring that preamble before a ``#`` is the whole of the change.
+``$`` and ``%`` keep Pygments' behaviour exactly, bare prompt included.
+A line that is no longer a prompt falls through to ``Generic.Output``,
+which is what it always was.
+
+This is a local convention rather than a universal one - outside these
+manuals a bare ``#`` really is how a root shell prompts - so it belongs
+here and not upstream.
+"""
+import re
+
+from pygments.lexers.shell import BashSessionLexer
+
+__version__ = '1.0'
+
+# Pygments' own preamble alternation, spelled twice: once as it has it,
+# and once with the empty branch removed. Keeping its wording rather than
+# writing a tidier one means a prompt shape it learns later still works.
+_PREAMBLE = (r'(?:\[.*?\])|(?:\(\S+\))?'
+             r'(?:| |sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)?|\[\S+[@:][^\n]+\].+)')
+_NAMED = (r'(?:\[.*?\])|(?:\(\S+\))?'
+          r'(?:sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)?|\[\S+[@:][^\n]+\].+)')
+
+
+class ConsoleSessionLexer(BashSessionLexer):
+    """A console session in which a bare ``#`` is not a root prompt."""
+
+    name = 'Console session'
+    aliases = ['console']
+    filenames = []
+    mimetypes = []
+
+    _ps1rgx = re.compile(
+        r'^((?:' + _PREAMBLE + r')\s*[$%]\s*'
+        r'|(?:' + _NAMED + r')\s*#\s*)(.*\n?)')
+
+
+def setup(app):
+    app.add_lexer('console', ConsoleSessionLexer)
+    return {
+        'version': __version__,
+        'parallel_read_safe': True,
+        'parallel_write_safe': True,
+    }