All of lore.kernel.org
 help / color / mirror / Atom feed
From: Trevor Woerner <twoerner@gmail.com>
To: docs@lists.yoctoproject.org
Subject: [PATCH 7/7] sphinx: mark placeholders in console and plain-text examples
Date: Wed, 25 Nov 2026 10:45:35 -0500	[thread overview]
Message-ID: <20261125154535.3486104-8-twoerner@gmail.com> (raw)
In-Reply-To: <20261125154535.3486104-1-twoerner@gmail.com>

Most of the values a reader has to replace are in the commands the
manuals tell them to run, where <recipe> and <path/to/repo> look exactly
like text to type verbatim.

This adds console-placeholder and text-placeholder. A console line
containing a placeholder is already valid, so they re-label the run with
a filter over the finished token stream rather than a new grammar. The
filter works on offsets, because the shell lexer reads "<" as a
redirection and splits the run before a filter sees it.

Both are opt-in per block, because angle brackets also mean other
things, such as the kernel's <idle> task in an ftrace listing. A C
include is the one case the filter decides for itself, since nobody
substitutes the header in "#include <near/dbus.h>".

AI-Generated: codex/claude-opus 5.5 (xhigh)
Signed-off-by: Trevor Woerner <twoerner@gmail.com>
---
 documentation/conf.py                         |   3 +-
 .../sphinx-static/theme_overrides.css         |   2 +-
 documentation/sphinx/placeholder-lexers.py    | 134 ++++++++++++++++++
 3 files changed, 137 insertions(+), 2 deletions(-)
 create mode 100644 documentation/sphinx/placeholder-lexers.py

diff --git a/documentation/conf.py b/documentation/conf.py
index 5db20ff021fc..d56c648d8701 100644
--- a/documentation/conf.py
+++ b/documentation/conf.py
@@ -85,7 +85,8 @@ extensions = [
     'scc-lexer',
     'wks-lexer',
     'pydevshell-lexer',
-    'console-lexer'
+    'console-lexer',
+    'placeholder-lexers'
 ]
 
 # current_version = "dev"
diff --git a/documentation/sphinx-static/theme_overrides.css b/documentation/sphinx-static/theme_overrides.css
index 2083ebb5baf4..2120c43245cc 100644
--- a/documentation/sphinx-static/theme_overrides.css
+++ b/documentation/sphinx-static/theme_overrides.css
@@ -264,7 +264,7 @@ a.sd-badge {
    survives a monochrome printout. The border is #5b8db9 rather than
    something paler because it has to clear 3:1 against the code block to
    count as a boundary: a lighter blue measured 2:1 and disappeared. */
-.rst-content .highlight-bitbake-placeholder .ge {
+.rst-content div[class*="-placeholder"] .ge {
     color: #144e8c;
     font-style: italic;
     font-weight: 600;
diff --git a/documentation/sphinx/placeholder-lexers.py b/documentation/sphinx/placeholder-lexers.py
new file mode 100644
index 000000000000..668a3b71e992
--- /dev/null
+++ b/documentation/sphinx/placeholder-lexers.py
@@ -0,0 +1,134 @@
+"""Marking placeholders in languages whose lexers already accept them.
+
+``bitbake-placeholder`` exists because BitBake *rejects* a placeholder in
+a variable name: ``<`` is not in its name charset, so
+``SRCREV:pn-<recipe>`` is not an assignment at all until a rule says so.
+Widening a grammar has to be done in the grammar.
+
+Everywhere else there is nothing to widen. ``$ devtool modify <recipe>``
+is a perfectly ordinary console line; the placeholder simply looks like
+text to type verbatim. All that is needed is to re-label the run, and
+that can be done after lexing, for any language, without touching how it
+parses.
+
+So this carries a filter rather than a dialect, and builds the dialects
+from it. Most of the manuals' placeholders are in console blocks, so this
+is where the convention is most often unreadable.
+
+The filter works on absolute offsets rather than on token values, because
+a base lexer may already have split the run: the shell lexer reads ``<``
+as a redirection operator, so ``<recipe>`` never arrives as one token.
+
+Output is marked as well as input. A session lexer can tell a typed line
+from a printed one, but that does not decide whether a run is a
+placeholder: a usage synopsis is printed by the tool and exists precisely
+to say what the reader supplies.
+
+Each dialect is opt-in per block. Angle brackets mean other things, such
+as ``<idle>-0`` in an ftrace listing, where ``<idle>`` is the kernel's own
+name for the idle task. Apart from a C ``#include``, which is excluded
+below, no pattern can tell those from a placeholder, so a person decides
+per block, and a block whose printed text should not be marked is simply
+left out.
+
+An address in the angle brackets RFC 822 wants is safe by construction
+rather than by exclusion: ``@`` is not in the placeholder charset.
+"""
+import importlib
+import re
+
+from pygments.filter import Filter
+from pygments.lexers.special import TextLexer
+from pygments.token import Generic
+
+# The corrected console lexer, not Pygments' own, so a placeholder block
+# reads a bare "#" the same way every other console block does. The
+# module name is not an identifier, so it cannot be a plain import.
+BashSessionLexer = importlib.import_module('console-lexer').ConsoleSessionLexer
+
+__version__ = '1.0'
+
+# Opens with a letter, then word characters, spaces and the punctuation a
+# path or a dotted name needs. No "@", so an e-mail address never matches.
+PLACEHOLDER = re.compile(r'<[A-Za-z][\w .\'/-]*>')
+
+# The one bracketed form that is never a placeholder however a block is
+# labelled, because it is C rather than prose: the reader does not
+# substitute the header in "#include <near/dbus.h>". This is mechanical
+# and needs no per-block judgement, unlike the opt-in itself.
+INCLUDE = re.compile(r'#\s*include\s*$')
+
+
+def placeholder_spans(text):
+    """Offsets of the runs to mark."""
+    return [m.span() for m in PLACEHOLDER.finditer(text)
+            if not INCLUDE.search(text[:m.start()].rsplit('\n', 1)[-1])]
+
+
+class PlaceholderFilter(Filter):
+    """Re-label placeholder runs in a finished token stream."""
+
+    def filter(self, lexer, stream):
+        tokens = list(stream)
+        text = ''.join(value for _, value in tokens)
+        spans = placeholder_spans(text)
+        if not spans:
+            yield from tokens
+            return
+
+        split, pos = [], 0
+        for ttype, value in tokens:
+            end = pos + len(value)
+            cuts = sorted({pos, end} | {edge for span in spans
+                                        for edge in span if pos < edge < end})
+            for a, b in zip(cuts, cuts[1:]):
+                inside = any(s <= a and b <= e for s, e in spans)
+                split.append(((Generic.Emph if inside else ttype), text[a:b]))
+            pos = end
+
+        # Merge adjacent pieces of the same type. Keeping the base lexer's
+        # boundaries emits one span per fragment, and an outlined style
+        # then draws a box around each half of the same placeholder.
+        merged = []
+        for ttype, value in split:
+            if merged and merged[-1][0] is ttype:
+                merged[-1] = (ttype, merged[-1][1] + value)
+            else:
+                merged.append((ttype, value))
+        yield from merged
+
+
+def _filtered(class_name, lexer_name, alias, base):
+    """A copy of `base` that marks placeholders and changes nothing else."""
+
+    def __init__(self, **options):
+        base.__init__(self, **options)
+        self.add_filter(PlaceholderFilter())
+
+    return type(class_name, (base,), {
+        '__init__': __init__,
+        '__doc__': '%s, with placeholders marked.' % lexer_name,
+        'name': lexer_name,
+        'aliases': [alias],
+        'filenames': [],
+        'mimetypes': [],
+    })
+
+
+ConsolePlaceholderLexer = _filtered(
+    'ConsolePlaceholderLexer', 'Console session with placeholders',
+    'console-placeholder', BashSessionLexer)
+
+TextPlaceholderLexer = _filtered(
+    'TextPlaceholderLexer', 'Plain text with placeholders',
+    'text-placeholder', TextLexer)
+
+
+def setup(app):
+    for lexer in (ConsolePlaceholderLexer, TextPlaceholderLexer):
+        app.add_lexer(lexer.aliases[0], lexer)
+    return {
+        'version': __version__,
+        'parallel_read_safe': True,
+        'parallel_write_safe': True,
+    }
-- 
2.50.0.173.g8b6f19ccfc3a


  parent reply	other threads:[~2026-11-25 15:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 15:45 [PATCH 0/7] sphinx: carry lexers for the languages the manuals show Trevor Woerner
2026-11-25 15:45 ` [PATCH 1/7] sphinx: carry a BitBake lexer as a local extension Trevor Woerner
2026-11-25 15:45 ` [PATCH 2/7] sphinx: add a lexer for BitBake with placeholders Trevor Woerner
2026-11-25 15:45 ` [PATCH 3/7] sphinx: add a lexer for kernel metadata SCC files Trevor Woerner
2026-11-25 15:45 ` [PATCH 4/7] sphinx: add a lexer for wic kickstart files Trevor Woerner
2026-11-25 15:45 ` [PATCH 5/7] sphinx: add a lexer for pydevshell sessions Trevor Woerner
2026-11-25 15:45 ` [PATCH 6/7] sphinx: read a bare "#" in a console block as output Trevor Woerner
2026-11-25 15:45 ` Trevor Woerner [this message]
2026-11-26 16:33 ` [docs] [PATCH 0/7] sphinx: carry lexers for the languages the manuals show Antonin Godard
2026-11-26 22:26   ` Trevor Woerner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125154535.3486104-8-twoerner@gmail.com \
    --to=twoerner@gmail.com \
    --cc=docs@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.