forked from IKS/rdfQuery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.xfn.foaf.js
91 lines (80 loc) · 2.81 KB
/
jquery.xfn.foaf.js
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
/*
* jQuery XFN for rdfQuery @VERSION
*
* Copyright (c) 2008 Jeni Tennison, Libby Miller
* Licensed under the MIT (MIT-LICENSE.txt)
*
* Depends:
* jquery.uri.js
* jquery.xmlns.js
* jquery.curie.js
* jquery.datatype.js
* jquery.rdf.js
*/
/*global jQuery */
(function ($) {
var
foaf = $.uri("http://xmlns.com/foaf/0.1/"),
work = $.rdf.resource('<' + $.uri.base() + '>'),
foafPersonClass = $.rdf.resource('<' + foaf + 'Person>'),
foafKnowsProp = $.rdf.resource('<' + foaf + 'knows>'),
foafWeblogProp = $.rdf.resource('<' + foaf + 'weblog>'),
person1Bnode = $.rdf.blank("[]"),
meRegex = /(?:^|\s)me(?:\s|$)/,
gleaner = function (options) {
var rel = this.attr('rel'),
href = this.attr('href'),
m = meRegex.exec(rel),
person2Bnode = $.rdf.blank("[]");
if (href !== undefined && m === null) {
if (options && options.about !== undefined) {
if (options.about === null) {
return true;
} else {
return options.about === $.uri.base() || options.about === href;
}
} else if (options && options.type !== undefined) {
if (options.type === null) {
return true;
} else {
return options.type === foafPersonClass.uri;
}
} else {
return [
$.rdf.triple(person1Bnode, $.rdf.type, foafPersonClass),
$.rdf.triple(person1Bnode, foafWeblogProp, work),
$.rdf.triple(person1Bnode, foafKnowsProp, person2Bnode),
$.rdf.triple(person2Bnode, foafWeblogProp, '<' + href + '>'),
$.rdf.triple(person2Bnode, $.rdf.type, foafPersonClass)
];
}
}
return options === undefined ? [] : false;
};
$.fn.xfn = function (relationship) {
//relationship e.g. 'friend' 'met' etc
if (relationship === undefined) {
var triples = $.map($(this), function (elem) {
return gleaner.call($(elem));
});
return $.rdf({ triples: triples });
} else {
$(this)
.filter('[href]') // only add to elements with href attributes
.each(function () {
var elem = $(this),
rel = elem.attr('rel');
if (rel === undefined || rel === '') {
elem.attr('rel', relationship);
} else if (!rel.toLowerCase().match(relationship.toLowerCase())) {
elem.attr('rel', rel + ' ' + relationship);
}
});
return this;
}
};
$.rdf.gleaners.push({
name: "xfn.foaf",
gleaner: gleaner
});
})(jQuery);