Vim Tips Wiki
(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
Tag: rollback
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1178
 
|id=1178
|previous=1174
+
|previous=1173
 
|next=1180
 
|next=1180
|created=March 18, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Gerhard Siegesmund
 
|author=Gerhard Siegesmund
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
When editing HTML pages or blog entries, you may need to enter an HTML link. This tip show a simple mapping to manually make a link from a URL and a page title. An alternative uses a script to automatically read the title by getting and parsing the HTML source from the website identified by the URL.
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>
 
   
  +
==Manually making a link==
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.
 
  +
Suppose you have some lines like this:
  +
<pre>
  +
This identifies a website:
  +
http://www.example.com
  +
The Example Site
  +
</pre>
   
  +
With the mapping below, you can move the cursor to anywhere in the second line (the target URL), then press F7 to convert the text to:
 
<pre>
 
<pre>
  +
This identifies a website:
" This small function simplifies the addition of links to an html-Page. The
 
  +
<a href="http://www.example.com">The Example Site</a>
" title of the linked page automagically is requested and added to the output
 
  +
</pre>
" The line is added after the current line.
 
  +
" Author: Gerhard Siegesmund
 
  +
The mapping for your [[vimrc]] is:
map <Leader>al :call AddLinkToText()<CR>
 
  +
<pre>
function! AddLinkToText()
 
  +
" Convert two lines (URL then TITLE) to one line: <a href=URL>TITLE</a>
let url = input("URL to add? ", "")
 
  +
map <F7> <Esc>I<a href="<Esc>A"><Esc>gJA</a><Esc>
if strlen(url) == 0
 
  +
</pre>
  +
  +
==Automatically determining the title==
  +
With the following script in your [[vimrc]], you can easily insert a link looking like this:
  +
<pre>
  +
<a href="URL" target="TARGET">PAGE TITLE</a>
  +
</pre>
  +
  +
You are prompted to enter the <code>URL</code> and <code>TARGET</code>. The script automatically determines <code>PAGE TITLE</code> by reading the HTML source from the website at URL.
  +
  +
When you are prompted to enter text, the normal command line shortcuts are available. For example, press Ctrl-R then <code>+</code> to insert the contents of the clipboard, or press the Up arrow key to scroll through the input history.
  +
  +
Assuming the default backslash leader key, you would type <code>\al</code> to add a link. You are prompted to enter the URL, and the target; if no target is entered, the link will not include a target field. The link is inserted after the line containing the cursor.
  +
<pre>
  +
" Prompt user to enter URL and optional TARGET.
  +
" Inserts an html link: a line after the cursor like
  +
" <a href="URL" target="TARGET">Page title</a>
  +
" where 'Page title' is determined from the html source read from URL.
  +
" Requires wget (or similar) tool to get source.
 
nnoremap <Leader>al :call AddLink()<CR>
 
function! AddLink()
 
let url = input('URL to add? ')
 
if empty(url)
 
return
 
return
 
endif
 
endif
 
let target = input('Target for this link? ')
" Save b register
 
 
if !empty(target)
let saveB = @b
 
" Get the target
+
let target = ' target="' . target . '"'
let target = input("Target for this link? ", "_blank")
 
if strlen(target) > ""
 
let target = " target=\"" . target . "\""
 
 
endif
 
endif
 
let html = system('wget -q -O - ' . shellescape(url))
" Get the source of the page
 
 
let regex = '\c.*head.*<title[^>]*>\_s*\zs.\{-}\ze\_s*<\/title>'
let code = system("lynx -dump -source " . url)
 
 
let title = substitute(matchstr(html, regex), "\n", ' ', 'g')
" Find the title of the page
 
  +
if empty(title)
let title = substitute(code, '\c.*head.*<title[^>]*>\(.*\)<\/title>.*head.*', '\1', '')
 
if title == code
+
let title = 'Unknown'
" If nothing changed we couldn't find the regular expression
 
let title = "Unknown"
 
 
endif
 
endif
 
put ='<a href=\"' . url . '\"' . target . '>' . title . '</a>'
" 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
 
endfunction
  +
</pre>
  +
  +
===Alternative tools===
  +
The script above includes this line which assumes you have [[wikipedia:Wget|<code>wget</code>]] installed (<code>-q</code> is quiet, and <code>-O -</code> outputs to stdout):
  +
<pre>
  +
let html = system('wget -q -O - ' . shellescape(url))
  +
</pre>
  +
  +
An alternative to <code>wget</code> would be to use the following if you have [[wikipedia:cURL|<code>curl</code>]] (<code>-s</code> is silent):
  +
<pre>
  +
let html = system('curl -s ' . shellescape(url))
  +
</pre>
  +
  +
Or use the following if you have [[wikipedia:Lynx (web browser)|<code>lynx</code>]] (<code>-source</code> outputs the source rather than a text rendering of the html):
  +
<pre>
  +
let html = system('lynx -source ' . shellescape(url))
 
</pre>
 
</pre>
   

Revision as of 06:11, 13 July 2012

Tip 1178 Printable Monobook Previous Next

created 2006 · complexity basic · author Gerhard Siegesmund · version 6.0


When editing HTML pages or blog entries, you may need to enter an HTML link. This tip show a simple mapping to manually make a link from a URL and a page title. An alternative uses a script to automatically read the title by getting and parsing the HTML source from the website identified by the URL.

Manually making a link

Suppose you have some lines like this:

This identifies a website:
http://www.example.com
The Example Site

With the mapping below, you can move the cursor to anywhere in the second line (the target URL), then press F7 to convert the text to:

This identifies a website:
<a href="http://www.example.com">The Example Site</a>

The mapping for your vimrc is:

" Convert two lines (URL then TITLE) to one line: <a href=URL>TITLE</a>
map <F7> <Esc>I<a href="<Esc>A"><Esc>gJA</a><Esc>

Automatically determining the title

With the following script in your vimrc, you can easily insert a link looking like this:

<a href="URL" target="TARGET">PAGE TITLE</a>

You are prompted to enter the URL and TARGET. The script automatically determines PAGE TITLE by reading the HTML source from the website at URL.

When you are prompted to enter text, the normal command line shortcuts are available. For example, press Ctrl-R then + to insert the contents of the clipboard, or press the Up arrow key to scroll through the input history.

Assuming the default backslash leader key, you would type \al to add a link. You are prompted to enter the URL, and the target; if no target is entered, the link will not include a target field. The link is inserted after the line containing the cursor.

" Prompt user to enter URL and optional TARGET.
" Inserts an html link: a line after the cursor like
"   <a href="URL" target="TARGET">Page title</a>
" where 'Page title' is determined from the html source read from URL.
" Requires wget (or similar) tool to get source.
nnoremap <Leader>al :call AddLink()<CR>
function! AddLink()
  let url = input('URL to add? ')
  if empty(url)
    return
  endif
  let target = input('Target for this link? ')
  if !empty(target)
    let target = ' target="' . target . '"'
  endif
  let html = system('wget -q -O - ' . shellescape(url))
  let regex = '\c.*head.*<title[^>]*>\_s*\zs.\{-}\ze\_s*<\/title>'
  let title = substitute(matchstr(html, regex), "\n", ' ', 'g')
  if empty(title)
    let title = 'Unknown'
  endif
  put ='<a href=\"' . url . '\"' . target . '>' . title . '</a>'
endfunction

Alternative tools

The script above includes this line which assumes you have wget installed (-q is quiet, and -O - outputs to stdout):

  let html = system('wget -q -O - ' . shellescape(url))

An alternative to wget would be to use the following if you have curl (-s is silent):

  let html = system('curl -s ' . shellescape(url))

Or use the following if you have lynx (-source outputs the source rather than a text rendering of the html):

  let html = system('lynx -source ' . shellescape(url))

Comments