You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

  1. """Serve QMK documentation locally
  2. """
  3. import http.server
  4. import os
  5. import shutil
  6. import webbrowser
  7. from milc import cli
  8. @cli.argument('-p', '--port', default=8936, type=int, help='Port number to use.')
  9. @cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.')
  10. @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True)
  11. def docs(cli):
  12. """Spin up a local HTTP server for the QMK docs.
  13. """
  14. os.chdir('docs')
  15. # If docsify-cli is installed, run that instead so we get live reload
  16. if shutil.which('docsify'):
  17. command = ['docsify', 'serve', '--port', f'{cli.config.docs.port}', '--open' if cli.config.docs.browser else '']
  18. cli.log.info(f"Running {{fg_cyan}}{str.join(' ', command)}{{fg_reset}}")
  19. cli.log.info("Press Control+C to exit.")
  20. try:
  21. cli.run(command, capture_output=False)
  22. except KeyboardInterrupt:
  23. cli.log.info("Stopping HTTP server...")
  24. else:
  25. # Fall back to Python HTTPServer
  26. with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd:
  27. cli.log.info(f"Serving QMK docs at http://localhost:{cli.config.docs.port}/")
  28. cli.log.info("Press Control+C to exit.")
  29. if cli.config.docs.browser:
  30. webbrowser.open(f'http://localhost:{cli.config.docs.port}')
  31. try:
  32. httpd.serve_forever()
  33. except KeyboardInterrupt:
  34. cli.log.info("Stopping HTTP server...")
  35. finally:
  36. httpd.shutdown()