about summary refs log tree commit diff stats
path: root/compile.lua
diff options
context:
space:
mode:
authorCase Duckworth2015-03-23 23:36:28 -0700
committerCase Duckworth2015-03-23 23:38:31 -0700
commit14fca7d45604d46d51e135279d3d5873b1f57f68 (patch)
treee5a14f851b5983d2070f57b1064ab48a85bc20b8 /compile.lua
parentAdd makefile (no tests yet) (diff)
downloadautocento-14fca7d45604d46d51e135279d3d5873b1f57f68.tar.gz
autocento-14fca7d45604d46d51e135279d3d5873b1f57f68.zip
Makefile v.01 is alive
Diffstat (limited to 'compile.lua')
-rw-r--r--compile.lua128
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
8function 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
18end
19function 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
25end
26function 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
33end
34
35function 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
52end
53
54function 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, '; '))
63end
64
65function 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
74end
75-- BEGIN MAIN STUFF HERE
76local files = filterout(arg, '^%-')
77if 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)
82end
83local args = filterout(arg, '^[^%-]')
84if not args or #args == 0 or args == {'-all'} then
85 args = {
86 '-html',
87 '-river',
88 '-lozenge',
89 }
90end
91
92if 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'), ".")
100end
101if intable(args, '-river') then
102 print("Compiling RIVER ... ")
103 compile(files, "lua/river.lua", "river", nil, {})
104 move(tabsub(files,'^.*/(.*)%.txt','%1.river'), "river")
105end
106if 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.")
128end