Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame^] | 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 | ) |