diff options
author | Nicolas Dechesne <nicolas.dechesne@linaro.org> | 2020-07-03 17:46:31 +0200 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-09-17 10:09:33 +0100 |
commit | 44e8d439aa57973437ea0f9d7ee60b8f55df9dd1 (patch) | |
tree | 48eef6bfd1207830c608ee352ad998db52b9749a /documentation/sphinx | |
parent | 18786fbffe299f81b7c156ba1cee00359589dc3f (diff) | |
download | poky-44e8d439aa57973437ea0f9d7ee60b8f55df9dd1.tar.gz |
sphinx: conf: add substitutions/global variables
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>
Diffstat (limited to 'documentation/sphinx')
-rw-r--r-- | documentation/sphinx/yocto-vars.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/documentation/sphinx/yocto-vars.py b/documentation/sphinx/yocto-vars.py new file mode 100644 index 0000000000..5689472991 --- /dev/null +++ b/documentation/sphinx/yocto-vars.py | |||
@@ -0,0 +1,38 @@ | |||
1 | #!/usr/bin/env python | ||
2 | import re | ||
3 | import yaml | ||
4 | |||
5 | import sphinx | ||
6 | from sphinx.application import Sphinx | ||
7 | |||
8 | __version__ = '1.0' | ||
9 | |||
10 | # Variables substitutions. Uses {VAR} subst using variables defined in poky.yaml | ||
11 | # Each .rst file is processed after source-read event (subst_vars_replace runs once per file) | ||
12 | subst_vars = {} | ||
13 | |||
14 | def subst_vars_replace(app: Sphinx, docname, source): | ||
15 | result = source[0] | ||
16 | for k in subst_vars: | ||
17 | result = result.replace("&"+k+";", subst_vars[k]) | ||
18 | source[0] = result | ||
19 | |||
20 | PATTERN = re.compile(r'&(.*?);') | ||
21 | def expand(val, src): | ||
22 | return PATTERN.sub(lambda m: expand(src.get(m.group(1), ''), src), val) | ||
23 | |||
24 | def setup(app: Sphinx): | ||
25 | #FIXME: if poky.yaml changes, files are not reprocessed. | ||
26 | with open("poky.yaml") as file: | ||
27 | subst_vars.update(yaml.load(file, Loader=yaml.FullLoader)) | ||
28 | |||
29 | for k in subst_vars: | ||
30 | subst_vars[k] = expand(subst_vars[k], subst_vars) | ||
31 | |||
32 | app.connect('source-read', subst_vars_replace) | ||
33 | |||
34 | return dict( | ||
35 | version = __version__, | ||
36 | parallel_read_safe = True, | ||
37 | parallel_write_safe = True | ||
38 | ) | ||