From 31c4a605db73349b16d4dc6e4a44903edea8e389 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Wed, 1 Dec 2021 16:10:18 +0100 Subject: [PATCH] Fix #16: templates should be optional --- README.md | 28 +++++++++++++++++++--------- doc/telekasten.txt | 21 ++++++++++++++------- lua/telekasten.lua | 31 +++++++++++++++++++------------ 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 252c86a..b431ef0 100644 --- a/README.md +++ b/README.md @@ -194,12 +194,15 @@ require('telekasten').setup({ weeklies_create_nonexisting = true, -- template for new notes (new_note, follow_link) + -- set to `nil` or do not specify if you do not want a template template_new_note = home .. '/' .. 'templates/new_note.md', -- template for newly created daily notes (goto_today) + -- set to `nil` or do not specify if you do not want a template template_new_daily = home .. '/' .. 'templates/daily.md', -- template for newly created weekly notes (goto_thisweek) + -- set to `nil` or do not specify if you do not want a template template_new_weekly= home .. '/' .. 'templates/weekly.md', -- image link style @@ -241,6 +244,13 @@ END | `plug_into_calendar` | activate calendar support if true (needs calendar-vim plugin) | true | | `calendar_opts` | options for calendar, see below | see below | +**Please note:** If you do not want to use a template, set its associated option to `nil` or remove it from your config. +```lua + -- template for new notes (new_note, follow_link) + -- set to `nil` or do not specify if you do not want a template + template_new_note = nil, +``` + The calendar support has its own options, contained in `calendar_opts`: | calendar setting | description | example | @@ -416,18 +426,18 @@ The functions `goto_today`, `goto_thisweek`, `find_daily_notes`, `find_weekly_no non-existing notes. This allows you to 'go to today' without having to create today's note beforehand. When you just type `[[some link]]` and then call `follow_link`, the 'some link' note can be generated. -The following table shows which command relies on what config option: +The following table shows what action creates what kind of non-existing note: -| telekasten function | config option | creates what | +| ACTION | config option | creates what | | --- | --- | --- | -| `goto_today` | `dailies_create_nonexisting` | today's daily note | +| `goto_today()` | `dailies_create_nonexisting` | today's daily note | | [ENTER] on a day in the calendar | `dailies_create_nonexisting` | selected day's daily note | -| `find_daily_notes` | `dailies_create_nonexisting` | today's daily note | -| `goto_thisweek` | `weeklies_create_nonexisting` | this week's weekly note | -| `find_weekly_notes` | `weeklies_create_nonexisting` | this week's weekly note | -| `follow_link` | `follow_creates_nonexisting` | new note | -| `new_note` | always true | new note | -| `new_templated_note` | always true | new note | +| `find_daily_notes()` | `dailies_create_nonexisting` | today's daily note | +| `goto_thisweek()` | `weeklies_create_nonexisting` | this week's weekly note | +| `find_weekly_notes()` | `weeklies_create_nonexisting` | this week's weekly note | +| `follow_link()` | `follow_creates_nonexisting` | new note | +| `new_note()` | always true | new note | +| `new_templated_note()` | always true | new note | If the associated option is `true`, non-existing notes will be created. diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 50dc3c0..7884d7f 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -80,6 +80,7 @@ telekasten.setup({opts}) -- specific note templates + -- set to `nil` or do not specify if you do not want a template template_new_note = '/path/to/file', -- template for new notes template_new_daily = '/path/to/file', -- template for new daily notes template_new_weekly = '/path/to/file', -- template for new weekly notes @@ -182,21 +183,27 @@ telekasten.setup({opts}) *telekasten.settings.template_new_note* template_new_note: ~ - Markdown template for new notes. + Markdown template for new notes. Set to `nil` if you don't want a + template. - Default: '~/zettelkasten/templates/new_note.md' + Default: `nil` + Example: `'~/zettelkasten/templates/daily.md'` *telekasten.settings.template_new_daily* template_new_daily: ~ - Markdown template for new daily notes. + Markdown template for new daily notes. Set to `nil` if you don't want + a template. - Default: '~/zettelkasten/templates/daily.md' + Default: `nil` + Example: `'~/zettelkasten/templates/daily.md'` *telekasten.settings.template_new_weekly* template_new_weekly: ~ - Markdown template for new weekly notes. + Markdown template for new weekly notes. Set to `nil` if you don't want + a template. - Default: '~/zettelkasten/templates/daily.md' + Default: `nil` + Example: `'~/zettelkasten/templates/daily.md'` Valid keys for {opts.calendar_opts} @@ -531,7 +538,7 @@ The specific templates and settings used are: See also |telekasten.template_files| below for more on the specific templates. -The folloet ing table shows you what action creates what kind of non-existing +The following table shows you what action creates what kind of non-existing note: +----------------------------------+--------------------------------+ diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 422780f..ae7a64a 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -38,9 +38,9 @@ M.Cfg = { weeklies_create_nonexisting = true, -- templates for new notes - template_new_note = home .. "/" .. "templates/new_note.md", - template_new_daily = home .. "/" .. "templates/daily_tk.md", - template_new_weekly = home .. "/" .. "templates/weekly_tk.md", + -- template_new_note = home .. "/" .. "templates/new_note.md", + -- template_new_daily = home .. "/" .. "templates/daily_tk.md", + -- template_new_weekly = home .. "/" .. "templates/weekly_tk.md", -- image link style -- wiki: ![[image name]] @@ -208,8 +208,10 @@ end local function create_note_from_template(title, filepath, templatefn, calendar_info) -- first, read the template file local lines = {} - for line in io.lines(templatefn) do - lines[#lines + 1] = line + if file_exists(templatefn) then + for line in io.lines(templatefn) do + lines[#lines + 1] = line + end end -- now write the output file, substituting vars line by line @@ -998,13 +1000,6 @@ local function Setup(cfg) M.Cfg.find_command = nil end - -- refresh templates - M.note_type_templates = { - normal = M.Cfg.template_new_note, - daily = M.Cfg.template_new_daily, - weekly = M.Cfg.template_new_weekly, - } - -- this looks a little messy if M.Cfg.plug_into_calendar then cfg.calendar_opts = cfg.calendar_opts or {} @@ -1022,6 +1017,18 @@ local function Setup(cfg) -- setup extensions to filter for M.Cfg.filter_extensions = cfg.filter_extensions or { M.Cfg.extension } + -- provide fake filenames for template loading to fail silently if template is configured off + M.Cfg.template_new_note = M.Cfg.template_new_note or 'none' + M.Cfg.template_new_daily = M.Cfg.template_new_daily or 'none' + M.Cfg.template_new_weekly = M.Cfg.template_new_weekly or 'none' + + -- refresh templates + M.note_type_templates = { + normal = M.Cfg.template_new_note, + daily = M.Cfg.template_new_daily, + weekly = M.Cfg.template_new_weekly, + } + if debug then print("Resulting config:") print("-----------------")