Merge pull request #73 from lambtho12/FlexibleOptionPaths

Feat: Support for absolute or relative path config
This commit is contained in:
Rene Schallner
2022-01-24 09:48:03 +01:00
committed by GitHub
3 changed files with 31 additions and 16 deletions

View File

@@ -225,12 +225,13 @@ require('telekasten').setup({
-- and thus the telekasten syntax will not be loaded either -- and thus the telekasten syntax will not be loaded either
auto_set_filetype = true, auto_set_filetype = true,
-- dir names for special notes (absolute path or subdir name)
dailies = home .. '/' .. 'daily', dailies = home .. '/' .. 'daily',
weeklies = home .. '/' .. 'weekly', weeklies = home .. '/' .. 'weekly',
templates = home .. '/' .. 'templates', templates = home .. '/' .. 'templates',
-- image subdir for pasting -- image (sub)dir for pasting
-- subdir name -- dir name (absolute path or subdir name)
-- or nil if pasted images shouldn't go into a special subdir -- or nil if pasted images shouldn't go into a special subdir
image_subdir = "img", image_subdir = "img",

View File

@@ -158,18 +158,21 @@ telekasten.setup({opts})
*telekasten.settings.daily* *telekasten.settings.daily*
daily: ~ daily: ~
Path to your daily notes, to separate them from 'normal' notes. Path to your daily notes, to separate them from 'normal' notes.
Accepts absolute path or sub-directory name.
Default: '~/zettelkasten/daily' Default: '~/zettelkasten/daily'
*telekasten.settings.weekly* *telekasten.settings.weekly*
weekly: ~ weekly: ~
Path to your weekly notes, to separate them from 'normal' notes. Path to your weekly notes, to separate them from 'normal' notes.
Accepts absolute path or sub-directory name.
Default: '~/zettelkasten/weekly' Default: '~/zettelkasten/weekly'
*telekasten.settings.templates* *telekasten.settings.templates*
templates: ~ templates: ~
Path to your note templates. Path to your note templates.
Accepts absolute path or sub-directory name.
Default: '~/zettelkasten/templates' Default: '~/zettelkasten/templates'
@@ -181,8 +184,9 @@ telekasten.setup({opts})
*telekasten.settings.image_subdir* *telekasten.settings.image_subdir*
image_subdir: ~ image_subdir: ~
Sub-directory where pasted images should go to. Set to `nil` if images Path to the directory where pasted images should go to. Accepts
should not go into a sub-directory. absolute path or sub-directory name.Set to `nil` if images should not
go into a sub-directory.
Default: `nil` Default: `nil`

View File

@@ -39,12 +39,13 @@ M.Cfg = {
-- and thus the telekasten syntax will not be loaded either -- and thus the telekasten syntax will not be loaded either
auto_set_filetype = true, auto_set_filetype = true,
-- dir names for special notes (absolute path or subdir name)
dailies = home .. "/" .. "daily", dailies = home .. "/" .. "daily",
weeklies = home .. "/" .. "weekly", weeklies = home .. "/" .. "weekly",
templates = home .. "/" .. "templates", templates = home .. "/" .. "templates",
-- image subdir for pasting -- image (sub)dir for pasting
-- subdir name -- dir name (absolute path or subdir name)
-- or nil if pasted images shouldn't go into a special subdir -- or nil if pasted images shouldn't go into a special subdir
image_subdir = nil, image_subdir = nil,
@@ -191,12 +192,7 @@ 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.dailies, "dailies")
ret = ret and check_dir_and_ask(M.Cfg.weeklies, "weeklies") 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.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")
return ret return ret
end end
@@ -206,6 +202,13 @@ local function escape(s)
return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1")
end end
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
end
return ret
-- sanitize strings -- sanitize strings
local escape_chars = function(string) local escape_chars = function(string)
return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%/]", { return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%/]", {
@@ -312,14 +315,15 @@ local function imgFromClipboard()
-- 00000090 10 66 d7 01 b1 e4 fb 79 7c f2 2c e7 cc 39 e7 3d |.f.....y|.,..9.=| -- 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 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 local relpath = pngname
if M.Cfg.image_subdir then if M.Cfg.image_subdir then
relpath = M.Cfg.image_subdir .. "/" .. pngname relpath = Path:new(M.Cfg.image_subdir):make_relative(M.Cfg.home)
pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir .. "/"
.. pngname
pngpath = M.Cfg.image_subdir .. "/" .. pngname
end end
pngpath = pngpath .. "/" .. pngname
os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath) os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath)
if file_exists(pngpath) then if file_exists(pngpath) then
@@ -2697,6 +2701,12 @@ local function Setup(cfg)
print("-----------------") print("-----------------")
print(vim.inspect(M.Cfg)) print(vim.inspect(M.Cfg))
end end
-- Convert all directories in full path
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 end
M.find_notes = FindNotes M.find_notes = FindNotes