randomfox (randomfox) wrote,
randomfox
randomfox

Vim: Convert HTML from "My Caches" page at geocaching.com into a list of cache links

This Vim macro takes the HTML table from the "My Caches" page at geocaching.com and converts it into a simple list of cache links. In order to use this macro:
  1. Go to the "My Caches" page.
  2. Invoke "View Source" in the web browser.
  3. Copy the portion of the table to be converted.
  4. Paste it into Vim.
  5. Enter visual mode and select the text that was pasted.
  6. Invoke the macro.



" ,M Convert new-style my caches HTML to a list of links.
function! Convert_mycaches_2() range
    let outstr = ""
    let lineno = a:firstline
    let m1 = '<A HREF="http://www\.geocaching\.com/seek/cache_details\.aspx.*</A>'
    let m2 = '.*<td.*>\(.*\)</td>.*'
    while lineno <= a:lastline
	let linkstr = matchstr(getline(lineno), m1, 0)

	if linkstr != ""

	    " Make the link tag lowercase.
	    let linkstr = substitute(linkstr, '<A HREF=', '\L&', '')
	    let linkstr = substitute(linkstr, '</A>', '\L&', '')

	    let lineno = lineno + 1

	    " Extract the state from the next line and insert into the link.
	    while lineno <= a:lastline
		let state = substitute(getline(lineno), m2, '\1', '')
		if state != ""
		    let outstr = outstr . substitute(linkstr, '</a>', ' (' . state . ')&\r', '')
		    break
		endif
		let lineno = lineno + 1
	    endwhile
	endif
	let lineno = lineno + 1
    endwhile
    silent execute "normal :" . a:firstline . "," . a:lastline . "change\<cr>" . outstr . "\<esc>"
endfunction

vmap ,M :call Convert_mycaches_2()<cr>

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments