Rewrote a good chunk of mapleconf to use liquid

This commit is contained in:
Alexander Hill
2026-02-02 20:41:52 -05:00
parent bc6e97f04c
commit 57c93ebe31
7 changed files with 72 additions and 162 deletions

View File

@@ -14,6 +14,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. ]]
local liquid = require("liquid")
local posix = require("posix")
local tinytoml = require("tinytoml")
@@ -22,150 +23,6 @@ local config_path = "/etc/maple.toml"
local sysroot = "/"
local templates = "/usr/share/mapleconf"
local function tokenize_block(inner)
local buffer = ""
local escaped = false
local quoted = false
local tokens = {}
for char in inner:gmatch(".") do
if escaped then
buffer = buffer .. char
escaped = false
elseif char == "\\" then
if quoted then
buffer = buffer .. char
end
escaped = true
elseif quoted then
buffer = buffer .. char
if char == "\"" then
quoted = false
table.insert(tokens, buffer)
buffer = ""
end
else
if char:match("%s") then
if #buffer > 0 then
table.insert(tokens, buffer)
buffer = ""
end
elseif char == "\"" then
buffer = buffer .. char
quoted = true
else
buffer = buffer .. char
end
end
end
if #buffer > 0 then
table.insert(tokens, buffer)
end
return tokens
end
-- Symbol Reference:
-- {"if", condition, true, false} - Executes true if condition is "truthy" and false otherwise
-- {"literal", string} - Writes `string` to the target
-- {"string", string} - Format and write string to the target
-- {"value", identifier} - Returns the value of the identifier
local function tokenize_template(raw)
local current = 0
local last = 0
local literal = true
local template = {}
while current do
current = string.find(raw, "@", current + 1)
if current then
-- TODO: What happens when a line ends with a backslash? ~ahill
if raw:sub(current - 1, current - 1) ~= "\\" then
if literal then
table.insert(template, { "literal", string.sub(raw, last + 1, current - 1) })
literal = false
else
if current - last == 1 then
table.insert(template, { "literal", "@" })
else
local inner = raw:sub(last + 1, current - 1)
if inner:sub(1, 1) ~= "-" then
local tokens = tokenize_block(inner)
print(#tokens)
for _, v in ipairs(tokens) do
print(v)
if v == "nil" then
table.insert(template, { "literal", "" })
elseif v:sub(1, 1) == "\"" and v:sub(-1, -1) == "\"" then
table.insert(template, { "string", v:sub(2, -2) })
else
table.insert(template, {"value", v})
end
end
-- ...
end
end
literal = true
end
last = current
end
else
table.insert(template, { "literal", string.sub(raw, last + 1) })
last = current
end
end
return template
end
local function render_template(source, target)
local raw = source:read("*all")
local result = ""
local template = tokenize_template(raw)
for _, v in ipairs(template) do
if v[1] == "literal" then
result = result .. v[2]
elseif v[1] == "string" then
result = result .. v[2]
elseif v[1] == "value" then
if config[v[2]] then
result = result .. config[v[2]]
else
print("Unable to locate " .. v[2])
return
end
else
print("BUG: Symbol type " .. v[1] .. " was not recognized!")
return
end
end
target:write(result)
end
local function render_directory(stack)
local path
if #stack == 0 then
@@ -200,7 +57,9 @@ local function render_directory(stack)
if template then
local target = io.open(sysroot .. fullpath, "w")
if target then
render_template(template, target)
local context = liquid.InterpreterContext:new(config)
local engine = liquid.Template:parse(template:read("*all"))
target:write(engine:render(context))
target:close()
end
template:close()