pandoc.lua

Mon, 20 Apr 2020 12:09:00 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 20 Apr 2020 12:09:00 -0500
changeset 19
d0b9a9c90832
parent 16
e377590e9b19
child 39
5fc5f93a8cad
permissions
-rw-r--r--

Avoid escape in README

16
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 local pandoc={}
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 local io = require('io')
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 local os = require('os')
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 function pandoc.pandoc(str)
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 -- Lua doesn't have bidirectional pipes without the posix library.
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 -- Keep things and compatible and use temporary files.
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 tmpname = os.tmpname()
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 file = io.open(tmpname, 'w')
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 file:write(str)
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 cmd = 'pandoc --from=markdown --to=html "' .. tmpname .. '"'
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 io.stdout:write('Executing ' .. cmd .. '\n')
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 h = io.popen(cmd, 'r')
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 result = h:read("*a")
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 h:close()
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 file:close()
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 return result
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 end
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
e377590e9b19 Support Pandoc rendering
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 return pandoc

mercurial