60 lines
1.8 KiB
VimL
60 lines
1.8 KiB
VimL
" Functions to create the front matter {{{1
|
|
" Helper function to put string in double quotes, and remove inside double
|
|
" quotes.
|
|
function s:escapeDQ(s)
|
|
return '"' .. substitute(a:s, '"', '', 'g') .. '"'
|
|
endfunction
|
|
|
|
" Create front matter (yaml)
|
|
function s:md_new_yaml(id, title, tags)
|
|
return [
|
|
\ '---',
|
|
\ 'title: ' .. s:escapeDQ(a:title),
|
|
\ 'date: ' .. strftime("%FT%T%z"),
|
|
\ 'tags: [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']',
|
|
\ 'identifier: ' .. s:escapeDQ(a:id),
|
|
\ '---'
|
|
\ ]
|
|
endfunction
|
|
|
|
" Create front matter (toml)
|
|
function s:md_new_toml(id, title, tags)
|
|
return [
|
|
\ '+++',
|
|
\ 'title ' .. s:escapeDQ(a:title),
|
|
\ 'date ' .. strftime("%FT%T%z"),
|
|
\ 'tags [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']',
|
|
\ 'identifier ' .. s:escapeDQ(a:id),
|
|
\ '+++'
|
|
\ ]
|
|
endfunction
|
|
|
|
" Create front matter (plain)
|
|
function s:plain_new(id, title, tags)
|
|
return [
|
|
\ 'title: ' .. a:title,
|
|
\ 'date: ' .. strftime("%F"),
|
|
\ 'tags: ' .. a:tags->join(' '),
|
|
\ 'identifier: ' .. a:id,
|
|
\ '---------------------------',
|
|
\ ]
|
|
endfunction
|
|
|
|
" Create front matter (org)
|
|
function s:org_new(id, title, tags)
|
|
return [
|
|
\ '#+title: ' .. a:title,
|
|
\ '#+date: [' .. strftime("%F %a %R") .. ']',
|
|
\ '#+filetags: :' .. map(copy(a:tags), {_, t -> substitute(t, ':', '', 'g') })->join(':') .. ':',
|
|
\ '#+identifier: ' .. a:id,
|
|
\ ]
|
|
endfunction
|
|
|
|
" Create front matter
|
|
function denote#frontmatter#new(ft, id, title, tags=[])
|
|
return a:ft == 'org' ? s:org_new(a:id, a:title, a:tags)
|
|
\ : a:ft == 'plain' ? s:plain_new(a:id, a:title, a:tags)
|
|
\ : g:denote_fm_md_type == 'toml' ? s:md_new_toml(a:id, a:title, a:tags)
|
|
\ : s:md_new_yaml(a:id, a:title, a:tags)
|
|
endfunction
|