" Vimball Archiver by Charles E. Campbell UseVimball finish autoload/ExtractLinks.vim [[[1 42 " ExtractLinks.vim: Replace inline links with unique references and a link table. " " DEPENDENCIES: " - ingo/err.vim autoload script " - ExtractMatches.vim plugin (for :SubstituteAndYankUnique) " " Copyright: (C) 2014 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.00.001 19-Feb-2014 file creation let s:save_cpo = &cpo set cpo&vim function! ExtractLinks#Command( range, arguments ) let l:expressions = ingo#plugin#setting#GetBufferLocal('ExtractLinks_Expressions', []) if empty(l:expressions) call ingo#err#Set('No link expressions defined') return 0 endif try let l:pattern = '\%(' . join(keys(l:expressions), '\|') . '\)' execute printf('%sSubstituteAndYankUnique/%s/%s/g/%s/%s', \ a:range, \ escape(l:pattern, '/'), \ escape(ingo#plugin#setting#GetBufferLocal('ExtractLinks_ReplacementInline'), '/'), \ escape(ingo#plugin#setting#GetBufferLocal('ExtractLinks_ReplacementExtraction'), '/'), \ a:arguments \) return 1 catch /^Vim\%((\a\+)\)\=:/ call ingo#err#SetVimException() return 0 endtry endfunction let &cpo = s:save_cpo unlet s:save_cpo " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : plugin/ExtractLinks.vim [[[1 39 " ExtractLinks.vim: Replace inline links with unique references and a link table. " " DEPENDENCIES: " - ExtractLinks.vim autoload script " - ingo/err.vim autoload script " " Copyright: (C) 2014 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.00.002 20-Feb-2014 Also provide default Expressions configuration. " 001 19-Feb-2014 file creation " Avoid installing twice or when in unsupported Vim version. if exists('g:loaded_ExtractLinks') || (v:version < 700) finish endif let g:loaded_ExtractLinks = 1 "- configuration --------------------------------------------------------------- if ! exists('g:ExtractLinks_Expressions') let g:ExtractLinks_Expressions = {'\%(^\|\s\|[(<\[{]\)\@<=\%(\%(file\|ftp\|gopher\|http\|https\|news\|scp\|ssh\|telnet\):\|\a\+://\)[^ \t<>''"]\+[^ \t.,;:!?)<>]': 'default'} endif if ! exists('g:ExtractLinks_ReplacementInline') let g:ExtractLinks_ReplacementInline = '[\#]' endif if ! exists('g:ExtractLinks_ReplacementExtraction') let g:ExtractLinks_ReplacementExtraction = '[\#] &\n' endif "- commands -------------------------------------------------------------------- command! -bar -nargs=? -range=% ExtractLinks if ! ExtractLinks#Command(',', ) | echoerr ingo#err#Get() | endif " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : ftplugin/html_ExtractLinks.vim [[[1 22 " html_ExtractLinks.vim: Pattern for HTML [links] for reference extraction. " " DEPENDENCIES: " - ExtractLinks.vim plugin " " Copyright: (C) 2014 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.00.001 20-Feb-2014 file creation if ! exists('b:ExtractLinks_Expressions') let b:ExtractLinks_Expressions = {} endif let b:ExtractLinks_Expressions['\[\([^][]\+\)\]'] = 'html' let b:ExtractLinks_ReplacementInline = '[\#]' let b:ExtractLinks_ReplacementExtraction = '
  • \1
  • \n' " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : ftplugin/markdown_ExtractLinks.vim [[[1 28 " markdown_ExtractLinks.vim: Pattern for Markdown links for link extraction. " " DEPENDENCIES: " - ExtractLinks.vim plugin " " Copyright: (C) 2014 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.00.002 20-Feb-2014 Need to filter out the expression from " html_ExtractLinks.vim. " 001 19-Feb-2014 file creation if ! exists('b:ExtractLinks_Expressions') let b:ExtractLinks_Expressions = {} else " Remove the conflicting expression for HTML, as Markdown leverages that " filetype. call filter(b:ExtractLinks_Expressions, 'v:val !=# "html"') endif let b:ExtractLinks_Expressions['\(!\?\)\[\([^]]\+\)\] \?(\([^)]\+\))'] = 'markdown' let b:ExtractLinks_ReplacementInline = '\1[\2][link\#]' let b:ExtractLinks_ReplacementExtraction = '[link\#]: \3 "\2"\n' " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : doc/ExtractLinks.txt [[[1 125 *ExtractLinks.txt* Replace inline links with unique references and a link table. EXTRACT LINKS by Ingo Karkat *ExtractLinks.vim* description |ExtractLinks-description| usage |ExtractLinks-usage| installation |ExtractLinks-installation| configuration |ExtractLinks-configuration| limitations |ExtractLinks-limitations| known problems |ExtractLinks-known-problems| todo |ExtractLinks-todo| history |ExtractLinks-history| ============================================================================== DESCRIPTION *ExtractLinks-description* In forums, emails, documentation, or other plain-text files, one often embeds references directly in the text. But many URLs are quite long, and this often breaks the formatting and natural flow of the text. It would be better to place those below the main text, in a neat reference section. But doing that manually is tedious and error-prone. This plugin leverages the |:SubstituteAndYankUnique| command of the |ExtractMatches.vim| plugin (vimscript #4795) to extract the (configurable) URLs found in the buffer and replace them with unique inline anchors. Additionally, an (again configurable) table of anchor and extracted URL is placed into a register, for easy pasting. The complete link extraction and referencing is therefore reduced to a short and painless > :ExtractMatches | $put In addition to a general global default, the plugin ships with predefined pattern and replacement definitions for: - Markdown [text](URL) ~ - HTML [text] or [link"] ~ SOURCE * Inspired by http://superuser.com/questions/717946/links-to-references-in-mutt-or-vim ============================================================================== USAGE *ExtractLinks-usage* *:ExtractLinks* :[range]ExtractLinks [x] Extract all links in [range] / the buffer with a (filetype-specific, by default "[N]") unique reference, and generate a corresponding link table that associates the references with the respective original links (default: "[N] {link}"). This is placed into register [x] / the default register, so it can be > :$put < into the buffer (probably at the end, as a legend). EXAMPLE *ExtractLinks-example* This text references http://www.vim.org/ and http://w3.org/. ~ :ExtractLinks This text references [1] and [2]. ~ :put [1] http://www.vim.org/ ~ [2] http://w3.org/ ~ ============================================================================== INSTALLATION *ExtractLinks-installation* This script is packaged as a |vimball|. If you have the "gunzip" decompressor in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress the archive first, e.g. using WinZip. Inside Vim, install by sourcing the vimball or via the |:UseVimball| command. > vim ExtractLinks*.vmb.gz :so % To uninstall, use the |:RmVimball| command. DEPENDENCIES *ExtractLinks-dependencies* - Requires Vim 7.0 or higher. - Requires the |ingo-library.vim| plugin (vimscript #4433), version 1.015 or higher. - Requires the |ExtractMatches.vim| plugin (vimscript #4795), version 1.20 or higher. ============================================================================== CONFIGURATION *ExtractLinks-configuration* For a permanent configuration, put the following commands into your |vimrc|: *b:ExtractLinks_Expressions* *g:ExtractLinks_Expressions* A Dictionary (to avoid multiple identical entries) containing regular expressions (as keys, the values are irrelevant) to capture links in the buffer: > let g:ExtractLinks_Expressions = {'\', 1} < *b:ExtractLinks_ReplacementInline* *g:ExtractLinks_ReplacementInline* The replacement of the matched link inside the buffer: > let g:ExtractLinks_ReplacementInline = '[\#]' < *b:ExtractLinks_ReplacementExtraction* *g:ExtractLinks_ReplacementExtraction* The replacement of the matched link for the link table: > let g:ExtractLinks_ReplacementExtraction = '[\#] &\n' < ============================================================================== LIMITATIONS *ExtractLinks-limitations* KNOWN PROBLEMS *ExtractLinks-known-problems* TODO *ExtractLinks-todo* IDEAS *ExtractLinks-ideas* ============================================================================== HISTORY *ExtractLinks-history* 1.00 20-Feb-2014 First published version. 0.01 19-Feb-2014 Started development. ============================================================================== Copyright: (C) 2014 Ingo Karkat The VIM LICENSE applies to this plugin; see |copyright|. Maintainer: Ingo Karkat ============================================================================== vim:tw=78:ts=8:ft=help:norl: