mirror of
git://git.yoctoproject.org/poky.git
synced 2025-07-19 21:09:03 +02:00

The Yocto Project documentation makes heavy use of 'global' variables. In Docbook these 'variables' are stored in the file poky.ent. This Docbook feature is not handled automatically with Pandoc. Sphinx has builtin support for substitutions however they are local to each reST file by default. They can be made global by using rst_prolog: rst_prolog A string of reStructuredText that will be included at the beginning of every source file that is read. However Sphinx substitution feature has several important limitations. For example, substitution does not work in code-block section. yocto-vars.py is an extension that processes .rst file to find and replace 'variables'. This plugin will do variables substitutions whenever a rst file is read, so it happens before sphinx parses the content. All variables are set in poky.yaml. It's a simple YAML file with pairs of variable/value, and the file is parsed once during setup. It's important to note that variables can reference other variables. poky.yaml was generated by converting poky.ent into a YAML format. To use a variable in the Yocto Project .rst files, make sure it is defined in poky.yaml, and then you can use : &DISTRO_NAME; For external links, Sphinx has a specific extension called extlinks, let's use it instead of variable substituions. Note that we intentionnally did not put the trailing '/' in the URL, this is to allow us to use :yocto_git:`/` trick to get the actual URL displayed in the HTML. (From yocto-docs rev: dc5f53fae8fdfdda04285869dd1419107b920bfe) Signed-off-by: Nicolas Dechesne <nicolas.dechesne@linaro.org> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
#!/usr/bin/env python
|
|
import re
|
|
import yaml
|
|
|
|
import sphinx
|
|
from sphinx.application import Sphinx
|
|
|
|
__version__ = '1.0'
|
|
|
|
# Variables substitutions. Uses {VAR} subst using variables defined in poky.yaml
|
|
# Each .rst file is processed after source-read event (subst_vars_replace runs once per file)
|
|
subst_vars = {}
|
|
|
|
def subst_vars_replace(app: Sphinx, docname, source):
|
|
result = source[0]
|
|
for k in subst_vars:
|
|
result = result.replace("&"+k+";", subst_vars[k])
|
|
source[0] = result
|
|
|
|
PATTERN = re.compile(r'&(.*?);')
|
|
def expand(val, src):
|
|
return PATTERN.sub(lambda m: expand(src.get(m.group(1), ''), src), val)
|
|
|
|
def setup(app: Sphinx):
|
|
#FIXME: if poky.yaml changes, files are not reprocessed.
|
|
with open("poky.yaml") as file:
|
|
subst_vars.update(yaml.load(file, Loader=yaml.FullLoader))
|
|
|
|
for k in subst_vars:
|
|
subst_vars[k] = expand(subst_vars[k], subst_vars)
|
|
|
|
app.connect('source-read', subst_vars_replace)
|
|
|
|
return dict(
|
|
version = __version__,
|
|
parallel_read_safe = True,
|
|
parallel_write_safe = True
|
|
)
|