2020-04-07 15:53:56 -07:00
|
|
|
require "open3"
|
|
|
|
require "nokogiri"
|
2020-12-14 19:16:27 -08:00
|
|
|
require "net/http"
|
|
|
|
require "json"
|
2020-04-07 15:53:56 -07:00
|
|
|
|
2020-12-14 19:16:27 -08:00
|
|
|
def render_cached(cache, display, string, render_comment = nil)
|
2020-05-09 18:14:43 -07:00
|
|
|
cache.fetch(string) do |new|
|
|
|
|
puts " Rendering #{render_comment || new}"
|
2020-12-14 20:48:03 -08:00
|
|
|
res = Net::HTTP.post URI("http://localhost:8000/render"),
|
2020-12-14 19:16:27 -08:00
|
|
|
{ :equations => [ { :str => string, :display => display } ] }.to_json,
|
|
|
|
"Content-Type" => "application/json"
|
|
|
|
cache[string] = JSON.parse(res.body)[0]
|
2020-04-07 15:53:56 -07:00
|
|
|
end
|
2020-05-09 18:14:43 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_katex_sub(inline_cache, display_cache, content)
|
|
|
|
rendered = content.gsub /\\\(((?:[^\\]|\\[^\)])*)\\\)/ do |match|
|
2020-12-14 19:16:27 -08:00
|
|
|
render_cached(inline_cache, false, $~[1])
|
2020-05-09 18:14:43 -07:00
|
|
|
end
|
2020-04-07 15:53:56 -07:00
|
|
|
rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match|
|
2020-12-14 19:16:27 -08:00
|
|
|
render_cached(display_cache, true, $~[1], "display")
|
2020-04-07 15:53:56 -07:00
|
|
|
end
|
|
|
|
return rendered
|
|
|
|
end
|
|
|
|
|
|
|
|
files = ARGV[0..-1]
|
2020-05-09 18:14:43 -07:00
|
|
|
inline_cache, display_cache = {}, {}
|
2020-04-07 15:53:56 -07:00
|
|
|
|
|
|
|
files.each do |file|
|
|
|
|
puts "Rendering file: #{file}"
|
|
|
|
document = Nokogiri::HTML.parse(File.open(file))
|
2020-11-02 00:02:24 -08:00
|
|
|
document.search('//*[not(ancestor-or-self::code)]/text()').each do |t|
|
2020-05-09 18:14:43 -07:00
|
|
|
t.replace(perform_katex_sub(inline_cache, display_cache, t.content))
|
2020-04-07 15:53:56 -07:00
|
|
|
end
|
2020-12-30 18:29:29 -08:00
|
|
|
File.write(file, document.to_html(encoding: 'UTF-8'))
|
2020-04-07 15:53:56 -07:00
|
|
|
end
|