From c90a167a1b833cd78d8f176003e4ce41e2147ee9 Mon Sep 17 00:00:00 2001 From: Thomas Lambert Date: Sat, 29 Apr 2023 01:03:25 +0200 Subject: [PATCH] refact: templates --- doc/telekasten.txt | 2 +- lua/telekasten.lua | 38 ++++++++++-------------------------- lua/telekasten/templates.lua | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 113f72a..1bd724c 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -915,7 +915,7 @@ The following substitutions will be made during new note creation: | in template | expands to | example | +-----------------+-----------------------+-----------------------------+ | `{{title}}` | the title of the note | My new note | -| `{{shorttitle}} | the short title of the note | dir/dir/My Note -> My Note | +| `{{shorttitle}}` | the short title of the note | dir/dir/My Note -> My Note | | `{{uuid}}` | UUID of the note | 202201271129 | | `{{date}}` | date in iso format | 2021-11-21 | | `{{time24}}` | time with 24 hour clock | 19:12:23 | diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 6491225..3e9343d 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -19,6 +19,7 @@ local tagutils = require("telekasten.utils.tags") local linkutils = require("telekasten.utils.links") local dateutils = require("telekasten.utils.dates") local fileutils = require("telekasten.utils.files") +local templates = require("telekasten.templates") local Path = require("plenary.path") local tkpickers = require("telekasten.pickers") local tkutils = require("telekasten.utils") @@ -426,33 +427,6 @@ end -- end of image stuff -local function linesubst(line, title, dates, uuid) - if dates == nil then - dates = - dateutils.calculate_dates(nil, M.Cfg.calendar_opts.calendar_monday) - end - if uuid == nil then - uuid = "" - end - - local shorttitle = string.match(title, "^.+/(.+)$") - if shorttitle == nil then - shorttitle = title - end - - local substs = vim.tbl_extend("error", dates, { - shorttitle = shorttitle:gsub("^%l", string.upper), - uuid = uuid, - title = title:gsub("^%l", string.upper), - }) - - for k, v in pairs(substs) do - line = line:gsub("{{" .. k .. "}}", v) - end - - return line -end - local function create_note_from_template( title, uuid, @@ -471,7 +445,15 @@ local function create_note_from_template( -- now write the output file, substituting vars line by line local ofile = io.open(filepath, "a") for _, line in pairs(lines) do - ofile:write(linesubst(line, title, calendar_info, uuid) .. "\n") + ofile:write( + templates.subst_templated_values( + line, + title, + calendar_info, + uuid, + M.Cfg.calendar_opts.calendar_monday + ) .. "\n" + ) end ofile:flush() diff --git a/lua/telekasten/templates.lua b/lua/telekasten/templates.lua index e69de29..40d6339 100644 --- a/lua/telekasten/templates.lua +++ b/lua/telekasten/templates.lua @@ -0,0 +1,38 @@ +-- Template handling + +local dateutils = require("telekasten.utils.dates") + +local M = {} + +function M.subst_templated_values(line, title, dates, uuid, calendar_monday) + -- Various date formats + if dates == nil then + dates = dateutils.calculate_dates(nil, calendar_monday) + end + + -- {{uuid}}: note UUID if any + if uuid == nil then + uuid = "" + end + + -- {{shorttitle}}: only filename, not full subdir paths + local shorttitle = string.match(title, "^.+/(.+)$") + if shorttitle == nil then + shorttitle = title + end + + -- Expend the substitutions table and proceed + local substs = vim.tbl_extend("error", dates, { + title = title:gsub("^%l", string.upper), + shorttitle = shorttitle:gsub("^%l", string.upper), + uuid = uuid, + }) + + for k, v in pairs(substs) do + line = line:gsub("{{" .. k .. "}}", v) + end + + return line +end + +return M