Vim Tips Wiki
(Updated the code with the change recommended in the comments, then removed the comments.)
(Remove html character entities)
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
If you use Vim to edit html pages or blog entries, you sometimes want to enter links of the form: <tt>&lt;a href="somelink" target="sometarget"&gt;title of the page&lt;/a&gt;</tt>
+
If you use Vim to edit html pages or blog entries, you sometimes want to enter links of the form: <tt><a href="somelink" target="sometarget">title of the page</a></tt>
   
 
With the following script (which only works under Unix with the lynx browser installed) you are interactively asked which URL to add, and what target to use (with the default "_blank"). The title of the page is automagically parsed from the source of the linked page using lynx.
 
With the following script (which only works under Unix with the lynx browser installed) you are interactively asked which URL to add, and what target to use (with the default "_blank"). The title of the page is automagically parsed from the source of the linked page using lynx.
Line 21: Line 21:
 
" The line is added after the current line.
 
" The line is added after the current line.
 
" Author: Gerhard Siegesmund
 
" Author: Gerhard Siegesmund
map &lt;leader&gt;al :call AddLinkToText()&lt;CR&gt;
+
map <Leader>al :call AddLinkToText()<CR>
 
function! AddLinkToText()
 
function! AddLinkToText()
 
let url = input("URL to add? ", "")
 
let url = input("URL to add? ", "")
Line 31: Line 31:
 
" Get the target
 
" Get the target
 
let target = input("Target for this link? ", "_blank")
 
let target = input("Target for this link? ", "_blank")
if strlen(target) &gt; ""
+
if strlen(target) > ""
 
let target = " target=\"" . target . "\""
 
let target = " target=\"" . target . "\""
 
endif
 
endif
Line 37: Line 37:
 
let code = system("lynx -dump -source " . url)
 
let code = system("lynx -dump -source " . url)
 
" Find the title of the page
 
" Find the title of the page
let title = substitute(code, '\c.*head.*&lt;title[^&gt;]*&gt;\(.*\)&lt;\/title&gt;.*head.*', '\1', '')
+
let title = substitute(code, '\c.*head.*<title[^>]*>\(.*\)<\/title>.*head.*', '\1', '')
 
if title == code
 
if title == code
 
" If nothing changed we couldn't find the regular expression
 
" If nothing changed we couldn't find the regular expression
Line 45: Line 45:
 
let title = substitute(title, "\n", " ", "g")
 
let title = substitute(title, "\n", " ", "g")
 
" Output the code
 
" Output the code
let @b = "&lt;a href=\"" . url . "\"" . target . "&gt;" . title . "&lt;/a&gt;"
+
let @b = "<a href=\"" . url . "\"" . target . ">" . title . "</a>"
 
put b
 
put b
 
" Restore b register
 
" Restore b register
Line 53: Line 53:
   
 
==Comments==
 
==Comments==
 
----
 

Revision as of 00:00, 30 September 2008

Tip 1178 Printable Monobook Previous Next

created March 18, 2006 · complexity basic · author Gerhard Siegesmund · version 6.0


If you use Vim to edit html pages or blog entries, you sometimes want to enter links of the form: <a href="somelink" target="sometarget">title of the page</a>

With the following script (which only works under Unix with the lynx browser installed) you are interactively asked which URL to add, and what target to use (with the default "_blank"). The title of the page is automagically parsed from the source of the linked page using lynx.

" This small function simplifies the addition of links to an html-Page. The
" title of the linked page automagically is requested and added to the output
" The line is added after the current line.
" Author: Gerhard Siegesmund
map <Leader>al :call AddLinkToText()<CR>
function! AddLinkToText()
  let url = input("URL to add? ", "")
  if strlen(url) == 0
    return
  endif
  " Save b register
  let saveB = @b
  " Get the target
  let target = input("Target for this link? ", "_blank")
  if strlen(target) > ""
    let target = " target=\"" . target . "\""
  endif
  " Get the source of the page
  let code = system("lynx -dump -source " . url)
  " Find the title of the page
  let title = substitute(code, '\c.*head.*<title[^>]*>\(.*\)<\/title>.*head.*', '\1', '')
  if title == code
    " If nothing changed we couldn't find the regular expression
    let title = "Unknown"
  endif
  " Remove newline-characters (not yet tested!)
  let title = substitute(title, "\n", " ", "g")
  " Output the code
  let @b = "<a href=\"" . url . "\"" . target . ">" . title . "</a>"
  put b
  " Restore b register
  let @b = saveB
endfunction

Comments