Reformat lua code with lua-format

This commit is contained in:
Marty Oehme 2020-10-29 19:00:23 +01:00
parent a365cef6c7
commit e5afcb5e15
Signed by: Marty
GPG key ID: B7538B8F50A1C800
4 changed files with 236 additions and 203 deletions

View file

@ -1,14 +1,17 @@
local ZK = {} local ZK = {}
local ls = require'zettelkasten.list' local ls = require 'zettelkasten.list'
function ZK.init(vimapi) function ZK.init(vimapi)
vim = vimapi or vim vim = vimapi or vim
ZK.options = { ZK.options = {
anchor_separator = vim.g["zettel_anchor_separator"] or vim.b["zettel_anchor_separator"] or "_", anchor_separator = vim.g["zettel_anchor_separator"] or
zettel_extension = vim.g["zettel_extension"] or vim.b["zettel_extension"] or ".md", vim.b["zettel_anchor_separator"] or "_",
zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes", 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 end
-- entrypoint for pressing the zettel key when the cursor -- entrypoint for pressing the zettel key when the cursor
@ -16,8 +19,8 @@ end
-- follow it) or over text (it will then turn it into a -- follow it) or over text (it will then turn it into a
-- zettel link) -- zettel link)
function ZK.follow_link() function ZK.follow_link()
assert(false, "NOT IMPLEMENTED") assert(false, "NOT IMPLEMENTED")
return '' return ''
end end
-- Return a valid zettelkasten anchor, -- Return a valid zettelkasten anchor,
@ -27,41 +30,41 @@ end
-- and a minute key. Returns nil if the date passed in is invalid. -- and a minute key. Returns nil if the date passed in is invalid.
-- If no date is passed in, returns Zettel anchor for current moment. -- If no date is passed in, returns Zettel anchor for current moment.
function ZK.create_anchor(date) function ZK.create_anchor(date)
local timestamp local timestamp
if pcall(function() timestamp=os.time(date) end) then if pcall(function() timestamp = os.time(date) end) then
return os.date('%y%m%d%H%M', timestamp) return os.date('%y%m%d%H%M', timestamp)
else else
return nil return nil
end end
end end
-- Returns a link to a markdown file with the date replaced with a zettel anchor, -- Returns a link to a markdown file with the date replaced with a zettel anchor,
-- and the text cleaned up to be useful in a link. -- and the text cleaned up to be useful in a link.
function ZK.create_link(text, date) function ZK.create_link(text, date)
text = text or "" text = text or ""
local o = ZK.options local o = ZK.options
if text == "" then if text == "" then
text = "" .. ZK.create_anchor(date) text = "" .. ZK.create_anchor(date)
else else
text = text:lower():gsub(" ", "-") text = text:lower():gsub(" ", "-")
text = "" .. ZK.create_anchor(date) .. o.anchor_separator .. text text = "" .. ZK.create_anchor(date) .. (o.anchor_separator or "_") ..
end text
return text .. (o.zettel_extension or ".md") end
return text .. (o.zettel_extension or ".md")
end end
-- Returns all zettel in path as a -- Returns all zettel in path as a
-- { "anchor" = "path/to/zettel/anchor filename.md" } -- { "anchor" = "path/to/zettel/anchor filename.md" }
-- table. -- table.
-- Recurses into subdirectories if recursive argument is true. -- Recurses into subdirectories if recursive argument is true.
function ZK.get_zettel_list(path, recursive) function ZK.get_zettel_list(path, recursive)
return ls.get_anchors_and_paths(path, recursive or false) return ls.get_anchors_and_paths(path, recursive or false)
end end
return { return {
init = ZK.init, init = ZK.init,
zettel_link_create = ZK.zettel_link_create, zettel_link_create = ZK.zettel_link_create,
create_anchor = ZK.create_anchor, create_anchor = ZK.create_anchor,
create_link = ZK.create_link, create_link = ZK.create_link,
get_zettel_list = ZK.get_zettel_list, get_zettel_list = ZK.get_zettel_list
} }

View file

@ -1,53 +1,62 @@
ZK = require'zettelkasten.init' ZK = require 'zettelkasten.init'
Test_date={ year=2019, month=10, day=29, hour=16, min=45 } Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
describe("Zettelkasten", function() describe("Zettelkasten", function()
before_each(function() before_each(function() ZK.init({g = {}, b = {}}) end)
ZK.init({ g={}, b={} })
end)
describe("anchor creation", function() describe("anchor creation", function()
it("should return zettel anchor from time passed in", function() it("should return zettel anchor from time passed in", function()
assert.same("1910291645", ZK.create_anchor(Test_date)) assert.same("1910291645", ZK.create_anchor(Test_date))
end)
it(
"should return zettel anchor from current moment if no argument passed in",
function()
assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor())
end)
it("should return nil if argument passed in is invalid", function()
assert.is_nil(ZK.create_anchor("My grandmother is lovely."))
end)
end) end)
it("should return zettel anchor from current moment if no argument passed in", function() describe("link creation", function()
assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor()) it(
end) "should return a markdown link with only zettel anchor on no text passed in",
function()
assert.same("1910291645.md", ZK.create_link(nil, Test_date))
end)
it("should return nil if argument passed in is invalid", function() it("should text to link", function()
assert.is_nil(ZK.create_anchor("My grandmother is lovely.")) assert.same("1910291645_isappended.md",
end) ZK.create_link("isappended", Test_date))
end) end)
describe("link creation", function() it("should return lowercased link text", function()
it("should return a markdown link with only zettel anchor on no text passed in", function() assert.same("1910291645_yesiamshouting.md",
assert.same("1910291645.md", ZK.create_link(nil, Test_date)) ZK.create_link("YESIAMSHOUTING", Test_date))
end) end)
it("should text to link", function() it("should return spaces in text replaced with dashes", function()
assert.same("1910291645_isappended.md", ZK.create_link("isappended", Test_date)) assert.same("1910291645_yes-indeed-a-space.md",
end) ZK.create_link("yes indeed a space", Test_date))
end)
it("should return lowercased link text", function() it(
assert.same("1910291645_yesiamshouting.md", ZK.create_link("YESIAMSHOUTING", Test_date)) "should place the contents of g:zettel_anchor_separator variable in link",
end) function()
vim = {g = {zettel_anchor_separator = "SEP"}, b = {}}
ZK.init(vim)
assert.same("1910291645SEParated.md",
ZK.create_link("arated", Test_date))
end)
it("should return spaces in text replaced with dashes", function() it("should append the filetype set in g:zettel_extension", function()
assert.same("1910291645_yes-indeed-a-space.md", ZK.create_link("yes indeed a space", Test_date)) vim = {g = {zettel_extension = ".something"}, b = {}}
ZK.init(vim)
assert.same("1910291645_theworld.something",
ZK.create_link("theworld", Test_date))
end)
end) 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 append the filetype set in g:zettel_extension", function()
vim = { g = { zettel_extension = ".something" }, b = {}}
ZK.init(vim)
assert.same("1910291645_theworld.something", ZK.create_link("theworld", Test_date))
end)
end)
end) end)

View file

@ -1,36 +1,32 @@
local ls = {} local ls = {}
local function isDirectory(ftype) local function isDirectory(ftype)
if ftype == 'directory' then return true end if ftype == 'directory' then return true end
return false return false
end end
-- TODO transform paths: -- TODO transform paths:
-- * to absolute value (e.g. ~ to home, scandir needs absolute) -- * to absolute value (e.g. ~ to home, scandir needs absolute)
-- * to ensure / at the end (or no /) gets taken into account -- * to ensure / at the end (or no /) gets taken into account
function ls.get_anchors_and_paths(path, recursive) function ls.get_anchors_and_paths(path, recursive)
-- TODO check for duplicates and warn user -- TODO check for duplicates and warn user
local zettel = {} local zettel = {}
local anchorreg = '^.*/?([%d][%d][%d][%d][%d][%d][%d][%d][%d][%d])[^/]*.md$' local anchorreg = '^.*/?([%d][%d][%d][%d][%d][%d][%d][%d][%d][%d])[^/]*.md$'
local handle = vim.loop.fs_scandir(path) local handle = vim.loop.fs_scandir(path)
while handle do while handle do
local name, ftype = vim.loop.fs_scandir_next(handle) local name, ftype = vim.loop.fs_scandir_next(handle)
if not name then break end if not name then break end
if isDirectory(ftype) and recursive then if isDirectory(ftype) and recursive then
local subdir = ls.get_anchors_and_paths(path .. "/" .. name, true) local subdir = ls.get_anchors_and_paths(path .. "/" .. name, true)
for k, v in pairs(subdir) do for k, v in pairs(subdir) do zettel[tostring(k)] = v end
zettel[tostring(k)] = v end
end
local anchor = string.match(name, anchorreg)
if anchor then zettel[tostring(anchor)] = name end
end end
return zettel
local anchor = string.match(name, anchorreg)
if anchor then
zettel[tostring(anchor)] = name
end
end
return zettel
end end
return ls return ls

View file

@ -1,114 +1,139 @@
ZK = require'zettelkasten.init' ZK = require 'zettelkasten.init'
-- these tests, I suppose, only work on unix due to the file structure -- these tests, I suppose, only work on unix due to the file structure
describe("zettel listing", function() describe("zettel listing", function()
before_each(function() before_each(function()
get_api_mock = function(files) get_api_mock = function(files)
return { return {
g = {}, g = {},
b = {}, b = {},
loop = { loop = {
fs_scandir = function() fs_scandir = function()
if #files == 0 then if #files == 0 then
return false return false
else else
return true return true
end end
end, end,
fs_scandir_next = function() return table.remove(files) end fs_scandir_next = function()
} return table.remove(files)
} end
end }
end) }
it("should return anchor-keyed table pointing to filename of zettel", function()
local file_list = { "1910291645 this-is-a-testfile.md" }
ZK.init(get_api_mock(file_list))
local expected = { ["1910291645"] = "1910291645 this-is-a-testfile.md", }
assert.same(expected, ZK.get_zettel_list("someDir"))
end)
it("should ignore any malformed files", function()
local file_list = {
"2010261208 this-should-be-picked-up.md",
"1910291645 this-is-a-testfile.md",
"this-is-not-a-testfile.md",
"1910271456 this-is-wrong-extension.txt",
"1812 this-is-ignored.md",
}
ZK.init(get_api_mock(file_list))
local expected = {
["1910291645"] = "1910291645 this-is-a-testfile.md",
["2010261208"] = "2010261208 this-should-be-picked-up.md",
}
assert.same(expected, ZK.get_zettel_list("someDir"))
end)
it("should recurse into directories if recursive argument passed in ", function()
local files = {
{ "1910271456 testfile.md", "file" },
{ "more-notes-here", "directory" },
{ "2010261208 another-testfile.md", "file" },
}
local vim_api_mock = {
g = {},
b = {},
loop = mock({
fs_scandir = function()
if #files == 0 then
return false
else
return true
end
end,
fs_scandir_next = function()
if #files == 0 then return nil end
local fname, ftype = unpack(table.remove(files))
return fname, ftype
end
})
}
ZK.init(vim_api_mock)
ZK.get_zettel_list("path/to/startingdir", true)
assert.spy(vim_api_mock.loop.fs_scandir).was_called(2)
assert.spy(vim_api_mock.loop.fs_scandir).was_called_with("path/to/startingdir/more-notes-here")
end)
it("should append all notes found in subdirectories when recursing", function()
local outer_files = { "subdir", "1234567890 myfile.md", "2345678901 another.md", }
local inner_files = { "2222222222 should-be-present.md", "3333333333 should-also-be-present.md" }
local files = outer_files
-- assert.is_true("not implemented")
local vim_api_mock = {
g = {},
b = {},
loop ={
fs_scandir = function()
if #files == 0 then return false end
return true
end,
fs_scandir_next = function()
if #files == 0 then return nil end
local fname, ftype = table.remove(files), 'file'
if fname == "subdir" then
files = inner_files
ftype = 'directory'
end
return fname, ftype
end end
}} end)
ZK.init(vim_api_mock)
local expected = { it("should return anchor-keyed table pointing to filename of zettel",
["1234567890"] = "1234567890 myfile.md", function()
["2345678901"] = "2345678901 another.md", local file_list = {"1910291645 this-is-a-testfile.md"}
["2222222222"] = "2222222222 should-be-present.md", ZK.init(get_api_mock(file_list))
["3333333333"] = "3333333333 should-also-be-present.md",
} local expected = {["1910291645"] = "1910291645 this-is-a-testfile.md"}
assert.same(expected, ZK.get_zettel_list('mydirectory', true)) assert.same(expected, ZK.get_zettel_list("someDir"))
end) end)
it("should ignore any malformed files", function()
local file_list = {
"2010261208 this-should-be-picked-up.md",
"1910291645 this-is-a-testfile.md", "this-is-not-a-testfile.md",
"1910271456 this-is-wrong-extension.txt", "1812 this-is-ignored.md"
}
ZK.init(get_api_mock(file_list))
local expected = {
["1910291645"] = "1910291645 this-is-a-testfile.md",
["2010261208"] = "2010261208 this-should-be-picked-up.md"
}
assert.same(expected, ZK.get_zettel_list("someDir"))
end)
it("should recurse into directories if recursive argument passed in ",
function()
local files = {
{"1910271456 testfile.md", "file"},
{"more-notes-here", "directory"},
{"2010261208 another-testfile.md", "file"}
}
local vim_api_mock = {
g = {},
b = {},
loop = mock({
fs_scandir = function()
if #files == 0 then
return false
else
return true
end
end,
fs_scandir_next = function()
if #files == 0 then return nil end
local fname, ftype = unpack(table.remove(files))
return fname, ftype
end
})
}
ZK.init(vim_api_mock)
ZK.get_zettel_list("path/to/startingdir", true)
assert.spy(vim_api_mock.loop.fs_scandir).was_called(2)
assert.spy(vim_api_mock.loop.fs_scandir).was_called_with(
"path/to/startingdir/more-notes-here")
end)
it("should append all notes found in subdirectories when recursing",
function()
local outer_files = {
"subdir", "1234567890 myfile.md", "2345678901 another.md"
}
local inner_files = {
"2222222222 should-be-present.md",
"3333333333 should-also-be-present.md"
}
local files = outer_files
-- assert.is_true("not implemented")
local vim_api_mock = {
g = {},
b = {},
loop = {
fs_scandir = function()
if #files == 0 then return false end
return true
end,
fs_scandir_next = function()
if #files == 0 then return nil end
local fname, ftype = table.remove(files), 'file'
if fname == "subdir" then
files = inner_files
ftype = 'directory'
end
return fname, ftype
end
}
}
ZK.init(vim_api_mock)
local expected = {
["1234567890"] = "1234567890 myfile.md",
["2345678901"] = "2345678901 another.md",
["2222222222"] = "2222222222 should-be-present.md",
["3333333333"] = "3333333333 should-also-be-present.md"
}
assert.same(expected, ZK.get_zettel_list('mydirectory', true))
end)
it("should adhere to the zettel extension defined in options", function()
local file_list = {"1910291645 myfile.wiki", "2345678901 another.wiki"}
local vim = get_api_mock(file_list)
vim.g['zettel_extension'] = '.wiki'
ZK.init(vim)
local expected = {
["1910291645"] = "1910291645 myfile.wiki",
["2345678901"] = "2345678901 another.wiki"
}
assert.same(expected, ZK.get_zettel_list('mydirectory'))
end)
end) end)