about summary refs log tree commit diff stats
path: root/test/compile.lua
blob: 765f348a11d78ccc6402fbc56f3ec984e54b5ef0 (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
#!/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

dir = "/d/Copy/writing/autocento/"

function filterout (list, 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)
    for k,v in pairs(table) do
        if v == item then return k end
    end
    return false
end
function tabsub (table, pattern, replace)
    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)
    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)
    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)
    local output = 'var files=['
    for _,file in pairs(files) do
        output = output .. file:gsub('.*', '"%0",')
    end
    output = output:gsub(',$', ']')
    return output
end

local files = filterout(arg, '^%-')
if not files or #files == 0 then
    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 ... ")
    -- TODO
end