generator.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. def generate_docs():
  7. """Generates the XAP protocol documentation by merging the definitions files, and producing the corresponding Markdown document under `/docs/`.
  8. """
  9. docs_list = []
  10. overall = None
  11. for file in get_xap_definition_files():
  12. overall = update_xap_definitions(overall, hjson.load(file.open(encoding='utf-8')))
  13. # Inject dummy bits for unspecified response flags
  14. for n in range(0, 8):
  15. if str(n) not in overall['response_flags']['bits']:
  16. overall['response_flags']['bits'][str(n)] = {'name': '', 'description': '', 'define': '-'}
  17. output_doc = QMK_FIRMWARE / "docs" / f"{file.stem}.md"
  18. docs_list.append(output_doc)
  19. output = render_xap_output('docs', 'docs.md.j2', overall)
  20. with open(output_doc, "w", encoding='utf-8') as out_file:
  21. out_file.write(output)
  22. output_doc = QMK_FIRMWARE / "docs" / f"xap_protocol.md"
  23. with open(output_doc, "w", encoding='utf-8') as out_file:
  24. out_file.write('''\
  25. # XAP Protocol Reference
  26. ''')
  27. for file in reversed(sorted(docs_list)):
  28. ver = file.stem[4:]
  29. out_file.write(f'* [XAP Version {ver}]({file.name})\n')