Feature: Utility function: linkify to extract link title from url (PR #982 from @jeromg)
This commit is contained in:
		| @@ -2828,6 +2828,79 @@ function! vimwiki#base#search(search_pattern) abort | ||||
|   endtry | ||||
| endfunction | ||||
|  | ||||
| " used by function linkify to extract web page <title> | ||||
| function! s:get_title(match) abort | ||||
|     " Do not overwrite if g:page_title is already set | ||||
|     " when there are multiple <title> tags, only use the first one | ||||
|     " this is a side effect of the substitute's 'n' flag (count number of | ||||
|     " occurences and evaluate \= for each one | ||||
|     if (g:page_title !=# '') | ||||
|         return | ||||
|     endif | ||||
|     let l:title = a:match | ||||
|  | ||||
|     " cleanup title so it's compatible with vimwiki links | ||||
|     let l:title = substitute(l:title, '\\', '', 'g') | ||||
|     let l:title = substitute(l:title, '\[', '(', 'g') | ||||
|     let l:title = substitute(l:title, '\]', ')', 'g') | ||||
|  | ||||
|     " cosmetic cleanup (html entities), maybe more to add | ||||
|     let l:title = substitute(l:title, '<', '<', 'g') | ||||
|     let l:title = substitute(l:title, '>', '>', 'g') | ||||
|     let l:title = substitute(l:title, ' ', ' ', 'g') | ||||
|  | ||||
|     " store title in global var | ||||
|     let g:page_title = l:title | ||||
| endfunction | ||||
|  | ||||
| " transform the url under the cursor to a wiki link | ||||
| function! vimwiki#base#linkify() abort | ||||
|     let g:page_title = '' | ||||
|  | ||||
|     " save existing value of @u and delete url under the cursor into @u | ||||
|     let l:save_reg = @u | ||||
|     exe 'normal! "udiW' | ||||
|  | ||||
|     " create a scratch buffer and switch to it | ||||
|     let current_buf = bufnr('') | ||||
|     let scratch_buf = bufnr('scratch',1) | ||||
|     exe 'sil! ' . scratch_buf . 'buffer' | ||||
|  | ||||
|     " load web page into scratch buffer using Nread with mode=2 | ||||
|     " FIXME: on Windows, with vim 7/8 (not with nvim), makes the cmd.exe window show up (annoying) | ||||
|     exe 'sil! :2Nread ' . @u | ||||
|  | ||||
|     " extract title from html | ||||
|     " Note: if URL cannot be downloaded the buffer is empty or contains a single | ||||
|     " line: 'Not found' | ||||
|     let page_ok=0 | ||||
|     if (wordcount().chars !=0 && getline(1) !=? 'Not found') | ||||
|         let page_ok=1 | ||||
|         " regex seems to work fine, but may not cover all cases | ||||
|         exe 'sil! :keepp %s/\v\<title.{-}\>((.|\r)+)\<\/title\>/\=s:get_title(submatch(1))/n' | ||||
|     endif | ||||
|  | ||||
|     " wipeout scratch buffer and switch to current | ||||
|     exe scratch_buf . 'bwipeout' | ||||
|     exe current_buf . 'buffer' | ||||
|  | ||||
|     if (page_ok) | ||||
|         " use template [[URL|DESCRIPTION]] | ||||
|         let template = g:vimwiki_global_vars.WikiLinkTemplate2 | ||||
|         let link = substitute(template, '__LinkUrl__', @u, '') | ||||
|         let link = substitute(link, '__LinkDescription__', g:page_title==#'' ? @u : g:page_title, '') | ||||
|         exe 'normal! i' . link | ||||
|     else | ||||
|         "if URL could not be downloaded, undo and display message | ||||
|         "TODO: other behaviours may be possible (user options?) | ||||
|         exe 'normal! u' | ||||
|         echomsg 'Error downloading URL: ' . @u | ||||
|     endif | ||||
|  | ||||
|     " restore initial value of @u | ||||
|     let @u = l:save_reg | ||||
| endfunction | ||||
|  | ||||
|  | ||||
| " ------------------------------------------------------------------------- | ||||
| " Load syntax-specific Wiki functionality | ||||
|   | ||||
| @@ -1189,6 +1189,19 @@ as a wiki page. | ||||
| To scan the page for new or changed definitions for reference-links, simply | ||||
| re-open the page ":e<CR>". | ||||
|  | ||||
| Extract title from external links~ | ||||
|  | ||||
| It is possible to automagically extract the title of a web page to create a | ||||
| link. The function #vimwiki#base#linkify() will get the URL under the cursor | ||||
| and replace the url with a markdown link. | ||||
|  | ||||
| If the URL cannot be retrieved, nothing is changed. | ||||
| If the <title> html tag cannot be extracted, the url is used as the link | ||||
| title. | ||||
|  | ||||
| Suggested mapping: > | ||||
|   nnoremap <silent> <leader>uu :call vimwiki#base#linkify()<cr> | ||||
|  | ||||
| ------------------------------------------------------------------------------ | ||||
| 5.3. Headers                                          *vimwiki-syntax-headers* | ||||
|  | ||||
| @@ -3762,6 +3775,7 @@ Contributors and their Github usernames in roughly chronological order: | ||||
|     - Ryan Winograd | ||||
|     - Birger Niklas (@BirgerNi) | ||||
|     - Chip Senkbeil (@chipsenkbeil) | ||||
|     - Jerome Gay (@jeromg) | ||||
|  | ||||
|  | ||||
| ============================================================================== | ||||
| @@ -3774,6 +3788,8 @@ http://code.google.com/p/vimwiki/issues/list. They may be accessible from | ||||
| https://github.com/vimwiki-backup/vimwiki/issues. | ||||
|  | ||||
| New:~ | ||||
|     * Feature: #837 extract web page <title> from URL under cursor and create | ||||
|       a nice wiki link | ||||
|     * Feature: #922 #928: g:vimwiki_tag_format to change the tag format | ||||
|     * Feature: Support Emoji (Conceal and Complete) | ||||
|     * Issue #209: Feature: Markdown: Support SetExt Heading | ||||
|   | ||||
		Reference in New Issue
	
	Block a user