summaryrefslogtreecommitdiffstats
path: root/documentation/sphinx/yocto-vars.py
blob: 5689472991db3e9bc2ca4373f4924b1abda00333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
    )