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
2 changes: 1 addition & 1 deletion autoload/wiki/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function! s:completer_wikilink.complete_page(regex) dict abort " {{{2

call map(l:cands, 'strpart(v:val, strlen(l:root)+1)')
call map(l:cands,
\ empty(g:wiki_link_extension)
\ empty(wiki#link#get_creator('url_extension'))
\ ? 'l:pre . fnamemodify(v:val, '':r'')'
\ : 'l:pre . v:val')
call s:filter_candidates(l:cands, a:regex)
Expand Down
22 changes: 17 additions & 5 deletions autoload/wiki/link.vim
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ endfunction

"}}}1

function! wiki#link#get_creator(...) abort " {{{1
let l:ft = expand('%:e')
if empty(l:ft) || index(g:wiki_filetypes, l:ft) < 0
let l:ft = g:wiki_filetypes[0]
endif
let l:c = get(g:wiki_link_creation, l:ft, g:wiki_link_creation._)

return a:0 > 0 ? l:c[a:1] : l:c
endfunction

" }}}1

function! wiki#link#show(...) abort "{{{1
let l:link = wiki#link#get()

Expand Down Expand Up @@ -162,16 +174,16 @@ endfunction
" }}}1

function! wiki#link#template(url, text) abort " {{{1
"
" Pick the relevant link template command to use based on the users
" settings. Default to the wiki style one if its not set.
"

try
return wiki#link#{g:wiki_link_target_type}#template(a:url, a:text)
let l:type = wiki#link#get_creator('link_type')
return wiki#link#{l:type}#template(a:url, a:text)
catch /E117:/
call wiki#log#warn(
\ 'Link target type does not exist: ' . g:wiki_link_target_type,
\ 'See ":help g:wiki_link_target_type" for help'
\ 'Target link type does not exist: ' . l:type,
\ 'See ":help g:wiki_link_creation" for help'
\)
endtry
endfunction
Expand Down
29 changes: 15 additions & 14 deletions autoload/wiki/link/word.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ function! s:matcher.toggle_template(text, _) abort " {{{1
" This template returns a wiki template for the provided word(s). It does
" a smart search for likely candidates and if there is no unique match, it
" asks for target link.
let l:creator = wiki#link#get_creator()

" Allow custom map of text -> url, text (without extension)
if !empty(g:wiki_map_text_to_link)
\ && (type(g:wiki_map_text_to_link) == v:t_func
\ || exists('*' . g:wiki_map_text_to_link))
let [l:url, l:text] = call(g:wiki_map_text_to_link, [a:text])
else
let l:url = a:text
let l:text = a:text
" Apply url transformer if available
let l:url = a:text
if has_key(l:creator, 'url_transform')
try
let l:url = l:creator.url_transform(a:text)
catch
call wiki#log#warn('There was a problem with the url transformer!')
endtry
endif

" Append extension if wanted
let l:url_root = l:url
if !empty(g:wiki_link_extension)
if !empty(l:creator.url_extension)
\ && strcharpart(l:url, strchars(l:url)-1) !=# '/'
let l:url .= g:wiki_link_extension
let l:url .= l:creator.url_extension
let l:url_actual = l:url
else
let l:url_actual = l:url . '.' . b:wiki.extension
Expand All @@ -47,7 +48,7 @@ function! s:matcher.toggle_template(text, _) abort " {{{1
" First try local page
let l:root_current = expand('%:p:h')
if filereadable(wiki#paths#s(printf('%s/%s', l:root_current, l:url_actual)))
return wiki#link#template(l:url, l:text)
return wiki#link#template(l:url, a:text)
endif

" If we are inside the journal, then links should by default point to the
Expand All @@ -58,7 +59,7 @@ function! s:matcher.toggle_template(text, _) abort " {{{1

" Check if target matches at wiki root
if filereadable(wiki#paths#s(printf('%s/%s', l:root, l:url_actual)))
return wiki#link#template(l:prefix . l:url, l:text)
return wiki#link#template(l:prefix . l:url, a:text)
endif
else
let l:root = l:root_current
Expand All @@ -73,15 +74,15 @@ function! s:matcher.toggle_template(text, _) abort " {{{1

" Solve trivial cases first
if len(l:candidates) == 0
return wiki#link#template(l:prefix . l:url, l:text)
return wiki#link#template(l:prefix . l:url, a:text)
endif

" Select with menu
let l:new = l:url . ' (NEW PAGE)'
let l:choice = wiki#ui#select(l:candidates + [l:new])
return empty(l:choice) ? l:url : (
\ l:choice ==# l:new
\ ? wiki#link#template(l:url, l:text)
\ ? wiki#link#template(l:url, a:text)
\ : wiki#link#template(l:prefix . l:choice, ''))
endfunction

Expand Down
30 changes: 18 additions & 12 deletions autoload/wiki/page.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ function! wiki#page#open(...) abort "{{{1
\ : wiki#ui#input(#{info: 'Open page (or create new): '})
if empty(l:page) | return | endif

if !empty(g:wiki_map_create_page)
\ && (type(g:wiki_map_create_page) == v:t_func
\ || exists('*' . g:wiki_map_create_page))
let l:page = call(g:wiki_map_create_page, [l:page])
" Apply url transformer if available
let l:link_creator = wiki#link#get_creator()
if has_key(l:link_creator, 'url_transform')
try
let l:page = l:link_creator.url_transform(l:page)
catch
call wiki#log#warn('There was a problem with the url transformer!')
endtry
endif

call wiki#url#parse('wiki:/' . l:page).follow()
Expand Down Expand Up @@ -427,7 +431,7 @@ function! s:path_to_url(root, path) abort " {{{1
let l:path = wiki#paths#relative(a:path, a:root)
let l:ext = '.' . fnamemodify(l:path, ':e')

return l:ext ==# g:wiki_link_extension
return l:ext ==# wiki#link#get_creator('url_extension')
\ ? l:path
\ : fnamemodify(l:path, ':r')
endfunction
Expand Down Expand Up @@ -477,24 +481,26 @@ endfunction

" }}}1
function! s:convert_links_to_html(lines) abort " {{{1
if g:wiki_link_target_type ==# 'md'
let l:creator = wiki#link#get_creator()

if l:creator.link_type ==# 'md'
let l:rx = '\[\([^\\\[\]]\{-}\)\]'
\ . '(\([^\(\)\\]\{-}\)' . g:wiki_link_extension
\ . '(\([^\(\)\\]\{-}\)' . l:creator.url_extension
\ . '\(#[^#\(\)\\]\{-}\)\{-})'
let l:sub = '[\1](\2.html\3)'
elseif g:wiki_link_target_type ==# 'wiki'
let l:rx = '\[\[\([^\\\[\]]\{-}\)' . g:wiki_link_extension
elseif l:creator.link_type ==# 'wiki'
let l:rx = '\[\[\([^\\\[\]]\{-}\)' . l:creator.url_extension
\ . '\(#[^#\\\[\]]\{-}\)'
\ . '|\([^\[\]\\]\{-}\)\]\]'
let l:sub = '\[\[\1.html\2|\3\]\]'
elseif g:wiki_link_target_type ==# 'org'
let l:rx = '\[\[\([^\\\[\]]\{-}\)' . g:wiki_link_extension
elseif l:creator.link_type ==# 'org'
let l:rx = '\[\[\([^\\\[\]]\{-}\)' . l:creator.url_extension
\ . '\(#[^#\\\[\]]\{-}\)'
\ . '\]\[\([^\[\]\\]\{-}\)\]\]'
let l:sub = '\[\[\1.html\2|\3\]\]'
else
return wiki#log#error(
\ 'g:wiki_link_target_type must be `wiki`, `md`, or `org` to',
\ 'g:wiki_link_creator link_type must be `wiki`, `md`, or `org` to',
\ 'replace link extensions on export.'
\)
endif
Expand Down
Loading