Created
December 1, 2010 11:24
-
-
Save Sixeight/723361 to your computer and use it in GitHub Desktop.
Ruby Advent Calendar jp source for unite.vim (need mattn's webapi-vim: https://github.com/mattn/webapi-vim)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s:save_cpo = &cpo | |
set cpo&vim | |
let s:source_ruby_advent_jp = { 'name': 'ruby_advent_jp' } | |
let s:source_ruby_advent_en = { 'name': 'ruby_advent_en' } | |
let s:ruby_advent_jp = [] | |
let s:ruby_advent_en = [] | |
function! unite#sources#ruby_advent#open_url(url) | |
if has('win32') | |
exe "!start rundll32 url.dll,FileProtocolHandler " . a:url | |
elseif has('mac') | |
call system("open '" . a:url . "' &") | |
elseif executable('xdg-open') | |
call system("xdg-open '" . a:url . "' &") | |
else | |
call system("firefox '" . a:url . "' &") | |
endif | |
endfunction | |
function! s:get_list(url) | |
let list = [] | |
let res = http#get(a:url) | |
let dom = html#parse(iconv(res.content, 'utf-8', &encoding)) | |
for comment in dom.findAll('div', {'class': 'comment_entry'}) | |
let anchor = comment.childNode('p').childNode('a') | |
if !empty(anchor) | |
let day = comment.childNode('p').child[0] | |
let url = anchor.attr['href'] | |
let name = day . anchor.child[0] | |
call add(list, [name, url]) | |
endif | |
endfor | |
return list | |
endfunction | |
function! s:get_list_jp() | |
let s:ruby_advent_jp = s:get_list('http://atnd.org/events/10430/') | |
endfunction | |
function! s:get_list_en() | |
let s:ruby_advent_en = s:get_list('http://atnd.org/events/10439/') | |
endfunction | |
function! s:source_ruby_advent_jp.gather_candidates(args, context) | |
if empty(s:ruby_advent_jp) | call s:get_list_jp() | endif | |
return map(copy(s:ruby_advent_jp), '{ | |
\ "word": v:val[0], | |
\ "source": "ruby_advent_jp", | |
\ "kind": "command", | |
\ "action__command": "call unite#sources#ruby_advent#open_url(''".v:val[1]."'')" | |
\ }') | |
endfunction | |
function! s:source_ruby_advent_en.gather_candidates(args, context) | |
if empty(s:ruby_advent_en) | call s:get_list_en() | endif | |
return map(copy(s:ruby_advent_en), '{ | |
\ "word": v:val[0], | |
\ "source": "ruby_advent_en", | |
\ "kind": "command", | |
\ "action__command": "call unite#sources#ruby_advent#open_url(''".v:val[1]."'')" | |
\ }') | |
endfunction | |
function! unite#sources#ruby_advent#define() | |
return executable('curl') ? [s:source_ruby_advent_jp, s:source_ruby_advent_en] : [] | |
endfunction | |
let &cpo = s:save_cpo | |
unlet s:save_cpo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
URLに#があるとWindowsでエラー出るっぽいです。
すんません。僕のバグですね。 forkしときました。