79 lines
2.5 KiB
VimL
79 lines
2.5 KiB
VimL
" Functions to manipulate and query the front matter
|
|
|
|
" 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
|
|
|
|
" Return the frontmatter of the specified file (as a list)
|
|
function s:getFrontmatter(filename)
|
|
let l:ft = fnamemodify(a:filename, ':e')
|
|
let l:cnt = l:ft == 'org' ? 4
|
|
\ : l:ft == 'txt' ? 5
|
|
\ : 6
|
|
call bufload(a:filename)
|
|
return getbufline(a:filename, 1, 1 + l:cnt)
|
|
endfunction
|
|
|
|
" Get title from front matter
|
|
function denote#frontmatter#setTitle(filename, title)
|
|
" Retrieve front matter (number of lines depends on ft), and replace title
|
|
let l:frontmatter = s:getFrontmatter(a:filename)
|
|
call map(l:frontmatter, { _, v -> substitute(v, '^#\?+\?title:\?\s*"\?\zs.\{-\}\ze\"\?$', a:title, '')})
|
|
call setbufline(a:filename, 1, l:frontmatter)
|
|
endfunction
|