chore(utils): reorganize utils

This commit is contained in:
Thomas Lambert
2023-04-28 22:57:41 +02:00
parent 9d2e4fd1e1
commit 88d9344b41
8 changed files with 2853 additions and 2853 deletions

View File

@@ -0,0 +1,116 @@
local M = {}
local date = require("telekasten.utils.luadate")
--- returns the day of week (1..Monday, ..., 7..Sunday) for Dec, 31st of year
--- see https://webspace.science.uu.nl/~gent0113/calendar/isocalendar.htm
--- see https://en.wikipedia.org/wiki/ISO_week_date
M.dow_for_year = function(year)
return (
year
+ math.floor(year / 4)
- math.floor(year / 100)
+ math.floor(year / 400)
) % 7
end
M.weeks_in_year = function(year)
local d = 0
local dy = M.dow_for_year(year) == 4 -- current year ends Thursday
local dyy = M.dow_for_year(year - 1) == 3 -- previous year ends Wednesday
if dy or dyy then
d = 1
end
return 52 + d
end
M.days_in_year = function(year)
local t = os.time({ year = year, month = 12, day = 31 })
return os.date("*t", t).yday
end
M.date_from_doy = function(year, doy)
local ret = {
year = year,
month = 1,
day = doy,
}
-- january is clear immediately
if doy < 32 then
return ret
end
local dmap = {
[1] = 31,
[2] = 28, -- will be fixed further down
[3] = 31,
[4] = 30,
[5] = 31,
[6] = 30,
[7] = 31,
[8] = 31,
[9] = 30,
[10] = 31,
[11] = 30,
[12] = 31,
}
if M.days_in_year(year) == 366 then
dmap[2] = 29
end
for month, d in pairs(dmap) do
doy = doy - d
if doy < 0 then
ret.day = doy + d
ret.month = month
return ret
end
end
return ret -- unreachable if input values are sane
end
-- the algo on wikipedia seems wrong, so we opt for full-blown luadate
M.isoweek_to_date = function(year, isoweek)
local ret = date(year .. "-W" .. string.format("%02d", isoweek) .. "-1")
return {
year = ret:getyear(),
month = ret:getmonth(),
day = ret:getday(),
}
end
local function check_isoweek(year, isoweek, ydate)
print("*********** KW " .. isoweek .. " " .. year .. ": ")
-- local ret = M.weeknumber_to_date(year, isoweek)
local ret = M.isoweek_to_date(year, isoweek)
local result = ret.year == ydate.year
and ret.month == ydate.month
and ret.day == ydate.day
print(
ret.year
.. "-"
.. ret.month
.. "-"
.. ret.day
.. " == "
.. ydate.year
.. "-"
.. ydate.month
.. "-"
.. ydate.day
.. " : "
.. tostring(result)
)
end
M.run_tests = function()
print(check_isoweek(2020, 1, { year = 2019, month = 12, day = 30 })) -- 30.12.2019
print(check_isoweek(2020, 52, { year = 2020, month = 12, day = 21 })) -- 21.12.2020
print(check_isoweek(2020, 53, { year = 2020, month = 12, day = 28 })) -- 28.12.2020
print(check_isoweek(2021, 1, { year = 2021, month = 1, day = 4 })) -- 4.1.2020
print(check_isoweek(2021, 52, { year = 2021, month = 12, day = 27 })) -- 27.12.2021
print(check_isoweek(2022, 1, { year = 2022, month = 1, day = 3 })) -- 3.1.2022
end
-- M.run_tests()
return M

View File

@@ -0,0 +1,32 @@
local M = {}
-- strip an extension from a file name, escaping "." properly, eg:
-- strip_extension("path/Filename.md", ".md") -> "path/Filename"
local function strip_extension(str, ext)
return str:gsub("(" .. ext:gsub("%.", "%%.") .. ")$", "")
end
function M.prompt_title(ext, defaultFile, callback)
local canceledStr = "__INPUT_CANCELLED__"
vim.ui.input({
prompt = "Title: ",
cancelreturn = canceledStr,
completion = "file",
default = defaultFile,
}, function(title)
if not title then
title = ""
end
if title == canceledStr then
vim.cmd("echohl WarningMsg")
vim.cmd("echomsg 'Note creation cancelled!'")
vim.cmd("echohl None")
else
title = strip_extension(title, ext)
callback(title)
end
end)
end
return M

View File

@@ -0,0 +1,131 @@
-- local async = require("plenary.async")
local scan = require("plenary.scandir")
local M = {}
local function file_exists(fn, file_list)
return file_list[fn] ~= nil
end
local function resolve_link(title, file_list, subdir_list, opts)
local fexists = false
local filename = title .. opts.extension
filename = filename:gsub("^%./", "") -- strip potential leading ./
if
opts.weeklies
and file_exists(opts.weeklies .. "/" .. filename, file_list)
then
filename = opts.weeklies .. "/" .. filename
fexists = true
end
if
opts.dailies and file_exists(opts.dailies .. "/" .. filename, file_list)
then
filename = opts.dailies .. "/" .. filename
fexists = true
end
if file_exists(opts.home .. "/" .. filename, file_list) then
filename = opts.home .. "/" .. filename
fexists = true
end
if fexists == false then
-- now search for it in all subdirs
local tempfn
for _, folder in pairs(subdir_list) do
tempfn = folder .. "/" .. filename
-- [[testnote]]
if file_exists(tempfn, file_list) then
filename = tempfn
fexists = true
-- print("Found: " .. filename)
break
end
end
end
if fexists == false then
-- default fn for creation
filename = opts.home .. "/" .. filename
end
return fexists, filename
end
-- TODO: cache mtimes and only update if changed
-- The reason we go over all notes in one go, is: backlinks
-- We generate 2 maps: one containing the number of links within a note
-- and a second one containing the number of backlinks to a note
-- Since we're parsing all notes anyway, we can mark linked notes as backlinked from the currently parsed note
M.generate_backlink_map = function(opts)
assert(opts ~= nil, "opts must not be nil")
-- TODO: check for code blocks
-- local in_fenced_code_block = false
-- also watch out for \t tabbed code blocks or ones with leading spaces that don't end up in a - or * list
-- first, find all notes
assert(opts.extension ~= nil, "Error: need extension in opts!")
assert(opts.home ~= nil, "Error: need home dir in opts!")
-- async seems to have lost await and we don't want to enter callback hell, hence we go sync here
local subdir_list = scan.scan_dir(opts.home, { only_dirs = true })
local file_list = {}
-- transform the file list
local _x = scan.scan_dir(opts.home, {
search_pattern = function(entry)
return entry:sub(-#opts.extension) == opts.extension
end,
})
for _, v in pairs(_x) do
file_list[v] = true
end
-- now process all the notes
local link_counts = {}
local backlink_counts = {}
for note_fn, _ in pairs(file_list) do
-- print("processing " .. note_fn .. "...")
-- go over file line by line
for line in io.lines(note_fn) do
for linktitle in line:gmatch("%[%[(.-)%]%]") do
-- strip # from title
linktitle = linktitle:gsub("#.*$", "")
-- now: inc our link count
link_counts[note_fn] = link_counts[note_fn] or 0
link_counts[note_fn] = link_counts[note_fn] + 1
-- and: inc the backlinks of the linked note
local fexists, backlinked_file =
resolve_link(linktitle, file_list, subdir_list, opts)
-- print(
-- "note for link `"
-- .. linktitle
-- .. "` = "
-- .. backlinked_file
-- .. " (exists: "
-- .. tostring(fexists)
-- .. ')'
-- )
if fexists and (note_fn ~= backlinked_file) then
backlink_counts[backlinked_file] = backlink_counts[backlinked_file]
or 0
backlink_counts[backlinked_file] = backlink_counts[backlinked_file]
+ 1
end
end
end
-- check if in comments block
-- find all links in the note and count them
-- add 1 (this note) as back-link to linked note
end
local ret = {
link_counts = link_counts,
backlink_counts = backlink_counts,
}
return ret
end
return M

View File

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,224 @@
local M = {}
M.is_tag_or_link_at = function(line, col, opts)
opts = opts or {}
local initial_col = col
local char
local is_tagline = opts.tag_notation == "yaml-bare"
and line:sub(1, 4) == "tags"
local seen_bracket = false
local seen_parenthesis = false
local seen_hashtag = false
local cannot_be_tag = false
-- Solves [[Link]]
-- at ^
-- In this case we try to move col forward to match the link.
if "[" == line:sub(col, col) then
col = math.max(col + 1, string.len(line))
end
while col >= 1 do
char = line:sub(col, col)
if seen_bracket then
if char == "[" then
return "link", col + 2
end
end
if seen_parenthesis then
-- Media link, currently identified by not link nor tag
if char == "]" then
return nil, nil
end
end
if char == "[" then
seen_bracket = true
elseif char == "(" then
seen_parenthesis = true
end
if is_tagline == true then
if char == " " or char == "\t" or char == "," or char == ":" then
if col ~= initial_col then
return "tag", col + 1
end
end
else
if char == "#" then
seen_hashtag = true
end
-- Tags should have a space before #, if not we are likely in a link
if char == " " and seen_hashtag and opts.tag_notation == "#tag" then
if not cannot_be_tag then
return "tag", col
end
end
if char == ":" and opts.tag_notation == ":tag:" then
if not cannot_be_tag then
return "tag", col
end
end
end
if char == " " or char == "\t" then
cannot_be_tag = true
end
col = col - 1
end
return nil, nil
end
M.get_tag_at = function(line, col, opts)
-- we ignore the rule: no tags begin with a numeric digit
local endcol = col + 1
local pattern = "[%w-_/]"
local char
while endcol <= #line do
char = line:sub(endcol, endcol)
if char:match(pattern) == nil then
if opts.tag_notation == ":tag:" then
if char == ":" then
local tag = line:sub(col, endcol)
return tag
end
else
return line:sub(col, endcol - 1)
end
end
endcol = endcol + 1
end
-- we exhausted the line
return line:sub(col, endcol)
end
-------------
-- testing --
-------------
local function _eval(line, col, opts)
local kind, newcol = M.is_tag_or_link_at(line, col, opts)
return { kind = kind, col = newcol, line = line }
end
local function _print_debug(x, prefix)
prefix = prefix or ""
for k, v in pairs(x) do
print(prefix .. k .. ": " .. tostring(v))
end
end
local function _expect(x, y)
for k, v in pairs(y) do
if x[k] ~= v then
print("expected:")
_print_debug(y, " ")
print("got:")
_print_debug(x, " ")
assert(false)
end
end
end
M._testme = function()
local line = ""
local col = 10
local opts = {}
local tag
local ret
assert(_eval(line, col, opts).kind == nil)
-- #tags
opts.tag_notation = "#tag"
line = "this is a #tag in a line"
-- lets find it
col = 13
_expect(_eval(line, col, opts), { col = 11, kind = "tag" })
ret = _eval(line, col, opts)
tag = M.get_tag_at(line, ret.col, opts)
-- print('tag .. `' .. tag .. '`')
assert(tag == "#tag")
-- lets be in the space after
col = 15
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- lets be in the next word
col = 16
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- lets be in the prev word
col = 9
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- lets change the tag notation but hit the tag col
col = 13
opts.tag_notation = ":tag:"
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- :tags:
opts.tag_notation = ":tag:"
line = "this is a :tag: in a line"
-- lets find it
col = 13
_expect(_eval(line, col, opts), { col = 11, kind = "tag" })
ret = _eval(line, col, opts)
tag = M.get_tag_at(line, ret.col, opts)
assert(tag == ":tag:")
-- lets be in the space after
col = 16
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- lets be in the next word
col = 17
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- lets be in the prev word
col = 9
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- lets change the tag notation but hit the tag col
col = 13
opts.tag_notation = "#tag"
_expect(_eval(line, col, opts), { col = nil, kind = nil })
-- tagline
line = "tags: [ first, second, third]"
opts.tag_notation = "yaml-bare"
col = 13
_expect(_eval(line, col, opts), { col = 9, kind = "tag" })
ret = _eval(line, col, opts)
tag = M.get_tag_at(line, ret.col, opts)
assert(tag == "first")
col = 9
_expect(_eval(line, col, opts), { col = 9, kind = "tag" })
col = 14
_expect(_eval(line, col, opts), { col = nil, kind = nil })
col = 18
_expect(_eval(line, col, opts), { col = 16, kind = "tag" })
--
line = "this is a [[link]] line"
col = 13
_expect(_eval(line, col, opts), { col = 13, kind = "link" })
line = "this is a [[link]] line"
col = 15
_expect(_eval(line, col, opts), { col = 13, kind = "link" })
end
return M

View File

@@ -0,0 +1,170 @@
local Job = require("plenary.job")
local M = {}
local hashtag_re = "(^|\\s|'|\")#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*"
-- PCRE hashtag allows to remove the hex color codes from hastags
local hashtag_re_pcre =
"(^|\\s|'|\")((?!(#[a-fA-F0-9]{3})(\\W|$)|(#[a-fA-F0-9]{6})(\\W|$))#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*)"
local colon_re = "(^|\\s):[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*:"
local yaml_re =
"(^|\\s)tags:\\s*\\[\\s*([a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*(,\\s*)*)*\\s*]"
local function command_find_all_tags(opts)
opts = opts or {}
opts.cwd = opts.cwd or "."
opts.templateDir = opts.templateDir or ""
opts.rg_pcre = opts.rg_pcre or false
-- do not list tags in the template directory
local globArg = ""
if opts.templateDir ~= "" then
globArg = "--glob=!" .. "**/" .. opts.templateDir .. "/*.md"
end
local re = hashtag_re
if opts.tag_notation == ":tag:" then
re = colon_re
end
if opts.tag_notation == "yaml-bare" then
re = yaml_re
end
local rg_args = {
"--vimgrep",
globArg,
"-o",
re,
"--",
opts.cwd,
}
-- PCRE engine allows to remove hex color codes from #hastags
if opts.rg_pcre and (re == hashtag_re) then
re = hashtag_re_pcre
rg_args = {
"--vimgrep",
"--pcre2",
globArg,
"-o",
re,
"--",
opts.cwd,
}
end
return "rg", rg_args
end
-- strips away leading ' or " , then trims whitespace
local function trim(s)
if s:sub(1, 1) == '"' or s:sub(1, 1) == "'" then
s = s:sub(2)
end
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
local function insert_tag(tbl, tag, entry)
entry.t = tag
if tbl[tag] == nil then
tbl[tag] = { entry }
else
tbl[tag][#tbl[tag] + 1] = entry
end
end
local function split(line, sep, n)
local startpos = 0
local endpos
local ret = {}
for _ = 1, n - 1 do
endpos = line:find(sep, startpos + 1)
ret[#ret + 1] = line:sub(startpos + 1, endpos - 1)
startpos = endpos
end
-- now the remainder
ret[n] = line:sub(startpos + 1)
return ret
end
local function yaml_to_tags(line, entry, ret)
local _, startpos = line:find("tags%s*:%s*%[")
local global_end = line:find("%]")
line = line:sub(startpos + 1, global_end)
local i = 1
local j
local prev_i = 1
local tag
while true do
i, j = line:find("%s*(%S*)%s*,", i)
if i == nil then
tag = line:sub(prev_i)
tag = tag:gsub("%s*(%S*)%s*", "%1")
else
tag = line:sub(i, j)
tag = tag:gsub("%s*(%S*)%s*,", "%1")
end
local new_entry = {}
-- strip trailing ]
tag = tag:gsub("]", "")
new_entry.t = tag
new_entry.l = entry.l
new_entry.fn = entry.fn
new_entry.c = startpos + (i or prev_i)
insert_tag(ret, tag, new_entry)
if i == nil then
break
end
i = j + 1
prev_i = i
end
end
local function parse_entry(opts, line, ret)
local s = split(line, ":", 4)
local fn, l, c, t = s[1], s[2], s[3], s[4]
t = trim(t)
local entry = { fn = fn, l = l, c = c }
if opts.tag_notation == "yaml-bare" then
yaml_to_tags(t, entry, ret)
elseif opts.tag_notation == ":tag:" then
insert_tag(ret, t, entry)
else
insert_tag(ret, t, entry)
end
end
M.do_find_all_tags = function(opts)
local cmd, args = command_find_all_tags(opts)
--print(cmd .. " " .. vim.inspect(args))
local ret = {}
local _ = Job:new({
command = cmd,
args = args,
enable_recording = true,
on_exit = function(j, return_val)
if return_val == 0 then
for _, line in pairs(j:result()) do
parse_entry(opts, line, ret)
end
else
print("rg return value: " .. tostring(return_val))
print("stderr: ", vim.inspect(j:stderr_result()))
end
end,
on_stderr = function(err, data, _)
print("error: " .. tostring(err) .. "data: " .. data)
end,
}):sync()
-- print("final results: " .. vim.inspect(ret))
return ret
end
return M