about summary refs log tree commit diff stats
path: root/compile.lua
blob: c02566eb3675032c93e7d4ecaf3f599bb2dae0bc (plain)
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
121
122
123
124
125
126
127
128
#!/usr/bin/env lua
-- A compiler for Autocento of the breakfast table
-- written in Lua (because we can, and because
-- it's good practice for Functional Programming)
-- ((OR WHATEVER YOU CALL IT, GAHD))
-- vim: fdm=indent

function filterout (list, filter)
    -- Remove elements that match filter
    local output = {}
    for _,v in ipairs(list) do
        if not string.match(v, filter) then
            -- table.insert Y U NO WORK?
            output[#output + 1] = v
        end
    end
    return output
end
function intable (table, item)
    -- Find out if an element's in a table
    for k,v in pairs(table) do
        if v == item then return k end
    end
    return false
end
function tabsub (table, pattern, replace)
    -- Replace a pattern in all table values
    local output = {}
    for k,v in pairs(table) do
        output[k] = v:gsub(pattern, replace)
    end
    return output
end

function compile (files, output_fmt, extension, template, args)
    -- Run pandoc on file list
    local errors = {}
    if template then table.insert(args, 'template="'..template..'"') end
    for _, file in pairs(files) do
        local pandoc_run = {
            'pandoc',
            '-f markdown',
            '-t '..output_fmt,
            table.concat(tabsub(args, "^", "--"), ' '),
            '-o '..file:gsub('^.*/(.-)%.%a+', '%1.'..extension),
            file
        }
        print("Compiling "..file.." to ".. extension)
        -- print(table.concat(pandoc_run, ' '))
        os.execute(table.concat(pandoc_run, ' '))
    end
end

function move (files, new_dir)
    -- Move files to destinations
    local exe = {}
    for _,file in pairs(files) do
        print("Moving "..file.." to "..new_dir.."/ ..")
        table.insert(exe, 'mv '..file..' '..new_dir..'/')
    end
    os.execute(table.concat(exe, ' && '))
    -- print(table.concat(exe, '; '))
end

function lozenge (files)
    -- Update lozenge.js file list
    local output = 'var files=['
    for _,file in pairs(files) do
        output = output .. file:gsub('.*', '"%0",')
    end
    output = output:gsub('"",','')
    output = output:gsub(',$', ']')
    return output
end
-- BEGIN MAIN STUFF HERE
local files = filterout(arg, '^%-')
if not files or #files == 0 then
    -- Error: need files to work on!
    -- TODO: don't need files if only arg is -lozenge
    print("> No file list. WUT?")
    os.exit(1)
end
local args = filterout(arg, '^[^%-]')
if not args or #args == 0 or args == {'-all'} then
    args = {
        '-html',
        '-river',
        '-lozenge',
    }
end

if intable(args, '-html') then
    print("Compiling HTML ... ")
    compile(files, "html5", "html", ".template.html", {
        "smart",
        "mathml",
        "section-divs",
    })
    -- move(tabsub(files,'^.*/(.*)%.txt','%1.html'), ".")
end
if intable(args, '-river') then
    print("Compiling RIVER ... ")
    compile(files, "lua/river.lua", "river", nil, {})
    move(tabsub(files,'^.*/(.*)%.txt','%1.river'), "river")
end
if intable(args, '-lozenge') then
    print("Updating lozenge.js with file list ... ")
    local f = assert(io.open("js/lozenge.js", "r"))
    local tloz = {}
    local HTMLs = io.popen("ls *.html")
    local lozfs = {}
    for line in HTMLs:lines() do
        table.insert(lozfs, line)
    end
    for line in f:lines() do
        if line:find("var files=") then
            table.insert(tloz, lozenge(lozfs))
        else
            table.insert(tloz, line)
        end
    end
    f:close()
    -- And write the file we've just read
    local _f = assert(io.open("js/lozenge.js", "w"))
    _f:write(table.concat(tloz, "\n"))
    _f:close()
    print("Done.")
end