Intermediate commit on new rendering rework.

This commit is contained in:
Danila Fedorin 2020-12-14 19:16:27 -08:00
parent 07f0596f6e
commit fbc0d85159
5 changed files with 31 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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";

12
blog/katexserver.js Normal file
View File

@ -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);

View File

@ -1,3 +1,3 @@
[
"katex"
"katex", "express", "body-parser"
]