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
27 changes: 24 additions & 3 deletions autoload/wiki/link.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ function! wiki#link#get_creator(...) abort " {{{1
return a:0 > 0 ? l:c[a:1] : l:c
endfunction

" }}}1
function! wiki#link#get_scheme(link_type) abort " {{{1
let l:scheme = get(g:wiki_link_default_schemes, a:link_type, '')

if type(l:scheme) == v:t_dict
let l:scheme = get(l:scheme, expand('%:e'), '')
endif

return l:scheme
endfunction

" }}}1

function! wiki#link#show(...) abort "{{{1
Expand All @@ -82,7 +93,17 @@ function! wiki#link#show(...) abort "{{{1
if empty(l:link) || l:link.type ==# 'word'
call wiki#log#info('No link detected')
else
call wiki#log#info('Link info', l:link.pprint())
let l:viewer = {
\ 'name': 'WikiLinkInfo',
\ 'items': l:link.describe()
\}
function! l:viewer.print_content() abort dict
for [l:key, l:value] in self.items
call append('$', printf(' %-14s %s', l:key, l:value))
endfor
endfunction

call wiki#scratch#new(l:viewer)
endif
endfunction

Expand Down Expand Up @@ -216,7 +237,7 @@ let s:matchers = [
\ wiki#link#ref_collapsed#matcher(),
\ wiki#link#ref_full#matcher(),
\ wiki#link#url#matcher(),
\ wiki#link#shortcite#matcher(),
\ wiki#link#cite#matcher(),
\ wiki#link#date#matcher(),
\ wiki#link#word#matcher(),
\]
Expand All @@ -231,7 +252,7 @@ let s:matchers_real = [
\ wiki#link#org#matcher(),
\ wiki#link#ref_definition#matcher(),
\ wiki#link#url#matcher(),
\ wiki#link#shortcite#matcher(),
\ wiki#link#cite#matcher(),
\]

" }}}1
74 changes: 32 additions & 42 deletions autoload/wiki/link/_template.vim
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function! s:matcher.create_link(match) dict abort " {{{1

" Get link text
let l:match.text = ''
if has_key(self, 'rx_text')
let [l:text, l:c1, l:c2] = s:matchstrpos(l:match.content, self.rx_text)
if has_key(l:match, 'rx_text')
let [l:text, l:c1, l:c2] = s:matchstrpos(l:match.content, l:match.rx_text)
if !empty(l:text)
let l:match.text = l:text
let l:match.text_pos_start = [l:match.pos_start[0], l:match.pos_start[1] + l:c1]
Expand All @@ -56,8 +56,8 @@ function! s:matcher.create_link(match) dict abort " {{{1

" Get link url
let l:match.url = l:match.content
if has_key(self, 'rx_url')
let [l:url, l:c1, l:c2] = s:matchstrpos(l:match.content, self.rx_url)
if has_key(l:match, 'rx_url')
let [l:url, l:c1, l:c2] = s:matchstrpos(l:match.content, l:match.rx_url)
if !empty(l:url)
let l:match.url = l:url
let l:match.url_pos_start = [l:match.pos_start[0], l:match.pos_start[1] + l:c1]
Expand All @@ -66,13 +66,19 @@ function! s:matcher.create_link(match) dict abort " {{{1
endif
let l:match.url_raw = l:match.url

" Matcher specific url parsing
call l:match.parse_url()
" Add scheme to URL if it is missing
let l:match.default_scheme = wiki#link#get_scheme(l:match.type)
if empty(matchstr(l:match.url, '^\w\+:'))
if !empty(l:match.default_scheme)
let l:match.url = l:match.default_scheme . ':' . l:match.url
endif
endif
let l:match.scheme = matchstr(l:match.url, '^\w\+:')

" Add toggle function
if !has_key(l:match, 'toggle_template')
\ && has_key(g:wiki_link_toggles, self.type)
let l:match.toggle_template = function(g:wiki_link_toggles[self.type])
\ && has_key(g:wiki_link_toggles, l:match.type)
let l:match.toggle_template = function(g:wiki_link_toggles[l:match.type])
endif

" Clean up
Expand All @@ -81,20 +87,17 @@ function! s:matcher.create_link(match) dict abort " {{{1
silent! unlet l:match.rx_text
silent! unlet l:match.create_link
silent! unlet l:match.match_at_cursor
silent! unlet l:match.parse_url

if has_key(l:match, 'create_link_post')
call l:match.create_link_post()
unlet l:match.create_link_post
endif

" Return the parsed link
return s:link.new(l:match)
endfunction

"}}}1
function! s:matcher.parse_url() dict abort " {{{1
if !empty(get(self, 'scheme', '')) && empty(matchstr(self.url, '^\w\+:'))
let self.url = self.scheme . ':' . self.url
endif
endfunction

" }}}1


let s:link = {}
Expand All @@ -120,33 +123,20 @@ function! s:link.replace(text) dict abort " {{{1
endfunction

" }}}1
function! s:link.pprint() dict abort " {{{1
let l:out = {
\ 'type': self.type,
\ 'scheme': get(self, 'scheme', 'NONE'),
\ 'text': get(self, 'text', ''),
\ 'url': self.url,
\ 'raw_url': self.url_raw,
\ 'raw': self.content,
\ 'follow': string(get(self, 'follow', '')),
\ 'toggle_template': string(get(self, 'toggle_template', '')),
\}
if l:out.raw_url ==# l:out.url
unlet l:out.raw_url
endif
if l:out.raw ==# l:out.url
unlet l:out.raw
endif
if empty(l:out.text)
unlet l:out.text
endif
if empty(l:out.follow)
unlet l:out.follow
function! s:link.describe() dict abort " {{{1
let l:content = [
\ ['Type:', self.type],
\ ['Match:', self.content],
\ ['URL:', self.url],
\]

if self.url !=# self.url_raw
let l:content += [['URL (raw):', self.url_raw]]
endif
if empty(l:out.toggle_template)
unlet l:out.toggle_template
endif
return l:out

let l:content += [['Description:', empty(self.text) ? 'N/A' : self.text]]

return l:content
endfunction

" }}}1
Expand Down
1 change: 0 additions & 1 deletion autoload/wiki/link/adoc_link.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

function! wiki#link#adoc_link#matcher() abort " {{{1
return extend(wiki#link#_template#matcher(), {
\ 'scheme': 'file',
\ 'type': 'adoc_link',
\ 'rx': g:wiki#rx#link_adoc_link,
\ 'rx_url': '\<link:\%(\[\zs[^]]\+\ze\]\|\zs[^[]\+\ze\)\[[^]]\+\]',
Expand Down
1 change: 0 additions & 1 deletion autoload/wiki/link/adoc_xref_bracket.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
function! wiki#link#adoc_xref_bracket#matcher() abort " {{{1
return extend(wiki#link#_template#matcher(), {
\ 'type': 'adoc_xref_bracket',
\ 'scheme': 'adoc',
\ 'rx': g:wiki#rx#link_adoc_xref_bracket,
\ 'rx_url': '<<\zs\%([^,>]\{-}\ze,[^>]\{-}\|[^>]\{-}\ze\)>>',
\ 'rx_text': '<<[^,>]\{-},\zs[^>]\{-}\ze>>',
Expand Down
1 change: 0 additions & 1 deletion autoload/wiki/link/adoc_xref_inline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
function! wiki#link#adoc_xref_inline#matcher() abort " {{{1
return extend(wiki#link#_template#matcher(), {
\ 'type': 'adoc_xref_inline',
\ 'scheme': 'adoc',
\ 'rx': g:wiki#rx#link_adoc_xref_inline,
\ 'rx_url': '\<xref:\%(\[\zs[^]]\+\ze\]\|\zs[^[]\+\ze\)\[[^]]*\]',
\ 'rx_text': '\<xref:\%(\[[^]]\+\]\|[^[]\+\)\[\zs[^]]*\ze\]',
Expand Down
18 changes: 18 additions & 0 deletions autoload/wiki/link/cite.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
" A simple wiki plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"

function! wiki#link#cite#matcher() abort " {{{1
return extend(wiki#link#_template#matcher(), deepcopy(s:matcher))
endfunction

" }}}1


let s:matcher = {
\ 'type': 'cite',
\ 'rx': wiki#rx#link_cite,
\ 'rx_url': wiki#rx#link_cite_url,
\}
1 change: 0 additions & 1 deletion autoload/wiki/link/date.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
function! wiki#link#date#matcher() abort " {{{1
return extend(wiki#link#_template#matcher(), {
\ 'type': 'date',
\ 'scheme': 'journal',
\ 'rx': g:wiki#rx#date,
\})
endfunction
Expand Down
1 change: 0 additions & 1 deletion autoload/wiki/link/md_fig.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ endfunction


let s:matcher = {
\ 'scheme': 'file',
\ 'type': 'md_fig',
\ 'rx': g:wiki#rx#link_md_fig,
\ 'rx_url': '!\[[^\\\[\]]\{-}\](\zs[^\\]\{-}\ze)',
Expand Down
2 changes: 1 addition & 1 deletion autoload/wiki/link/ref_shortcut.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let s:matcher = {
\ 'rx_target': '\[\zs' . wiki#rx#reflabel . '\ze\]',
\}

function! s:matcher.parse_url() dict abort " {{{1
function! s:matcher.create_link_post() dict abort " {{{1
let self.id = matchstr(self.content, self.rx_target)

" Locate target url
Expand Down
25 changes: 0 additions & 25 deletions autoload/wiki/link/shortcite.vim

This file was deleted.

3 changes: 1 addition & 2 deletions autoload/wiki/link/word.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ endfunction


let s:matcher = {
\ 'rx' : wiki#rx#word,
\ 'scheme' : '',
\ 'type' : 'word',
\ 'rx' : wiki#rx#word,
\}

function! s:matcher.toggle_template(text, _) abort " {{{1
Expand Down
5 changes: 3 additions & 2 deletions autoload/wiki/rx.vim
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ let wiki#rx#link_ref_full =
\ . '[\]\[]\@!'
let wiki#rx#link_ref_definition =
\ '^\s*\[' . wiki#rx#reflabel . '\]:\s\+' . wiki#rx#url
let wiki#rx#link_shortcite = '\%(\s\|^\|\[\)\zs@[-_.a-zA-Z0-9]\+[-_a-zA-Z0-9]'
let wiki#rx#link_cite = '\%(\s\|^\|\[\)\zs@[-_.a-zA-Z0-9]\+[-_a-zA-Z0-9]'
let wiki#rx#link_cite_url = '\%(\s\|^\|\[\)@\zs[-_.a-zA-Z0-9]\+[-_a-zA-Z0-9]'
let wiki#rx#link_wiki = '\[\[\/\?[^\\\]]\{-}\%(|[^\\\]]\{-}\)\?\]\]'
let wiki#rx#link = join([
\ wiki#rx#link_wiki,
Expand All @@ -68,5 +69,5 @@ let wiki#rx#link = join([
\ wiki#rx#link_ref_shortcut,
\ wiki#rx#link_ref_full,
\ wiki#rx#url,
\ wiki#rx#link_shortcite,
\ wiki#rx#link_cite,
\], '\|')
10 changes: 5 additions & 5 deletions autoload/wiki/template.vim
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ endfunction

" }}}1
function! s:summary.parse_link(link) abort dict " {{{1
let l:link = wiki#url#parse(a:link)
if !filereadable(l:link.path) | return '' | endif
let l:url = wiki#url#parse(a:link)
if !filereadable(l:url.path) | return '' | endif

let l:order = 1
let l:entry = {
Expand All @@ -164,7 +164,7 @@ function! s:summary.parse_link(link) abort dict " {{{1
\}

let l:lnum = 0
for l:line in readfile(l:link.path)
for l:line in readfile(l:url.path)
let l:lnum += 1

" Ignore everything after title lines (except in weekly summaries)
Expand Down Expand Up @@ -211,9 +211,9 @@ function! s:summary.parse_link(link) abort dict " {{{1
endif

" Fix pure-anchor links
if l:link.stripped =~# '\d\{4}-\d\d-\d\d'
if l:url.stripped =~# '\d\{4}-\d\d-\d\d'
let l:line = substitute(l:line, '\(\[\[\|\](\)\zs\ze\#',
\ fnamemodify(l:link.path, ':t:r'), 'g')
\ fnamemodify(l:url.path, ':t:r'), 'g')
endif

call add(l:entry.lines, l:line)
Expand Down
Loading