make uuid more useful

This commit is contained in:
lambtho12
2022-01-27 11:31:17 +01:00
parent 2e4d282344
commit c1008bff22
3 changed files with 22 additions and 16 deletions

View File

@@ -721,6 +721,7 @@ Currently, the following substitutions will be made during new note creation:
| specifier in template | expands to | example | | specifier in template | expands to | example |
| --- | --- | --- | | --- | --- | --- |
| `{{title}}` | the title of the note | My new note | | `{{title}}` | the title of the note | My new note |
| `{{uuid}}` | UUID for the note | 202201271129 |
| `{{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 | | `{{prevday}}` | previous day's date in iso format | 2021-11-20 |
| `{{nextday}}` | next day's date in iso format | 2021-11-22 | | `{{nextday}}` | next day's date in iso format | 2021-11-22 |

View File

@@ -759,6 +759,7 @@ The following substitutions will be made during new note creation:
| 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 |
| `{{uuid}}` | UUID of the note | 202201271129 |
| `{{date}}` | date in iso format | 2021-11-21 | | `{{date}}` | date in iso format | 2021-11-21 |
| `{{prevday}}` | previous day, iso | 2021-11-20 | | `{{prevday}}` | previous day, iso | 2021-11-20 |
| `{{nextday}}` | next day, iso | 2021-11-22 | | `{{nextday}}` | next day, iso | 2021-11-22 |

View File

@@ -157,21 +157,20 @@ local function random_variable(length)
return res return res
end end
local function append_uuid(opts, title) local function getuuid(opts)
opts.prefix_title_by_uuid = opts.prefix_title_by_uuid opts.prefix_title_by_uuid = opts.prefix_title_by_uuid
or M.Cfg.prefix_title_by_uuid or M.Cfg.prefix_title_by_uuid
opts.uuid_type = opts.uuid_type or M.Cfg.uuid_type opts.uuid_type = opts.uuid_type or M.Cfg.uuid_type
if opts.prefix_title_by_uuid then
local uuid local uuid
if opts.prefix_title_by_uuid then
if opts.uuid_type ~= "rand" then if opts.uuid_type ~= "rand" then
uuid = os.date(opts.uuid_type) uuid = os.date(opts.uuid_type)
else else
uuid = random_variable(6) uuid = random_variable(6)
end end
title = uuid .. "-" .. title
end end
return title return uuid
end end
local function print_error(s) local function print_error(s)
@@ -495,7 +494,7 @@ local function calculate_dates(date)
return dates return dates
end end
local function linesubst(line, title, dates) local function linesubst(line, title, dates, uuid)
if dates == nil then if dates == nil then
dates = calculate_dates() dates = calculate_dates()
end end
@@ -524,6 +523,7 @@ local function linesubst(line, title, dates)
saturday = dates.saturday, saturday = dates.saturday,
title = title, title = title,
uuid = uuid,
} }
for k, v in pairs(substs) do for k, v in pairs(substs) do
line = line:gsub("{{" .. k .. "}}", v) line = line:gsub("{{" .. k .. "}}", v)
@@ -534,6 +534,7 @@ end
local function create_note_from_template( local function create_note_from_template(
title, title,
uuid,
filepath, filepath,
templatefn, templatefn,
calendar_info calendar_info
@@ -549,7 +550,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) .. "\n") ofile:write(linesubst(line, title, calendar_info, uuid) .. "\n")
end end
ofile:flush() ofile:flush()
@@ -1194,7 +1195,7 @@ local function FindDailyNotes(opts)
or M.Cfg.dailies_create_nonexisting == true or M.Cfg.dailies_create_nonexisting == true
) )
then then
create_note_from_template(today, fname, M.note_type_templates.daily) create_note_from_template(today, _, fname, M.note_type_templates.daily)
opts.erase = true opts.erase = true
opts.erase_file = fname opts.erase_file = fname
end end
@@ -1243,7 +1244,7 @@ local function FindWeeklyNotes(opts)
or M.Cfg.weeklies_create_nonexisting == true or M.Cfg.weeklies_create_nonexisting == true
) )
then then
create_note_from_template(title, fname, M.note_type_templates.weekly) create_note_from_template(title, _, fname, M.note_type_templates.weekly)
opts.erase = true opts.erase = true
opts.erase_file = fname opts.erase_file = fname
end end
@@ -1599,6 +1600,7 @@ local function GotoDate(opts)
then then
create_note_from_template( create_note_from_template(
word, word,
_,
fname, fname,
M.note_type_templates.daily, M.note_type_templates.daily,
opts.dates opts.dates
@@ -1824,8 +1826,6 @@ local function on_create_with_template(opts, title)
return return
end end
title = append_uuid(opts, title)
opts = opts or {} opts = opts or {}
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
@@ -1834,7 +1834,8 @@ local function on_create_with_template(opts, title)
opts.new_note_location = opts.new_note_location or M.Cfg.new_note_location opts.new_note_location = opts.new_note_location or M.Cfg.new_note_location
opts.template_handling = opts.template_handling or M.Cfg.template_handling opts.template_handling = opts.template_handling or M.Cfg.template_handling
local pinfo = Pinfo:new({ title = title, opts }) local uuid = getuuid(opts)
local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts })
local fname = pinfo.filepath local fname = pinfo.filepath
if pinfo.fexists == true then if pinfo.fexists == true then
-- open the new note -- open the new note
@@ -1855,6 +1856,7 @@ local function on_create_with_template(opts, title)
-- TODO: pass in the calendar_info returned from the pinfo -- TODO: pass in the calendar_info returned from the pinfo
create_note_from_template( create_note_from_template(
title, title,
uuid,
fname, fname,
template, template,
pinfo.calendar_info pinfo.calendar_info
@@ -1907,15 +1909,15 @@ local function on_create(opts, title)
return return
end end
title = append_uuid(opts, title) local uuid = getuuid(opts)
local pinfo = Pinfo:new({ title = uuid .. "-" .. title, opts })
local pinfo = Pinfo:new({ title = title, opts })
local fname = pinfo.filepath local fname = pinfo.filepath
if pinfo.fexists ~= true then if pinfo.fexists ~= true then
-- TODO: pass in the calendar_info returned in pinfo -- TODO: pass in the calendar_info returned in pinfo
create_note_from_template( create_note_from_template(
title, title,
uuid,
fname, fname,
pinfo.template, pinfo.template,
pinfo.calendar_info pinfo.calendar_info
@@ -1927,7 +1929,7 @@ local function on_create(opts, title)
find_files_sorted({ find_files_sorted({
prompt_title = "Created note...", prompt_title = "Created note...",
cwd = pinfo.root_dir, cwd = pinfo.root_dir,
default_text = title, default_text = uuid .. "-" .. title,
find_command = M.Cfg.find_command, find_command = M.Cfg.find_command,
attach_mappings = function(_, map) attach_mappings = function(_, map)
actions.select_default:replace(picker_actions.select_default) actions.select_default:replace(picker_actions.select_default)
@@ -2061,8 +2063,10 @@ local function FollowLink(opts)
end end
if #pinfo.filepath > 0 then if #pinfo.filepath > 0 then
local uuid = getuuid()
create_note_from_template( create_note_from_template(
title, title,
uuid,
pinfo.filepath, pinfo.filepath,
pinfo.template, pinfo.template,
pinfo.calendar_info pinfo.calendar_info
@@ -2428,7 +2432,7 @@ local function GotoThisWeek(opts)
or M.Cfg.weeklies_create_nonexisting == true or M.Cfg.weeklies_create_nonexisting == true
) )
then then
create_note_from_template(title, fname, M.note_type_templates.weekly) create_note_from_template(title, _, fname, M.note_type_templates.weekly)
opts.erase = true opts.erase = true
opts.erase_file = fname opts.erase_file = fname
end end