| 1 |
-- builds luahyperbolic.sty from Lua source files |
| 2 |
-- usage: lua build.lua |
| 3 |
|
| 4 |
-------------------------------------------------- |
| 5 |
-- utilities |
| 6 |
-------------------------------------------------- |
| 7 |
|
| 8 |
local function read(path) |
| 9 |
local f = assert(io.open(path, "r")) |
| 10 |
local s = f:read("*a") |
| 11 |
f:close() |
| 12 |
return s |
| 13 |
end |
| 14 |
|
| 15 |
local function write(path, content) |
| 16 |
local f = assert(io.open(path, "w")) |
| 17 |
f:write(content) |
| 18 |
f:close() |
| 19 |
end |
| 20 |
|
| 21 |
local function strip_requires(code) |
| 22 |
return code:gsub("[^\n]*require%s*%b()%s*\n", "") |
| 23 |
end |
| 24 |
|
| 25 |
local function replace_return(code, varname) |
| 26 |
return code:gsub("return%s+m%s*$", varname .. " = m") |
| 27 |
end |
| 28 |
|
| 29 |
local function wrap_module(code) |
| 30 |
return "do\n" .. code .. "\nend\n" |
| 31 |
end |
| 32 |
|
| 33 |
local function process_module(path, name) |
| 34 |
local code = read(path) |
| 35 |
code = strip_requires(code) |
| 36 |
code = replace_return(code, name) |
| 37 |
code = wrap_module(code) |
| 38 |
return code |
| 39 |
end |
| 40 |
|
| 41 |
-------------------------------------------------- |
| 42 |
-- process modules |
| 43 |
-------------------------------------------------- |
| 44 |
|
| 45 |
local complex_code = process_module( |
| 46 |
"complex.lua", |
| 47 |
"complex" |
| 48 |
) |
| 49 |
|
| 50 |
local core_code = process_module( |
| 51 |
"luahyperbolic-core.lua", |
| 52 |
"core" |
| 53 |
) |
| 54 |
|
| 55 |
local tikz_code = process_module( |
| 56 |
"luahyperbolic-tikz.lua", |
| 57 |
"tikz" |
| 58 |
) |
| 59 |
|
| 60 |
local tilings_code = process_module( |
| 61 |
"luahyperbolic-tilings.lua", |
| 62 |
"tilings" |
| 63 |
) |
| 64 |
|
| 65 |
-------------------------------------------------- |
| 66 |
-- API construction |
| 67 |
-------------------------------------------------- |
| 68 |
|
| 69 |
local api_code = [[ |
| 70 |
|
| 71 |
-- public API |
| 72 |
hyper = {} |
| 73 |
|
| 74 |
-- structured access |
| 75 |
hyper.core = core |
| 76 |
hyper.tikz = tikz |
| 77 |
hyper.tilings = tilings |
| 78 |
|
| 79 |
local function merge(dst, src) |
| 80 |
for k,v in pairs(src) do |
| 81 |
dst[k] = v |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
-- flattened API |
| 86 |
merge(hyper, core) |
| 87 |
merge(hyper, tikz) |
| 88 |
merge(hyper, tilings) |
| 89 |
|
| 90 |
]] |
| 91 |
|
| 92 |
-------------------------------------------------- |
| 93 |
-- concatenate Lua code |
| 94 |
-------------------------------------------------- |
| 95 |
|
| 96 |
local timestamp = os.date("%Y/%m/%d %H:%M:%S") |
| 97 |
local today = os.date("%Y/%m/%d") |
| 98 |
|
| 99 |
local all_code = table.concat({ |
| 100 |
|
| 101 |
"-- AUTO-GENERATED FILE", |
| 102 |
"-- DO NOT EDIT", |
| 103 |
"-- TIMESTAMP : " .. timestamp, |
| 104 |
"", |
| 105 |
|
| 106 |
"-- internal modules", |
| 107 |
"local core", |
| 108 |
"local tikz", |
| 109 |
"local tilings", |
| 110 |
"", |
| 111 |
|
| 112 |
complex_code, |
| 113 |
core_code, |
| 114 |
tikz_code, |
| 115 |
tilings_code, |
| 116 |
|
| 117 |
api_code |
| 118 |
|
| 119 |
}, "\n") |
| 120 |
|
| 121 |
-------------------------------------------------- |
| 122 |
-- insert into template and write template |
| 123 |
-------------------------------------------------- |
| 124 |
|
| 125 |
local template = read("luahyperbolic.template.sty") |
| 126 |
|
| 127 |
template = template:gsub("===TIMESTAMPPLACEHOLDER===", timestamp) |
| 128 |
template = template:gsub("===DATEPLACEHOLDER===", today) |
| 129 |
|
| 130 |
local final_sty = template:gsub("%-%- ===LUACODEPLACEHOLDER===", function() |
| 131 |
return all_code |
| 132 |
end) |
| 133 |
|
| 134 |
write("../luahyperbolic.sty", final_sty) |
| 135 |
|
| 136 |
print("luahyperbolic.sty written successfully") |