forked from mattn/webapi-vim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.vim
More file actions
263 lines (241 loc) · 7.96 KB
/
atom.vim
File metadata and controls
263 lines (241 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
" atom
" Last Change: 2010-09-10
" Maintainer: Yasuhiro Matsumoto <[email protected]>
" 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:system = function(get(g:, 'webapi#system_function', 'system'))
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": [],
\ "app:control": {},
\ "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))', '')
let key = substitute(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 abort
let quote = &shellxquote == '"' ? "'" : '"'
let bits = substitute(s:system("xxd -ps ".quote.a: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() abort
return deepcopy(s:entry_template)
endfunction
function! s:createXml(entry) abort
let entry = webapi#xml#createElement("entry")
let entry.attr["xmlns"] = "http://purl.org/atom/ns#"
let entry.attr["xmlns:app"] = "http://www.w3.org/2007/app"
for key in keys(a:entry)
let l:keytype = type(a:entry[key])
if l:keytype == 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)
elseif l:keytype == 3
if key == "category"
for l:category in a:entry['category']
let node = webapi#xml#createElement(key)
let node.attr["term"] = l:category
call add(entry.child, node)
endfor
endif
elseif l:keytype == 4
let node = webapi#xml#createElement(key)
if key == "app:control"
let l:draft_node = webapi#xml#createElement("app:draft")
if exists("a:entry['app:control']['app:draft']")
call l:draft_node.value(a:entry['app:control']['app:draft'])
else
call l:draft_node.value('no')
endif
call add(node.child, l:draft_node)
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) abort
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) abort
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, ...) abort
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, ...) abort
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) abort
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 node.name == 'category'
let l:category = deepcopy(s:category_template)
let l:category['term'] = has_key(node.attr, 'term') ? node.attr['term'] : ''
let l:category['scheme'] = has_key(node.attr, 'scheme') ? node.attr['scheme'] : ''
let l:category['label'] = has_key(node.attr, 'label') ? node.attr['label'] : ''
call add(a:target.category, l:category)
elseif node.name == 'app:control'
for l:item in node.child
if type(l:item) == 4 && l:item.name == 'app:draft'
let a:target['app:control'] = {'app:draft': l:item.child[0]}
endif
unlet l:item
endfor
elseif type(a:target[node.name]) == 3
call add(a:target[node.name], a:parent.value())
else
let a:target[node.name] = node.value()
endif
unlet node
endfor
endfunction
function! webapi#atom#getFeed(uri, user, pass) abort
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) abort
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) abort
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: