generate_docs.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """This script generates the XAP protocol documentation.
  2. """
  3. import hjson
  4. from milc import cli
  5. from qmk.constants import QMK_FIRMWARE
  6. from qmk.path import normpath
  7. from qmk.commands import dump_lines
  8. from qmk.keyboard import keyboard_completer, keyboard_folder
  9. from qmk.xap.common import get_xap_definition_files, update_xap_definitions, merge_xap_defs, render_xap_output
  10. def _patch_spec_for_docs(spec):
  11. # Inject dummy bits for unspecified response flags
  12. for n in range(0, 8):
  13. if str(n) not in spec['response_flags']['bits']:
  14. spec['response_flags']['bits'][str(n)] = {'name': '', 'description': '', 'define': '-'}
  15. @cli.subcommand('Generates the XAP protocol documentation.', hidden=False if cli.config.user.developer else True)
  16. def xap_generate_docs(cli):
  17. """Generates the XAP protocol documentation by merging the definitions files, and producing the corresponding Markdown document under `/docs/`.
  18. """
  19. versions = []
  20. overall = None
  21. for file in get_xap_definition_files():
  22. overall = update_xap_definitions(overall, hjson.load(file.open(encoding='utf-8')))
  23. _patch_spec_for_docs(overall)
  24. output_doc = QMK_FIRMWARE / "docs" / f"{file.stem}.md"
  25. versions.append(overall['version'])
  26. output = render_xap_output('docs', 'docs.md.j2', overall)
  27. with open(output_doc, "w", encoding='utf-8') as out_file:
  28. out_file.write(output)
  29. output_doc = QMK_FIRMWARE / "docs" / "xap_protocol.md"
  30. output = render_xap_output('docs', 'versions.md.j2', overall, versions=versions)
  31. with open(output_doc, "w", encoding='utf-8') as out_file:
  32. out_file.write(output)
  33. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  34. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  35. @cli.argument('-km', '--keymap', help='The keymap\'s name - "default" if not specified')
  36. @cli.argument('-kb', '--keyboard', required=True, type=keyboard_folder, completer=keyboard_completer, help='Name of the keyboard')
  37. @cli.subcommand('Generates the XAP protocol documentation for a given keyboard/keymap.', hidden=False if cli.config.user.developer else True)
  38. def xap_generate_keyboard_docs(cli):
  39. """Generates the XAP protocol documentation for a given keyboard/keymap and producing the corresponding Markdown.
  40. """
  41. spec = merge_xap_defs(cli.args.keyboard, cli.args.keymap or 'default')
  42. _patch_spec_for_docs(spec)
  43. output = render_xap_output('docs', 'docs.md.j2', spec)
  44. dump_lines(cli.args.output, output.split('\n'), cli.args.quiet)