add latex support

This commit is contained in:
caandt 2024-10-01 00:01:04 -05:00
parent cec4c65edf
commit 3d522f7f24
4 changed files with 40 additions and 5 deletions

4
.gitignore vendored
View file

@ -1,3 +1,5 @@
__pycache__/ __pycache__/
music/ /music/
*.ttf *.ttf
/static/fonts
/static/katex.min.css

View file

@ -4,6 +4,8 @@ OUT=/tmp/2h
FLAGS= FLAGS=
CTF=$(patsubst %.md,$(OUT)/%.html,$(wildcard ctf/**/*.md)) 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) all: $(OUT) $(OUT)/ctf $(CTF)
$(OUT): pages templates static rootstatic build.py static/songlist.json build.py static/highlight.css $(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 static/highlight.css: build.py
python build.py highlight $(FLAGS) 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 server: all
@trap 'kill 0' SIGINT; \ @trap 'kill 0' SIGINT; \
flask run & \ flask run & \

View file

@ -114,19 +114,32 @@ def format_tags(tags):
def md_to_html(path): def md_to_html(path):
import markdown import markdown
from markdown.extensions.codehilite import CodeHiliteExtension from markdown.extensions.codehilite import CodeHiliteExtension
from markdown.extensions.tables import TableExtension
import frontmatter import frontmatter
input = frontmatter.load(path) 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: if minimal:
ext = ['fenced_code', 'toc', TableExtension()] import latex2mathml.converter
def convert(t, _):
return latex2mathml.converter.convert(t)
markdown_katex.wrapper.tex2html = convert
else: 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: if 'toc' in input:
input.content = '[TOC]\n' + input.content input.content = '[TOC]\n' + input.content
html = markdown.markdown(input.content, extensions=ext) html = markdown.markdown(input.content, extensions=ext)
return f'''--- return f'''---
title: {repr(input['title'])} title: {repr(input['title'])}
sheets: ["/static/highlight.css"] sheets: {repr(sheets)}
--- ---
<div class="container"> <div class="container">
<article class="panel"> <article class="panel">

View file

@ -7,6 +7,17 @@ pkgs.mkShell {
python311Packages.mutagen python311Packages.mutagen
python311Packages.flask python311Packages.flask
python311Packages.beautifulsoup4 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 { (python311Packages.buildPythonPackage rec {
pname = "minify-html"; pname = "minify-html";
version = "0.15.0"; version = "0.15.0";
@ -27,5 +38,6 @@ pkgs.mkShell {
python311 python311
nginx nginx
inotify-tools inotify-tools
nodePackages.katex
]; ];
} }