Skip to content

Commit

Permalink
feat(tempus): allow dates to be converted to norg-compatible dates wi…
Browse files Browse the repository at this point in the history
…th `tostring()`
  • Loading branch information
vhyrro committed May 15, 2023
1 parent 5420f65 commit 3ec5f96
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lua/neorg/modules/core/tempus/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,15 @@ module.public = {
local hour, minute, second = vim.re.match(word, time_regex)

if hour and minute then
output.time = {
output.time = setmetatable({
hour = tonumber(hour),
minute = tonumber(minute),
second = second and tonumber(second) or nil,
}
}, {
__tostring = function()
return word
end,
})

goto continue
end
Expand All @@ -289,7 +293,7 @@ module.public = {
local valid_month_name, valid_month_number = next(valid_months)

output.month = {
name = valid_month_name,
name = neorg.lib.title(valid_month_name),
number = valid_month_number,
}

Expand All @@ -315,7 +319,7 @@ module.public = {
local valid_weekday_name, valid_weekday_number = next(valid_weekdays)

output.weekday = {
name = valid_weekday_name,
name = neorg.lib.title(valid_weekday_name),
number = valid_weekday_number,
}

Expand All @@ -331,7 +335,20 @@ module.public = {
::continue::
end

return output
return setmetatable(output, {
__tostring = function()
local function d(str)
return str and (tostring(str) .. " ") or ""
end

return d(output.weekday and output.weekday.name)
.. d(output.day)
.. d(output.month and output.month.name)
.. d(output.year and string.format("%04d", output.year))
.. d(output.time and tostring(output.time))
.. (output.timezone or "")
end,
})
end,
}

Expand Down

0 comments on commit 3ec5f96

Please sign in to comment.