aboutsummaryrefslogtreecommitdiff
path: root/lua/simple-denote
diff options
context:
space:
mode:
authorDefaultGen <gravel.justness270@slmail.me>2024-06-03 19:20:17 -0400
committerDefaultGen <gravel.justness270@slmail.me>2024-06-03 19:20:17 -0400
commit45c6c2c12f97f91d0550db2aa888f68f68116e9a (patch)
treecd83b3c27d1ef48c8d742ffd6436b04013809997 /lua/simple-denote
parent17b947c5b2c0b67de7d2557f93793fcc284c6674 (diff)
downloadsimple-denote.nvim-45c6c2c12f97f91d0550db2aa888f68f68116e9a.tar.gz
Small refactor
Diffstat (limited to 'lua/simple-denote')
-rw-r--r--lua/simple-denote/internal.lua96
-rw-r--r--lua/simple-denote/util.lua16
2 files changed, 59 insertions, 53 deletions
diff --git a/lua/simple-denote/internal.lua b/lua/simple-denote/internal.lua
index d3c3987..7eabe61 100644
--- a/lua/simple-denote/internal.lua
+++ b/lua/simple-denote/internal.lua
@@ -1,41 +1,50 @@
local M = {}
+local util = require("simple-denote.util")
+
+---@param tags table
+--- Create a_tag_string from tags table
+function M.make_tag_str(tags)
+ if not tags then return "" end
+ local tags_str = ""
+ for i, tag in pairs(tags) do
+ tag = util.plain_format(tag)
+ tags_str = tags_str .. tag
+ if i < #tags then
+ tags_str = tags_str .. "_"
+ end
+ end
+ return "__" .. tags_str:lower()
+end
+
+---@param title heading text
+---@param ext file extension
+function M.set_heading(title, ext)
+ prefix = ""
+ if ext == "md" then
+ prefix = "# "
+ elseif ext == "org" or ext == "norg" then
+ prefix = "* "
+ end
+ vim.api.nvim_buf_set_lines(0, 0, 1, false, {prefix .. title})
+end
---@param options table
---@param name string
---@param tags table|nil
--- Opens your note
function M.note(options, name, tags)
- local path = options.dir
- local file = ""
- local date = os.date("%Y%m%dT%H%M%S")
- local tags_str = ""
+ name = util.trim(name)
local og_name = name
- name = name:gsub("^%s*(.-)%s*$", "%1")
- name = name:gsub("[^%w%s]","")
- name = name:gsub("%s","-")
- if tags then
- for i, tag in pairs(tags) do
- tag = tag:gsub("[^%w%s]","")
- tags_str = tags_str .. tag
- if i < #tags then
- tags_str = tags_str .. "_"
- end
- end
- end
- file = date .. "--" .. name:lower()
- if tags_str ~= "" then
- file = file .. "__" .. tags_str:lower()
+ local tags_str = M.make_tag_str(tags)
+ name = util.plain_format(name)
+ local file = options.dir .. os.date("%Y%m%dT%H%M%S")
+ if name ~= "" then
+ file = file .. "--"
end
- path = path .. file .. "." .. options.ext
- vim.cmd("e " .. path)
- if options.add_heading then
- prefix = ""
- if options.ext == "md" then
- prefix = "# "
- elseif options.ext == "org" or options.ext == "norg" then
- prefix = "* "
- end
- vim.api.nvim_buf_set_lines(0, 0, 1, false, {prefix .. og_name})
+ file = file .. name .. tags_str .. "." .. options.ext
+ vim.cmd("e " .. file)
+ if options.add_heading and og_name ~= "" then
+ M.set_heading(og_name, options.ext)
vim.cmd("norm! o")
end
vim.cmd("startinsert")
@@ -46,28 +55,24 @@ end
---@param new_title string
--- Retitles the filename and changes the first heading of the note
function M.retitle(options, filename, new_title)
- local new_filename = filename:match("^(.*%d%d%d%d%d%d%d%dT%d%d%d%d%d%d).*")
- local tags = filename:match(".*(__.*)%..*")
+ local new_filename = filename:match("^(.*%d%d%d%d%d%d%d%dT%d%d%d%d%d%d).-")
+ local tags_str = filename:match(".*(__.*)%..*")
if new_filename == nil then
error("This file doesn't look like it has a Denote-style name")
end
- new_title = new_title:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace
-
- -- Replace first line with new heading
+ new_title = util.trim(new_title)
if options.retitle_heading and new_title ~= "" then
local first_char = vim.api.nvim_buf_get_lines(0, 0, 1, false)[1]:sub(1, 1)
if (first_char == "#" and options.ext == "md") or
(first_char == "*" and (options.ext == "org" or options.ext == "norg")) then
- vim.api.nvim_buf_set_lines(0, 0, 1, false, {first_char .. " " .. new_title})
+ M.set_heading(new_title, options.ext)
end
end
- new_title = new_title:lower()
- new_title = new_title:gsub("[^%w%s]","")
- new_title = new_title:gsub("%s","-")
+ new_title = util.plain_format(new_title)
if new_title ~= "" then
new_title = "--" .. new_title
end
- new_filename = new_filename .. new_title .. (tags or "") .. "." .. options.ext
+ new_filename = new_filename .. new_title .. (tags_str or "") .. "." .. options.ext
if filename ~= new_filename then
vim.cmd('saveas ' .. new_filename)
vim.cmd('silent !rm ' .. filename)
@@ -79,22 +84,13 @@ end
---@param new_tags table
---Replaces the tags in filename with the tags in new_tags
function M.retag(options, filename, new_tags)
- local tags_str = ""
local new_filename = filename:match("^(.*)__.*$")
if new_filename == nil then
new_filename = filename:match("^(.*)%..*$")
end
- if new_tags then
- for i, tag in pairs(new_tags) do
- tag = tag:gsub("[^%w%s]","")
- tags_str = tags_str .. tag
- if i < #new_tags then
- tags_str = tags_str .. "_"
- end
- end
- end
+ local tags_str = M.make_tag_str(new_tags)
if tags_str ~= "" then
- new_filename = new_filename .. "__" .. tags_str:lower()
+ new_filename = new_filename .. tags_str
end
new_filename = new_filename .. "." .. options.ext
if filename ~= new_filename then
diff --git a/lua/simple-denote/util.lua b/lua/simple-denote/util.lua
index 643e2e7..7c01449 100644
--- a/lua/simple-denote/util.lua
+++ b/lua/simple-denote/util.lua
@@ -24,9 +24,19 @@ function M.splitstring(str, delim)
return items
end
---- Escape a string to use as a lua pattern
-function M.escape(str)
- return str:gsub("(%W)", "%%%1")
+--- Trim whitespace on either end of string
+function M.trim(str)
+ local from = str:match"^%s*()"
+ return from > #str and "" or str:match(".*%S", from)
+end
+
+--- Remove special chars, make lowercase, spaces to dashes
+function M.plain_format(str)
+ str = str:lower()
+ str = str:gsub("[^%w%s]","")
+ str = M.trim(str)
+ str = str:gsub("%s+","-")
+ return str
end
return M