Browse Source

Intermediate commit on new rendering rework.

master
Danila Fedorin 2 years ago
parent
commit
fbc0d85159
  1. 3
      blog/builder.sh
  2. 17
      blog/convert.rb
  3. 7
      blog/default.nix
  4. 12
      blog/katexserver.js
  5. 2
      blog/packages.json

3
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

17
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

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

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

2
blog/packages.json

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

Loading…
Cancel
Save