aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/docs.py
blob: 09cb8c040b35103084b459ab41cc55163d9153d9 (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
import json


def extract_macro_calls(unit, macro_value_name):
    if not unit.get(macro_value_name):
        return []

    return filter(None, unit.get(macro_value_name).replace('$' + macro_value_name, '').split())


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

        kv = 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 onprocess_docs(unit, *args):
    build_tool = unit.get('_DOCS_BUILDER_VALUE')
    if build_tool:
        if build_tool not in ['mkdocs', 'yfm']:
            unit.message(['error', 'Unsupported build tool {}'.format(build_tool)])
        if build_tool == 'mkdocs' and not unit.get('MODDIR').startswith('devtools/ymake/tests'):
            unit.message(['error', '`mkdocs` builder is prohibited to use in `DOCS` multimodule. Use `MKDOCS` multimodule instead of `DOCS`.'])
    else:
        build_tool = 'yfm'
        unit.ondocs_builder([build_tool])
    if build_tool == 'yfm' and unit.enabled('_DOCS_USE_PLANTUML'):
        unit.on_docs_yfm_use_plantuml([])
    orig_variables = macro_calls_to_dict(unit, extract_macro_calls(unit, '_DOCS_VARS_VALUE'))
    variables = {k: unit.get(k) or v for k, v in orig_variables.items()}
    if variables:
        if build_tool == 'mkdocs':
            unit.set(['_DOCS_VARS_FLAG', ' '.join(['--var {}={}'.format(k, v) for k, v in variables.items()])])
        elif build_tool == 'yfm':
            unit.set(['_DOCS_VARS_FLAG', '--vars {}'.format(json.dumps(json.dumps(variables, sort_keys=True)))])
        else:
            assert False, 'Unexpected build_tool value: [{}]'.format(build_tool)


def onprocess_mkdocs(unit, *args):
    orig_variables = macro_calls_to_dict(unit, extract_macro_calls(unit, '_DOCS_VARS_VALUE'))
    variables = {k: unit.get(k) or v for k, v in orig_variables.items()}
    unit.set(['_DOCS_VARS_FLAG', ' '.join(['--var {}={}'.format(k, v) for k, v in variables.items()])])