diff --git a/lua/zettelkasten/init.lua b/lua/zettelkasten/init.lua index 194a75f..b91a443 100644 --- a/lua/zettelkasten/init.lua +++ b/lua/zettelkasten/init.lua @@ -1,18 +1,7 @@ local ZK = {} local ls = require 'zettelkasten.list' - -function ZK.init(vimapi) - vim = vimapi or vim - ZK.options = { - anchor_separator = vim.g["zettel_anchor_separator"] or - vim.b["zettel_anchor_separator"] or "_", - zettel_extension = vim.g["zettel_extension"] or - vim.b["zettel_extension"] or ".md" - -- zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes", - -- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor` - } -end +local o = require 'zettelkasten.options' -- entrypoint for pressing the zettel key when the cursor -- is either on an existing link (it will then @@ -42,15 +31,13 @@ end -- and the text cleaned up to be useful in a link. function ZK.create_link(text, date) text = text or "" - local o = ZK.options if text == "" then text = "" .. ZK.create_anchor(date) else text = text:lower():gsub(" ", "-") - text = "" .. ZK.create_anchor(date) .. (o.anchor_separator or "_") .. - text + text = "" .. ZK.create_anchor(date) .. o.anchor().separator .. text end - return text .. (o.zettel_extension or ".md") + return text .. o.zettel().extension end -- Returns all zettel in path as a @@ -62,7 +49,6 @@ function ZK.get_zettel_list(path, recursive) end return { - init = ZK.init, zettel_link_create = ZK.zettel_link_create, create_anchor = ZK.create_anchor, create_link = ZK.create_link, diff --git a/lua/zettelkasten/init_spec.lua b/lua/zettelkasten/init_spec.lua index acd5c75..1a6bef7 100644 --- a/lua/zettelkasten/init_spec.lua +++ b/lua/zettelkasten/init_spec.lua @@ -2,7 +2,8 @@ ZK = require 'zettelkasten.init' Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45} describe("Zettelkasten", function() - before_each(function() ZK.init({g = {}, b = {}}) end) + before_each(function() _G.vim = {g = {}, b = {}} end) + after_each(function() _G.vim = nil end) describe("anchor creation", function() it("should return zettel anchor from time passed in", function() @@ -42,21 +43,29 @@ describe("Zettelkasten", function() ZK.create_link("yes indeed a space", Test_date)) end) - it( - "should place the contents of g:zettel_anchor_separator variable in link", - function() - vim = {g = {zettel_anchor_separator = "SEP"}, b = {}} - ZK.init(vim) - assert.same("1910291645SEParated.md", - ZK.create_link("arated", Test_date)) - end) + it("should add contents of g:zettel_anchor_separator variable to link", + function() + vim.g.zettel_anchor_separator = "SEP" + assert.same("1910291645SEParated.md", + ZK.create_link("arated", Test_date)) + end) + it("should add contents of b:zettel_anchor_separator variable to link", + function() + vim.b.zettel_anchor_separator = "---" + assert.same("1910291645---arated.md", + ZK.create_link("arated", Test_date)) + end) it("should append the filetype set in g:zettel_extension", function() - vim = {g = {zettel_extension = ".something"}, b = {}} - ZK.init(vim) + vim.g.zettel_extension = ".something" assert.same("1910291645_theworld.something", ZK.create_link("theworld", Test_date)) end) + it("should append the filetype set in b:zettel_extension", function() + vim.b.zettel_extension = ".somethingelse" + assert.same("1910291645_theworld.somethingelse", + ZK.create_link("theworld", Test_date)) + end) end) end) diff --git a/lua/zettelkasten/list.lua b/lua/zettelkasten/list.lua index 056b063..af76209 100644 --- a/lua/zettelkasten/list.lua +++ b/lua/zettelkasten/list.lua @@ -35,8 +35,11 @@ end -- Take a list of zettel as an optional variable, without which -- it will use the (recursive) results of the zettel_root directory. function ls.get_zettel(anchor, all) + if not all then all = ls.get_anchors_and_paths('somepath') end + return all[anchor] end +function ls.open_zettel(anchor, all) end return ls diff --git a/lua/zettelkasten/list_spec.lua b/lua/zettelkasten/list_spec.lua index 11ffd6d..64a64f0 100644 --- a/lua/zettelkasten/list_spec.lua +++ b/lua/zettelkasten/list_spec.lua @@ -1,4 +1,4 @@ -ls = require'zettelkasten.list' +local ls = require 'zettelkasten.list' -- these tests, I suppose, only work on unix due to the file structure local function simple_api_mock(files) @@ -13,20 +13,14 @@ local function simple_api_mock(files) return true end end, - fs_scandir_next = function() - return table.remove(files) - end + fs_scandir_next = function() return table.remove(files) end } } end describe("get_anchors_and_paths", function() - before_each(function() - get_api_mock = simple_api_mock - end) - after_each(function() - _G.vim = nil - end) + before_each(function() get_api_mock = simple_api_mock end) + after_each(function() _G.vim = nil end) it("should return anchor-keyed table pointing to filename of zettel", function() @@ -136,19 +130,37 @@ describe("get_anchors_and_paths", function() ["2345678901"] = "2345678901 another.wiki" } - assert.same(expected, ls.get_anchors_and_paths('mydirectory', false, vim.g)) + assert.same(expected, + ls.get_anchors_and_paths('mydirectory', false, vim.g)) end) + + it("should default to the zettel root dir", + function() pending("not implemented") end) end) describe("get_zettel", function() it("should return the correct zettel by id", function() local file_list = { ["1910291645"] = "1910291645 myfile.md", - ["2345678901"] = "2345678901 another.md", + ["2345678901"] = "2345678901 another.md" } _G.vim = simple_api_mock(file_list) - assert.same("1910291645 myfile.md", ls.get_zettel("1910291645", file_list)) + assert.same("1910291645 myfile.md", + ls.get_zettel("1910291645", file_list)) + end) + + it("should default to the zettel root dir if no list passed in", function() + local file_list = {"1910291645 myfile.wiki", "2345678901 another.wiki"} + _G.vim = simple_api_mock(file_list) + + pending("not implemented") + assert.same("1910291645 myfile.wiki", ls.get_zettel("1910291645")) end) end) + +describe("open_zettel", function() + it("should set the current buffer to the zettel passed in as anchor", + function() pending("not implemented") end) +end) diff --git a/lua/zettelkasten/options.lua b/lua/zettelkasten/options.lua new file mode 100644 index 0000000..f748d74 --- /dev/null +++ b/lua/zettelkasten/options.lua @@ -0,0 +1,24 @@ +local Opt = {} + +-- setting defaults +local ZETTEL_EXTENSION = ".md" +local ANCHOR_SEPARATOR = "_" +-- TODO zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes", +-- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor` + +function Opt.zettel() + local options = {} + options.extension = + vim.g["zettel_extension"] or vim.b["zettel_extension"] or + ZETTEL_EXTENSION + return options +end + +function Opt.anchor() + local options = {} + options.separator = vim.g["zettel_anchor_separator"] or + vim.b["zettel_anchor_separator"] or ANCHOR_SEPARATOR + return options +end + +return Opt diff --git a/lua/zettelkasten/options_spec.lua b/lua/zettelkasten/options_spec.lua new file mode 100644 index 0000000..0ea22bd --- /dev/null +++ b/lua/zettelkasten/options_spec.lua @@ -0,0 +1,31 @@ +local opt = require 'zettelkasten.options' + +before_each(function() _G.vim = {g = {}, b = {}} end) +after_each(function() _G.vim = nil end) + +describe("zettel options", function() + + it("should return the global zettel extension if set in vim", function() + _G.vim.g.zettel_extension = ".myextension" + assert.same(".myextension", opt.zettel().extension) + end) + it("should return the buffer zettel extension if set in vim", function() + _G.vim.b.zettel_extension = ".mybufextension" + assert.same(".mybufextension", opt.zettel().extension) + 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) +end)