2020-10-29 17:17:00 +00:00
|
|
|
local ls = {}
|
2021-04-30 13:07:36 +00:00
|
|
|
-- TODO rename to files.lua? since it's the only module interacting solely w/ the file system
|
2020-10-29 17:17:00 +00:00
|
|
|
|
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
|
|
|
|
|
2021-04-30 13:07:36 +00:00
|
|
|
local function isFile(ftype)
|
|
|
|
if ftype == 'file' then return true end
|
|
|
|
return false
|
|
|
|
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
|
|
|
|
|
2021-04-30 13:07:36 +00:00
|
|
|
-- Returns a set of valid zettels in the form
|
|
|
|
-- { anchor = fullpathname }.
|
|
|
|
-- Takes a (flat) set of files to iterate over in the form that
|
|
|
|
-- get_all_files produces.
|
2020-10-29 18:00:23 +00:00
|
|
|
-- TODO transform paths:
|
|
|
|
-- * to ensure / at the end (or no /) gets taken into account
|
2021-04-30 13:07:36 +00:00
|
|
|
function ls.get_anchors_and_paths(fileset)
|
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
|
|
|
|
2021-04-30 13:07:36 +00:00
|
|
|
for full_path, name in pairs(fileset) do
|
|
|
|
local anchor = string.match(name, anchorreg)
|
|
|
|
if anchor then zettel[tostring(anchor)] = full_path end
|
|
|
|
end
|
|
|
|
return zettel
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns a set of all files at the target directory, as well
|
|
|
|
-- as subdirectories if the recursive argument is set to true.
|
|
|
|
-- Set has the form { "full/path/name.md" = "name.md" }
|
|
|
|
function ls.get_all_files(path, recursive)
|
|
|
|
local f = {}
|
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
|
2021-04-30 13:07:36 +00:00
|
|
|
local subdir = ls.get_all_files(path .. "/" .. name, true)
|
|
|
|
for k, v in pairs(subdir) do f[tostring(k)] = v end
|
2020-10-29 18:00:23 +00:00
|
|
|
end
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2021-04-30 13:07:36 +00:00
|
|
|
if isFile(ftype) then f[tostring(path .. "/" .. name)] = name end
|
2020-10-29 17:17:00 +00:00
|
|
|
end
|
2021-04-30 13:07:36 +00:00
|
|
|
|
|
|
|
return f
|
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.
|
2021-04-30 14:16:13 +00:00
|
|
|
-- Takes a set of files as optional variable.
|
|
|
|
-- If no set provided will use the (recursive) results
|
|
|
|
-- of zettel_root directory.
|
|
|
|
function ls.get_zettel_by_anchor(anchor, files)
|
|
|
|
files = files or ls.get_all_files(o.zettel().rootdir, true)
|
|
|
|
local zettels = ls.get_anchors_and_paths(files)
|
|
|
|
if not zettels then return end
|
2020-10-30 15:04:23 +00:00
|
|
|
|
2021-04-30 14:16:13 +00:00
|
|
|
return zettels[anchor]
|
2020-10-30 12:48:17 +00:00
|
|
|
end
|
|
|
|
|
2021-04-30 14:10:16 +00:00
|
|
|
-- Returns the path to the zettel defined by a reference link.
|
|
|
|
-- Takes a set of files as optional variable in.
|
|
|
|
-- If no set provided will use the (recursive) results
|
|
|
|
-- of zettel_root directory.
|
2021-04-30 13:52:54 +00:00
|
|
|
function ls.get_zettel_by_ref(ref, files)
|
2021-04-30 14:10:16 +00:00
|
|
|
files = files or ls.get_all_files(o.zettel().rootdir, true)
|
|
|
|
|
2021-04-30 14:02:13 +00:00
|
|
|
local name_only_match
|
2021-04-30 13:52:54 +00:00
|
|
|
for full_path, bname in pairs(files) do
|
2021-04-30 14:02:13 +00:00
|
|
|
if full_path == ref then return full_path end
|
|
|
|
if bname == ref then name_only_match = full_path end
|
2021-04-30 13:52:54 +00:00
|
|
|
end
|
2021-04-30 14:02:13 +00:00
|
|
|
return name_only_match
|
2021-04-30 13:52:54 +00:00
|
|
|
end
|
|
|
|
|
2020-10-29 17:17:00 +00:00
|
|
|
return ls
|