aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/docs.py
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-11-05 08:02:27 +1100
committerGitHub <noreply@github.com>2021-11-05 08:02:27 +1100
commit6437045166cafab14f2fbe5fcae44f6b066adbc5 (patch)
treeb774d69b5ae926b9c5cf583a95fe28c8c39942f6 /lib/python/qmk/cli/docs.py
parentc85109b1089b646641ec28e81d9530b8d1d0f263 (diff)
downloadqmk_firmware-6437045166cafab14f2fbe5fcae44f6b066adbc5.tar.gz
qmk_firmware-6437045166cafab14f2fbe5fcae44f6b066adbc5.zip
`qmk docs`: Run `docsify serve` if available (#15056)
Diffstat (limited to 'lib/python/qmk/cli/docs.py')
-rw-r--r--lib/python/qmk/cli/docs.py32
1 files changed, 23 insertions, 9 deletions
diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py
index d8f9b045a..c24b914bc 100644
--- a/lib/python/qmk/cli/docs.py
+++ b/lib/python/qmk/cli/docs.py
@@ -2,6 +2,7 @@
2""" 2"""
3import http.server 3import http.server
4import os 4import os
5import shutil
5import webbrowser 6import webbrowser
6 7
7from milc import cli 8from milc import cli
@@ -11,20 +12,33 @@ from milc import cli
11@cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.') 12@cli.argument('-b', '--browser', action='store_true', help='Open the docs in the default browser.')
12@cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True) 13@cli.subcommand('Run a local webserver for QMK documentation.', hidden=False if cli.config.user.developer else True)
13def docs(cli): 14def docs(cli):
14 """Spin up a local HTTPServer instance for the QMK docs. 15 """Spin up a local HTTP server for the QMK docs.
15 """ 16 """
16 os.chdir('docs') 17 os.chdir('docs')
17 18
18 with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd: 19 # If docsify-cli is installed, run that instead so we get live reload
19 cli.log.info(f"Serving QMK docs at http://localhost:{cli.config.docs.port}/") 20 if shutil.which('docsify'):
20 cli.log.info("Press Control+C to exit.") 21 command = ['docsify', 'serve', '--port', f'{cli.config.docs.port}', '--open' if cli.config.docs.browser else '']
21 22
22 if cli.config.docs.browser: 23 cli.log.info(f"Running {{fg_cyan}}{str.join(' ', command)}{{fg_reset}}")
23 webbrowser.open(f'http://localhost:{cli.config.docs.port}') 24 cli.log.info("Press Control+C to exit.")
24 25
25 try: 26 try:
26 httpd.serve_forever() 27 cli.run(command, capture_output=False)
27 except KeyboardInterrupt: 28 except KeyboardInterrupt:
28 cli.log.info("Stopping HTTP server...") 29 cli.log.info("Stopping HTTP server...")
29 finally: 30 else:
30 httpd.shutdown() 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
36 if cli.config.docs.browser:
37 webbrowser.open(f'http://localhost:{cli.config.docs.port}')
38
39 try:
40 httpd.serve_forever()
41 except KeyboardInterrupt:
42 cli.log.info("Stopping HTTP server...")
43 finally:
44 httpd.shutdown()