@@ -85,7 +85,8 @@ extensions = [
'scc-lexer',
'wks-lexer',
'pydevshell-lexer',
- 'console-lexer'
+ 'console-lexer',
+ 'placeholder-lexers'
]
# current_version = "dev"
@@ -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;
new file mode 100644
@@ -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,
+ }
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