Commit 50b0296b authored by Gio's avatar Gio

added utils.expandVars for easier custom config file filling

parent 0a29a268
......@@ -84,4 +84,53 @@ function utils.applyNetTemplate10(template)
return template
end
--! This function is inspired to http://lua-users.org/wiki/VarExpand
--! version: 0.0.1
--! code: Ketmar // Avalon Group
--! licence: public domain
--! expand $var and ${var} in string
--! ${var} can call Lua functions: ${string.rep(' ', 10)}
--! `$' can be screened with `\'
--! `...': args for $<number>
--! if `...' is just a one table -- take it as args
function utils.expandVars(s, ...)
local args = {...}
args = #args == 1 and type(args[1]) == "table" and args[1] or args;
--! return true if there was an expansion
local function DoExpand(iscode)
local was = false
local mask = iscode and "()%$(%b{})" or "()%$([%a%d_]*)"
local drepl = iscode and "\\$" or "\\\\$"
s = s:gsub(mask,
function(pos, code)
if s:sub(pos-1, pos-1) == "\\" then
return "$"..code
else
was = true
local v, err
if iscode then
code = code:sub(2, -2)
else
local n = tonumber(code)
if n then v = args[n] end
end
if not v then
v, err = loadstring("return "..code)
if not v then error(err) end
v = v()
end
if v == nil then v = "" end
v = tostring(v):gsub("%$", drepl)
return v
end
end)
if not (iscode or was) then s = s:gsub("\\%$", "$") end
return was
end
repeat DoExpand(true); until not DoExpand(false)
return s
end
return utils
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment