-
Notifications
You must be signed in to change notification settings - Fork 9
/
jquery.cclicense.js
82 lines (77 loc) · 2.39 KB
/
jquery.cclicense.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
/*
* jQuery CC License for rdfQuery @VERSION
*
* Copyright (c) 2008 Jeni Tennison
* 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
cc = $.uri("http://creativecommons.org/ns#"),
work = $.rdf.resource('<' + $.uri.base() + '>'),
ccLicenseClass = $.rdf.resource('<' + cc + 'License>'),
ccWorkClass = $.rdf.resource('<' + cc + 'Work>'),
ccLicenseProp = $.rdf.resource('<' + cc + 'license>'),
licenseRegex = /(?:^|\s)(?:(\S+):)?license(?:\s|$)/,
gleaner = function (options) {
var rel = this.attr('rel'),
href = this.attr('href'),
license,
m = licenseRegex.exec(rel);
if (href !== undefined && m !== null && (m[1] === undefined || this.xmlns(m[1]) === cc)) {
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 === ccLicenseClass.value || options.type === ccWorkClass.value;
}
} else {
license = $.rdf.resource('<' + href + '>');
return [
$.rdf.triple(work, $.rdf.type, ccWorkClass),
$.rdf.triple(license, $.rdf.type, ccLicenseClass),
$.rdf.triple(work, ccLicenseProp, license)
];
}
}
return options === undefined ? [] : false;
};
$.fn.cclicense = function (triple) {
if (triple === 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', 'license');
} else if (!licenseRegex.test(rel)) {
elem.attr('rel', rel + ' license');
}
});
return this;
}
};
$.rdf.gleaners.push({
name: "cclicense",
gleaner: gleaner
});
})(jQuery);