|  | 
|  | 1 | +# Handles merging mkdocs config in a way the native inheritance feature doesn't quite cover | 
|  | 2 | +# | 
|  | 3 | +# This relies on a key in "extra.overrides" | 
|  | 4 | +# Valid keys are `custom_dir: str` and `hooks: [-path/to.py, -path/to.py]`, e.g. | 
|  | 5 | +# | 
|  | 6 | +# ```yml | 
|  | 7 | +# extra: | 
|  | 8 | +# overrides: | 
|  | 9 | +# custom_dir: overrides | 
|  | 10 | +# extra_css: | 
|  | 11 | +# - docs/_global/css/global.css | 
|  | 12 | +# - docs/_global/css/global-syntax-highlight.css | 
|  | 13 | +# extra_javascript: | 
|  | 14 | +# - docs/_global/js/global.js | 
|  | 15 | +# hooks: | 
|  | 16 | +# - hooks/local_override.py | 
|  | 17 | +# - hooks/local_override2.py | 
|  | 18 | +# not_in_nav: | 
|  | 19 | +# - gitignore_style/path/to/exclude | 
|  | 20 | +# theme_features: | 
|  | 21 | +# - theme.feature1 | 
|  | 22 | +# - theme.feature2 | 
|  | 23 | +# ``` | 
|  | 24 | + | 
|  | 25 | +import os | 
|  | 26 | + | 
|  | 27 | +from pathspec.gitignore import GitIgnoreSpec | 
|  | 28 | + | 
|  | 29 | +import mkdocs | 
|  | 30 | +from mkdocs.config.defaults import MkDocsConfig | 
|  | 31 | +from mkdocs.config.config_options import (File, FilesystemObject, Hooks, ListOfItems, PathSpec) | 
|  | 32 | +from mkdocs.structure.files import (File as FileStructure, Files) | 
|  | 33 | + | 
|  | 34 | +# Load any local files into mkdocs | 
|  | 35 | +def append_local_files(files: Files, config: MkDocsConfig, local_files: list[str]): | 
|  | 36 | + for local_file_path in local_files: | 
|  | 37 | + local_file = FileStructure( | 
|  | 38 | + path= local_file_path, | 
|  | 39 | + src_dir=config["docs_dir"] + "/../", | 
|  | 40 | + dest_dir=config["site_dir"], | 
|  | 41 | + use_directory_urls=False, | 
|  | 42 | + ) | 
|  | 43 | + | 
|  | 44 | + files.append(local_file) | 
|  | 45 | + | 
|  | 46 | +# Load any override hooks | 
|  | 47 | +def merge_local_hooks(config: MkDocsConfig, hooks: list[str]): | 
|  | 48 | + try: | 
|  | 49 | + paths = ListOfItems(File(exists=True)).validate(hooks) | 
|  | 50 | + except Exception as e: | 
|  | 51 | + raise e | 
|  | 52 | + | 
|  | 53 | + for name, path in zip(hooks, paths): | 
|  | 54 | + config.plugins[name] = Hooks._load_hook(mkdocs, name, path) | 
|  | 55 | + | 
|  | 56 | +# Handle multiple "not in nav" entries | 
|  | 57 | +# These are of a pathspec.gitignore.GitIgnoreSpec format and need to be converted to a multiline string | 
|  | 58 | +def merge_local_not_in_nav(config: MkDocsConfig, not_in_nav: list[GitIgnoreSpec]): | 
|  | 59 | + nav_str = "\n".join(not_in_nav) | 
|  | 60 | + config["not_in_nav"] += PathSpec().run_validation(nav_str) | 
|  | 61 | + | 
|  | 62 | +# Add additional theme_override folder | 
|  | 63 | +def merge_local_theme_override(config: MkDocsConfig, custom_dir: str): | 
|  | 64 | + try: | 
|  | 65 | + local_override_path = FilesystemObject(exists=True).validate(custom_dir) | 
|  | 66 | + except Exception as e: | 
|  | 67 | + raise e | 
|  | 68 | + | 
|  | 69 | + config.theme.dirs.insert(1, local_override_path) | 
|  | 70 | + | 
|  | 71 | +# Load any override theme features | 
|  | 72 | +def merge_local_theme_features(config: MkDocsConfig, theme_features: list[str]): | 
|  | 73 | + for local_feature in theme_features: | 
|  | 74 | + config.theme["features"].append(local_feature) | 
|  | 75 | + | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +##### MkDocs Event Hooks | 
|  | 79 | + | 
|  | 80 | +def on_files(files: Files, config: MkDocsConfig): | 
|  | 81 | + if "overrides" in config.extra: | 
|  | 82 | + extra_overrides = config.extra["overrides"] | 
|  | 83 | + | 
|  | 84 | + if "extra_css" in extra_overrides: | 
|  | 85 | + extra_css = extra_overrides["extra_css"] | 
|  | 86 | + append_local_files(files, config, extra_css) | 
|  | 87 | + | 
|  | 88 | + if "extra_javascript" in extra_overrides: | 
|  | 89 | + extra_javascript = extra_overrides["extra_javascript"] | 
|  | 90 | + append_local_files(files, config, extra_javascript) | 
|  | 91 | + | 
|  | 92 | +def on_config(config: MkDocsConfig): | 
|  | 93 | + if "overrides" in config.extra: | 
|  | 94 | + extra_overrides = config.extra["overrides"] | 
|  | 95 | + | 
|  | 96 | + # Keep Hooks first | 
|  | 97 | + if "hooks" in extra_overrides: | 
|  | 98 | + hooks = extra_overrides["hooks"] | 
|  | 99 | + merge_local_hooks(config, hooks) | 
|  | 100 | + | 
|  | 101 | + if "custom_dir" in extra_overrides: | 
|  | 102 | + custom_dir = extra_overrides["custom_dir"] | 
|  | 103 | + merge_local_theme_override(config, custom_dir) | 
|  | 104 | + | 
|  | 105 | + if "extra_css" in extra_overrides: | 
|  | 106 | + extra_css = extra_overrides["extra_css"] | 
|  | 107 | + config.extra_css.extend(extra_css) | 
|  | 108 | + | 
|  | 109 | + if "extra_javascript" in extra_overrides: | 
|  | 110 | + extra_javascript = extra_overrides["extra_javascript"] | 
|  | 111 | + config.extra_javascript.extend(extra_javascript) | 
|  | 112 | + | 
|  | 113 | + if "not_in_nav" in extra_overrides: | 
|  | 114 | + not_in_nav = extra_overrides["not_in_nav"] | 
|  | 115 | + merge_local_not_in_nav(config, not_in_nav) | 
|  | 116 | + | 
|  | 117 | + if "theme_features" in extra_overrides: | 
|  | 118 | + theme_features = extra_overrides["theme_features"] | 
|  | 119 | + merge_local_theme_features(config, theme_features) | 
0 commit comments