From: Trevor Woerner <twoerner@gmail.com>
To: docs@lists.yoctoproject.org
Subject: [PATCH 2/7] sphinx: add a lexer for BitBake with placeholders
Date: Wed, 25 Nov 2026 10:45:30 -0500 [thread overview]
Message-ID: <20261125154535.3486104-3-twoerner@gmail.com> (raw)
In-Reply-To: <20261125154535.3486104-1-twoerner@gmail.com>
The manuals write <like this> for a value the reader has to replace, and
BitBake cannot express that. Inside a quoted value it renders as ordinary
string content, indistinguishable from text to type verbatim; in a
variable name it is not valid BitBake at all.
This adds a bitbake-placeholder lexer that accepts a placeholder where
BitBake's charsets do not and marks it wherever it appears. Everything
else is inherited from the BitBake lexer, so a block using it still
reports a genuine mistake.
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 | 20 +++
documentation/sphinx/bitbake-placeholder.py | 145 ++++++++++++++++++
3 files changed, 167 insertions(+), 1 deletion(-)
create mode 100644 documentation/sphinx/bitbake-placeholder.py
diff --git a/documentation/conf.py b/documentation/conf.py
index cdd554e30c0a..95bb24002544 100644
--- a/documentation/conf.py
+++ b/documentation/conf.py
@@ -80,7 +80,8 @@ extensions = [
'sphinx_tags',
'sphinxcontrib.rsvgconverter',
'yocto-vars',
- 'bitbake-lexer'
+ 'bitbake-lexer',
+ 'bitbake-placeholder'
]
# current_version = "dev"
diff --git a/documentation/sphinx-static/theme_overrides.css b/documentation/sphinx-static/theme_overrides.css
index 85f01eafbb94..2083ebb5baf4 100644
--- a/documentation/sphinx-static/theme_overrides.css
+++ b/documentation/sphinx-static/theme_overrides.css
@@ -252,3 +252,23 @@ a.sd-badge {
}
}
+
+/* A placeholder in a bitbake-placeholder block: something the reader is
+ meant to replace, such as <recipe> in SRCREV:pn-<recipe>.
+
+ The box is the point. A placeholder usually abuts real text it is not
+ part of - pn-<recipe>, <some.host>/<somepath> - and an outline is what
+ shows where one ends and the other begins. Colour alone does not.
+
+ Italic carries the same meaning without any colour at all, so the mark
+ 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 {
+ color: #144e8c;
+ font-style: italic;
+ font-weight: 600;
+ border: 1px solid #5b8db9;
+ border-radius: 3px;
+ padding: 0 2px;
+}
diff --git a/documentation/sphinx/bitbake-placeholder.py b/documentation/sphinx/bitbake-placeholder.py
new file mode 100644
index 000000000000..8141ac7b5687
--- /dev/null
+++ b/documentation/sphinx/bitbake-placeholder.py
@@ -0,0 +1,145 @@
+"""BitBake with documentation placeholders: the ``bitbake-placeholder`` dialect.
+
+The manuals write ``<like this>`` for "replace this with your own value".
+BitBake has no way to express that, so the convention behaves differently
+depending on where it lands: inside a quoted value it is legal BitBake and
+takes ordinary string colour, indistinguishable from a literal; in a
+variable name it is not BitBake at all, because ``<`` is not in BitBake's
+name charset.
+
+This lexer differs from plain BitBake in exactly two ways, and in no
+others:
+
+- it **accepts** a placeholder where BitBake's charsets do not, so
+ ``SRCREV:pn-<recipe> = "${AUTOREV}"`` lexes as the assignment it is
+ plainly meant to be
+- it **marks** a placeholder wherever it appears, so a reader can tell it
+ apart from text to be typed verbatim
+
+Everything else is inherited, so a block tagged ``bitbake-placeholder``
+still fails on a genuine mistake. That is the point of a dialect rather
+than a permissive lexer: it admits one convention, not all errors.
+
+``Generic.Emph`` carries the placeholder. ``Name.Placeholder`` would be
+the obvious choice and is a trap: it has no entry in Pygments'
+``STANDARD_TYPES``, so the HTML formatter walks up to ``Name`` and emits
+``class="n"``, which is indistinguishable from every other name.
+"""
+import importlib
+
+from pygments.lexer import bygroups, inherit, this, using
+from pygments.token import Generic, Keyword, Name, Operator, String, \
+ Whitespace
+
+__version__ = '1.0'
+
+
+def _base_lexer():
+ """The BitBake lexer this dialect extends.
+
+ Prefer the carried one, since it is the lexer the manuals are written
+ against, and fall back to the stock lexer if ``bitbake-lexer.py`` is
+ absent, so this file does not depend on it.
+
+ Resolving through ``get_lexer_by_name('bitbake')`` does not work here:
+ ``app.add_lexer`` registers with Sphinx rather than with Pygments, so
+ the lookup returns the stock lexer even when the carried one is
+ registered.
+ """
+ try:
+ return importlib.import_module('bitbake-lexer').BitBakeLexer
+ except Exception:
+ from pygments.lexers.bitbake import BitBakeLexer
+ return BitBakeLexer
+
+
+_PH = r'<[^<>\n]+>'
+
+# A variable name that may carry placeholders. Deliberately not derived
+# from the base lexer's own pattern: this file has to work against either
+# base, and a name is the one thing the two agree on.
+_NCHAR = r'(?:\$\{[^{}\s]+\}|[A-Za-z0-9_\-.+/~${}])'
+_PNAME = r':?(?:' + _NCHAR + r'|' + _PH + r')+'
+_POVERRIDE = r'(?::(?:' + _NCHAR + r'|' + _PH + r')+)*'
+_ASSIGN = r'(?:\?\?=|\?=|:=|\+=|=\+|\.=|=\.|=)'
+
+# Fire only where a placeholder is actually present in the name. Without
+# this the dialect would accept ``PV = 1.2.3``, which no BitBake accepts:
+# the rules below are unguarded, so anything they match bypasses the
+# inherited rules and their checks. A dialect has to admit one convention,
+# not all errors, and this is what holds it to that.
+_HAS_PH = (r'(?=(?:export[ \t]+)?(?:' + _NCHAR + r'|:)*' + _PH + r')')
+
+
+class BitBakePlaceholderLexer(_base_lexer()):
+ """BitBake, plus the manuals' ``<replace this>`` convention."""
+
+ name = 'BitBake with placeholders'
+ aliases = ['bitbake-placeholder']
+ filenames = []
+ mimetypes = []
+ url = 'https://docs.yoctoproject.org'
+
+ tokens = {
+ 'root': [
+ # An assignment whose name or override chain carries a
+ # placeholder. This comes first because the inherited rules are
+ # guarded by BitBake's own regexes, which reject such a name
+ # outright, so the line would never reach them.
+ #
+ # A placeholder inside a varflag, ``FOO[<flag>] = "1"``, is not
+ # handled: the manuals contain none, and supporting a form
+ # nobody writes is a rule nobody can check.
+ (r'^' + _HAS_PH
+ + r'(export[ \t]+)?(' + _PNAME + r')(' + _POVERRIDE + r')'
+ r'([ \t]*)(' + _ASSIGN + r')',
+ bygroups(Keyword.Type, using(this, state='placeholder-name'),
+ using(this, state='placeholder-name'), Whitespace,
+ Operator),
+ 'value'),
+ inherit,
+ ],
+
+ # Split a name into its placeholder and non-placeholder parts, so
+ # ``pn-<recipe>`` shows which half the reader has to replace.
+ 'placeholder-name': [
+ (_PH, Generic.Emph),
+ (r':', Name.Decorator),
+ (r'[^<:\n]+', Name.Variable),
+ (r'[^\n]', Name.Variable),
+ ],
+
+ # Marking, as opposed to accepting. A placeholder inside a value is
+ # already valid BitBake; it just looks like a literal.
+ #
+ # The second rule in each state is what makes the first one work.
+ # The inherited states end in a greedy run of "anything that is not
+ # a quote, dollar or backslash", which swallows a whole value
+ # including its placeholders as one token, so the rule above never
+ # gets a position to match at. Stopping at the next "<" hands each
+ # placeholder back to it.
+ 'value': [
+ (_PH, Generic.Emph),
+ (r'[^<\s"\'$\\]+', String),
+ inherit,
+ ],
+ 'string-double': [
+ (_PH, Generic.Emph),
+ (r'[^<"\\$\n]+', String.Double),
+ inherit,
+ ],
+ 'string-single': [
+ (_PH, Generic.Emph),
+ (r"[^<'\\$\n]+", String.Single),
+ inherit,
+ ],
+ }
+
+
+def setup(app):
+ app.add_lexer('bitbake-placeholder', BitBakePlaceholderLexer)
+ return {
+ 'version': __version__,
+ 'parallel_read_safe': True,
+ 'parallel_write_safe': True,
+ }
--
2.50.0.173.g8b6f19ccfc3a
next prev 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 ` Trevor Woerner [this message]
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 ` [PATCH 7/7] sphinx: mark placeholders in console and plain-text examples Trevor Woerner
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-3-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox