From 2364a0dcf6c0db3f22d0123881971a86cf4cc3d8 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 7 Apr 2020 17:52:27 -0700 Subject: [PATCH] Use nokogiri to semantically replace math in HTML --- blog/builder.sh | 14 +++++++++++++- blog/convert.rb | 2 +- blog/default.nix | 9 +++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/blog/builder.sh b/blog/builder.sh index ed97ec5..37c7b79 100644 --- a/blog/builder.sh +++ b/blog/builder.sh @@ -1,3 +1,15 @@ source $stdenv/setup + +# Install KaTeX for back-end rendering. +HOME="." npm install katex + +# Build site with Hugo cp -r $src/* . -hugo --baseUrl="https://danilafe.com" --destination=$out +hugo --baseUrl="http://localhost:5000" + +# Render math in HTML and XML files. +find public/ -regex "public/.*\.html" | xargs ruby $converter + +# Output result +mkdir $out +cp -r public/* $out/ diff --git a/blog/convert.rb b/blog/convert.rb index cd6602e..86461ea 100644 --- a/blog/convert.rb +++ b/blog/convert.rb @@ -11,7 +11,7 @@ def perform_katex_sub(content) end end rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match| - puts " Rendering display #{$~[1][0..30].strip}" + puts " Rendering display." Open3.popen3("$(npm bin)/katex -d") do |i, o, e, t| i.write $~[1] i.close diff --git a/blog/default.nix b/blog/default.nix index 4279979..10d9718 100644 --- a/blog/default.nix +++ b/blog/default.nix @@ -1,9 +1,9 @@ -{ stdenv, hugo, fetchgit }: +{ stdenv, hugo, fetchgit, nodejs, ruby }: let url = "https://dev.danilafe.com/Web-Projects/blog-static.git"; - rev = "b04d82f0b3d949c1d4cd64246cc9c9558241f9ed"; - sha256 = "02w05ff3mb8x3x65822vzg8lil0q6w4rfkj098fa0lwv8wv6x4cy"; + rev = "7f3883fb3994f8ae1a64446a88b729662c9244dc"; + sha256 = "065by3rjg7a43dqdl5ib07c0xi86balb5x2l54jqnlh1hz00bd85"; in stdenv.mkDerivation { name = "blog-static"; @@ -12,5 +12,6 @@ in inherit url rev sha256; }; builder = ./builder.sh; - buildInputs = [ hugo ]; + converter = ./convert.rb; + buildInputs = [ hugo nodejs (ruby.withPackages (ps: [ ps.nokogiri ])) ]; }