-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmustache-rdf.js
137 lines (107 loc) · 3.14 KB
/
mustache-rdf.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
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
// need this function
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
function MustacheRDF(rdf, ns) {
this.rdf = rdf;
this.ns = ns;
this.reg = /{{(.*?)}}/g; // this is for curly brackets
this.loopBlock = /{{#(.*?)}}([\s\S]*?){{\/}}/g;
this.singleBlock = /{{\.}}/g;
this.loop = /<div\b[^>]*>((?!<\/?div\b).)*<div/g;
this.init();
}
MustacheRDF.prototype.init = function() {
var that = this;
// do loop
var loopReplaced = $('body').html().replace(this.loopBlock, function(){
//console.log(arguments);
var htmlToLoop = arguments[2];
var triple = arguments[1];
var html = '';
var obj = that.findPattern(triple);
var arr = obj.matches;
for(var i in arr) {
html += htmlToLoop.replace(that.reg, function() {
// {{?s rdfs:label ?label}} -> {{cco:xx rdfs:label ?label}}
// transform the variable found into full uri
var triple = arguments[1];
if(triple == '.') {
return arr[i];
} else {
triple = triple.replace(obj.argName, arr[i]);
var a = that.findPattern(triple).matches;
return a[0];
}
});
}
return html;
});
$('body').html(loopReplaced);
// replace all instance of curly
var replaced = $('body').html().replace(this.reg, function() {
var triple = arguments[1];
var arr = that.findPattern(triple).matches;
return arr[0];
});
$('body').html(replaced);
}
MustacheRDF.prototype.expandTriple = function(triple) {
// break it into triples, they're separated by space
var tripleArr = triple.split(' ');
// for each triple, see if it starts with a namespace in this.ns
for(var i in tripleArr) {
// replace 'a' with rdf:type
if(tripleArr[i] == 'a') {
tripleArr[i] = 'rdf:type';
}
// expand namespaces
for(var n in this.ns) {
if(tripleArr[i].startsWith(n)) {
tripleArr[i] = tripleArr[i].replace(n, this.ns[n], 'g');
break;
}
}
}
return tripleArr;
}
MustacheRDF.prototype.findTriple = function(triple) {
var ret = {};
ret.matches = [];
var arg = ['subject', 'predicate', 'object'];
// supporting only 1 arg for now
for(var x in triple) {
if(triple[x].startsWith('?')) {
arg = arg[x];
ret.argName = triple[x];
break;
}
}
for(var i in this.rdf) {
var subject = this.rdf[i].subject;
var predicate = this.rdf[i].predicate;
var object = this.rdf[i].object;
if(
(triple[0].startsWith('?') || triple[0] == subject)
&&
(triple[1].startsWith('?') || triple[1] == predicate)
&&
(triple[2].startsWith('?') || triple[2] == object)
){
// matched
var str = this.rdf[i][arg];
str = $.trim(str);
str = str.replace(/^\\n|\\n$/g, ''); // remove newline from beginning and end
str = str.replace(/\\n/g, '<br>');
ret.matches.push(str);
}
}
return ret;
}
MustacheRDF.prototype.findPattern = function(triple) {
var tripleArr = this.expandTriple(triple);
var obj = this.findTriple(tripleArr);
return obj;
}