Write pygments styles
This commit is contained in:
parent
e0508041a6
commit
27f068a82f
3 changed files with 36 additions and 0 deletions
34
hooks/pygments-styles.py
Normal file
34
hooks/pygments-styles.py
Normal file
|
@ -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"<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
|
||||||
|
))
|
|
@ -16,6 +16,7 @@ hooks:
|
||||||
- hooks/static.py
|
- hooks/static.py
|
||||||
- hooks/redirects.py
|
- hooks/redirects.py
|
||||||
- hooks/globals.py
|
- hooks/globals.py
|
||||||
|
- hooks/pygments-styles.py
|
||||||
|
|
||||||
markdown_extensions:
|
markdown_extensions:
|
||||||
- attr_list
|
- attr_list
|
||||||
|
|
|
@ -3,3 +3,4 @@ jinja2-simple-tags
|
||||||
mkdocs-kroki-plugin
|
mkdocs-kroki-plugin
|
||||||
pymdown-extensions
|
pymdown-extensions
|
||||||
pygments
|
pygments
|
||||||
|
minify-html
|
||||||
|
|
Loading…
Reference in a new issue