-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender-asciiart-filter.lua
executable file
·120 lines (110 loc) · 3.61 KB
/
render-asciiart-filter.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
CONFIGFILE=os.getenv("ASCIIART_CONFIG") or "render-asciiart-filter.config"
local config = {}
local configfile,err = loadfile(CONFIGFILE, "t", config)
if configfile then
configfile() -- load the config
else
io.stderr:write(err)
end
local outputdir=io.open("rendered","r")
if outputdir~=nil then
io.close(outputdir)
else
os.execute("mkdir rendered")
end
LIBDIR=os.getenv("ASCIIART_LIBDIR") or "lib"
local renderer = {
render_ditaa = function(text, attrs)
if attrs[1] then
attrs = attrs[1][2]
else
attrs = config.ditaa.defaultargs or ""
end
params = {"-jar", LIBDIR .. "/ditaa.jar"}
for w in attrs:gmatch("%S+") do
table.insert(params, w)
end
table.insert(params, "-")
table.insert(params, "-")
-- local param_s=table.concat(params, " ")
-- io.stderr:write("param str: " .. param_s .. "\n")
-- io.stderr:write("param sha1: " .. pandoc.sha1(param_s) .. "\n")
-- return pandoc.pipe("java", params, text)
return {"java", params, text}
end,
render_plantuml = function(text, attrs)
if attrs[1] then
attrs = attrs[1][2]
else
attrs = config.plantuml.defaultargs or ""
end
params = {"-jar", LIBDIR .. "/plantuml.jar", "-tpng", "-p", "-Sbackgroundcolor=transparent"}
for w in attrs:gmatch("%S+") do
table.insert(params, w)
end
return {"java", params, text}
end,
render_dot = function(text, attrs)
if attrs[1] then
attrs = attrs[1][2]
else
attrs = config.dot.defaultargs or ""
end
params = {"-Tpng"}
for w in attrs:gmatch("%S+") do
table.insert(params, w)
end
return {"dot", params, text}
end,
render_qr = function(text, attrs)
if attrs[1] then
attrs = attrs[1][2]
else
attrs = config.qr.defaultargs or ""
end
params = {"-o", "-"}
for w in attrs:gmatch("%S+") do
table.insert(params, w)
end
return {"qrencode", params, text}
end,
}
images = {}
function Pandoc(blocks)
local pfile = io.popen('ls -a rendered/*.png 2> /dev/null')
for fname in pfile:lines() do
if not images[fname] then
io.stderr:write("removing obsolete '" .. fname .. "'\n")
os.remove(fname)
end
end
pfile:close()
return nil
end
function CodeBlock(elem, attr)
for format, render_cmd in pairs(renderer) do
if elem.classes[1] == format then
local filetype = "png"
local mimetype = "image/png"
local cmd=render_cmd(elem.text, elem.attributes or {})
local fname = "rendered/" .. format .. "-" .. pandoc.sha1(cmd[1] .. table.concat(cmd[2], " ") .. cmd[3]) .. "." .. filetype
local data = nil
local f=io.open(fname,"rb")
if f~=nil then
io.stderr:write("cached " .. format .. " rendering found: '" .. fname .. "'\n")
data = f:read("*all")
f:close()
else
io.stderr:write("rendering " .. format .. ": '" .. fname .. "'\n")
data = pandoc.pipe(cmd[1], cmd[2], cmd[3])
local f=io.open(fname, "wb")
f:write(data)
f:close()
end
images[fname] = true
pandoc.mediabag.insert(fname, mimetype, data)
return pandoc.Para{ pandoc.Image({pandoc.Str("rendered")}, fname) }
end
end
end
return {{CodeBlock = CodeBlock}, {Pandoc=Pandoc}}