-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtearly.js
220 lines (181 loc) · 5.95 KB
/
tearly.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
'use strict';
// boot tiddlers:
// boot tiddlers are those that need to be inserted to the end of document.
(function () {
function Dav()
{
}
Dav.put = function (url, content) {
return new Promise((resolve, reject) => {
var req = new XMLHttpRequest();
req.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve();
}
else {
reject(Error(this.statusText + "\n" + this.responseText));
}
};
req.open("PUT", url);
req.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
req.send(content);
});
};
Dav.delete = function (path) {
};
this.D = Dav;
}).call(this);
(function () {
function DummyDav()
{
}
DummyDav.put = function (url, content) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("DAV-OK", url, content);
resolve();
}, 2000*Math.random());
});
};
DummyDav.delete = function (path) {
};
this.DD = DummyDav;
}).call(this);
(function () {
function Underscode()
{
}
// preserve only [A-Za-z0-9-.]
Underscode.encode = function (text) {
text = encodeURIComponent(text);
text = text.replace(/[!'()*~_]/g, c => '%' + c.charCodeAt(0).toString(16));
text = text.replace(/%/g, "_");
text = text.replace(/_[A-F0-9]{2}/g, c => c.toLowerCase());
return text;
}
Underscode.decode = function (text) {
text = text.replace(/_/g, "%");
text = decodeURIComponent(text);
return text;
}
this.U = Underscode;
}).call(this);
(function () {
function SimpleTiddlerFormat()
{
}
SimpleTiddlerFormat.encode = function (tiddler) {
var fields = tiddler.fields;
var rest = JSON.stringify(fields, (k, v) => (k === "title" || k === "text") ? undefined : v);
var text = fields.title + "\n" + rest + "\n" + fields.text;
return text;
}
SimpleTiddlerFormat.decode = function (text) {
text = text.split("\n");
var title = text.splice(0, 1)[0];
var fields = text.splice(0, 1)[0];
text = text.join("\n");
fields.title = title;
fields.text = text;
var tiddler = {
fields: fields,
};
return tiddler;
}
this.S = SimpleTiddlerFormat;
}).call(this);
(function () {
// FIXME: debug only
var Underscode = this.U;
var Dav = this.D;
var SimpleTiddlerFormat = this.S;
function Tearly(wiki)
{
this.wiki = wiki;
}
Tearly.prototype.sync_filter = function () {
return this.wiki.getTiddlerText("$:/config/SyncFilter");
}
Tearly.prototype.index_template = function () {
// FIXME: dummy
// TODO: read from a tiddler
return `
<noscript>
This document would load these tiddlers:
<div id="bootstrap_non_boot">
$non-boot-tiddlers$
</div>
<div id="bootstrap_boot">
$boot-tiddlers$
</div>
</noscript>
<code>hello world</code>
<script>
console.log(bootstrap_non_boot.textContent, bootstrap_boot.textContent);
</script>
`;
}
Tearly.prototype.tiddler_of = function (title) {
var tiddler = this.wiki.getTiddler(title);
return SimpleTiddlerFormat.encode(tiddler);
}
Tearly.prototype.tiddlers = function () {
var filter = this.wiki.compileFilter(this.sync_filter());
return filter.call(this.wiki);
}
Tearly.prototype.partitioned_tiddlers = function () {
var tiddlers = new Set(this.tiddlers());
// TODO: use a tiddler to allow customization
var boot_tiddlers = new Set([
"$:/boot/bootprefix.js",
"$:/boot/boot.js",
]);
var non_boot_tiddlers = new Set(tiddlers);
boot_tiddlers.forEach(title => non_boot_tiddlers.delete(title));
return {
all: tiddlers,
non_boot: non_boot_tiddlers,
boot: boot_tiddlers,
};
}
Tearly.prototype.base_url = function (href) {
return "http://127.0.0.1:7777"; // TODO
}
Tearly.prototype.url_from_path = function (path, base_url) {
if (base_url === undefined) base_url = this.base_url(location.href);
return base_url + path;
}
Tearly.prototype.path_from_title = function (title) {
return "/tiddlers/" + Underscode.encode(title);
}
Tearly.prototype.url_from_title = function (title, base_url) {
var path = this.path_from_title(title);
return this.url_from_path(path, base_url);
}
Tearly.prototype.bootstrap = function () {
var tiddlers = this.partitioned_tiddlers();
console.log(tiddlers);
// put index
var index = this.index_template();
var non_boot_tiddlers = [...tiddlers.non_boot].map(title => this.path_from_title(title)).join("\n");
var boot_tiddlers = [...tiddlers.boot].map(title => this.path_from_title(title)).join("\n");
index = index.replace("$non-boot-tiddlers$", non_boot_tiddlers);
index = index.replace("$boot-tiddlers$", boot_tiddlers);
Dav.put(this.url_from_path("/index.html"), index)
.then(ok => {
var pending = [];
tiddlers.all.forEach(title => {
var url = this.url_from_title(title);
var tiddler = this.tiddler_of(title);
pending.push(Dav.put(url, tiddler));
})
return Promise.all(pending);
})
.then(ok => {
console.log("BOOTSTRAPED");
})
;
}
this.T = Tearly;
this.t = new Tearly($tw.wiki);
}).call(this);