From f6c419fb879626a61f01272cd409e4f15a62cb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Schuhb=C3=A4ck?= Date: Wed, 21 Apr 2021 16:16:46 +0200 Subject: [PATCH] Add wikilocal option showing description in generated links --- autoload/vimwiki/base.vim | 12 +++++++--- autoload/vimwiki/vars.vim | 3 +++ doc/vimwiki.txt | 15 ++++++++++++ test/link_generation.vader | 49 +++++++++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index 6088cf6..2953577 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -437,12 +437,14 @@ function! vimwiki#base#generate_links(create, ...) abort function! GeneratorLinks.f() abort let lines = [] - let links = vimwiki#base#get_wikilinks(vimwiki#vars#get_bufferlocal('wiki_nr'), 0, s:pattern) + let wiki_nr = vimwiki#vars#get_bufferlocal('wiki_nr') + let links = vimwiki#base#get_wikilinks(wiki_nr, 0, s:pattern) call sort(links) let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' ' let l:diary_file_paths = vimwiki#diary#get_diary_files() + let use_caption = vimwiki#vars#get_wikilocal('generated_links_caption', wiki_nr) for link in links let link_infos = vimwiki#base#resolve_link(link) if !vimwiki#base#is_diary_file(link_infos.filename, copy(l:diary_file_paths)) @@ -451,14 +453,18 @@ function! vimwiki#base#generate_links(create, ...) abort let link_caption = vimwiki#base#read_caption(link_infos.filename) if link_caption ==? '' " default to link if caption not found let link_caption = link + else + if use_caption + " switch to [[URL|DESCRIPTION]] if caption is not empty + " Link2 is the same for mardown syntax + let link_tpl = vimwiki#vars#get_syntaxlocal('Link2') + endif endif - " Replace Url, Description let entry = s:safesubstitute(link_tpl, '__LinkUrl__', link, '') let entry = s:safesubstitute(entry, '__LinkDescription__', link_caption, '') " Replace Extension - let wiki_nr = vimwiki#vars#get_bufferlocal('wiki_nr') let extension = vimwiki#vars#get_wikilocal('ext', wiki_nr) let entry = substitute(entry, '__FileExtension__', extension, 'g') diff --git a/autoload/vimwiki/vars.vim b/autoload/vimwiki/vars.vim index ed1a731..d076277 100644 --- a/autoload/vimwiki/vars.vim +++ b/autoload/vimwiki/vars.vim @@ -507,6 +507,7 @@ function! s:get_default_wikilocal() abort \ 'bullet_types': {'type': type([]), 'default': []}, \ 'cycle_bullets': {'type': type(0), 'default': 0}, \ 'html_filename_parameterization': {'type': type(0), 'default': 0, 'min': 0, 'max': 1}, + \ 'generated_links_caption': {'type': type(0), 'default': 0 }, \ 'index': {'type': type(''), 'default': 'index', 'min_length': 1}, \ 'links_space_char': {'type': type(''), 'default': ' ', 'min_length': 1}, \ 'list_ignore_newline': {'type': type(0), 'default': 1, 'min': 0, 'max': 1}, @@ -1285,8 +1286,10 @@ function! s:normalize_syntax_settings(syntax) abort " command generate link form file name (generate_link) if a:syntax ==# 'markdown' let syntax_dic.Link1 = syntax_dic.Weblink1Template + let syntax_dic.Link2 = syntax_dic.Weblink1Template else let syntax_dic.Link1 = vimwiki#vars#get_global('WikiLinkTemplate1') + let syntax_dic.Link2 = vimwiki#vars#get_global('WikiLinkTemplate2') endif endfunction diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt index 9a4a8f7..64a0473 100644 --- a/doc/vimwiki.txt +++ b/doc/vimwiki.txt @@ -2672,6 +2672,18 @@ If set to 1 (true), cycle through |bullet_types| when changing list element identation +*vimwiki-option-generated_links_caption* +------------------------------------------------------------------------------ +Key Default value~ +generated_links_caption 0 + +Default syntax only. If set to 1 (true), use the [[URL|DESCRIPTIOM]] when +calling |:VimwikiGenerateLinks|. DESCRIPTION will be the first header in the +corresbonding file found in the first n lines, where n is set by +|g:vimwiki_max_scan_for_caption| (Default 5). If no caption is found fallback +to [[URL]] only. + + *vimwiki-option-listsyms* ------------------------------------------------------------------------------ Key Default value~ @@ -3900,6 +3912,7 @@ Contributors and their Github usernames in roughly chronological order: - Fergus Collins (@C-Fergus) - Matthew Toohey (@mtoohey31) - Brennen Bearnes + - Stefan Schuhbäck (@stefanSchuhbaeck) ============================================================================== 16. Changelog *vimwiki-changelog* @@ -3911,6 +3924,8 @@ http://code.google.com/p/vimwiki/issues/list. They may be accessible from https://github.com/vimwiki-backup/vimwiki/issues. New:~ + * Feature: Add option to use link Description in default syntax if + caption is present. |generated_links_caption| * PR #1106: Fix trailing closing brace and comma typo * Feature: VimwikiColorize maps in visual and normal mode `wc` * Feature: PR #686: add a flag lists_return to disable mappings diff --git a/test/link_generation.vader b/test/link_generation.vader index cd42a12..65b1cf3 100644 --- a/test/link_generation.vader +++ b/test/link_generation.vader @@ -1,6 +1,6 @@ # Automatic link generation # Related to: -# - wiki file discovery +# - wiki file discovery # - buffer list insertion (see: vimwiki#base#update_listing_in_buffer) @@ -9,11 +9,11 @@ Execute (Reset sw to default (due to batch)): # 1 VimwikiGenerateLinks {{{1 ########################## -# Wiki Syntax {{{2 +# Wiki Syntax (no caption, default) {{{2 ################# Execute (Log): - Log 'Wiki Syntax' + Log 'Wiki Syntax (no caption, default)' call ReloadVimwiki() AssertEqual '-1_margin', vimwiki#vars#get_wikilocal('list_margin') . '_margin' @@ -52,6 +52,49 @@ Expect (The links with a header (bis)): Execute (Clean Test.wiki): call DeleteFile('$HOME/testwiki/Test.wiki') +# Wiki Syntax (with caption) {{{2 +################# + +Execute (Log): + Log 'Wiki Syntax (with caption)' + let vimwiki_default.generated_links_caption = 1 + call ReloadVimwiki() + AssertEqual '-1_margin', vimwiki#vars#get_wikilocal('list_margin') . '_margin' + +Given (Void): + +Execute (VimwikiGenerateLinks): + edit $HOME/testwiki/Test.wiki + VimwikiGenerateLinks + +Expect (The links with a header): + + + = Generated Links = + - [[buzz_bozz|Buzz Bozz]] + - [[index|Test Wiki]] + - [[link_syntax]] + - [[link_syntax/nested]] + +Execute (VimwikiGenerateLinks x 2): + edit $HOME/testwiki/Test.wiki + VimwikiGenerateLinks + call append('$', 'Last Line') + VimwikiGenerateLinks + +Expect (The links with a header (bis)): + + + = Generated Links = + - [[buzz_bozz|Buzz Bozz]] + - [[index|Test Wiki]] + - [[link_syntax]] + - [[link_syntax/nested]] + + Last Line + +Execute (Clean Test.wiki): + call DeleteFile('$HOME/testwiki/Test.wiki') # Markdown Syntax {{{2 #################