diff options
Diffstat (limited to 'lib/python/qmk/cli/docs.py')
| -rw-r--r-- | lib/python/qmk/cli/docs.py | 41 |
1 files changed, 12 insertions, 29 deletions
diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py index c24b914bc1..d28dddf194 100644 --- a/lib/python/qmk/cli/docs.py +++ b/lib/python/qmk/cli/docs.py | |||
| @@ -1,44 +1,27 @@ | |||
| 1 | """Serve QMK documentation locally | 1 | """Serve QMK documentation locally |
| 2 | """ | 2 | """ |
| 3 | import http.server | ||
| 4 | import os | ||
| 5 | import shutil | 3 | import shutil |
| 6 | import webbrowser | 4 | from qmk.docs import prepare_docs_build_area, run_docs_command |
| 7 | 5 | ||
| 8 | from milc import cli | 6 | from milc import cli |
| 9 | 7 | ||
| 10 | 8 | ||
| 11 | @cli.argument('-p', '--port', default=8936, type=int, help='Port number to use.') | ||
| 12 | @cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.') | ||
| 13 | @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True) | 9 | @cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True) |
| 14 | def docs(cli): | 10 | def docs(cli): |
| 15 | """Spin up a local HTTP server for the QMK docs. | 11 | """Spin up a local HTTP server for the QMK docs. |
| 16 | """ | 12 | """ |
| 17 | os.chdir('docs') | ||
| 18 | 13 | ||
| 19 | # If docsify-cli is installed, run that instead so we get live reload | 14 | if not shutil.which('doxygen'): |
| 20 | if shutil.which('docsify'): | 15 | cli.log.error('doxygen is not installed. Please install it and try again.') |
| 21 | command = ['docsify', 'serve', '--port', f'{cli.config.docs.port}', '--open' if cli.config.docs.browser else ''] | 16 | return |
| 22 | 17 | ||
| 23 | cli.log.info(f"Running {{fg_cyan}}{str.join(' ', command)}{{fg_reset}}") | 18 | if not shutil.which('yarn'): |
| 24 | cli.log.info("Press Control+C to exit.") | 19 | cli.log.error('yarn is not installed. Please install it and try again.') |
| 20 | return | ||
| 25 | 21 | ||
| 26 | try: | 22 | if not prepare_docs_build_area(is_production=False): |
| 27 | cli.run(command, capture_output=False) | 23 | return False |
| 28 | except KeyboardInterrupt: | ||
| 29 | cli.log.info("Stopping HTTP server...") | ||
| 30 | else: | ||
| 31 | # Fall back to Python HTTPServer | ||
| 32 | with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd: | ||
| 33 | cli.log.info(f"Serving QMK docs at http://localhost:{cli.config.docs.port}/") | ||
| 34 | cli.log.info("Press Control+C to exit.") | ||
| 35 | 24 | ||
| 36 | if cli.config.docs.browser: | 25 | if not cli.config.general.verbose: |
| 37 | webbrowser.open(f'http://localhost:{cli.config.docs.port}') | 26 | cli.log.info('Serving docs at http://localhost:5173/ (Ctrl+C to stop)') |
| 38 | 27 | run_docs_command('run', 'docs:dev') | |
| 39 | try: | ||
| 40 | httpd.serve_forever() | ||
| 41 | except KeyboardInterrupt: | ||
| 42 | cli.log.info("Stopping HTTP server...") | ||
| 43 | finally: | ||
| 44 | httpd.shutdown() | ||
