Permalink
Browse files

setup gist plugin

  • Loading branch information...
1 parent ac56e67 commit 40b2c12b111ae739aec13cfd4835dc3b64541bbc @SKoschnicke committed Dec 12, 2012
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,224 @@
+" atom
+" Last Change: 2010-09-10
+" Maintainer: Yasuhiro Matsumoto <mattn.jp@gmail.com>
+" License: This file is placed in the public domain.
+" Reference:
+" http://tools.ietf.org/rfc/rfc5023.txt
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+let s:author_template = {
+\ "name": "",
+\}
+
+let s:link_template = {
+\ "rel": "",
+\ "href": "",
+\}
+
+let s:category_template = {
+\ "term": "",
+\ "scheme": "",
+\ "label": "",
+\}
+
+let s:feed_template = {
+\ "id": "",
+\ "icon": "",
+\ "logo": "",
+\ "title": "",
+\ "link": [],
+\ "category": [],
+\ "author": [],
+\ "contirubutor": [],
+\ "entry": [],
+\}
+
+let s:entry_template = {
+\ "id": "",
+\ "icon": "",
+\ "logo": "",
+\ "title": "",
+\ "link": [],
+\ "category": [],
+\ "author": [],
+\ "contirubutor": [],
+\ "copyright": "",
+\ "content": "",
+\ "content.type": "text/plain",
+\ "content.mode": "escaped",
+\ "summary": "",
+\ "created": "",
+\ "updated": "",
+\}
+
+for s:name in ['author', 'link', 'category', 'feed', 'entry']
+ for s:key in keys(eval('s:'.s:name.'_template'))
+ let key = substitute(s:key, '\.\(.\)', '\=toupper(submatch(1))', '')
+ exe "function s:".s:name."_template.set".toupper(key[0]).key[1:]."(v) dict\n"
+ \. " let self['".s:key."'] = a:v\n"
+ \. "endfunction\n"
+ exe "function s:".s:name."_template.get".toupper(key[0]).key[1:]."() dict\n"
+ \. " return self['".s:key."']\n"
+ \. "endfunction\n"
+ endfor
+endfor
+function s:entry_template.setContentFromFile(file) dict
+ let quote = &shellxquote == '"' ? "'" : '"'
+ let bits = substitute(system("xxd -ps ".quote.file.quote), "[ \n\r]", '', 'g')
+ let self['mode'] = "base64"
+ let self['content'] = webapi#base64#b64encodebin(bits)
+endfunction
+
+unlet s:name
+unlet s:key
+
+function! webapi#atom#newEntry()
+ return deepcopy(s:entry_template)
+endfunction
+
+function! s:createXml(entry)
+ let entry = webapi#xml#createElement("entry")
+ let entry.attr["xmlns"] = "http://purl.org/atom/ns#"
+
+ for key in keys(a:entry)
+ if type(a:entry[key]) == 1 && key !~ '\.'
+ let node = webapi#xml#createElement(key)
+ call node.value(a:entry[key])
+ if key == "content"
+ let node.attr["type"] = a:entry['content.type']
+ let node.attr["mode"] = a:entry['content.mode']
+ endif
+ call add(entry.child, node)
+ endif
+ endfor
+ let xml = '<?xml version="1.0" encoding="utf-8"?>' . entry.toString()
+ return iconv(xml, &encoding, "utf-8")
+endfunction
+
+function! s:createWsse(user, pass)
+ let now = localtime()
+ let nonce = webapi#sha1#sha1(now . " " . now)[0:28]
+ let created = strftime("%Y-%m-%dT%H:%M:%SZ", now)
+ let passworddigest = webapi#base64#b64encodebin(webapi#sha1#sha1(nonce.created.a:pass))
+ let nonce = webapi#base64#b64encode(nonce)
+ return 'UsernameToken Username="'.a:user.'", PasswordDigest="'.passworddigest.'", Nonce="'.nonce.'", Created="'.created.'"'
+endfunction
+
+function! webapi#atom#deleteEntry(uri, user, pass)
+ let res = webapi#http#post(a:uri, "",
+ \ {
+ \ "Content-Type": "application/x.atom+xml",
+ \ "X-WSSE": s:createWsse(a:user, a:pass)
+ \ }, "DELETE")
+ return res
+endfunction
+
+function! webapi#atom#updateEntry(uri, user, pass, entry, ...)
+ let headdata = a:0 > 0 ? a:000[0] : {}
+ let headdata["Content-Type"] = "application/x.atom+xml"
+ let headdata["X-WSSE"] = s:createWsse(a:user, a:pass)
+ let res = webapi#http#post(a:uri, s:createXml(a:entry), headdata, "PUT")
+ let location = filter(res.header, 'v:val =~ "^Location:"')
+ if len(location)
+ return split(location[0], '\s*:\s\+')[1]
+ endif
+ return ''
+endfunction
+
+function! webapi#atom#createEntry(uri, user, pass, entry, ...)
+ let headdata = a:0 > 0 ? a:000[0] : {}
+ let headdata["Content-Type"] = "application/x.atom+xml"
+ let headdata["X-WSSE"] = s:createWsse(a:user, a:pass)
+ let headdata["WWW-Authenticate"] = "WSSE profile=\"UsernameToken\""
+ let res = webapi#http#post(a:uri, s:createXml(a:entry), headdata, "POST")
+ let location = filter(res.header, 'v:val =~ "^Location:"')
+ if len(location)
+ return split(location[0], '\s*:\s\+')[1]
+ endif
+ return ''
+endfunction
+
+function! s:parse_node(target, parent)
+ for node in a:parent.child
+ if type(node) != 4 || !has_key(a:target, node.name)
+ unlet node
+ continue
+ endif
+ if node.name == 'content'
+ let a:target[node.name] = node.value()
+ if has_key(node.attr, 'type')
+ let a:target['content.type'] = node.attr['type']
+ endif
+ if has_key(node.attr, 'type')
+ let a:target['content.type'] = node.attr['type']
+ endif
+ elseif node.name == 'link'
+ let link = deepcopy(s:link_template)
+ for attr in keys(node.attr)
+ if !has_key(link, attr)
+ continue
+ endif
+ let link[attr] = node.attr[attr]
+ endfor
+ call add(a:target.link, link)
+ elseif node.name == 'author'
+ let author = deepcopy(s:author_template)
+ for item in node.child
+ if type(item) == 4 && has_key(author, item.name)
+ let author[item.name] = item.value()
+ endif
+ unlet item
+ endfor
+ call add(a:target.author, author)
+ elseif node.name == 'entry'
+ let entry = deepcopy(s:entry_template)
+ call s:parse_node(entry, node)
+ call add(a:target.entry, entry)
+ elseif type(a:target[node.name]) == 3
+ call add(a:target[node.name], parent.value())
+ else
+ let a:target[node.name] = node.value()
+ endif
+ unlet node
+ endfor
+endfunction
+
+function! webapi#atom#getFeed(uri, user, pass)
+ let headdata = {}
+ if len(a:user) > 0 && len(a:pass) > 0
+ let headdata["X-WSSE"] = s:createWsse(a:user, a:pass)
+ endif
+ let res = webapi#http#get(a:uri, {}, headdata)
+ let dom = webapi#xml#parse(res.content)
+ let feed = deepcopy(s:feed_template)
+ call s:parse_node(feed, dom)
+ return feed
+endfunction
+
+function! webapi#atom#getService(uri, user, pass)
+ let headdata = {}
+ if len(a:user) > 0 && len(a:pass) > 0
+ let headdata["X-WSSE"] = s:createWsse(a:user, a:pass)
+ endif
+ let res = webapi#http#get(a:uri, {}, headdata)
+ return webapi#xml#parse(res.content)
+endfunction
+
+function! webapi#atom#getEntry(uri, user, pass)
+ let headdata = {}
+ if len(a:user) > 0 && len(a:pass) > 0
+ let headdata["X-WSSE"] = s:createWsse(a:user, a:pass)
+ endif
+ let res = webapi#http#get(a:uri, {}, headdata)
+ let dom = webapi#xml#parse(res.content)
+ let entry = deepcopy(s:entry_template)
+ call s:parse_node(entry, dom)
+ return entry
+endfunction
+
+let &cpo = s:save_cpo
+unlet s:save_cpo
+
+" vim:set et:
@@ -0,0 +1,140 @@
+" base64 codec
+" Last Change: 2010-07-25
+" Maintainer: Yukihiro Nakadaira <yukihiro.nakadaira@gmail.com>
+" License: This file is placed in the public domain.
+" Reference:
+" [The Base16, Base32, and Base64 Data Encodings]
+" http://tools.ietf.org/rfc/rfc3548.txt
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+function! webapi#base64#b64encode(data)
+ let b64 = s:b64encode(s:str2bytes(a:data), s:standard_table, '=')
+ return join(b64, '')
+endfunction
+
+function! webapi#base64#b64encodebin(data)
+ let b64 = s:b64encode(s:binstr2bytes(a:data), s:standard_table, '=')
+ return join(b64, '')
+endfunction
+
+function! webapi#base64#b64decode(data)
+ let bytes = s:b64decode(split(a:data, '\zs'), s:standard_table, '=')
+ return s:bytes2str(bytes)
+endfunction
+
+function! webapi#base64#test()
+ if webapi#base64#b64encode("hello, world") ==# "aGVsbG8sIHdvcmxk"
+ echo "test1: ok"
+ else
+ echoerr "test1: failed"
+ endif
+ if webapi#base64#b64encode("hello, worldx") ==# "aGVsbG8sIHdvcmxkeA=="
+ echo "test2: ok"
+ else
+ echoerr "test2: failed"
+ endif
+ if webapi#base64#b64encode("hello, worldxx") ==# "aGVsbG8sIHdvcmxkeHg="
+ echo "test3: ok"
+ else
+ echoerr "test3: falied"
+ endif
+ if webapi#base64#b64encode("hello, worldxxx") ==# "aGVsbG8sIHdvcmxkeHh4"
+ echo "test4: ok"
+ else
+ echoerr "test4: falied"
+ endif
+ if webapi#base64#b64decode(webapi#base64#b64encode("hello, world")) ==# "hello, world"
+ echo "test5: ok"
+ else
+ echoerr "test5: failed"
+ endif
+ if webapi#base64#b64decode(webapi#base64#b64encode("hello, worldx")) ==# "hello, worldx"
+ echo "test6: ok"
+ else
+ echoerr "test6: failed"
+ endif
+ if webapi#base64#b64decode(webapi#base64#b64encode("hello, worldxx")) ==# "hello, worldxx"
+ echo "test7: ok"
+ else
+ echoerr "test7: failed"
+ endif
+ if webapi#base64#b64decode(webapi#base64#b64encode("hello, worldxxx")) ==# "hello, worldxxx"
+ echo "test8: ok"
+ else
+ echoerr "test8: failed"
+ endif
+endfunction
+
+let s:standard_table = [
+ \ "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
+ \ "Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
+ \ "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
+ \ "w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"]
+
+let s:urlsafe_table = [
+ \ "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
+ \ "Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
+ \ "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
+ \ "w","x","y","z","0","1","2","3","4","5","6","7","8","9","-","_"]
+
+function! s:b64encode(bytes, table, pad)
+ let b64 = []
+ for i in range(0, len(a:bytes) - 1, 3)
+ let n = a:bytes[i] * 0x10000
+ \ + get(a:bytes, i + 1, 0) * 0x100
+ \ + get(a:bytes, i + 2, 0)
+ call add(b64, a:table[n / 0x40000])
+ call add(b64, a:table[n / 0x1000 % 0x40])
+ call add(b64, a:table[n / 0x40 % 0x40])
+ call add(b64, a:table[n % 0x40])
+ endfor
+ if len(a:bytes) % 3 == 1
+ let b64[-1] = a:pad
+ let b64[-2] = a:pad
+ endif
+ if len(a:bytes) % 3 == 2
+ let b64[-1] = a:pad
+ endif
+ return b64
+endfunction
+
+function! s:b64decode(b64, table, pad)
+ let a2i = {}
+ for i in range(len(a:table))
+ let a2i[a:table[i]] = i
+ endfor
+ let bytes = []
+ for i in range(0, len(a:b64) - 1, 4)
+ let n = a2i[a:b64[i]] * 0x40000
+ \ + a2i[a:b64[i + 1]] * 0x1000
+ \ + (a:b64[i + 2] == a:pad ? 0 : a2i[a:b64[i + 2]]) * 0x40
+ \ + (a:b64[i + 3] == a:pad ? 0 : a2i[a:b64[i + 3]])
+ call add(bytes, n / 0x10000)
+ call add(bytes, n / 0x100 % 0x100)
+ call add(bytes, n % 0x100)
+ endfor
+ if a:b64[-1] == a:pad
+ unlet a:b64[-1]
+ endif
+ if a:b64[-2] == a:pad
+ unlet a:b64[-1]
+ endif
+ return bytes
+endfunction
+
+function! s:binstr2bytes(str)
+ return map(range(len(a:str)/2), 'eval("0x".a:str[v:val*2 : v:val*2+1])')
+endfunction
+
+function! s:str2bytes(str)
+ return map(range(len(a:str)), 'char2nr(a:str[v:val])')
+endfunction
+
+function! s:bytes2str(bytes)
+ return eval('"' . join(map(copy(a:bytes), 'printf(''\x%02x'', v:val)'), '') . '"')
+endfunction
+
+let &cpo = s:save_cpo
+unlet s:save_cpo
Oops, something went wrong.

0 comments on commit 40b2c12

Please sign in to comment.