blob: 5689472991db3e9bc2ca4373f4924b1abda00333 [file] [log] [blame]
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001#!/usr/bin/env python
2import re
3import yaml
4
5import sphinx
6from 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)
12subst_vars = {}
13
14def 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
20PATTERN = re.compile(r'&(.*?);')
21def expand(val, src):
22 return PATTERN.sub(lambda m: expand(src.get(m.group(1), ''), src), val)
23
24def 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 )