docs.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. """Serve QMK documentation locally
  2. """
  3. import http.server
  4. import os
  5. import webbrowser
  6. from milc import cli
  7. @cli.argument('-p', '--port', default=8936, type=int, help='Port number to use.')
  8. @cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.')
  9. @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True)
  10. def docs(cli):
  11. """Spin up a local HTTPServer instance for the QMK docs.
  12. """
  13. os.chdir('docs')
  14. with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd:
  15. cli.log.info(f"Serving QMK docs at http://localhost:{cli.config.docs.port}/")
  16. cli.log.info("Press Control+C to exit.")
  17. if cli.config.docs.browser:
  18. webbrowser.open(f'http://localhost:{cli.config.docs.port}')
  19. try:
  20. httpd.serve_forever()
  21. except KeyboardInterrupt:
  22. cli.log.info("Stopping HTTP server...")
  23. finally:
  24. httpd.shutdown()