-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyper.js
233 lines (223 loc) · 7.22 KB
/
hyper.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
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
var HYPER = HYPER || {};
(function(HYPER) {
HYPER.WikiPage = function(name, display) {
var that = this;
this.name = name;
this.$el = $("<span>")
.addClass("wikipage")
.append($("<span>", {title: name})
.addClass('name')
.html(display || name)
.click(function() { that.toggle(); }));
};
HYPER.WikiPage.prototype.fetch_raw = function(cb) {
// Loads RAW page and triggers callback
var that = this;
if(this.raw) { return cb(this.raw); }
$.get('/raw/'+this.name, {}, function(txt, status) {
if(status === 'success') {
that.raw = txt;
cb(txt);
}
else {
console.log("bad status", statusCode, that.name);
}
});
};
HYPER.WikiPage.prototype.toggle = function() {
if(this.expanded) {
this.expanded = false;
this.contract();
}
else {
this.expanded = true;
this.expand();
}
};
HYPER.WikiPage.prototype.contract = function() {
this.$el.find(".wikibody").remove();
};
HYPER.WikiPage.prototype.expand = function() {
var that = this;
this.fetch_raw(function(txt) {
that.$el.append(that.parse().addClass('wikibody'));
});
};
HYPER.WikiPage.prototype.parse = function() {
var that = this;
this.templates = [];
this.links = [];
var onTemplate = function(tpl) { that.templates.push(tpl); }
var onLink = function(lnk) { that.links.push(lnk); }
return paragraphs(this.raw, function(para) {
return templates(para,
function(txt) { return wikilinks(txt, onLink); },
onTemplate);
});
};
HYPER.Book = function(spec) {
this.spec = spec;
};
HYPER.Tree = function() {
};
HYPER.Template = function(raw) {
var that = this;
this.raw = raw;
// basic parser.
var fields = raw.split('|');
this.args = [];
this.kw = {};
fields.forEach(function(val) {
var kv = val.split('=');
var k = strip(kv[0]);
if(kv.length===1) {
that.args.push(k);
}
else {
that.kw[k] = strip(kv[1]);
}
});
this.$el = $("<div>")
.addClass('template')
.append($("<span>")
.addClass('name')
.html(this.args[0])
.click(function() {that.toggle();}));
if(this.is_book()) {
this.$el
.addClass('hasbook');
}
};
HYPER.Template.prototype.toggle = function() {
// XXX: Abstract?
if(this.expanded) {
this.expanded = false;
this.contract();
}
else {
this.expanded = true;
this.expand();
}
};
HYPER.Template.prototype.expand = function() {
var that = this;
this.$rendered = $("<div>")
.addClass('templatebody')
.appendTo(this.$el);
this.args.slice(1).forEach(function(arg) {
$("<span>")
.addClass('arg')
.html(arg)
.appendTo(that.$rendered);
});
var $table = $("<table>")
.appendTo(this.$rendered);
for(var key in this.kw) {
var val = this.kw[key];
$("<tr>")
.appendTo($table)
.append($("<td>").html(key))
.append($("<td>").html(val));
};
};
HYPER.Template.prototype.contract = function() {
this.$rendered.remove();
};
HYPER.Template.prototype.is_book = function() {
if('isbn' in this.kw) {
return this.kw['isbn'];
}
};
function strip(txt) {
return txt
.replace(/^\s*/g, '')
.replace(/\s*$/g, '');
}
function split_parse(txt, splitExp, onMatch) {
var $out = $("<div>");
txt.split(splitExp).forEach(function(seg) {
$out.append(onMatch(seg));
});
return $out;
};
function paragraphs(txt, onMatch) {
return split_parse(txt,
/\n\n/g,
function(para) {return $("<p>").append(onMatch(para));});
};
function sweep_parse(txt, startExp, endExp, onMatch, onFill, startLen, endLen) {
var $out = $("<span>");
var cur_idx = 0;
var last_end= 0;
var starts = [];
while(true) {
var next_st = txt.slice(cur_idx).search(startExp);
var next_end= txt.slice(cur_idx).search(endExp);
if(next_st < next_end && next_st >= 0) {
starts.push(next_st + cur_idx);
cur_idx += next_st + startLen;
}
else if(next_end >= 0) {
var start = starts.pop();
if(start !== undefined && starts.length === 0) {
if(last_end < start) {
$out.append(onFill(txt.slice(last_end, start)));
}
last_end = cur_idx + next_end;
$out.append(onMatch(txt.slice(start+startLen, last_end)));
last_end += endLen;
}
cur_idx += next_end+endLen;
}
else {
$out.append(onFill(txt.slice(last_end)));
return $out;
}
}
};
function templates(txt, onFill, onTemplate) {
return sweep_parse(txt, '{{', '}}', function(templ) {
var temp = new HYPER.Template(templ);
// temp.expand();
// console.log(temp);
onTemplate(temp);
return temp.$el;
}, onFill, 2, 2);
}
function wikihacks(txt) {
function headings(txt) {
var i = 6;
var out = txt.slice(0);
while(i>0) {
var bar = '';
for(var j=0; j<i; j++) {
bar += '=';
}
var re = new RegExp('^'+bar+'(.*)'+bar, 'gm');
out = out.replace(re, '<h'+i+'>$1</h'+i+'>\n');
i--;
}
return out;
}
return headings(txt)
.replace(/[\n^][\*#]/g, '<br>')
.replace(/'''''([^']*)'''''/g, '<b><i>$1</i></b>')
.replace(/'''([^']*)'''/g, '<b>$1</b>')
.replace(/''([^']*)''/g, '<i>$1</i>')
.replace(/\[http[\S]* ([^\]]*)]/g, '$1')
}
function wikilinks(txt, onWikilink) {
return sweep_parse(txt, /\[\[/g, /\]\]/g, function(link) {
if(link.indexOf("File:") >= 0 || link.indexOf("Image:") >= 0) {
return [];
}
var split = link.split('|');
var page = new HYPER.WikiPage(split[0], split[split.length-1]);
onWikilink(page);
return page.$el;
// return $("<a>", {href: '/a/'+split[0]}).html(split[split.length-1]);
}, wikihacks, 2, 2);
};
function identity(x) { return x; }
function wipe(x) { return ''; }
})(HYPER);