Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions autoload/wiki/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function! wiki#buffer#init() abort " {{{1
call s:init_buffer_commands()
call s:init_buffer_mappings()

call s:apply_template()
call wiki#template#init()

if exists('#User#WikiBufferInitialized')
doautocmd <nomodeline> User WikiBufferInitialized
Expand Down Expand Up @@ -173,19 +173,3 @@ function! s:init_buffer_mappings() abort " {{{1
endfunction

" }}}1

function! s:apply_template() abort " {{{1
if filereadable(expand('%')) | return | endif

let l:match = matchlist(expand('%:t:r'), '^\(\d\d\d\d\)_\(\w\)\(\d\d\)$')
if empty(l:match) | return | endif
let [l:year, l:type, l:number] = l:match[1:3]

if l:type ==# 'w'
call wiki#template#weekly_summary(l:year, l:number)
elseif l:type ==# 'm'
call wiki#template#monthly_summary(l:year, l:number)
endif
endfunction

" }}}1
1 change: 1 addition & 0 deletions autoload/wiki/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function! s:accept_page(lines) abort "{{{1

if len(a:lines) == 2 || !empty(a:lines[1])
call wiki#page#open(a:lines[0])
sleep 1
else
let l:file = split(a:lines[2], '#####')[0]
execute 'edit ' . l:file
Expand Down
23 changes: 16 additions & 7 deletions autoload/wiki/nav.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ endfunction

" }}}1

function! wiki#nav#add_to_stack(link) abort " {{{1
let s:position_stack += [a:link]
endfunction

let s:position_stack = []

" }}}1
function! wiki#nav#get_previous() abort "{{{1
let l:previous = get(s:position_stack, -1, [])
if !empty(l:previous) | return l:previous | endif

let l:file = expand('#:p')
if filereadable(l:file) | return [l:file, 1] | endif
endfunction

" }}}1
function! wiki#nav#return() abort "{{{1
if g:wiki_write_on_nav | update | endif

Expand All @@ -32,10 +48,3 @@ function! wiki#nav#return() abort "{{{1
endfunction

" }}}1
function! wiki#nav#add_to_stack(link) abort " {{{1
let s:position_stack += [a:link]
endfunction

let s:position_stack = []

" }}}1
10 changes: 1 addition & 9 deletions autoload/wiki/page.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ function! wiki#page#open(page) abort "{{{1
\ !empty(g:wiki_map_create_page) && exists('*' . g:wiki_map_create_page)
\ ? call(g:wiki_map_create_page, [a:page])
\ : a:page
let l:url = wiki#url#parse('wiki:/' . l:page)

if !filereadable(l:url.path)
redraw!
call wiki#log#info('Opening new page "' . l:page . '"')
sleep 1
end

call l:url.follow()
call wiki#url#parse('wiki:/' . l:page).follow()
endfunction

"}}}1
Expand Down
86 changes: 86 additions & 0 deletions autoload/wiki/template.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,92 @@
" Email: karl.yngve@gmail.com
"

function! wiki#template#init() abort " {{{1
if filereadable(expand('%')) | return | endif

let l:context = {
\ 'date': strftime("%F"),
\ 'name': expand('%:t:r'),
\ 'origin': wiki#nav#get_previous(),
\ 'path': expand('%:p'),
\ 'path_wiki': wiki#paths#shorten_relative(expand('%:p')),
\ 'time': strftime("%H:%M"),
\}

for l:template in g:wiki_templates
if s:template_match(l:template, l:context)
return s:template_apply(l:template, l:context)
endif
endfor

let l:match = matchlist(expand('%:t:r'), '^\(\d\d\d\d\)_\(\w\)\(\d\d\)$')
if empty(l:match) | return | endif
let [l:year, l:type, l:number] = l:match[1:3]

if l:type ==# 'w'
call wiki#template#weekly_summary(l:year, l:number)
elseif l:type ==# 'm'
call wiki#template#monthly_summary(l:year, l:number)
endif
endfunction

" }}}1

function! wiki#template#case_title(text, ...) abort " {{{1
return join(map(split(a:text), {_, x -> toupper(x[0]) . strpart(x, 1)}))
endfunction

" }}}1


function! s:template_match(t, ctx) abort " {{{1
if has_key(a:t, 'match_re')
return a:ctx.name =~# a:t.match_re
elseif has_key(a:t, 'match_func')
return a:t.match_func(a:ctx)
endif
endfunction

" }}}1
function! s:template_apply(t, ctx) abort " {{{1
if has_key(a:t, 'source_func')
return a:t.source_func(a:ctx)
endif

let l:source = get(a:t, 'source_filename', '')
if !filereadable(l:source) | return | endif

" Interpolate the context "variables"
let l:lines = join(readfile(l:source), "\n")
for [l:key, l:value] in items(a:ctx)
let l:lines = substitute(l:lines, '{' . l:key . '}', l:value, 'g')
endfor

" Interpolate user functions
let [l:match, l:c1, l:c2] = matchstrpos(l:lines, '{{[a-zA-Z#_]\+\s\+[^}]*}}')
while !empty(l:match)
let l:parts = matchlist(l:match, '{{\([a-zA-Z#_]\+\)\s\+\([^}]*\)}}')
let l:func = l:parts[1]
let l:arg = l:parts[2]
try
let l:value = call(l:func, [l:arg])
catch /E117:/
let l:value = ''
endtry

let l:pre = l:lines[:l:c1-1]
let l:post = l:lines[l:c2:]
let l:lines = l:pre . l:value . l:post

let [l:match, l:c1, l:c2] = matchstrpos(
\ l:lines, '{{[a-zA-Z#_]\+\s\+[^}]*}}', l:c2+1)
endwhile

call append(0, split(l:lines, "\n"))
endfunction

" }}}1

function! wiki#template#weekly_summary(year, week) abort " {{{1
let l:parser = s:summary.new()

Expand Down
5 changes: 5 additions & 0 deletions autoload/wiki/url/wiki.vim
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function! s:handler.follow(...) abort dict " {{{1

execute l:cmd fnameescape(self.path)

if !filereadable(self.path)
redraw!
call wiki#log#info('Opened new page "' . self.stripped . '"')
end

if exists('l:old_position')
let b:wiki = get(b:, 'wiki', {})
call wiki#nav#add_to_stack(l:old_position)
Expand Down
Loading