generate_docs.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """This script generates the XAP protocol documentation.
  2. """
  3. import hjson
  4. from qmk.constants import QMK_FIRMWARE
  5. from qmk.xap.common import get_xap_definition_files, update_xap_definitions, render_xap_output
  6. from milc import cli
  7. @cli.subcommand('Generates the XAP protocol documentation.', hidden=False if cli.config.user.developer else True)
  8. def xap_generate_docs(cli):
  9. """Generates the XAP protocol documentation by merging the definitions files, and producing the corresponding Markdown document under `/docs/`.
  10. """
  11. docs_list = []
  12. overall = None
  13. for file in get_xap_definition_files():
  14. overall = update_xap_definitions(overall, hjson.load(file.open(encoding='utf-8')))
  15. # Inject dummy bits for unspecified response flags
  16. for n in range(0, 8):
  17. if str(n) not in overall['response_flags']['bits']:
  18. overall['response_flags']['bits'][str(n)] = {'name': '', 'description': '', 'define': '-'}
  19. output_doc = QMK_FIRMWARE / "docs" / f"{file.stem}.md"
  20. docs_list.append(output_doc)
  21. output = render_xap_output('docs', 'docs.md.j2', overall)
  22. with open(output_doc, "w", encoding='utf-8') as out_file:
  23. out_file.write(output)
  24. output_doc = QMK_FIRMWARE / "docs" / "xap_protocol.md"
  25. with open(output_doc, "w", encoding='utf-8') as out_file:
  26. out_file.write('''\
  27. # XAP Protocol Reference
  28. ''')
  29. for file in reversed(sorted(docs_list)):
  30. ver = file.stem[4:]
  31. out_file.write(f'* [XAP Version {ver}]({file.name})\n')