-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinsert-examples.lua
More file actions
executable file
·29 lines (25 loc) · 758 Bytes
/
insert-examples.lua
File metadata and controls
executable file
·29 lines (25 loc) · 758 Bytes
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
#!/usr/bin/env lua
if #arg < 2 then return end
local latex = [[
\addcontentsline{toc}{subsection}{%s}
\hypertarget{%s}{}
%s
]]
local function texoutput(n, i)
local c = {}
local f = io.open('examples/'..n..'.tex')
local iter = f:lines()
repeat until iter() == [[\begin{document}]]
for l in iter do
if l == [[\end{document}]] then break else table.insert(c, l) end
end
f:close()
return latex:format(i, n, table.concat(c, '\n')):gsub('%%','%%%%')
end
local content, dest = io.open(arg[1]):read('*a'), io.open(arg[2], 'w')
local includecmd = [[\includeexample{([^}]*)}{([^}]*)}]]
for n, i in content:gmatch(includecmd) do
content = content:gsub(includecmd, texoutput(n, i), 1)
end
dest:write(content)
dest:close()