diff options
Diffstat (limited to 'compile.lua')
-rw-r--r-- | compile.lua | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/compile.lua b/compile.lua deleted file mode 100644 index c02566e..0000000 --- a/compile.lua +++ /dev/null | |||
@@ -1,128 +0,0 @@ | |||
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 | function filterout (list, filter) | ||
9 | -- Remove elements that match filter | ||
10 | local output = {} | ||
11 | for _,v in ipairs(list) do | ||
12 | if not string.match(v, filter) then | ||
13 | -- table.insert Y U NO WORK? | ||
14 | output[#output + 1] = v | ||
15 | end | ||
16 | end | ||
17 | return output | ||
18 | end | ||
19 | function intable (table, item) | ||
20 | -- Find out if an element's in a table | ||
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 | -- Replace a pattern in all table values | ||
28 | local output = {} | ||
29 | for k,v in pairs(table) do | ||
30 | output[k] = v:gsub(pattern, replace) | ||
31 | end | ||
32 | return output | ||
33 | end | ||
34 | |||
35 | function compile (files, output_fmt, extension, template, args) | ||
36 | -- Run pandoc on file list | ||
37 | local errors = {} | ||
38 | if template then table.insert(args, 'template="'..template..'"') end | ||
39 | for _, file in pairs(files) do | ||
40 | local pandoc_run = { | ||
41 | 'pandoc', | ||
42 | '-f markdown', | ||
43 | '-t '..output_fmt, | ||
44 | table.concat(tabsub(args, "^", "--"), ' '), | ||
45 | '-o '..file:gsub('^.*/(.-)%.%a+', '%1.'..extension), | ||
46 | file | ||
47 | } | ||
48 | print("Compiling "..file.." to ".. extension) | ||
49 | -- print(table.concat(pandoc_run, ' ')) | ||
50 | os.execute(table.concat(pandoc_run, ' ')) | ||
51 | end | ||
52 | end | ||
53 | |||
54 | function move (files, new_dir) | ||
55 | -- Move files to destinations | ||
56 | local exe = {} | ||
57 | for _,file in pairs(files) do | ||
58 | print("Moving "..file.." to "..new_dir.."/ ..") | ||
59 | table.insert(exe, 'mv '..file..' '..new_dir..'/') | ||
60 | end | ||
61 | os.execute(table.concat(exe, ' && ')) | ||
62 | -- print(table.concat(exe, '; ')) | ||
63 | end | ||
64 | |||
65 | function lozenge (files) | ||
66 | -- Update lozenge.js file list | ||
67 | local output = 'var files=[' | ||
68 | for _,file in pairs(files) do | ||
69 | output = output .. file:gsub('.*', '"%0",') | ||
70 | end | ||
71 | output = output:gsub('"",','') | ||
72 | output = output:gsub(',$', ']') | ||
73 | return output | ||
74 | end | ||
75 | -- BEGIN MAIN STUFF HERE | ||
76 | local files = filterout(arg, '^%-') | ||
77 | if not files or #files == 0 then | ||
78 | -- Error: need files to work on! | ||
79 | -- TODO: don't need files if only arg is -lozenge | ||
80 | print("> No file list. WUT?") | ||
81 | os.exit(1) | ||
82 | end | ||
83 | local args = filterout(arg, '^[^%-]') | ||
84 | if not args or #args == 0 or args == {'-all'} then | ||
85 | args = { | ||
86 | '-html', | ||
87 | '-river', | ||
88 | '-lozenge', | ||
89 | } | ||
90 | end | ||
91 | |||
92 | if intable(args, '-html') then | ||
93 | print("Compiling HTML ... ") | ||
94 | compile(files, "html5", "html", ".template.html", { | ||
95 | "smart", | ||
96 | "mathml", | ||
97 | "section-divs", | ||
98 | }) | ||
99 | -- move(tabsub(files,'^.*/(.*)%.txt','%1.html'), ".") | ||
100 | end | ||
101 | if intable(args, '-river') then | ||
102 | print("Compiling RIVER ... ") | ||
103 | compile(files, "lua/river.lua", "river", nil, {}) | ||
104 | move(tabsub(files,'^.*/(.*)%.txt','%1.river'), "river") | ||
105 | end | ||
106 | if intable(args, '-lozenge') then | ||
107 | print("Updating lozenge.js with file list ... ") | ||
108 | local f = assert(io.open("js/lozenge.js", "r")) | ||
109 | local tloz = {} | ||
110 | local HTMLs = io.popen("ls *.html") | ||
111 | local lozfs = {} | ||
112 | for line in HTMLs:lines() do | ||
113 | table.insert(lozfs, line) | ||
114 | end | ||
115 | for line in f:lines() do | ||
116 | if line:find("var files=") then | ||
117 | table.insert(tloz, lozenge(lozfs)) | ||
118 | else | ||
119 | table.insert(tloz, line) | ||
120 | end | ||
121 | end | ||
122 | f:close() | ||
123 | -- And write the file we've just read | ||
124 | local _f = assert(io.open("js/lozenge.js", "w")) | ||
125 | _f:write(table.concat(tloz, "\n")) | ||
126 | _f:close() | ||
127 | print("Done.") | ||
128 | end | ||