From fbc0d851592ad4ddacd2ebf216caa61fcf9840fd Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 14 Dec 2020 19:16:27 -0800 Subject: [PATCH] Intermediate commit on new rendering rework. --- blog/builder.sh | 3 +++ blog/convert.rb | 17 +++++++++-------- blog/default.nix | 7 ++++++- blog/katexserver.js | 12 ++++++++++++ blog/packages.json | 2 +- 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 blog/katexserver.js diff --git a/blog/builder.sh b/blog/builder.sh index a2054f2..8ab7ac3 100644 --- a/blog/builder.sh +++ b/blog/builder.sh @@ -15,4 +15,7 @@ mkdir $out cp -r public/$publicPath/* $out/ # Render math in HTML and XML files. +node $server & +SERVER_ID=$! find $out/ -regex "$out/.*\.html" | xargs ruby $converter +kill $SERVER_ID diff --git a/blog/convert.rb b/blog/convert.rb index 697dc2d..8d5232b 100644 --- a/blog/convert.rb +++ b/blog/convert.rb @@ -1,23 +1,24 @@ require "open3" require "nokogiri" +require "net/http" +require "json" -def render_cached(cache, command, string, render_comment = nil) +def render_cached(cache, display, string, render_comment = nil) cache.fetch(string) do |new| puts " Rendering #{render_comment || new}" - cache[string] = Open3.popen3(command) do |i, o, e, t| - i.write new - i.close - o.read.force_encoding(Encoding::UTF_8).strip - end + res = Net::HTTP.post URI("http://localhost:3000/render"), + { :equations => [ { :str => string, :display => display } ] }.to_json, + "Content-Type" => "application/json" + cache[string] = JSON.parse(res.body)[0] end end def perform_katex_sub(inline_cache, display_cache, content) rendered = content.gsub /\\\(((?:[^\\]|\\[^\)])*)\\\)/ do |match| - render_cached(inline_cache, "katex", $~[1]) + render_cached(inline_cache, false, $~[1]) end rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match| - render_cached(display_cache, "katex -d", $~[1], "display") + render_cached(display_cache, true, $~[1], "display") end return rendered end diff --git a/blog/default.nix b/blog/default.nix index dfe5a4d..bb3da43 100644 --- a/blog/default.nix +++ b/blog/default.nix @@ -21,7 +21,12 @@ let extraFlags = if settings.drafts then " -D " else ""; builder = ./builder.sh; converter = ./convert.rb; - buildInputs = [ hugo requiredPackages.katex (ruby.withPackages (ps: [ ps.nokogiri ])) ]; + server = ./katexserver.js; + buildInputs = [ + hugo + requiredPackages.katex requiredPackages.express requiredPackages.bodyParser + (ruby.withPackages (ps: [ ps.nokogiri ])) + ]; }; rev = "b921ddfc8de1282cc82a0d90b2927bf2a5c0ee68"; sha256 = "1gg7dq9y1hg14qjlj2w1rn0xjgqw58s8kpd5x6c0wfpwszcm87ji"; diff --git a/blog/katexserver.js b/blog/katexserver.js new file mode 100644 index 0000000..c24814f --- /dev/null +++ b/blog/katexserver.js @@ -0,0 +1,12 @@ +const katex = require('katex'); +const express = require('express'); +const bodyParser = require('body-parser'); + +app = express(); +app.use(bodyParser.json()); +app.post('/render', (req, res) => { + console.log(req.body) + res.send(req.body.equations.map(eq => + katex.renderToString(eq.str, { throwOnError: false, displayMode: eq.display }))); +}); +app.listen(3000); diff --git a/blog/packages.json b/blog/packages.json index c6b288f..e3fd054 100644 --- a/blog/packages.json +++ b/blog/packages.json @@ -1,3 +1,3 @@ [ - "katex" + "katex", "express", "body-parser" ]