summaryrefslogtreecommitdiffstats
path: root/build/plugins/docs.py
blob: cba377330b74f30968211d0bf29d823668707c29 (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json

from ymake import Unit, macro


def extract_macro_calls(unit, macro_value_name):
    value = unit.get_subst(macro_value_name).strip()
    if not value:
        return []

    return filter(None, value.split())


def macro_calls_to_dict(unit, calls):
    def split_args(arg):
        if arg is None:
            return None

        kv = list(filter(None, arg.split('=')))
        if len(kv) != 2:
            unit.message(
                [
                    'error',
                    'Invalid variables specification "{}": value expected to be in form %name%=%value% (with no spaces)'.format(
                        arg
                    ),
                ]
            )
            return None

        return kv

    return dict(filter(None, map(split_args, calls)))


def get_variables(unit):
    orig_variables = macro_calls_to_dict(unit, extract_macro_calls(unit, '_DOCS_VARS_VALUE'))
    return {k: unit.get(k) or v for k, v in orig_variables.items()}


@macro
def PROCESS_DOCS(unit: Unit, *args: tuple[str, ...]):
    if unit.enabled('_DOCS_USE_PLANTUML'):
        unit.on_docs_yfm_use_plantuml([])

    if unit.get('_DOCS_DIR_VALUE') == '':
        unit.on_yfm_docs_dir([unit.get('_YFM_DOCS_DIR_DEFAULT_VALUE')])

    variables = get_variables(unit)
    if variables:
        unit.set(['_DOCS_VARS_FLAG', '--vars {}'.format(json.dumps(json.dumps(variables, sort_keys=True)))])


@macro
def _APPEND_DOCS_DIR_FLAG(unit: Unit, *args: tuple[str, ...]):
    assert len(args) == 1
    docs_dir = args[0]

    ns = unit.get('_DOCS_DIR_INTERNAL_NAMESPACE')
    if not ns:
        ns = docs_dir
    last = unit.get('_DOCS_DIR_VALUE')
    unit.set(['_DOCS_DIR_VALUE', f'{last} --docs-dir {docs_dir} {ns}'])