From 3d522f7f2457a56d0508c04e824b81e822e7b7c6 Mon Sep 17 00:00:00 2001 From: caandt Date: Tue, 1 Oct 2024 00:01:04 -0500 Subject: [PATCH] add latex support --- .gitignore | 4 +++- Makefile | 8 ++++++++ build.py | 21 +++++++++++++++++---- shell.nix | 12 ++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 36e99fc..c14e1fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ __pycache__/ -music/ +/music/ *.ttf +/static/fonts +/static/katex.min.css diff --git a/Makefile b/Makefile index f4d8eb3..e4f1696 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ OUT=/tmp/2h FLAGS= CTF=$(patsubst %.md,$(OUT)/%.html,$(wildcard ctf/**/*.md)) +KATEX_PATH=$(shell nix path-info nixpkgs#nodePackages.katex)/lib/node_modules/katex/dist + all: $(OUT) $(OUT)/ctf $(CTF) $(OUT): pages templates static rootstatic build.py static/songlist.json build.py static/highlight.css @@ -23,6 +25,12 @@ static/songlist.json: music build.py static/highlight.css: build.py python build.py highlight $(FLAGS) +static/katex.min.css: + ln -sf "$(KATEX_PATH)"/katex.min.css static/katex.min.css + +static/fonts: + ln -sf "$(KATEX_PATH)"/fonts static/fonts + server: all @trap 'kill 0' SIGINT; \ flask run & \ diff --git a/build.py b/build.py index d3476a0..ba94f1d 100644 --- a/build.py +++ b/build.py @@ -114,19 +114,32 @@ def format_tags(tags): def md_to_html(path): import markdown from markdown.extensions.codehilite import CodeHiliteExtension - from markdown.extensions.tables import TableExtension import frontmatter input = frontmatter.load(path) + import markdown_katex.extension + import markdown_katex.wrapper + ext = ['fenced_code', 'toc', 'tables', 'markdown_katex'] + markdown_katex.extension.KATEX_STYLES = '' + sheets = ['/static/highlight.css'] if minimal: - ext = ['fenced_code', 'toc', TableExtension()] + import latex2mathml.converter + def convert(t, _): + return latex2mathml.converter.convert(t) + markdown_katex.wrapper.tex2html = convert else: - ext = ['fenced_code', CodeHiliteExtension(css_class='hl'), 'toc', TableExtension()] + real = markdown_katex.wrapper.tex2html + def convert(*args): + sheets.append('/static/katex.min.css') + markdown_katex.wrapper.tex2html = real + return real(*args) + markdown_katex.wrapper.tex2html = convert + ext.append(CodeHiliteExtension(css_class='hl')) if 'toc' in input: input.content = '[TOC]\n' + input.content html = markdown.markdown(input.content, extensions=ext) return f'''--- title: {repr(input['title'])} -sheets: ["/static/highlight.css"] +sheets: {repr(sheets)} ---
diff --git a/shell.nix b/shell.nix index 768cd44..600b399 100644 --- a/shell.nix +++ b/shell.nix @@ -7,6 +7,17 @@ pkgs.mkShell { python311Packages.mutagen python311Packages.flask python311Packages.beautifulsoup4 + (python311Packages.buildPythonPackage rec { + pname = "markdown-katex"; + version = "202406.1035"; + src = pkgs.fetchurl { + url = "https://files.pythonhosted.org/packages/f4/34/780748d7d78fb4024dd1a290844374dd0d9830da35593c534638a4e9e54e/markdown_katex-202406.1035-py2.py3-none-any.whl"; + hash = "sha256-wXE+hYVN3stkGtliQ6i2zWc2e/G/jTm0OzaA1/KxiE0="; + }; + format = "wheel"; + doCheck = false; + }) + python311Packages.latex2mathml (python311Packages.buildPythonPackage rec { pname = "minify-html"; version = "0.15.0"; @@ -27,5 +38,6 @@ pkgs.mkShell { python311 nginx inotify-tools + nodePackages.katex ]; }