docs.py 1.6 KB

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