From ea03546662bc8f1c411da39f4d2ad7e247c2c71c Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 20:57:12 +0100 Subject: [PATCH 1/4] Feat: Support for absolute or relative path config --- README.md | 5 +++-- doc/telekasten.txt | 8 ++++++-- lua/telekasten.lua | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 370cd45..14b973f 100644 --- a/README.md +++ b/README.md @@ -225,12 +225,13 @@ require('telekasten').setup({ -- and thus the telekasten syntax will not be loaded either auto_set_filetype = true, + -- dir names for special notes (absolute path or subdir name) dailies = home .. '/' .. 'daily', weeklies = home .. '/' .. 'weekly', templates = home .. '/' .. 'templates', - -- image subdir for pasting - -- subdir name + -- image (sub)dir for pasting + -- dir name (absolute path or subdir name) -- or nil if pasted images shouldn't go into a special subdir image_subdir = "img", diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 65596d2..1ed71f0 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -158,18 +158,21 @@ telekasten.setup({opts}) *telekasten.settings.daily* daily: ~ Path to your daily notes, to separate them from 'normal' notes. + Accepts absolute path or sub-directory name. Default: '~/zettelkasten/daily' *telekasten.settings.weekly* weekly: ~ Path to your weekly notes, to separate them from 'normal' notes. + Accepts absolute path or sub-directory name. Default: '~/zettelkasten/weekly' *telekasten.settings.templates* templates: ~ Path to your note templates. + Accepts absolute path or sub-directory name. Default: '~/zettelkasten/templates' @@ -181,8 +184,9 @@ telekasten.setup({opts}) *telekasten.settings.image_subdir* image_subdir: ~ - Sub-directory where pasted images should go to. Set to `nil` if images - should not go into a sub-directory. + Path to the directory where pasted images should go to. Accepts + absolute path or sub-directory name.Set to `nil` if images should not + go into a sub-directory. Default: `nil` diff --git a/lua/telekasten.lua b/lua/telekasten.lua index efad941..267b86e 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -39,12 +39,13 @@ M.Cfg = { -- and thus the telekasten syntax will not be loaded either auto_set_filetype = true, + -- dir names for special notes (absolute path or subdir name) dailies = home .. "/" .. "daily", weeklies = home .. "/" .. "weekly", templates = home .. "/" .. "templates", - -- image subdir for pasting - -- subdir name + -- image (sub)dir for pasting + -- dir name (absolute path or subdir name) -- or nil if pasted images shouldn't go into a special subdir image_subdir = nil, @@ -188,12 +189,13 @@ local function global_dir_check() ret = ret and check_dir_and_ask(M.Cfg.dailies, "dailies") ret = ret and check_dir_and_ask(M.Cfg.weeklies, "weeklies") ret = ret and check_dir_and_ask(M.Cfg.templates, "templates") + ret = ret and check_dir_and_ask(M.Cfg.image_subdir, "images") - local img_dir = M.Cfg.home - if M.Cfg.image_subdir then - img_dir = img_dir .. "/" .. M.Cfg.image_subdir - end - ret = ret and check_dir_and_ask(img_dir, "image_subdir") + -- local img_dir = M.Cfg.home + -- if M.Cfg.image_subdir then + -- img_dir = img_dir .. "/" .. M.Cfg.image_subdir + -- end + -- ret = ret and check_dir_and_ask(img_dir, "M.Cfg.image_subdir") return ret end @@ -203,6 +205,14 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end +local function make_absolute_path(path) + local ret = path + if not (Path:new(path):is_absolute()) and path ~= nil then + ret = M.Cfg.home .. "/" .. path + end + return ret +end + -- ---------------------------------------------------------------------------- -- image stuff local function imgFromClipboard() @@ -251,10 +261,12 @@ local function imgFromClipboard() local relpath = pngname if M.Cfg.image_subdir then - relpath = M.Cfg.image_subdir .. "/" .. pngname - pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir + relpath = Path:new(M.Cfg.image_subdir):make_relative(M.Cfg.home) + .. "/" + .. pngname + --pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir end - pngpath = pngpath .. "/" .. pngname + pngpath = M.Cfg.image_subdir .. "/" .. pngname os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath) if file_exists(pngpath) then @@ -2562,6 +2574,12 @@ local function Setup(cfg) print("-----------------") print(vim.inspect(M.Cfg)) end + + -- Convert all directories in full path + M.Cfg.image_subdir = make_absolute_path(M.Cfg.image_subdir) + M.Cfg.dailies = make_absolute_path(M.Cfg.dailies) + M.Cfg.weeklies = make_absolute_path(M.Cfg.weeklies) + M.Cfg.templates = make_absolute_path(M.Cfg.templates) end M.find_notes = FindNotes From 2b9f22ff898d34453b4e4050994936e330739954 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 21:01:54 +0100 Subject: [PATCH 2/4] Chore: Remove commented code --- lua/telekasten.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 267b86e..70734af 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -191,12 +191,6 @@ local function global_dir_check() ret = ret and check_dir_and_ask(M.Cfg.templates, "templates") ret = ret and check_dir_and_ask(M.Cfg.image_subdir, "images") - -- local img_dir = M.Cfg.home - -- if M.Cfg.image_subdir then - -- img_dir = img_dir .. "/" .. M.Cfg.image_subdir - -- end - -- ret = ret and check_dir_and_ask(img_dir, "M.Cfg.image_subdir") - return ret end From 5e31b13cfb06658f8f99fda0f7475435d5e3509e Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 21:08:36 +0100 Subject: [PATCH 3/4] Fix: path to save img from clipboard --- lua/telekasten.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 70734af..45007d8 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -251,16 +251,15 @@ local function imgFromClipboard() -- 00000090 10 66 d7 01 b1 e4 fb 79 7c f2 2c e7 cc 39 e7 3d |.f.....y|.,..9.=| local pngname = "pasted_img_" .. os.date("%Y%m%d%H%M%S") .. ".png" - local pngpath = M.Cfg.home + local pngpath = M.Cfg.home .. "/" .. pngname local relpath = pngname if M.Cfg.image_subdir then relpath = Path:new(M.Cfg.image_subdir):make_relative(M.Cfg.home) .. "/" .. pngname - --pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir + pngpath = M.Cfg.image_subdir .. "/" .. pngname end - pngpath = M.Cfg.image_subdir .. "/" .. pngname os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath) if file_exists(pngpath) then From c2084a30c7ba87784f03f806f2b97a6e24a4a297 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Fri, 21 Jan 2022 14:58:38 +0100 Subject: [PATCH 4/4] Refact: better name for make_absolute_path() --- lua/telekasten.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 45007d8..52bbf5d 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -199,7 +199,7 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end -local function make_absolute_path(path) +local function make_config_path_absolute(path) local ret = path if not (Path:new(path):is_absolute()) and path ~= nil then ret = M.Cfg.home .. "/" .. path @@ -2569,10 +2569,10 @@ local function Setup(cfg) end -- Convert all directories in full path - M.Cfg.image_subdir = make_absolute_path(M.Cfg.image_subdir) - M.Cfg.dailies = make_absolute_path(M.Cfg.dailies) - M.Cfg.weeklies = make_absolute_path(M.Cfg.weeklies) - M.Cfg.templates = make_absolute_path(M.Cfg.templates) + M.Cfg.image_subdir = make_config_path_absolute(M.Cfg.image_subdir) + M.Cfg.dailies = make_config_path_absolute(M.Cfg.dailies) + M.Cfg.weeklies = make_config_path_absolute(M.Cfg.weeklies) + M.Cfg.templates = make_config_path_absolute(M.Cfg.templates) end M.find_notes = FindNotes