2020-10-29 17:17:00 +00:00
|
|
|
local ls = {}
|
|
|
|
|
2020-11-04 21:27:30 +00:00
|
|
|
local o = require 'zettelkasten.options'
|
|
|
|
|
2020-10-29 17:17:00 +00:00
|
|
|
local function isDirectory(ftype)
|
2020-10-29 18:00:23 +00:00
|
|
|
if ftype == 'directory' then return true end
|
|
|
|
return false
|
2020-10-29 17:17:00 +00:00
|
|
|
end
|
|
|
|
|
2020-11-06 16:28:05 +00:00
|
|
|
local function cleanPath(path)
|
|
|
|
if path:match("^~") then path = os.getenv("HOME") .. path:sub(2) end
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
2020-10-29 18:00:23 +00:00
|
|
|
-- TODO transform paths:
|
|
|
|
-- * to ensure / at the end (or no /) gets taken into account
|
2020-11-06 15:11:24 +00:00
|
|
|
function ls.get_anchors_and_paths(path, recursive)
|
2020-10-29 18:00:23 +00:00
|
|
|
-- TODO check for duplicates and warn user
|
|
|
|
local zettel = {}
|
2020-11-06 14:33:39 +00:00
|
|
|
local anchorreg = '^.*/?(' .. o.anchor().regex .. ')[^/]*%' ..
|
2020-11-04 21:27:30 +00:00
|
|
|
o.zettel().extension .. '$'
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2020-11-06 16:28:05 +00:00
|
|
|
path = cleanPath(path)
|
|
|
|
|
2020-10-29 18:00:23 +00:00
|
|
|
local handle = vim.loop.fs_scandir(path)
|
|
|
|
while handle do
|
|
|
|
local name, ftype = vim.loop.fs_scandir_next(handle)
|
|
|
|
if not name then break end
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2020-10-29 18:00:23 +00:00
|
|
|
if isDirectory(ftype) and recursive then
|
|
|
|
local subdir = ls.get_anchors_and_paths(path .. "/" .. name, true)
|
|
|
|
for k, v in pairs(subdir) do zettel[tostring(k)] = v end
|
|
|
|
end
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2020-10-29 18:00:23 +00:00
|
|
|
local anchor = string.match(name, anchorreg)
|
2020-11-06 16:28:05 +00:00
|
|
|
if anchor then zettel[tostring(anchor)] = path .. "/" .. name end
|
2020-10-29 17:17:00 +00:00
|
|
|
end
|
2020-10-29 18:00:23 +00:00
|
|
|
return zettel
|
2020-10-29 17:17:00 +00:00
|
|
|
end
|
|
|
|
|
2020-10-30 12:48:17 +00:00
|
|
|
-- Returns the path to the zettel defined by the anchor argument.
|
|
|
|
-- 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)
|
2020-11-06 15:35:36 +00:00
|
|
|
all = all or ls.get_anchors_and_paths(o.zettel().rootdir, true)
|
|
|
|
if not all then return end
|
2020-10-30 15:04:23 +00:00
|
|
|
|
2020-10-30 12:48:17 +00:00
|
|
|
return all[anchor]
|
|
|
|
end
|
|
|
|
|
2020-10-29 17:17:00 +00:00
|
|
|
return ls
|