-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmark
367 lines (310 loc) · 11.9 KB
/
bookmark
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rdfe="http://redfoot.net/3.0/rdf#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:code="http://redfoot.net/3.0/code#"
xmlns:redfoot="http://redfoot.net/3.0/redfoot#"
xmlns:server="http://redfoot.net/3.0/server#"
xmlns:template="http://redfoot.net/3.0/template#"
xmlns:kid='http://redfoot.net/3.0/kid#'
xmlns:aspect="http://redfoot.net/3.0/aspect#"
>
<rdfe:RDFXMLDocument rdf:about="">
<rdfs:label>Bookmark Aspect RDF/XML</rdfs:label>
</rdfe:RDFXMLDocument>
<rdfe:Namespace rdf:about="#">
<rdfs:label>Bookmark</rdfs:label>
<rdfs:comment>The Redfoot bookmark namespace.</rdfs:comment>
</rdfe:Namespace>
<owl:Class rdf:ID="Bookmark">
<rdfs:label>Bookmark</rdfs:label>
<rdfs:comment></rdfs:comment>
<rdfs:subClassOf rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
<template:content>
<kid:PagePartHandler rdf:ID="BookmarkHandler">
<kid:template rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
<span xmlns:kid="http://purl.org/kid/ns#" kid:omit="">
<?python
import time
bookmark = request.uri.abstract()
BOOKMARK = redfoot.namespace("http://redfoot.net/3.0/bookmark#")
DC_created = URIRef("http://purl.org/dc/terms/created")
DC_creator = URIRef("http://purl.org/dc/elements/1.1/creator")
refersTo = redfoot.value(bookmark, BOOKMARK.refersTo)
if refersTo:
creator = redfoot.value(bookmark, DC_creator)
c = redfoot.value(bookmark, DC_created)
try:
created = time.strftime("%A %d %B, %Y", time.gmtime(float(c)))
except:
created = None
categories = list(redfoot.objects(bookmark, BOOKMARK.category))
?>
<span kid:if="refersTo" >
<p>This bookmark refers to
<a href="${refersTo}">${redfoot.label(refersTo) or "--"}</a>.
<span kid:if="created">The bookmark was created on ${created}.</span>
<span kid:if="creator">The bookmark was added by
<a href="${request.relative(creator.concrete())}">${redfoot.label(creator) or creator}</a>.
</span>
</p>
<span kid:if="categories"><p>The bookmark is in the following categories:
<ul>
<li kid:for="category in categories">
<a href="${request.relative(category.concrete())}">${redfoot.label(category) or category}</a>
</li>
</ul>
</p>
</span>
</span>
<span kid:if="not refersTo" >
<p>This bookmark does not refer to anything :(</p>
</span>
</span>
]]>
</kid:template>
</kid:PagePartHandler>
</template:content>
</owl:Class>
<owl:ObjectProperty rdf:ID="refersTo">
<rdfs:label>refers to</rdfs:label>
<rdfs:comment></rdfs:comment>
<rdfs:domain rdf:resource="#Bookmark"/>
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
</owl:ObjectProperty>
<owl:Class rdf:ID="Category">
<rdfs:label>Category</rdfs:label>
<rdfs:comment></rdfs:comment>
<rdfs:subClassOf rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
<template:content>
<kid:PagePartHandler rdf:ID="CategoryHandler">
<kid:template rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
<span xmlns:kid="http://purl.org/kid/ns#" kid:omit="">
<?python
import time
category = request.uri.abstract()
BOOKMARK = redfoot.namespace("http://redfoot.net/3.0/bookmark#")
SERVER = redfoot.namespace("http://redfoot.net/3.0/server#")
DC_creator = URIRef("http://purl.org/dc/elements/1.1/creator")
DC_created = URIRef("http://purl.org/dc/terms/created")
bookmarks = set()
for bookmark in redfoot.subjects(BOOKMARK.category, category):
if (bookmark, SERVER.site, request.host) in redfoot:
uri = redfoot.value(bookmark, BOOKMARK.refersTo)
label = redfoot.label(uri) or uri
c = redfoot.value(bookmark, DC_created)
try:
created = time.strftime("%A %d %B, %Y", time.gmtime(float(c)))
except Exception, e:
redfoot.log.warning("Couldn't format timestamp: %s" % e)
created = None
bookmarks.add((c, created, label, uri, bookmark))
bookmarks = list(bookmarks)
bookmarks.sort()
bookmarks.reverse()
?>
<ul class="bookmarks">
<li kid:for="c, created, label, uri, bookmark in bookmarks">
<h3><a href="${uri}">${label}</a></h3>
<div class="meta">
<a href="${bookmark.concrete()}">bookmark</a>
<span kid:if="created">created on ${created}</span>.
</div>
</li>
</ul>
</span>
]]>
</kid:template>
</kid:PagePartHandler>
</template:content>
</owl:Class>
<owl:ObjectProperty rdf:ID="category">
<rdfs:label>category</rdfs:label>
<rdfs:comment></rdfs:comment>
<rdfs:domain rdf:resource="#Bookmark"/>
<rdfs:range rdf:resource="#Category"/>
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
<!-- Allow literals in range to support using category for tags -->
</owl:ObjectProperty>
<aspect:Aspect rdf:ID="aspect">
<rdfs:label>Bookmark</rdfs:label>
<aspect:item rdf:resource="#bookmarks"/>
<aspect:item rdf:resource="#add_bookmark"/>
</aspect:Aspect>
<code:Module rdf:ID="module">
<rdfs:label></rdfs:label>
<rdfs:comment></rdfs:comment>
<code:python rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
from rdflib.events import Event
class BookmarkCreatedEvent(Event):
"""
This event is fired when a bookmark has been created, it has the following attributes:
- 'uri' identifier of bookmark
"""
]]>
</code:python>
</code:Module>
<template:Section rdf:ID="bookmarks">
<aspect:location>/bookmarks/</aspect:location>
<rdfs:label>Bookmarks</rdfs:label>
<rdfs:comment></rdfs:comment>
<rdfs:member rdf:resource="http://redfoot.net/3.0/comment#exclude"/>
<template:default_section_rank>25.0</template:default_section_rank>
<template:content>
<kid:PagePartHandler rdf:ID="Bookmarks">
<kid:template rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
<span xmlns:kid="http://purl.org/kid/ns#" kid:omit="">
<?python
import time
BOOKMARK = redfoot.namespace("http://redfoot.net/3.0/bookmark#")
SERVER = redfoot.namespace("http://redfoot.net/3.0/server#")
DC_creator = URIRef("http://purl.org/dc/elements/1.1/creator")
DC_created = URIRef("http://purl.org/dc/terms/created")
bookmarks = set()
for bookmark in redfoot.subjects(RDF.type, BOOKMARK.Bookmark):
if (bookmark, SERVER.site, request.host) in redfoot:
uri = redfoot.value(bookmark, BOOKMARK.refersTo)
label = redfoot.label(uri) or uri
c = redfoot.value(bookmark, DC_created)
try:
created = time.strftime("%A %d %B, %Y", time.gmtime(float(c)))
except Exception, e:
redfoot.log.warning("Couldn't format timestamp: %s" % e)
created = None
bookmarks.add((c, created, label, uri, bookmark))
bookmarks = list(bookmarks)
bookmarks.sort()
bookmarks.reverse()
?>
<p>
Drag and drop this to you bookmark bar: <a href="javascript:void(document.location='${request.uri}add_bookmark');">Add ${redfoot.label(request.site)} Bookmark</a>
</p>
<ul class="bookmarks">
<li kid:for="c, created, label, uri, bookmark in bookmarks">
<h3><a href="${uri}">${label}</a></h3>
<div class="meta">
<a href="${bookmark.concrete()}">bookmark</a>
<span kid:if="created">created on ${created}</span>.
</div>
</li>
</ul>
</span>
]]>
</kid:template>
</kid:PagePartHandler>
</template:content>
</template:Section>
<server:Page rdf:ID="add_bookmark">
<aspect:location>/bookmarks/add_bookmark</aspect:location>
<rdfs:label>Add Bookmark</rdfs:label>
<rdfs:comment></rdfs:comment>
<server:allow rdf:resource="http://redfoot.net/3.0/user#User"/>
<server:long_running>true</server:long_running>
<server:handler>
<server:PagePartHandler rdf:ID="add_bookmark_handler">
<code:python rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
import logging
_logger = logging.getLogger(__uri__)
auth = redfoot.module(URIRef("auth#module", base=redfoot.uri))
bookmark_module = redfoot.module(URIRef("#module", base=__uri__))
from sgmllib import SGMLParser
class BookmarkParser(SGMLParser):
def __init__(self, bookmark, uri):
SGMLParser.__init__(self)
self.bookmark = bookmark
self.uri = uri
self.context = redfoot.get_context(redfoot.context_id(bookmark))
self.data = ""
def reset(self):
SGMLParser.reset(self)
def handle_data(self, data):
self.data += data
def start_title(self, attrs):
self.data = ""
def end_title(self):
c = self.context
if (self.uri, RDFS.label, None) not in c:
c.add((self.uri, RDFS.label, Literal(self.data)))
self.data = ""
def unknown_endtag(self, tag):
pass
import md5
import urlparse
from time import time
from twisted.web.client import getPage
from twisted.web import http
BOOKMARK = redfoot.namespace("http://redfoot.net/3.0/bookmark#")
SERVER = redfoot.namespace("http://redfoot.net/3.0/server#")
SESSION = redfoot.namespace("http://redfoot.net/3.0/session#")
DC_creator = URIRef("http://purl.org/dc/elements/1.1/creator")
DC_created = URIRef("http://purl.org/dc/terms/created")
uri = request.headers.get("referer")
if not auth.allow(request):
response.setStatus(403, "Forbidden")
response.write("Must be logged in to add a bookmark.")
response.finish()
elif uri:
uri = URIRef(uri)
b = URIRef("/bookmark/%s/" % md5.new(uri).hexdigest(), base=request.host).abstract()
c = redfoot.get_context(redfoot.context_id(b))
c.add((b, RDFS.label, Literal("Bookmark")))
c.add((b, RDF.type, BOOKMARK.Bookmark))
c.add((b, BOOKMARK.refersTo, uri))
if (b, DC_created, None) not in redfoot:
c.add((b, DC_created, Literal("%s" % time())))
c.add((b, SERVER.site, request.host))
scheme, netloc, url, params, query, fragment = urlparse.urlparse(uri)
host = URIRef(urlparse.urlunparse((scheme, netloc, "", "", "", "")))
# TODO: d = getPage(host.encode("utf-8")); d.addCallback(updateCategory)
category = URIRef("/bookmarks/category/%s/" % md5.new(host).hexdigest(), base=request.host).abstract()
c.remove((category, RDFS.label, None))
c.add((category, RDFS.label, redfoot.label(host) or Literal(host)))
c.add((category, RDF.type, BOOKMARK.Category))
c.add((b, BOOKMARK.category, category))
if request.session_id:
# TODO: add a uid method to request?
uid = redfoot.value(request.session_id, SESSION.uid)
if uid:
if (b, DC_creator, None) not in redfoot:
c.add((b, DC_creator, uid))
def finish(page):
if page is not None:
import urllib
parser = BookmarkParser(b, uri)
parser.feed(page)
response.setStatus(http.SEE_OTHER, "The Bookmark Page")
response.setHeader("Location", b.concrete())
response.finish()
try:
redfoot.dispatcher.dispatch(bookmark_module.BookmarkCreatedEvent(uri=b))
except Exception, e:
_logger.exception(e)
def error(result):
finish(None)
if host==request.host:
# Don't try to fetch page from ourself... just finish up.
finish(None)
else:
# else some of the bits turn unicode again and cause a twisted
# TypeError("Data must not be unicode")
urlparse.clear_cache()
d = getPage(uri.encode("utf-8"))
d.addCallback(finish)
d.addErrback(error)
else:
response.setStatus(http.PRECONDITION_FAILED, "no referer")
response.write("No referer specified")
response.finish()
]]>
</code:python>
</server:PagePartHandler>
</server:handler>
</server:Page>
</rdf:RDF>