From 27f068a82f8aab827ad3ec71927f466631907740 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 14 Sep 2023 21:28:35 +0100 Subject: [PATCH] Write pygments styles --- hooks/pygments-styles.py | 34 ++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + requirements.txt | 1 + 3 files changed, 36 insertions(+) create mode 100644 hooks/pygments-styles.py diff --git a/hooks/pygments-styles.py b/hooks/pygments-styles.py new file mode 100644 index 0000000..d067bd1 --- /dev/null +++ b/hooks/pygments-styles.py @@ -0,0 +1,34 @@ +import tempfile +import shutil +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 + +PYGMENTS_STYLE_DIR = tempfile.mkdtemp() + +shutil.rmtree(PYGMENTS_STYLE_DIR, ignore_errors=True) +os.mkdir(PYGMENTS_STYLE_DIR) + +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 + )) diff --git a/mkdocs.yml b/mkdocs.yml index db3d10b..2c8577a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -16,6 +16,7 @@ hooks: - hooks/static.py - hooks/redirects.py - hooks/globals.py + - hooks/pygments-styles.py markdown_extensions: - attr_list diff --git a/requirements.txt b/requirements.txt index f6fc905..c956d46 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ jinja2-simple-tags mkdocs-kroki-plugin pymdown-extensions pygments +minify-html