local util = {}
function util.split_str(str, sep)
local tbl = {}
local n = 0
if sep == nil then sep = "%S+" end
for sub in string.gmatch(str, sep) do
n = n + 1
tbl[n] = sub
end
return tbl
end
function util.split_lines(str)
local tbl = {}
local si = 1
for i = 1, #str do
if string.find(string.sub(str, i, i), "\n") ~= nil then
tbl[#tbl + 1] = string.sub(str, si, i - 1)
si = i + 1
end
end
return tbl
end
function util.extract_str(full_str, find_str, end_str)
local fi1, fi2 = string.find(full_str, find_str, 1)
local ei1, ei2 = string.find(full_str, end_str, fi2)
if end_str == "\n" then
return string.sub(full_str, fi2 + 1, ei1 - 2)
else
return string.sub(full_str, fi2 + 1, ei1 - 1)
end
end
function util.to_bool(str)
local bool = { ["true"] = true, ["false"] = false }
return bool(str)
end
function util.table_keys(tbl)
local keys = {}
local n = 0
for k, v in pairs(tbl) do
n = n + 1
keys[n] = k
end
return keys
end
function util.has_key(tbl, str)
local keys = util.table_keys(tbl)
for k = 1, #keys do
if str == keys[k] then
do return true end
end
end
return false
end
function util.replace_vars(str, vars, vals)
local text = str
for k, v in pairs(vars) do
text = string.gsub(text, v, vals[k])
end
return text
end
function util.replace_feed_entities(str, mode)
-- Pre-processing before replacing feed variables
-- "%" is a special character for string.gsub() and similar functions, and
-- will cause an error if not escaped.
local ents = {
percent = { "%%", "%" },
}
-- Used post-processing of feed variables, remove empty log metadata tags
if mode == "post" then
ents = {
percent = { "%", "%%" },
author = { "