A little command-line helper for publishing Gemini capsules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gemwriter/util.lua

159 lines
3.2 KiB

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 = { " <author>\n <name></name>\n </author>\n", "" },
subtitle = { "<subtitle></subtitle>\n", "" },
title = { "<title></title>\n", "" },
}
end
local text = str
for e, c in pairs(ents) do
text = string.gsub(text, c[1], c[2])
end
return text
end
function util.make_dir(dir)
os.execute("test -d " .. dir .. " || mkdir -p " .. dir)
end
function util.ls_grep(dir, str)
local ls_cmd = io.popen("ls " .. dir .. " | grep " .. str)
return ls_cmd:read("*a")
end
function util.get_shell_var(str)
local var = os.getenv(str)
if var == "" then
return nil
else
return var
end
end
function util.replace_shell_vars(str)
-- Replace common shell variable paths
if string.find(str, "%$") ~= nil then
local shell_vars = { "HOME", "USER" }
for v = 1, #shell_vars do
str = string.gsub(str, "$" .. shell_vars[v],
util.get_shell_var(shell_vars[v]))
end
end
return str
end
function util.extract_file_date(dir, file)
-- Atom feed date format: 2022-01-15T00:00:00+00:00
local ls_cmd = io.popen("ls -l --time-style=\"+%Y-%m-%dT%H:%M:%S%z\" " ..
dir .. " | grep " .. file .. " | awk '{print $6}'")
return ls_cmd:read("*a")
end
function util.read_file(file)
local fh = io.open(file, "r")
local text = ""
if fh ~= nil then
text = fh:read("*a")
fh:close()
end
return text
end
function util.write_file(file, str)
local fh = io.open(file, "w")
fh:write(str)
fh:close()
end
return util