34 lines
987 B
Python
34 lines
987 B
Python
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"<style>{styles}</styles>"
|
|
|
|
minified_styles = minify(wrapped_styles, minify_css=True, keep_comments=True)
|
|
|
|
return minified_styles.removeprefix("<style>").removesuffix("</style>")
|
|
|
|
|
|
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
|
|
))
|