38 lines
1017 B
Lua
38 lines
1017 B
Lua
local M = {}
|
|
|
|
local registers = {
|
|
project_name = "__PROJECT_NAME__",
|
|
company_name = "__COMPANY_NAME__",
|
|
file_name = "__FILE_NAME__",
|
|
}
|
|
|
|
---@param kind_path string Filetype of template
|
|
---@param kind string Template in language table
|
|
---@param context table<string, string> A string to replace temp values in template
|
|
---@return string Content Formatted string
|
|
function M.fetch(kind_path, kind, context)
|
|
kind, context = kind or "", context or {}
|
|
if not kind or kind == "" then
|
|
return ""
|
|
end
|
|
local ok, mod = pcall(require, kind_path)
|
|
if not ok or type(mod) ~= "table" or not mod[kind] then
|
|
return ""
|
|
end
|
|
|
|
local template = mod[kind] or ""
|
|
context = context or {}
|
|
|
|
if ok and type(mod) == "table" then
|
|
for key, placeholder in pairs(registers) do
|
|
local value = context[key]
|
|
if value then
|
|
template = string.gsub(template, placeholder, value)
|
|
end
|
|
end
|
|
end
|
|
return template
|
|
end
|
|
|
|
return M
|