| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import shutil |
| 5 |
import tempfile |
| 6 |
from os.path import isdir, isfile, join |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
from urllib.parse import urlsplit |
| 9 |
|
| 10 |
from mkdocs.commands.build import build |
| 11 |
from mkdocs.config import load_config |
| 12 |
from mkdocs.livereload import LiveReloadServer, _serve_url |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from mkdocs.config.defaults import MkDocsConfig |
| 16 |
|
| 17 |
log = logging.getLogger(__name__) |
| 18 |
|
| 19 |
|
| 20 |
def serve( |
| 21 |
config_file: str | None = None, |
| 22 |
livereload: bool = True, |
| 23 |
build_type: str | None = None, |
| 24 |
watch_theme: bool = False, |
| 25 |
watch: list[str] = [], |
| 26 |
*, |
| 27 |
open_in_browser: bool = False, |
| 28 |
**kwargs, |
| 29 |
) -> None: |
| 30 |
""" |
| 31 |
Start the MkDocs development server. |
| 32 |
|
| 33 |
By default it will serve the documentation on http://localhost:8000/ and |
| 34 |
it will rebuild the documentation and refresh the page automatically |
| 35 |
whenever a file is edited. |
| 36 |
""" |
| 37 |
# Create a temporary build directory, and set some options to serve it |
| 38 |
# PY2 returns a byte string by default. The Unicode prefix ensures a Unicode |
| 39 |
# string is returned. And it makes MkDocs temp dirs easier to identify. |
| 40 |
site_dir = tempfile.mkdtemp(prefix='mkdocs_') |
| 41 |
|
| 42 |
def get_config(): |
| 43 |
config = load_config( |
| 44 |
config_file=config_file, |
| 45 |
site_dir=site_dir, |
| 46 |
**kwargs, |
| 47 |
) |
| 48 |
config.watch.extend(watch) |
| 49 |
return config |
| 50 |
|
| 51 |
is_clean = build_type == 'clean' |
| 52 |
is_dirty = build_type == 'dirty' |
| 53 |
|
| 54 |
config = get_config() |
| 55 |
config.plugins.on_startup(command=('build' if is_clean else 'serve'), dirty=is_dirty) |
| 56 |
|
| 57 |
host, port = config.dev_addr |
| 58 |
mount_path = urlsplit(config.site_url or '/').path |
| 59 |
config.site_url = serve_url = _serve_url(host, port, mount_path) |
| 60 |
|
| 61 |
def builder(config: MkDocsConfig | None = None): |
| 62 |
log.info("Building documentation...") |
| 63 |
if config is None: |
| 64 |
config = get_config() |
| 65 |
config.site_url = serve_url |
| 66 |
|
| 67 |
build(config, serve_url=None if is_clean else serve_url, dirty=is_dirty) |
| 68 |
|
| 69 |
server = LiveReloadServer( |
| 70 |
builder=builder, host=host, port=port, root=site_dir, mount_path=mount_path |
| 71 |
) |
| 72 |
|
| 73 |
def error_handler(code) -> bytes | None: |
| 74 |
if code in (404, 500): |
| 75 |
error_page = join(site_dir, f'{code}.html') |
| 76 |
if isfile(error_page): |
| 77 |
with open(error_page, 'rb') as f: |
| 78 |
return f.read() |
| 79 |
return None |
| 80 |
|
| 81 |
server.error_handler = error_handler |
| 82 |
|
| 83 |
try: |
| 84 |
# Perform the initial build |
| 85 |
builder(config) |
| 86 |
|
| 87 |
if livereload: |
| 88 |
# Watch the documentation files, the config file and the theme files. |
| 89 |
server.watch(config.docs_dir) |
| 90 |
if config.config_file_path: |
| 91 |
server.watch(config.config_file_path) |
| 92 |
|
| 93 |
if watch_theme: |
| 94 |
for d in config.theme.dirs: |
| 95 |
server.watch(d) |
| 96 |
|
| 97 |
# Run `serve` plugin events. |
| 98 |
server = config.plugins.on_serve(server, config=config, builder=builder) |
| 99 |
|
| 100 |
for item in config.watch: |
| 101 |
server.watch(item) |
| 102 |
|
| 103 |
try: |
| 104 |
server.serve(open_in_browser=open_in_browser) |
| 105 |
except KeyboardInterrupt: |
| 106 |
log.info("Shutting down...") |
| 107 |
finally: |
| 108 |
server.shutdown() |
| 109 |
finally: |
| 110 |
config.plugins.on_shutdown() |
| 111 |
if isdir(site_dir): |
| 112 |
shutil.rmtree(site_dir) |