import tempfile import os from pygments.formatters.html import HtmlFormatter from mkdocs.structure.files import File from minify_html import minify from mkdocs.utils import write_file, clean_directory PYGMENTS_STYLE_DIR = tempfile.mkdtemp() def on_post_build(config): clean_directory(PYGMENTS_STYLE_DIR) on_build_error = on_pre_build = on_post_build def minify_css(styles: str) -> str: wrapped_styles = f"") def on_files(files, config): style_file = os.path.join(PYGMENTS_STYLE_DIR, "pygments.css") style = minify_css(HtmlFormatter(style="monokai").get_style_defs()) write_file(style.encode(), style_file) files.append(File( path=os.path.basename(style_file), src_dir=PYGMENTS_STYLE_DIR, dest_dir=os.path.join(config["site_dir"], "assets"), use_directory_urls=False ))