Support loading TEMPLATE_PATH/katex_config.json.

Fri, 17 Jul 2020 10:00:47 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 17 Jul 2020 10:00:47 -0500
changeset 30
3ac53da03439
parent 29
d4b0773f5e06
child 31
7b21ec8ec96a

Support loading TEMPLATE_PATH/katex_config.json.

handlers/render.lua file | annotate | diff | comparison | revisions
markdown_it.js file | annotate | diff | comparison | revisions
markdown_it.lua file | annotate | diff | comparison | revisions
--- a/handlers/render.lua	Mon Jul 06 13:34:06 2020 -0500
+++ b/handlers/render.lua	Fri Jul 17 10:00:47 2020 -0500
@@ -92,13 +92,12 @@
     return newenv
 end
 
-function handlers_render.render(file, env, path_prefix)
+function handlers_render.render(file, env, path_prefix, renderer)
     local data=env.pages[file].data
     if data then
         local newenv=handlers_render.env_for(file, env, path_prefix)
         local data2=handlers_render.process_lua(data, newenv)
         meta = env.pages[file].meta
-        renderer = meta.renderer or "markdown-it"
         if renderer == "markdown-it" then
             return markdown_it.markdown(data2, env)
         elseif renderer == "pandoc" then
@@ -119,6 +118,8 @@
     local dst = path.join(env.paths.dst, page.destination)
     local tmpl = path.join(env.paths.tmpl,
                            page.meta.template or "page.template")
+    renderer = page.meta.renderer or "markdown-it"
+
     local deps = {src}
     
     local build=page.meta.always_build
@@ -131,12 +132,15 @@
             end
         end
         table.insert(deps, tmpl)
+        if renderer=="markdown-it" then
+            markdown_it.add_deps(deps, env)
+        end
         build=dependency.simple_update_check(dst, deps)
     end
     
     if build then
         log.log("Render "..file.."\n")
-        local content=handlers_render.render(file, env, page.meta)
+        local content=handlers_render.render(file, env, page.meta, renderer)
         local page_template=filecache.get(tmpl)
 
         local newenv=table.join({content=content}, handlers_render.env_for(file, env))
--- a/markdown_it.js	Mon Jul 06 13:34:06 2020 -0500
+++ b/markdown_it.js	Fri Jul 17 10:00:47 2020 -0500
@@ -21,10 +21,13 @@
 };
 
 // process.argv[0] seems to be the node executable itself
-if(process.argv.length<=2){
-    console.error(`Usage: ${process.argv[1]} input_file`)
+if(process.argv.length<=2 || process.argv.length>4){
+    console.error(`Usage: ${process.argv[1]} input_file [katex_options.json]`)
     process.exit(1);
 }
+if(process.argv.length>2){
+    katexOptions = JSON.parse(fs.readFileSync(process.argv[3]));
+}
 
 fs.readFile(process.argv[2], 'utf8', function (err, input) {
     var output, md;
--- a/markdown_it.lua	Mon Jul 06 13:34:06 2020 -0500
+++ b/markdown_it.lua	Fri Jul 17 10:00:47 2020 -0500
@@ -2,6 +2,17 @@
 
 local io = require('io')
 local os = require('os')
+local path = require('mypath')
+local lfs = require('lfs')
+
+function markdown_it.katex_config(env)
+    katex_config = path.join(env.paths.tmpl, "katex_config.json")
+    if lfs.attributes(katex_config) then
+        return katex_config
+    else
+        return nil
+    end
+end
 
 function markdown_it.markdown(str, env)
     -- Lua doesn't have bidirectional pipes without the posix library.
@@ -12,6 +23,11 @@
     cmd = string.format("node '%s/markdown_it.js' '%s'",
                         env.paths.lgen_location,
                         tmpname)
+    -- Provide TEMPLATE_PATH/katex_config.json if it exists
+    katex_config = markdown_it.katex_config(env)
+    if katex_config then
+        cmd = cmd .. " '" .. katex_config .. "'"
+    end
     io.stdout:write('Executing ' .. cmd .. '\n')
     h = io.popen(cmd, 'r')
     result = h:read("*a")
@@ -20,4 +36,11 @@
     return result
 end
 
+function markdown_it.add_deps(deps, env)
+    katex_config = markdown_it.katex_config(env)
+    if katex_config then
+        table.insert(deps, katex_config)
+    end
+end
+
 return markdown_it

mercurial