improvements from PR comments

This commit is contained in:
Liquidmantis
2021-12-12 12:56:10 -07:00
parent 3a344eb959
commit d39ed91001
3 changed files with 71 additions and 74 deletions

View File

@@ -627,14 +627,14 @@ 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 |
| `{{yesterday}}` | yesterday's date in iso format | 2021-11-20 | | `{{prevday}}` | previous day's date in iso format | 2021-11-20 |
| `{{tomorrow}}` | tomorrow's date in iso format | 2021-11-22 | | `{{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 |
| `{{lastweek}}` | prior week of the year | 45 | | `{{prevweek}}` | previous week of the year | 45 |
| `{{nextweek}}` | next week of the year | 47 | | `{{nextweek}}` | next week of the year | 47 |
| `{{isoweek}}` | week of the year in iso format | 2021-W46 | | `{{isoweek}}` | week of the year in iso format | 2021-W46 |
| `{{isolastweek}}` | prior week of the year in iso format | 2021-W45 | | `{{isoprevweek}}` | previous week of the year in iso format | 2021-W45 |
| `{{isonextweek}}` | next week of the year in iso format| 2021-W47 | | `{{isonextweek}}` | next week of the year in iso format| 2021-W47 |
| `{{year}}` | year | 2021 | | `{{year}}` | year | 2021 |
| `{{monday}}` | monday's date | 2021-11-16 | | `{{monday}}` | monday's date | 2021-11-16 |

View File

@@ -673,15 +673,15 @@ 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 |
| `{{yesterday}}` | yesterday, iso | 2021-11-20 | | `{{prevday}}` | previous day, iso | 2021-11-20 |
| `{{tomorrow}}` | tomorrow, iso | 2021-11-22 | | `{{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 |
| `{{lastweek}}` | prior week | 45 | | `{{prevweek}}` | previous week | 45 |
| `{{nextweek}}` | next week | 47 | | `{{nextweek}}` | next week | 47 |
| `{{isoweek}}` | week in iso format | 2021-46 | | `{{isoweek}}` | week in iso format | 2021-46 |
| `{{lastisoweek}}` | last week, iso | 2021-45 | | `{{isoprevweek}}` | last week, iso | 2021-45 |
| `{{nextisoweek}}` | next week, iso | 2021-47 | | `{{isonextweek}}` | next week, iso | 2021-47 |
| `{{year}}` | year | 2021 | | `{{year}}` | year | 2021 |
| `{{monday}}` | Monday, iso | 2021-11-15 | | `{{monday}}` | Monday, iso | 2021-11-15 |
| `{{tuesday}}` | Tuesday, iso | 2021-11-16 | | `{{tuesday}}` | Tuesday, iso | 2021-11-16 |

View File

@@ -174,13 +174,13 @@ local function daysuffix(day)
end end
local daymap = { local daymap = {
"Sunday",
"Monday", "Monday",
"Tuesday", "Tuesday",
"Wednesday", "Wednesday",
"Thursday", "Thursday",
"Friday", "Friday",
"Saturday", "Saturday",
"Sunday",
} }
local monthmap = { local monthmap = {
"January", "January",
@@ -203,27 +203,53 @@ local dateformats = {
isoweek = "%Y-W%V" isoweek = "%Y-W%V"
} }
local function relativedates_today() local function calculate_dates(cinfo)
local now = os.time() local time = os.time(cinfo) -- convert date input to a timestamp
local dinfo = os.date("*t") local dinfo = os.date("*t", time)
local wday = dinfo.wday - 1
local oneday = 24 * 60 * 60 -- hours * days * seconds local oneday = 24 * 60 * 60 -- hours * days * seconds
local oneweek = 7 * oneday local oneweek = 7 * oneday
local oneyear = 365 * oneday
local df = dateformats local df = dateformats
local opts = {}
opts.yesterday = os.date(df.date, now - oneday) local opts = {}
opts.tomorrow = os.date(df.date, now + oneday) opts.date = os.date(df.date)
opts.lastweek = os.date(df.week, now - oneweek) local wday = dinfo.wday - 1 -- compensate for 1-indexed os.date output
opts.nextweek = os.date(df.week, now + oneweek) opts.hdate = daymap[wday]
opts.isolastweek = os.date(df.isoweek, os.time() - oneweek) .. ", "
opts.isonextweek = os.date(df.isoweek, os.time() + oneweek) .. monthmap[dinfo.month]
.. " "
.. dinfo.day
.. daysuffix(dinfo.day)
.. ", "
.. dinfo.year
opts.month = dinfo.month
opts.year = dinfo.year
opts.day = dinfo.day
opts.prevday = os.date(df.date, time - oneday)
opts.nextday = os.date(df.date, time + oneday)
opts.week = os.date(df.week, time)
opts.prevweek = os.date(df.week, time - oneweek)
opts.nextweek = os.date(df.week, time + oneweek)
opts.isoweek = os.date(df.isoweek, time)
opts.isoprevweek = os.date(df.isoweek, time - oneweek)
opts.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 opts.week == 52 and opts.month == 1 then
opts.isoweek = os.date(df.isoweek, time - oneyear)
end
-- Find the Sunday that started this week regardless of the calendar -- Find the Sunday that started this week regardless of the calendar
-- display preference. Then use that as the base to calculate the dates -- display preference. Then use that as the base to calculate the dates
-- for the days of the current week. -- for the days of the current week.
-- Finally, adjust Sunday to suit user calendar preference. -- Finally, adjust Sunday to suit user calendar preference.
local starting_sunday = now - (wday * oneday) local starting_sunday = time - (wday * oneday)
local sunday_offset = 0 local sunday_offset = 0
if M.Cfg.calendar_opts.calendar_monday == 1 then if M.Cfg.calendar_opts.calendar_monday == 1 then
sunday_offset = 7 sunday_offset = 7
@@ -239,57 +265,29 @@ local function relativedates_today()
return opts return opts
end end
local function calenderinfo_today() local function linesubst(line, title, calendar_info)
local dinfo = os.date("*t") local dates = calculate_dates(calendar_info)
local opts = {}
opts.date = os.date(dateformats.date)
local wday = dinfo.wday - 1
if wday == 0 then
wday = 7
end
if wday == 6 then
wday = 1
end
opts.hdate = daymap[wday]
.. ", "
.. monthmap[dinfo.month]
.. " "
.. dinfo.day
.. daysuffix(dinfo.day)
.. ", "
.. dinfo.year
opts.week = os.date(dateformats.week)
opts.isoweek = os.date(dateformats.isoweek)
opts.month = dinfo.month
opts.year = dinfo.year
opts.day = dinfo.day
return opts
end
local function linesubst(line, title, calendar_info, relative_dates)
local cinfo = calendar_info or calenderinfo_today()
local rdates = relative_dates or relativedates_today()
local substs = { local substs = {
date = cinfo.date, hdate = dates.hdate,
hdate =cinfo.hdate, week = dates.week,
week = cinfo.week, date = dates.date,
isoweek = cinfo.isoweek, isoweek = dates.isoweek,
year = cinfo.year, year = dates.year,
yesterday = rdates.yesterday, prevday = dates.prevday,
tomorrow = rdates.tomorrow, nextday = dates.nextday,
lastweek = rdates.lastweek, prevweek = dates.prevweek,
nextweek = rdates.nextweek, nextweek = dates.nextweek,
isolastweek = rdates.isolastweek, isoprevweek = dates.isoprevweek,
isonextweek = rdates.isonextweek, isonextweek = dates.isonextweek,
sunday = rdates.sunday, sunday = dates.sunday,
monday = rdates.monday, monday = dates.monday,
tuesday = rdates.tuesday, tuesday = dates.tuesday,
wednesday = rdates.wednesday, wednesday = dates.wednesday,
thursday = rdates.thursday, thursday = dates.thursday,
friday = rdates.friday, friday = dates.friday,
saturday = rdates.saturday, saturday = dates.saturday,
title = title, title = title,
} }
@@ -304,8 +302,7 @@ local function create_note_from_template(
title, title,
filepath, filepath,
templatefn, templatefn,
calendar_info, calendar_info
relative_dates
) )
-- first, read the template file -- first, read the template file
local lines = {} local lines = {}
@@ -318,7 +315,7 @@ local function create_note_from_template(
-- now write the output file, substituting vars line by line -- now write the output file, substituting vars line by line
local ofile = io.open(filepath, "a") local ofile = io.open(filepath, "a")
for _, line in pairs(lines) do for _, line in pairs(lines) do
ofile:write(linesubst(line, title, calendar_info, relative_dates) .. "\n") ofile:write(linesubst(line, title, calendar_info) .. "\n")
end end
ofile:close() ofile:close()
@@ -1144,7 +1141,7 @@ end
-- find today's daily note and create it if necessary. -- find today's daily note and create it if necessary.
-- --
local function GotoToday(opts) local function GotoToday(opts)
opts = opts or calenderinfo_today() opts = opts or calculate_dates()
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