blob: 8083d7da19f02f1ce5f4fddd424cf77e3a321da5 [file] [log] [blame]
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001#!/usr/bin/env python
2import re
Andrew Geisslerc3d88e42020-10-02 09:45:00 -05003import sys
Andrew Geisslerc9f78652020-09-18 14:11:35 -05004
5import sphinx
6from sphinx.application import Sphinx
7
Andrew Geisslerc3d88e42020-10-02 09:45:00 -05008# This extension uses pyyaml, report an explicit
9# error message if it's not installed
10try:
11 import yaml
12except ImportError:
13 sys.stderr.write("The Yocto Project Sphinx documentation requires PyYAML.\
14 \nPlease make sure to install pyyaml python package.\n")
15 sys.exit(1)
16
Andrew Geisslerc9f78652020-09-18 14:11:35 -050017__version__ = '1.0'
18
19# Variables substitutions. Uses {VAR} subst using variables defined in poky.yaml
20# Each .rst file is processed after source-read event (subst_vars_replace runs once per file)
21subst_vars = {}
22
23def subst_vars_replace(app: Sphinx, docname, source):
24 result = source[0]
25 for k in subst_vars:
26 result = result.replace("&"+k+";", subst_vars[k])
27 source[0] = result
28
29PATTERN = re.compile(r'&(.*?);')
30def expand(val, src):
31 return PATTERN.sub(lambda m: expand(src.get(m.group(1), ''), src), val)
32
33def setup(app: Sphinx):
34 #FIXME: if poky.yaml changes, files are not reprocessed.
35 with open("poky.yaml") as file:
36 subst_vars.update(yaml.load(file, Loader=yaml.FullLoader))
37
38 for k in subst_vars:
39 subst_vars[k] = expand(subst_vars[k], subst_vars)
40
41 app.connect('source-read', subst_vars_replace)
42
43 return dict(
44 version = __version__,
45 parallel_read_safe = True,
46 parallel_write_safe = True
47 )