2020-10-29 17:17:00 +00:00
|
|
|
local ls = {}
|
|
|
|
|
|
|
|
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-10-29 18:00:23 +00:00
|
|
|
-- TODO transform paths:
|
|
|
|
-- * to absolute value (e.g. ~ to home, scandir needs absolute)
|
|
|
|
-- * to ensure / at the end (or no /) gets taken into account
|
2020-10-29 18:09:13 +00:00
|
|
|
function ls.get_anchors_and_paths(path, recursive, options)
|
|
|
|
options = options or {}
|
2020-10-29 18:00:23 +00:00
|
|
|
-- TODO check for duplicates and warn user
|
|
|
|
local zettel = {}
|
2020-10-29 18:09:13 +00:00
|
|
|
local anchorreg = '^.*/?([%d][%d][%d][%d][%d][%d][%d][%d][%d][%d])[^/]*%' ..
|
|
|
|
(options.zettel_extension or '.md') .. '$'
|
2020-10-29 17:17:00 +00:00
|
|
|
|
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)
|
|
|
|
if anchor then zettel[tostring(anchor)] = 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
|
|
|
|
|
|
|
|
return ls
|