Fix: Link substitution of __FileExtension__ #914

This commit is contained in:
Tinmarino 2020-06-16 17:47:40 -04:00
parent 865b402c06
commit e1826702b7
3 changed files with 33 additions and 16 deletions

View File

@ -2189,7 +2189,7 @@ endfunction
" Construct a regular expression matching from template (with special " Construct a regular expression matching from template (with special
" characters properly escaped), by substituting rxUrl for __LinkUrl__, rxDesc " characters properly escaped), by substituting rxUrl for __LinkUrl__, rxDesc
" for __LinkDescription__, rxStyle for __LinkStyle__ and rxExtension for " for __LinkDescription__, rxStyle for __LinkStyle__ and rxExtension for
" __FileExtention__. The four arguments rxUrl, rxDesc, rxStyle and " __FileExtension__. The four arguments rxUrl, rxDesc, rxStyle and
" rxExtension are copied verbatim, without any special character escapes or " rxExtension are copied verbatim, without any special character escapes or
" substitutions. " substitutions.
function! vimwiki#base#apply_template(template, rxUrl, rxDesc, rxStyle, rxExtension) abort function! vimwiki#base#apply_template(template, rxUrl, rxDesc, rxStyle, rxExtension) abort
@ -2204,7 +2204,7 @@ function! vimwiki#base#apply_template(template, rxUrl, rxDesc, rxStyle, rxExtens
let lnk = s:safesubstitute(lnk, '__LinkStyle__', a:rxStyle, 'g') let lnk = s:safesubstitute(lnk, '__LinkStyle__', a:rxStyle, 'g')
endif endif
if a:rxExtension !=? '' if a:rxExtension !=? ''
let lnk = s:safesubstitute(lnk, '__FileExtention__', a:rxExtension, 'g') let lnk = s:safesubstitute(lnk, '__FileExtension__', a:rxExtension, 'g')
endif endif
return lnk return lnk
endfunction endfunction
@ -2254,6 +2254,7 @@ endfunction
" Treat link string towards normalization " Treat link string towards normalization
" [__LinkDescription__](__LinkUrl__.FileExtension)
function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) abort function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) abort
let url = matchstr(a:str, a:rxUrl) let url = matchstr(a:str, a:rxUrl)
if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' && vimwiki#vars#get_global('markdown_link_ext') if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' && vimwiki#vars#get_global('markdown_link_ext')
@ -2266,8 +2267,11 @@ function! vimwiki#base#normalize_link_helper(str, rxUrl, rxDesc, template) abort
let descr = s:clean_url(url) let descr = s:clean_url(url)
if descr ==# '' | return url | endif if descr ==# '' | return url | endif
endif endif
" Substiture placeholders
let lnk = s:safesubstitute(a:template, '__LinkDescription__', descr, '') let lnk = s:safesubstitute(a:template, '__LinkDescription__', descr, '')
let lnk = s:safesubstitute(lnk, '__LinkUrl__', url, '') let lnk = s:safesubstitute(lnk, '__LinkUrl__', url, '')
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
let lnk = s:safesubstitute(lnk, '__FileExtension__', file_extension , '')
return lnk return lnk
endfunction endfunction
@ -2358,12 +2362,16 @@ function! s:normalize_link_syntax_n() abort
let sub = s:safesubstitute( let sub = s:safesubstitute(
\ vimwiki#vars#get_global('WikiLinkTemplate1'), '__LinkUrl__', lnk, '') \ vimwiki#vars#get_global('WikiLinkTemplate1'), '__LinkUrl__', lnk, '')
endif endif
" Replace file extension
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
let sub = s:safesubstitute(sub, '__FileExtension__', file_extension , '')
call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub) call vimwiki#base#replacestr_at_cursor('\V'.lnk, sub)
return return
endif endif
endfunction endfunction
" TODO mutualize most code with syntax_n
" Normalize link in visual mode Enter keypress " Normalize link in visual mode Enter keypress
function! s:normalize_link_syntax_v() abort function! s:normalize_link_syntax_v() abort
let sel_save = &selection let sel_save = &selection
@ -2376,12 +2384,16 @@ function! s:normalize_link_syntax_v() abort
normal! gv""y normal! gv""y
" Set substitution " Set substitution
" Replace Url
if vimwiki#base#is_diary_file(expand('%:p')) if vimwiki#base#is_diary_file(expand('%:p'))
let sub = vimwiki#base#normalize_link_in_diary(@") let sub = vimwiki#base#normalize_link_in_diary(@")
else else
let sub = s:safesubstitute(vimwiki#vars#get_global('WikiLinkTemplate1'), let sub = s:safesubstitute(vimwiki#vars#get_global('WikiLinkTemplate1'),
\ '__LinkUrl__', @", '') \ '__LinkUrl__', @", '')
endif endif
" Replace file extension
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
let sub = s:safesubstitute(sub, '__FileExtension__', file_extension , '')
" Put substitution in register " and change text " Put substitution in register " and change text
let sc = vimwiki#vars#get_wikilocal('links_space_char') let sc = vimwiki#vars#get_wikilocal('links_space_char')

View File

@ -130,10 +130,16 @@ function! s:normalize_link_syntax_v() abort
\ '__LinkUrl__', visual_selection, '') \ '__LinkUrl__', visual_selection, '')
endif endif
" replace spaces with new character if option is set " Replace spaces with new character if option is set
let link = substitute(link, '\s', vimwiki#vars#get_wikilocal('links_space_char'), 'g') let link = substitute(link, '\s', vimwiki#vars#get_wikilocal('links_space_char'), 'g')
" Replace description
let link = s:safesubstitute(link, '__LinkDescription__', visual_selection, '') let link = s:safesubstitute(link, '__LinkDescription__', visual_selection, '')
" Replace file extension
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
let link = s:safesubstitute(link, '__FileExtension__', file_extension , '')
call setreg('"', substitute(link, '\n', '', ''), visualmode()) call setreg('"', substitute(link, '\n', '', ''), visualmode())
" paste result " paste result
@ -148,15 +154,12 @@ endfunction
function! vimwiki#markdown_base#normalize_link(is_visual_mode) abort function! vimwiki#markdown_base#normalize_link(is_visual_mode) abort
if 0
" Syntax-specific links if !a:is_visual_mode
else call s:normalize_link_syntax_n()
if !a:is_visual_mode elseif line("'<") == line("'>")
call s:normalize_link_syntax_n() " action undefined for multi-line visual mode selections
elseif line("'<") == line("'>") call s:normalize_link_syntax_v()
" action undefined for multi-line visual mode selections
call s:normalize_link_syntax_v()
endif
endif endif
endfunction endfunction

View File

@ -874,10 +874,12 @@ function! s:populate_extra_markdown_vars() abort
let mkd_syntax.rxWeblink1Suffix = ')' let mkd_syntax.rxWeblink1Suffix = ')'
let mkd_syntax.rxWeblink1EscapeCharsSuffix = '\(\\\)\@<!\()\)' let mkd_syntax.rxWeblink1EscapeCharsSuffix = '\(\\\)\@<!\()\)'
let mkd_syntax.rxWeblink1Separator = '](' let mkd_syntax.rxWeblink1Separator = ']('
let rxWeblink1Ext = '' let rxWeblink1Ext = ''
if vimwiki#vars#get_global('markdown_link_ext') if vimwiki#vars#get_global('markdown_link_ext')
let rxWeblink1Ext = '__FileExtention__' let rxWeblink1Ext = '__FileExtension__'
endif endif
" [DESCRIPTION](URL) " [DESCRIPTION](URL)
let mkd_syntax.Weblink1Template = mkd_syntax.rxWeblink1Prefix . '__LinkDescription__'. let mkd_syntax.Weblink1Template = mkd_syntax.rxWeblink1Prefix . '__LinkDescription__'.
\ mkd_syntax.rxWeblink1Separator. '__LinkUrl__'. rxWeblink1Ext. \ mkd_syntax.rxWeblink1Separator. '__LinkUrl__'. rxWeblink1Ext.
@ -902,18 +904,18 @@ function! s:populate_extra_markdown_vars() abort
\ mkd_syntax.rx_wikilink_md_prefix . \ mkd_syntax.rx_wikilink_md_prefix .
\ '.*' . \ '.*' .
\ rx_wikilink_md_separator . \ rx_wikilink_md_separator .
\ '\zs__LinkUrl__\ze\%(#.*\)\?\%(__FileExtention__\)\?'. \ '\zs__LinkUrl__\ze\%(#.*\)\?\%(__FileExtension__\)\?'.
\ mkd_syntax.rx_wikilink_md_suffix . \ mkd_syntax.rx_wikilink_md_suffix .
\ '\|' . \ '\|' .
\ mkd_syntax.rx_wikilink_md_prefix . \ mkd_syntax.rx_wikilink_md_prefix .
\ '\zs__LinkUrl__\ze\%(#.*\)\?\%(__FileExtention__\)\?'. \ '\zs__LinkUrl__\ze\%(#.*\)\?\%(__FileExtension__\)\?'.
\ rx_wikilink_md_separator . \ rx_wikilink_md_separator .
\ mkd_syntax.rx_wikilink_md_suffix . \ mkd_syntax.rx_wikilink_md_suffix .
\ '\|' . \ '\|' .
\ mkd_syntax.rxWeblink1Prefix. \ mkd_syntax.rxWeblink1Prefix.
\ '.*' . \ '.*' .
\ mkd_syntax.rxWeblink1Separator. \ mkd_syntax.rxWeblink1Separator.
\ '\zs__LinkUrl__\ze\%(#.*\)\?\%(__FileExtention__\)\?'. \ '\zs__LinkUrl__\ze\%(#.*\)\?\%(__FileExtension__\)\?'.
\ mkd_syntax.rxWeblink1EscapeCharsSuffix \ mkd_syntax.rxWeblink1EscapeCharsSuffix
" 1. [DESCRIPTION](URL) " 1. [DESCRIPTION](URL)