diff options
Diffstat (limited to 'compile.lua')
-rw-r--r-- | compile.lua | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/compile.lua b/compile.lua new file mode 100644 index 0000000..f70cfbc --- /dev/null +++ b/compile.lua | |||
@@ -0,0 +1,98 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | -- A compiler for Autocento of the breakfast table | ||
3 | -- written in Lua (because we can, and because | ||
4 | -- it's good practice for Functional Programming) | ||
5 | -- ((OR WHATEVER YOU CALL IT, GAHD)) | ||
6 | -- vim: fdm=indent | ||
7 | |||
8 | dir = "/d/Copy/writing/autocento/" | ||
9 | |||
10 | function filterout (list, filter) | ||
11 | local output = {} | ||
12 | for _,v in ipairs(list) do | ||
13 | if not string.match(v, filter) then | ||
14 | -- table.insert Y U NO WORK? | ||
15 | output[#output + 1] = v | ||
16 | end | ||
17 | end | ||
18 | return output | ||
19 | end | ||
20 | function intable (table, item) | ||
21 | for k,v in pairs(table) do | ||
22 | if v == item then return k end | ||
23 | end | ||
24 | return false | ||
25 | end | ||
26 | function tabsub (table, pattern, replace) | ||
27 | local output = {} | ||
28 | for k,v in pairs(table) do | ||
29 | output[k] = v:gsub(pattern, replace) | ||
30 | end | ||
31 | return output | ||
32 | end | ||
33 | |||
34 | function compile (files, output_fmt, extension, template, args) | ||
35 | local errors = {} | ||
36 | if template then table.insert(args, 'template="'..template..'"') end | ||
37 | for _, file in pairs(files) do | ||
38 | local pandoc_run = { | ||
39 | 'pandoc', | ||
40 | '-f markdown', | ||
41 | '-t '..output_fmt, | ||
42 | table.concat(tabsub(args, "^", "--"), ' '), | ||
43 | '-o '..file:gsub('^.*/(.-)%.%a+', '%1.'..extension), | ||
44 | file | ||
45 | } | ||
46 | print("Compiling "..file.." to ".. extension) | ||
47 | -- print(table.concat(pandoc_run, ' ')) | ||
48 | os.execute(table.concat(pandoc_run, ' ')) | ||
49 | end | ||
50 | end | ||
51 | |||
52 | function move (files, new_dir) | ||
53 | local exe = {} | ||
54 | for _,file in pairs(files) do | ||
55 | print("Moving "..file.." to "..new_dir.."/ ..") | ||
56 | table.insert(exe, 'mv '..file..' '..new_dir..'/') | ||
57 | end | ||
58 | os.execute(table.concat(exe, ' && ')) | ||
59 | -- print(table.concat(exe, '; ')) | ||
60 | end | ||
61 | |||
62 | function lozenge (files) | ||
63 | local output = 'var files=[' | ||
64 | for _,file in pairs(files) do | ||
65 | output = output .. file:gsub('.*', '"%0",') | ||
66 | end | ||
67 | output = output:gsub(',$', ']') | ||
68 | return output | ||
69 | end | ||
70 | |||
71 | local files = filterout(arg, '^%-') | ||
72 | if not files or #files == 0 then | ||
73 | print("> No file list. WUT?") | ||
74 | os.exit(1) | ||
75 | end | ||
76 | local args = filterout(arg, '^[^%-]') | ||
77 | if not args or #args == 0 or args == {'-all'} then | ||
78 | args = { | ||
79 | '-html', | ||
80 | '-river', | ||
81 | '-lozenge', | ||
82 | } | ||
83 | end | ||
84 | |||
85 | if intable(args, '-html') then | ||
86 | print("Compiling HTML ... ") | ||
87 | compile(files, "html5", "html", ".template.html", { "smart", "mathml" }) | ||
88 | -- move(tabsub(files,'^.*/(.*)%.txt','%1.html'), ".") | ||
89 | end | ||
90 | if intable(args, '-river') then | ||
91 | print("Compiling RIVER ... ") | ||
92 | compile(files, "lua/river.lua", "river", nil, {}) | ||
93 | move(tabsub(files,'^.*/(.*)%.txt','%1.river'), "river") | ||
94 | end | ||
95 | if intable(args, '-lozenge') then | ||
96 | print("Updating lozenge.js with file list ... ") | ||
97 | -- TODO | ||
98 | end | ||