Files
vim-denote/autoload/denote/frontmatter.vim

94 lines
3.1 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
" Set tags in front matter
function denote#frontmatter#setTags(filename, tags)
let l:frontmatter = s:getFrontmatter(a:filename)
let l:tagline = denote#frontmatter#new(fnamemodify(a:filename, ':t'), '', '', a:tags)
\ ->filter('v:val =~ "^\\(#+file\\)\\?tags"')[0]
call map(l:frontmatter, { _, v -> v =~ '^\(#+file\)\?tags' ? l:tagline : v})
call setbufline(a:filename, 1, l:frontmatter)
endfunction
" Prepend frontmatter to file
function denote#frontmatter#prepend(filename, id, title, tags=[])
let l:frontmatter = denote#frontmatter#new(fnamemodify(a:filename, ':t'), a:id, a:title, a:tags)
call appendbufline(a:filename, 0, l:frontmatter)
endfunction