Vim script で URL からタイトルを取得

ちょっと欲しかったので webapi.vim を使用して簡単に書いてみた。

[ソース]

function! s:get_title(url)
    let body = webapi#http#get(a:url).content
    let enc = matchstr(body, 'charset=\zs[^"'']*\ze["'']')
    if empty(enc)
       let enc = matchstr(body, 'charset=["'']\zs[^"'']*\ze["'']')
    endif
    return iconv(matchstr(body, '<title>\zs.*\ze<\/title>'), empty(enc) ? 'uft-8' : enc, &enc)
endfunction

echo s:get_title("http://d.hatena.ne.jp/osyo-manga/20130531/1370008189")
echo s:get_title("http://mattn.kaoriya.net/software/vim/20120409105637.htm")
echo s:get_title("http://en.cppreference.com/w/cpp/thread/async")

[出力]

Vim Advent Calendar 2012 中間発表 - C++でゲームプログラミング
Big Sky :: webapi-vim のネームスペースを変えました。
std::async - cppreference.com


エンコーディング回りの処理は適当。