Merge branch 'feature-template-dates' into main

This commit is contained in:
Rene Schallner
2021-12-13 17:19:36 +01:00
3 changed files with 150 additions and 53 deletions

View File

@@ -631,9 +631,24 @@ Currently, the following substitutions will be made during new note creation:
| --- | --- | --- | | --- | --- | --- |
| `{{title}}` | the title of the note | My new note | | `{{title}}` | the title of the note | My new note |
| `{{date}}` | date in iso format | 2021-11-21 | | `{{date}}` | date in iso format | 2021-11-21 |
| `{{prevday}}` | previous day's date in iso format | 2021-11-20 |
| `{{nextday}}` | next day's date in iso format | 2021-11-22 |
| `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | | `{{hdate}}` | date in long format | Sunday, November 21st, 2021 |
| `{{week}}` | week of the year | 46 | | `{{week}}` | week of the year | 46 |
| `{{prevweek}}` | previous week of the year | 45 |
| `{{nextweek}}` | next week of the year | 47 |
| `{{isoweek}}` | week of the year in iso format | 2021-W46 |
| `{{isoprevweek}}` | previous week of the year in iso format | 2021-W45 |
| `{{isonextweek}}` | next week of the year in iso format| 2021-W47 |
| `{{year}}` | year | 2021 | | `{{year}}` | year | 2021 |
| `{{monday}}` | monday's date | 2021-11-16 |
| `{{tuesday}}` | tuesday's date | 2021-11-17 |
| `{{wednesday}}` | wednesday's date | 2021-11-18 |
| `{{thursday}}` | thursday's date | 2021-11-19 |
| `{{friday}}` | friday's date | 2021-11-20 |
| `{{saturday}}` | saturday's date | 2021-11-21 |
| `{{sunday}}` | sunday's date | 2021-11-22 |
Note: Sunday will be adjusted by the user's `calendar_monday` preference.
As an example, this is my template for new notes: As an example, this is my template for new notes:

View File

@@ -667,16 +667,31 @@ that are used for creating new notes.
The following substitutions will be made during new note creation: The following substitutions will be made during new note creation:
+-------------+-----------------------+-----------------------------+ +-----------------+-----------------------+-----------------------------+
| specifier | | | | specifier | | |
| in template | expands to | example | | in template | expands to | example |
+-------------+-----------------------+-----------------------------+ +-----------------+-----------------------+-----------------------------+
| `{{title}}` | the title of the note | My new note | | `{{title}}` | the title of the note | My new note |
| `{{date}}` | date in iso format | 2021-11-21 | | `{{date}}` | date in iso format | 2021-11-21 |
| `{{prevday}}` | previous day, iso | 2021-11-20 |
| `{{nextday}}` | next day, iso | 2021-11-22 |
| `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | | `{{hdate}}` | date in long format | Sunday, November 21st, 2021 |
| `{{week}}` | week of the year | 46 | | `{{week}}` | week of the year | 46 |
| `{{prevweek}}` | previous week | 45 |
| `{{nextweek}}` | next week | 47 |
| `{{isoweek}}` | week in iso format | 2021-46 |
| `{{isoprevweek}}` | last week, iso | 2021-45 |
| `{{isonextweek}}` | next week, iso | 2021-47 |
| `{{year}}` | year | 2021 | | `{{year}}` | year | 2021 |
+-------------+-----------------------+-----------------------------+ | `{{monday}}` | Monday, iso | 2021-11-15 |
| `{{tuesday}}` | Tuesday, iso | 2021-11-16 |
| `{{wednesday}}` | Wednesday, iso | 2021-11-17 |
| `{{thursday}}` | Thursday, iso | 2021-11-18 |
| `{{friday}}` | Friday, iso | 2021-11-19 |
| `{{saturday}}` | Saturday, iso | 2021-11-20 |
| `{{sunday}}` | Sunday, iso (see note)| 2021-11-21 |
+-----------------+-----------------------+-----------------------------+
Note: Sunday is adjusted to match the user's `calendar_monday` preference.
As an example, this is my template for new notes: As an example, this is my template for new notes:
> >

View File

@@ -205,18 +205,33 @@ local monthmap = {
"December", "December",
} }
local function calenderinfo_today() local dateformats = {
local dinfo = os.date("*t") date = "%Y-%m-%d",
local opts = {} week = "%V",
opts.date = os.date("%Y-%m-%d") isoweek = "%Y-W%V",
}
local function calculate_dates(date)
local time = os.time(date)
local dinfo = os.date("*t", time) -- this normalizes the input to a full date table
local oneday = 24 * 60 * 60 -- hours * days * seconds
local oneweek = 7 * oneday
local oneyear = 365 * oneday
local df = dateformats
local dates = {}
-- this is to compensate for the calendar showing M-Su, but os.date Su is
-- always wday = 1
local wday = dinfo.wday - 1 local wday = dinfo.wday - 1
if wday == 0 then if wday == 0 then
wday = 7 wday = 7
end end
if wday == 6 then
wday = 1 dates.year = dinfo.year
end dates.month = dinfo.month
opts.hdate = daymap[wday] dates.day = dinfo.day
dates.hdate = daymap[wday]
.. ", " .. ", "
.. monthmap[dinfo.month] .. monthmap[dinfo.month]
.. " " .. " "
@@ -224,20 +239,70 @@ local function calenderinfo_today()
.. daysuffix(dinfo.day) .. daysuffix(dinfo.day)
.. ", " .. ", "
.. dinfo.year .. dinfo.year
opts.week = os.date("%V")
opts.month = dinfo.month dates.date = os.date(df.date, time)
opts.year = dinfo.year dates.prevday = os.date(df.date, time - oneday)
opts.day = dinfo.day dates.nextday = os.date(df.date, time + oneday)
return opts dates.week = os.date(df.week, time)
dates.prevweek = os.date(df.week, time - oneweek)
dates.nextweek = os.date(df.week, time + oneweek)
dates.isoweek = os.date(df.isoweek, time)
dates.isoprevweek = os.date(df.isoweek, time - oneweek)
dates.isonextweek = os.date(df.isoweek, time + oneweek)
-- things get a bit hairy at the year rollover. W01 only starts the first week ofs
-- January if it has more than 3 days. Partial weeks with less than 4 days are
-- considered W52, but os.date still sets the year as the new year, so Jan 1 2022
-- would appear as being in 2022-W52. That breaks linear linking respective
-- of next/prev week, so we want to put the days of that partial week in
-- January in 2021-W52. This tweak will only change the ISO formatted week string.
if dates.week == 52 and dates.month == 1 then
dates.isoweek = os.date(df.isoweek, time - oneyear)
end
-- Find the Sunday that started this week regardless of the calendar
-- display preference. Then use that as the base to calculate the dates
-- for the days of the current week.
-- Finally, adjust Sunday to suit user calendar preference.
local starting_sunday = time - (wday * oneday)
local sunday_offset = 0
if M.Cfg.calendar_opts.calendar_monday == 1 then
sunday_offset = 7
end
dates.monday = os.date(df.date, starting_sunday + (1 * oneday))
dates.tuesday = os.date(df.date, starting_sunday + (2 * oneday))
dates.wednesday = os.date(df.date, starting_sunday + (3 * oneday))
dates.thursday = os.date(df.date, starting_sunday + (4 * oneday))
dates.friday = os.date(df.date, starting_sunday + (5 * oneday))
dates.saturday = os.date(df.date, starting_sunday + (6 * oneday))
dates.sunday = os.date(df.date, starting_sunday + (sunday_offset * oneday))
return dates
end end
local function linesubst(line, title, calendar_info) local function linesubst(line, title, dates)
local cinfo = calendar_info or calenderinfo_today()
local substs = { local substs = {
date = cinfo.date, hdate = dates.hdate,
hdate = cinfo.hdate, week = dates.week,
week = cinfo.week, date = dates.date,
year = cinfo.year, isoweek = dates.isoweek,
year = dates.year,
prevday = dates.prevday,
nextday = dates.nextday,
prevweek = dates.prevweek,
nextweek = dates.nextweek,
isoprevweek = dates.isoprevweek,
isonextweek = dates.isonextweek,
sunday = dates.sunday,
monday = dates.monday,
tuesday = dates.tuesday,
wednesday = dates.wednesday,
thursday = dates.thursday,
friday = dates.friday,
saturday = dates.saturday,
title = title, title = title,
} }
for k, v in pairs(substs) do for k, v in pairs(substs) do
@@ -632,7 +697,7 @@ local function FindDailyNotes(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
local today = os.date("%Y-%m-%d") local today = os.date(dateformats.date)
local fname = M.Cfg.dailies .. "/" .. today .. M.Cfg.extension local fname = M.Cfg.dailies .. "/" .. today .. M.Cfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if if
@@ -677,7 +742,7 @@ local function FindWeeklyNotes(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
local title = os.date("%Y-W%V") local title = os.date(dateformats.isoweek)
local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if if
@@ -1390,19 +1455,19 @@ local function YankLink()
end end
-- --
-- GotoToday: -- GotoDate:
-- ---------- -- ----------
-- --
-- find today's daily note and create it if necessary. -- find note for date and create it if necessary.
-- --
local function GotoToday(opts) local function GotoDate(opts)
opts = opts or calenderinfo_today() opts.dates = calculate_dates(opts.date_table)
opts.insert_after_inserting = opts.insert_after_inserting opts.insert_after_inserting = opts.insert_after_inserting
or M.Cfg.insert_after_inserting or M.Cfg.insert_after_inserting
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
local word = opts.date or os.date("%Y-%m-%d") local word = opts.date or os.date(dateformats.date)
local fname = M.Cfg.dailies .. "/" .. word .. M.Cfg.extension local fname = M.Cfg.dailies .. "/" .. word .. M.Cfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
@@ -1417,7 +1482,7 @@ local function GotoToday(opts)
word, word,
fname, fname,
M.note_type_templates.daily, M.note_type_templates.daily,
opts opts.dates
) )
opts.erase = true opts.erase = true
opts.erase_file = fname opts.erase_file = fname
@@ -1450,6 +1515,20 @@ local function GotoToday(opts)
}) })
end end
--
-- GotoToday:
-- ----------
--
-- find today's daily note and create it if necessary.
--
local function GotoToday(opts)
opts = opts or {}
local today = os.date(dateformats.date)
opts.date_table = os.date("*t")
opts.date = today
GotoDate(opts)
end
-- --
-- FindNotes: -- FindNotes:
-- ---------- -- ----------
@@ -1720,7 +1799,7 @@ local function GotoThisWeek(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
local title = os.date("%Y-W%V") local title = os.date(dateformats.isoweek)
local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if if
@@ -1772,23 +1851,11 @@ end
-- action on enter on a specific day: -- action on enter on a specific day:
-- preview in telescope, stay in calendar on cancel, open note in other window on accept -- preview in telescope, stay in calendar on cancel, open note in other window on accept
local function CalendarAction(day, month, year, weekday, _) local function CalendarAction(day, month, year, weekday, _)
local today = string.format("%04d-%02d-%02d", year, month, day)
local opts = {} local opts = {}
opts.date = today opts.date = string.format("%04d-%02d-%02d", year, month, day)
opts.hdate = daymap[weekday] opts.date_table = { year = year, month = month, day = day }
.. ", "
.. monthmap[tonumber(month)]
.. " "
.. day
.. daysuffix(day)
.. ", "
.. year
opts.week = "n/a" -- TODO: calculate the week somehow
opts.month = month
opts.year = year
opts.day = day
opts.calendar = true opts.calendar = true
GotoToday(opts) GotoDate(opts)
end end
local function ShowCalendar(opts) local function ShowCalendar(opts)