Add anchor regex ceontent as changeable option

This commit is contained in:
Marty Oehme 2020-11-06 15:33:39 +01:00
parent d29d92cc0a
commit 65e3b0bc1c
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 35 additions and 16 deletions

View file

@ -15,7 +15,7 @@ function ls.get_anchors_and_paths(path, recursive, options)
-- TODO check for duplicates and warn user
local zettel = {}
-- TODO let user set as option, at least remove magic var
local anchorreg = '^.*/?([%d][%d][%d][%d][%d][%d][%d][%d][%d][%d])[^/]*%' ..
local anchorreg = '^.*/?(' .. o.anchor().regex .. ')[^/]*%' ..
o.zettel().extension .. '$'
local handle = vim.loop.fs_scandir(path)
@ -39,7 +39,9 @@ end
-- it will use the (recursive) results of the zettel_root directory.
function ls.get_zettel(anchor, all)
-- TODO why is there 'somepath' here?
if not all then all = ls.get_anchors_and_paths('/home/marty/documents/notes') end
if not all then
all = ls.get_anchors_and_paths('/home/marty/documents/notes')
end
return all[anchor]
end

View file

@ -15,7 +15,11 @@ local zettel_defaults = {
}
}
local anchor_defaults = {
separator = {vimname = "zettel_anchor_separator", default = "_"}
separator = {vimname = "zettel_anchor_separator", default = "_"},
regex = {
vimname = "zettel_anchor_regex",
default = '[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]'
}
}
-- remaining options

View file

@ -15,19 +15,6 @@ describe("zettel options", function()
end)
it("should return the default zettel extension if not set in vim",
function() assert.same(".md", opt.zettel().extension) end)
end)
describe("zettel options", function()
it("should return the global anchor separator if set in vim", function()
_G.vim.g.zettel_anchor_separator = "SEPARATE"
assert.same("SEPARATE", opt.anchor().separator)
end)
it("should return the buffer anchor separator if set in vim", function()
_G.vim.b.zettel_anchor_separator = "--"
assert.same("--", opt.anchor().separator)
end)
it("should return the default anchor separator if not set in vim",
function() assert.same("_", opt.anchor().separator) end)
it("should return the global link style if set in vim", function()
_G.vim.g.zettel_link_style = "wiki"
@ -59,3 +46,29 @@ describe("zettel options", function()
assert.is_error(function() opt.zettel() end)
end)
end)
describe("anchor options", function()
it("should return the global anchor separator if set in vim", function()
_G.vim.g.zettel_anchor_separator = "SEPARATE"
assert.same("SEPARATE", opt.anchor().separator)
end)
it("should return the buffer anchor separator if set in vim", function()
_G.vim.b.zettel_anchor_separator = "--"
assert.same("--", opt.anchor().separator)
end)
it("should return the default anchor separator if not set in vim",
function() assert.same("_", opt.anchor().separator) end)
it("should return the anchor regex if set in vim", function()
vim.g.zettel_anchor_regex =
"[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]?[%d]?"
-- allowing both 10- and 12-digit time-based zettel ids
assert.same('[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]?[%d]?',
opt.anchor().regex)
end)
it("should return the default anchor regex if not set in vim", function()
assert.same('[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]',
opt.anchor().regex)
end)
end)