markdown_it.lua

changeset 30
3ac53da03439
parent 21
3c71c525cec2
child 39
5fc5f93a8cad
equal deleted inserted replaced
29:d4b0773f5e06 30:3ac53da03439
1 local markdown_it={} 1 local markdown_it={}
2 2
3 local io = require('io') 3 local io = require('io')
4 local os = require('os') 4 local os = require('os')
5 local path = require('mypath')
6 local lfs = require('lfs')
7
8 function markdown_it.katex_config(env)
9 katex_config = path.join(env.paths.tmpl, "katex_config.json")
10 if lfs.attributes(katex_config) then
11 return katex_config
12 else
13 return nil
14 end
15 end
5 16
6 function markdown_it.markdown(str, env) 17 function markdown_it.markdown(str, env)
7 -- Lua doesn't have bidirectional pipes without the posix library. 18 -- Lua doesn't have bidirectional pipes without the posix library.
8 -- Keep things and compatible and use temporary files. 19 -- Keep things and compatible and use temporary files.
9 tmpname = os.tmpname() 20 tmpname = os.tmpname()
10 file = io.open(tmpname, 'w') 21 file = io.open(tmpname, 'w')
11 file:write(str) 22 file:write(str)
12 cmd = string.format("node '%s/markdown_it.js' '%s'", 23 cmd = string.format("node '%s/markdown_it.js' '%s'",
13 env.paths.lgen_location, 24 env.paths.lgen_location,
14 tmpname) 25 tmpname)
26 -- Provide TEMPLATE_PATH/katex_config.json if it exists
27 katex_config = markdown_it.katex_config(env)
28 if katex_config then
29 cmd = cmd .. " '" .. katex_config .. "'"
30 end
15 io.stdout:write('Executing ' .. cmd .. '\n') 31 io.stdout:write('Executing ' .. cmd .. '\n')
16 h = io.popen(cmd, 'r') 32 h = io.popen(cmd, 'r')
17 result = h:read("*a") 33 result = h:read("*a")
18 h:close() 34 h:close()
19 file:close() 35 file:close()
20 return result 36 return result
21 end 37 end
22 38
39 function markdown_it.add_deps(deps, env)
40 katex_config = markdown_it.katex_config(env)
41 if katex_config then
42 table.insert(deps, katex_config)
43 end
44 end
45
23 return markdown_it 46 return markdown_it

mercurial