-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add template feature for new pages #83
Conversation
I see this hasn't really had much progress over the past year but for what it's worth, the specification describes most of what I would use it for. I use two different styles of notes depending on if they're named "index" or not. This could be implemented by checking for the name of the file against a list of templates of the same filename, and failing that it could then fallback to a generic template. Something like: let g:wiki_specific_template_files = [
\ '.index.md;',
\ '/home/user/templates/index.md',
\ '.categories.md;',
\ '/home/user/templates/categories.md',
\]
let g:wiki_generic_template_files = [
\ '.mytemplate.md;',
\ '/home/user/templates/fallback.md',
\] Where the filename would compare against items in Apologies for not offering to help, I have made attempts to figure this out already but I don't know vimscript well enough. |
Thanks! That's worth a bunch. The lack of progress is mostly due to lack of interest. If I had a strong personal interest it would of course already have been implemented. But with external interest and input I would not mind picking up the threads and finishing this.
No problem! From my perspective, having a discussion to settle the discussion is in itself an important contribution!
You raise an interesting idea - that it should be possible to apply different templates depending on e.g. the target file name. How about the following refined idea?
That is, we define a list of templates whose "behaviour" is specified with a dictionary. The template feature would be implemented similar to this: " new file is created first, then:
for template in g:wiki_templates
if applies(template, context)
call apply(template, context)
break
endif
endfor Here the
What do you think? |
This seems like a better method, both for identifying the template to be used and for the templates themselves, as A suggestion based on the discussion of the An alternative for the title might be to use the display text of the link used to create the file (i.e.
I am not sure what you mean by "this is only applicable if match_re is not defined." In the example code under let g:wiki_templates = [
\ { 'match_re': 'index\.md',
\ 'source_filename': '/home/user/templates/index.md'},
\ { 'match_re': 'foo\.md',
\ 'source_filename': '.footemplate.md;'},
\ { 'match_func': {x -> v:true},
\ 'source_func': function('TemplateFallback')},
\]
Great! Happy to help. Once I get around to sorting the HTML export and citations, these templates will be the last wishlist item before I'll be content with my notes setup so I'm looking forward to it! |
Just an irritating comment (so late in the process): I also initially found lack of templates a missing feature, but completely forgot about it half a year down the road. It is very easy to create templates with snippets, so wiki.vim does not strictly need templates. |
This was discussed in #46 (comment), the advantage of templates is that they are automatic and for a lot of users would be much easier to set up if there is a requirement to access document properties, as there is in the current spec.
Apologise for adding this after the initial suggestion too, I realise it also may have been quite irritating. Got a bit carried away with your enthusiastic response about having discussions! |
hi folks so I wrote a Lua function to add date to my journal entries, I mentioned that here in #160 as Karl had suggested that we should try to be compatible with Vim and older nVim, here's the same in VimL fun! CurrentEntry()
let filepath = g:wiki_root.'journal/'.strftime('%Y-%m-%d').'.md'
if !filereadable(filepath)
call writefile([strftime('# %a, %d %B %y')], filepath)
endif
exe 'edit' filepath
endfun PS:
|
Good to hear we agree!
Yes, I think we can have several context elements. Let me start the implementation now with the current specification, then it should be easy to expand after.
I propose to let this be "future work" for now; when we have a working version, then feel free to open a new issue with a feature request.
The point is that a specific "template" defines either In the example, the
Agreed, but I also agree with @c-fergus that the idea has its merits of being @anshul-march Thanks for the vimscript variant! Reg.
I'm not fully sure what you mean. Can you be more specific? |
Let's take the example from the docs # {{wiki#template#case_title {pagetitle}}}
Created: {date} {time}
# Introduction
# Conclusion I was thinking that just like PS:
|
Exactly: The proposed syntax is that |
Ok, it's starting to take shape. Still too early for real testing, e.g. no interpolation of the source files yet. But the source function should already work quite well. The code should be quite easy to read - see here. |
I believe the only thing left is to implement the template file functionality, i.e. variable and function interpolation, as well as the file search with the Please note the minor update of the specification; I've combined the template file variables with the context object. |
Does this mean that the only difference between contexts and variables is function substitution? Is this difference arbitrary or can this also be added as a context to bring both to parity? Now that you've made this spec change I can't think of a reason to seperate variables and contexts other than implementation difficulties, but I would assume the easiest way to implement variables would be to either use the contexts defined in Apologies for any ambiguity in referring to "contexts," I mean to say the elements of the context dictionary. |
I don't really understand what you're asking. What it means, is that the variables available in the text file format are exactly the keys of the context object. I.e., the table presented under |
Ok, now I believe everything should work. It would be nice if someone wanted to test. I've simplified the template source path for now; if things work and you think it will be useful, then I will be glad to consider more flexible ways of specifying the template source file. As the new features should not break any old features, and since it does not interfere, I believe it is safe to merge already and then apply changes on top afterwards. This also makes it easier for everyone to test, since you don't need to checkout the feature branch. |
I've pushed a couple of minor fixes/adjustments to the master branch as well, now. Feel free to 1) continue discussing and giving feedback here, or 2) open new issues on specific problems or suggestions for the new template feature. |
Okay I see from your last commit how the function substitution works. It answers my question. I copied the docs example code exactly to test but couldn't get new files to adopt a template, I'll test again tonight. |
Hi Karl, I tried using the template feature. This is how I am setting templates for my daily journal and this works. let g:wiki_templates = [
\ { 'match_re': strftime('%Y-%m-%d'),
\ 'source_filename': '/Users/anshul/wiki/journal/.template.md'},
\] I do not understand quite clearly how to use
|
|
Hi @c-fergus I just tested your suggestions, and this worked. Thank you! |
I would adjust this, because you are not really defining a proper regex. This should be better, I think, as it should match any file whose name matches an ISO date (but not a page that contains a date): let g:wiki_templates = [
\ { 'match_re': '^\d\d\d\d-\d\d-\d\d$',
\ 'source_filename': '/Users/anshul/wiki/journal/.template.md'},
\]
I believe @c-fergus answered this correctly. The point here is that the argument is a dictionary, and you need to access the correct keys (e.g. The value for the Note that the conditional can be trivially simplified:
Also, perhaps you wanted something "looser" than the name being exactly function! NewFunc(ctx)
return a:ctx.name =~ '[Rr]ust'
endfun
Feel free to ask if there is something in particular that does not work. It works for me, and it seems to work for @anshul-march..? |
Yes, it works. function! NewFunc(ctx)
return a:ctx.name =~ '[Rr]ust'
endfun this is great, I didn't even know one can do this
yes, I didn't consider this case at all, thank you I noticed that if i try to make a link with for uniformity, is there a way that the files are always created with lowercase? what I mean, if I use |
Yes; you want the let g:wiki_map_create_page = 'ToLower'
function ToLower(name) abort
return tolower(a:name)
endfunction
" In fact, since 'tolower()' already exists, you can probably use it directly
" without defining a custom function (but with a custom function you can do
" more, e.g. convert spaces to underscores):
let g:wiki_map_create_page = 'tolower' |
Sorry; this was partly wrong. You probably want that function, but you also want |
But; there is currently no feature to fully convert the link url. To do that, you need more heavy customization. So, the proper answer to your question is sort of no; sort of yes. To convert the wiki scheme url before opening it; i.e. make both wiki.vim/autoload/wiki/url/wiki.vim Lines 30 to 61 in 00502ed
So, this is more complex, but it is doable if you know some more Vimscript. |
Essentially, I guess one could implement a "converter" that is applied here as well similar to the |
Sure, it's the regex that isn't working for me, everything else seems great. Using function! MatchIndex(dict)
return a:dict.name == 'index'
endfunction
let g:wiki_templates = [
\ { 'match_func': function('MatchIndex'),
\ 'source_filename': $HOME . '/Documents/notes/templates/templateindex.rmd'},
\] Using let g:wiki_templates = [
\ { 'match_re': `index\.rmd'),
\ 'source_filename': $HOME . '/Documents/notes/templates/templateindex.rmd'},
\] Just using 'index' as the regex match works but this also matches files that just have 'index' in the name which I don't want. |
Ah, yes - the regex doesn't work because the |
Ah okay I think I'm sorted now then, I opened #167 to address the example code which prompted me to use the extensions. |
I've been unsuccessful using wiki templates. I am certain the error is mine. I have included my attempt to setup two templates. I've read the doc and this thread multiple times. Any help is appreciated. let s:journal = g:wiki_root . '/.journal.md'
let s:template = g:wiki_root . '/.template.md'
let g:wiki_templates = [
\ { 'match_re': '^\d\d\d\d-\d\d-\d\d$',
\ 'source_filename': s:journal},
\ { 'match_re': '*',
\ 'source_filename': s:template}
\] I echoed |
There's a mistake in your let s:journal = g:wiki_root . '/.journal.md'
let s:template = g:wiki_root . '/.template.md'
let g:wiki_templates = [
\ { 'match_re': '^\d\d\d\d-\d\d-\d\d$',
\ 'source_filename': s:journal},
\ { 'match_re': '.*',
\ 'source_filename': s:template}
\]
" Or perhaps better:
let g:wiki_templates = [
\ { 'match_re': '^\d\d\d\d-\d\d-\d\d$',
\ 'source_filename': s:journal},
\ { 'match_func': {x -> v:true},
\ 'source_filename': s:template}
\] By the way, it would be useful to know your value of |
The first example worked for 2nd template only. I used put= to gather the g:wiki values.
The second example did not work for either template:
Journal files are located in /home/traap/git/wiki/journal/... 2021-07-13.md is an example file name. I tried two journal entries as follows (both did not use the template).
Both test cases did create the a journal file with md extension. |
I'm sorry, but this is a little bit hard to understand. Perhaps you can open a new issue to make the thread more focussed and easier to follow? I also think it's good to avoid "spamming" this feature thread with a possible configuration assist or bug report. If/When you open a new issue, please note:
If I understand your |
Tasks:
See also #46 for more context and discussion.
Feedback wanted: @Corey-Keller, @gauteh, @jabirali
Note: I think the specs are quite good now. I think the most important thing now is to agree on formats and structure, then we can add more interpolation features later, if we deem it necessary.