docs.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import json
  2. def extract_macro_calls(unit, macro_value_name):
  3. if not unit.get(macro_value_name):
  4. return []
  5. return filter(None, unit.get(macro_value_name).replace('$' + macro_value_name, '').split())
  6. def macro_calls_to_dict(unit, calls):
  7. def split_args(arg):
  8. if arg is None:
  9. return None
  10. kv = list(filter(None, arg.split('=')))
  11. if len(kv) != 2:
  12. unit.message(
  13. [
  14. 'error',
  15. 'Invalid variables specification "{}": value expected to be in form %name%=%value% (with no spaces)'.format(
  16. arg
  17. ),
  18. ]
  19. )
  20. return None
  21. return kv
  22. return dict(filter(None, map(split_args, calls)))
  23. def get_variables(unit):
  24. orig_variables = macro_calls_to_dict(unit, extract_macro_calls(unit, '_DOCS_VARS_VALUE'))
  25. return {k: unit.get(k) or v for k, v in orig_variables.items()}
  26. def onprocess_docs(unit, *args):
  27. if unit.enabled('_DOCS_USE_PLANTUML'):
  28. unit.on_docs_yfm_use_plantuml([])
  29. if unit.get('_DOCS_DIR_VALUE') == '':
  30. unit.on_yfm_docs_dir([unit.get('_YFM_DOCS_DIR_DEFAULT_VALUE')])
  31. variables = get_variables(unit)
  32. if variables:
  33. unit.set(['_DOCS_VARS_FLAG', '--vars {}'.format(json.dumps(json.dumps(variables, sort_keys=True)))])