markdown_it.lua

Sun, 06 Sep 2020 22:12:52 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 06 Sep 2020 22:12:52 +0300
changeset 35
2f927eae429b
parent 30
3ac53da03439
child 39
5fc5f93a8cad
permissions
-rw-r--r--

Don't auto-create directories that will be empty

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

mercurial