Move anchor prepend function to anchor module

This commit is contained in:
Marty Oehme 2020-10-31 11:01:53 +01:00
parent 1742b74d5a
commit 320007e14b
Signed by: Marty
GPG key ID: B7538B8F50A1C800
8 changed files with 83 additions and 67 deletions

View file

@ -1,12 +1,14 @@
local A = {}
local o = require 'zettelkasten.options'
-- Return a valid zettelkasten anchor,
-- composed of yymmddHHMM.
--
-- date can be passed in as a table containing a year, a month, a day, an hour,
-- and a minute key. Returns nil if the date passed in is invalid.
-- If no date is passed in, returns Zettel anchor for current moment.
function A.create_anchor(date)
function A.create(date)
local timestamp
if pcall(function() timestamp = os.time(date) end) then
return os.date('%y%m%d%H%M', timestamp)
@ -15,4 +17,13 @@ function A.create_anchor(date)
end
end
-- TODO think about making clean/anchor/extension function module private
-- Returns the text passed in with the anchor passed in prepended.
function A.prepend(anchor, text)
if not text or text == "" then return anchor end
text = anchor .. o.anchor().separator .. text
return text
end
return A

View file

@ -4,15 +4,38 @@ Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("anchor creation", function()
describe("create", function()
it("should return zettel anchor from time passed in",
function() assert.same("1910291645", A.create_anchor(Test_date)) end)
function() assert.same("1910291645", A.create(Test_date)) end)
it(
"should return zettel anchor from current moment if no argument passed in",
function() assert.same(os.date('%y%m%d%H%M'), A.create_anchor()) end)
function() assert.same(os.date('%y%m%d%H%M'), A.create()) end)
it("should return nil if argument passed in is invalid", function()
assert.is_nil(A.create_anchor("My grandmother is lovely."))
it("should return nil if argument passed in is invalid",
function() assert.is_nil(A.create("My grandmother is lovely.")) end)
end)
describe("prepend", function()
it("should append text to anchor", function()
assert.same("1910291645_isappended",
A.prepend("1910291645", "isappended"))
end)
it("should not add a separator if no text appended",
function() assert.same("1910291645", A.prepend("1910291645", "")) end)
it("should return solely the anchor if no text is passed in",
function() assert.same("1910291645", A.prepend("1910291645", nil)) end)
it("should return solely the anchor if empty text is passed in",
function() assert.same("1910291645", A.prepend("1910291645", "")) end)
it("should add contents of g:zettel_anchor_separator variable to text",
function()
vim.g.zettel_anchor_separator = "SEP"
assert.same("1910291645SEParated", A.prepend("1910291645", "arated"))
end)
it("should add contents of b:zettel_anchor_separator variable to text",
function()
vim.b.zettel_anchor_separator = "---"
assert.same("1910291645---arated", A.prepend("1910291645", "arated"))
end)
end)

View file

@ -12,6 +12,9 @@ function ZK.get_zettel_list(path, recursive)
return ls.get_anchors_and_paths(path, recursive or false, ZK.options)
end
-- Return a valid zettelkasten anchor for the current time,
-- composed of yymmddHHMM.
function ZK.create_anchor() return a.create() end
return {
get_zettel_list = ZK.get_zettel_list

View file

@ -1,5 +1,7 @@
ZK = require 'zettelkasten.init'
describe("Zettelkasten", function()
it("should create an anchor for the current datetime",
function() assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor()) end)
end)

View file

@ -3,19 +3,9 @@ local L = {}
local o = require 'zettelkasten.options'
local a = require 'zettelkasten.anchor'
-- TODO split up into clean/anchor/style functions, make private
-- Returns a link to a markdown file with the anchor replaced with a zettel anchor,
-- Returns the text passed in with the anchor passed in prepended,
function L.prepend_anchor(anchor, text)
if not text or text == "" then return anchor end
text = anchor .. o.anchor().separator .. text
return text
end
-- Returns the text cleaned up to be more useful in a link.
-- Spaces are replaced by dashes and everything is lowercased.
function L.clean(text)
function L.urlify(text)
text = text or ""
return text:lower():gsub(" ", "-")
end
@ -24,10 +14,14 @@ end
-- at the end.
function L.append_extension(text) return text .. o.zettel().extension end
local function check_link_empty(link)
if not link or link == "" then error("Link is not allowed to be empty.") end
local function must_have(content)
if not content or content == "" then
error("Link is not allowed to be empty.")
end
end
-- Returns text with surrounding whitespace trimmed. Returns empty string
-- if only whitespace.
local function trimmed(text)
return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)'
end
@ -35,7 +29,7 @@ end
-- Returns a markdown-compatible transformation of the link and text combination
-- passed in.
function L.style_markdown(link, text)
check_link_empty(link)
must_have(link)
return "[" .. trimmed(text) .. "](" .. link .. ")"
end
@ -43,7 +37,7 @@ end
-- Returns a wikilink-compatible transformation of the link and text combination
-- passed in, adding the text as a pipe if it exists.
function L.style_wiki(link, text)
check_link_empty(link)
must_have(link)
local pipe = ""
text = trimmed(text)
@ -51,16 +45,15 @@ function L.style_wiki(link, text)
return "[[" .. link .. pipe .. "]]"
end
-- Returns the correctly formatted link to a zettel with the anchor passed in.
-- Returns a correctly formatted link to a new zettel.
-- Takes an optional link text which will be added to the link.
-- Takes an optional style according to which the link will be transformed.
function L.create(anchor, text, style)
local link = L.clean(text)
style = style or o.zettel().link_style
if style == "markdown" then
link = L.append_extension(L.prepend_anchor(anchor, link))
return L.style_markdown(link, text)
local link = (a.prepend(anchor, L.urlify(text)))
return L.style_markdown(L.append_extension(link), text)
elseif style == "wiki" then
return L.style_wiki(anchor, text)

View file

@ -4,42 +4,13 @@ Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("prepend_anchor", function()
it("should append text to link", function()
assert.same("1910291645_isappended",
link.prepend_anchor("1910291645", "isappended"))
end)
it("should not add a separator if no text appended", function()
assert.same("1910291645", link.prepend_anchor("1910291645", ""))
end)
it("should return solely the anchor if no text is passed in", function()
assert.same("1910291645", link.prepend_anchor("1910291645", nil))
end)
it("should return solely the anchor if empty text is passed in", function()
assert.same("1910291645", link.prepend_anchor("1910291645", ""))
end)
it("should add contents of g:zettel_anchor_separator variable to link",
function()
vim.g.zettel_anchor_separator = "SEP"
assert.same("1910291645SEParated",
link.prepend_anchor("1910291645", "arated"))
end)
it("should add contents of b:zettel_anchor_separator variable to link",
function()
vim.b.zettel_anchor_separator = "---"
assert.same("1910291645---arated",
link.prepend_anchor("1910291645", "arated"))
end)
end)
describe("clean", function()
describe("urlify", function()
it("should return lowercased link text", function()
assert.same("yesiamshouting", link.clean("YESIAMSHOUTING"))
assert.same("yesiamshouting", link.urlify("YESIAMSHOUTING"))
end)
it("should return spaces in text replaced with dashes", function()
assert.same("yes-indeed-a-space", link.clean("yes indeed a space"))
assert.same("yes-indeed-a-space", link.urlify("yes indeed a space"))
end)
end)