From 44e8d439aa57973437ea0f9d7ee60b8f55df9dd1 Mon Sep 17 00:00:00 2001 From: Nicolas Dechesne Date: Fri, 3 Jul 2020 17:46:31 +0200 Subject: 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 Signed-off-by: Richard Purdie --- documentation/sphinx/yocto-vars.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 documentation/sphinx/yocto-vars.py (limited to 'documentation/sphinx/yocto-vars.py') 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 @@ +#!/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 + ) -- cgit v1.2.3-54-g00ecf