aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/template.lua
diff options
context:
space:
mode:
authormoxie <moxie@3kgcat.fi>2026-03-14 10:28:08 +0200
committermoxie <moxie@3kgcat.fi>2026-03-14 10:29:54 +0200
commitc8dc1635f8a921269f714117f414bbc7ba24f9fd (patch)
tree336a0bb466a5e023c0a0e6472c9876ff7458de68 /lua/muwiki/template.lua
parentffc0482ab89924cb35155fa82033e3d0ddc6c93d (diff)
refactor: consolidate modules and improve structure
Diffstat (limited to 'lua/muwiki/template.lua')
-rw-r--r--lua/muwiki/template.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/lua/muwiki/template.lua b/lua/muwiki/template.lua
new file mode 100644
index 0000000..d8eca16
--- /dev/null
+++ b/lua/muwiki/template.lua
@@ -0,0 +1,35 @@
+local config = require('muwiki.config')
+local io_module = require('muwiki.io')
+
+local M = {}
+
+local function process_template(template, title)
+ local date_fmt = config.options.date_fmt or '%Y-%m-%d'
+ local date = os.date(date_fmt)
+ local result = template:gsub('${title}', title):gsub('${date}', date)
+
+ if not result:match('\n$') then
+ result = result .. '\n'
+ end
+
+ return result
+end
+
+function M.init_file(bufnr, filepath)
+ if io_module.file_exists(filepath) then
+ return
+ end
+
+ local filename = vim.fs.basename(filepath)
+
+ if config.options.use_template then
+ local title = filename:gsub('%.md$', ''):gsub('_', ' ')
+ local content = process_template(config.options.template, title)
+ local lines = vim.split(content, '\n', { plain = true })
+ vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
+ end
+
+ vim.notify(string.format('%s (unsaved)', filename), vim.log.levels.INFO)
+end
+
+return M