-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
588 lines (518 loc) · 17.5 KB
/
index.html
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
'use strict';
var wiki = {
config : {
HomePageName : 'FrontPage',
EditPageName : 'Edit',
HelpPageName : 'Help',
IndexPageName : 'Index',
BackupPageName : 'Backup',
WikiPattern : '(([A-Z][a-z]+([A-Z][a-z]+)+)|([0-9]{4}/[0-9]{1,2}(/[0-9]{1,2})?))',
URLPattern : '(mailto|http|https|ftp):[\x21-\x7E]*',
getLinkPattern : function() {
return new RegExp('(' + this.URLPattern + ')|(' + this.WikiPattern + ')', 'g');
}
},
main : function() {
this.db = this.db_localStorage;
var ret = this.db.init();
if (!ret) {
return;
}
this.view.init(this.config);
this.view._id('linkToEditPage').onclick = this.editCurrentPage;
this.view._id('content').ondblclick = function(e){
if (!! wiki.view._id('textarea')) {
return;
}
wiki.editCurrentPage();
};
window.onhashchange = function() {
wiki.navigate();
}
this.navigate();
},
getHashPage: function() {
return location.hash.substr(1);
},
navigate: function() {
var pagename = wiki.getHashPage();
switch (pagename) {
case wiki.config.HomePageName:
case '': // no hash
if (wiki.db.exists(wiki.config.HomePageName)) {
wiki.openHomePage();
} else if (wiki.view.getCurrentPageName() == wiki.config.HelpPageName) {
// edit first wiki page
wiki.editPage(pagename);
} else {
// show welcome page
wiki.openHelpPage();
}
break;
case wiki.config.HelpPageName:
wiki.openHelpPage();
break;
case wiki.config.IndexPageName:
wiki.showIndexPage();
break;
case wiki.config.BackupPageName:
wiki.openBackupPage();
break;
default:
wiki.openPage(pagename);
break;
}
return false;
},
openPage : function(pagename){
this.view.setPageTitle(pagename);
this.view.showEditLink();
this.view.setDateLink(pagename);
if(! this.db.exists(pagename) ){
this.editPage(pagename);
}else{
var content = this.db.read(pagename);
content = content.replace(this.config.getLinkPattern(), this.makeLink );
this.view.setContent("<pre>" + content + "</pre>");
}
},
openHomePage : function(){
this.openPage(this.config.HomePageName);
return false;
},
openHelpPage : function (){
var pagename = this.config.HelpPageName;
this.view.setPageTitle(pagename);
this.view.hideEditLink();
var content = this.view._id('help').innerText;
content = content.replace(this.config.getLinkPattern(), this.makeLink );
this.view.setContent("<pre>" + content + "</pre>");
return false;
},
openBackupPage : function() {
this.view.setPageTitle(this.config.BackupPageName);
this.view.hideEditLink();
var content = this.db.dump();
this.view.renderBackupPage(content);
return false;
},
editCurrentPage : function (){
if (wiki.view._id('editLink').style.display != 'none') {
wiki.editPage(wiki.view.getCurrentPageName());
}
return false;
},
showIndexPage : function (){
this.view.setPageTitle(this.config.IndexPageName);
this.view.hideEditLink();
var pairs = this.db.pairs();
this.view.renderIndexPage(pairs, this.getStoredKeyword());
return false;
},
savePage : function (pagename){
var content = this.view.getInputValue();
if (content) {
this.db.save(pagename, content);
this.openPage(pagename);
} else {
this.db.remove(pagename);
this.openHomePage();
return false;
}
return false;
},
editPage : function(pagename) {
this.view.setPageTitle(pagename);
var content = '';
if( this.db.exists(pagename) ){
content = this.db.read(pagename);
}
this.view.renderInputForm(pagename, content);
},
editCancel : function(pagename) {
if (this.db.exists(pagename)) {
this.navigate();
} else {
history.back();
}
},
makeLink : function($0,$1,$2){
var _this = wiki;
var matched = $0;
var link;
if(matched.match(_this.config.URLPattern)) {
// URLとマッチした場合
link = "<a href='" + matched + "' class='ext' target='_blank'>" + matched + "</a>";
} else if(matched.match(_this.config.WikiPattern)){
// Wikiページっぽい
var pagename = RegExp.$1;
if(_this.db.exists(pagename)){
link = "<a href='#" + pagename + "' class='int' >" + pagename + "</a>";
} else {
link = pagename + "<a href='#" + pagename + "' title='create a Page' class='new'></a>";
}
}
return link;
},
searchPage : function(keyword) {
this.view.showAllLists();
if (! keyword) {
return;
}
var words = keyword.split(/[ ]+/); // 半角スペース+全角スペース
for (var i = 0; i < words.length; ++i) {
this.view.hideUnmatchedLists(words[i]);
}
},
storeSearchKeyword : function(keyword) {
wiki.db.save(wiki.config.IndexPageName, keyword);
},
getStoredKeyword : function() {
return wiki.db.read(wiki.config.IndexPageName);
},
selectRestoreFile : function() {
this.view._id('restore').click();
return false;
},
restoreBackup : function() {
var file = this.view._id('restore').files[0];
var fr = new FileReader();
fr.onload = function() {
try {
var data = JSON.parse(fr.result);
if (confirm('Are you sure?')) {
wiki.db.restore(data);
} else {
// debugging
console.log(fr.result === wiki.db.dump());
for (var k in data)
if (data[k] !== wiki.db.read(k))
console.log('data <-> db differ:' + k);
var keys = wiki.db.keys();
for (var k, i = 0; i < keys.length; i++) {
k = keys[i];
if (data[k] !== wiki.db.read(k))
console.log('db <-> data differ:' + k);
}
debugger;
}
} catch(e) {
console.error(e);
alert('restore error:' + e);
}
}
fr.readAsText(file);
}
}; // wiki
//============ Storage Object ===================
//===== localStorage ======
wiki.db_localStorage = {
init : function() {return true;},
exists : function(pagename){
return pagename in localStorage;
},
read : function(pagename) {
return localStorage.getItem(pagename);
},
keys : function(){
var ret = [];
for (var i = 0; i < localStorage.length; i++) {
ret.push(localStorage.key(i));
}
return ret;
},
save : function(pagename, content){
localStorage.setItem(pagename, content);
},
remove : function(pagename) {
localStorage.removeItem(pagename);
},
dump : function() {
return JSON.stringify(localStorage);
},
restore : function(obj) {
localStorage.clear();
for (var page in obj) {
if (obj.hasOwnProperty(page)) {
this.save(page, obj[page]);
}
}
},
pairs : function() {
return JSON.parse(this.dump());
}
};
//============ View Object ===================
wiki.view = {
init : function(config) {
this._id("linkToHomePage").innerText = config.HomePageName;
this._id("linkToEditPage").innerText = config.EditPageName;
this._id("linkToIndexPage").innerText = config.IndexPageName;
this._id("linkToHelpPage").innerText = config.HelpPageName;
this._id("linkToBackupPage").innerText = config.BackupPageName;
this._id('linkToHomePage').href += config.HomePageName;
this._id('linkToIndexPage').href += config.IndexPageName;
this._id('linkToHelpPage').href += config.HelpPageName;
this._id('linkToBackupPage').href += config.BackupPageName;
},
setPageTitle : function(pagename) {
document.title = pagename;
this._id("headerH1").innerText = pagename;
},
showEditLink : function() {
this._id("editLink").style.display = "";
},
setDateLink : function(pagename) {
var date = new Date(pagename),
prev = this._id('prevPage'),
next = this._id('nextPage');
if (isNaN(date.getDate())) {
prev.style.display = 'none';
next.style.display = 'none';
} else {
prev.style.display = '';
next.style.display = '';
prev.href = '#' + this._dayCalc(date, -1);
next.href = '#' + this._dayCalc(date, 1);
}
},
_dayCalc : function(dateExpr, dateOper) {
var d = new Date(dateExpr);
d.setDate(d.getDate() + dateOper || 0);
return [
d.getFullYear(),
("0" + (d.getMonth()+1)).slice(-2),
("0" + d.getDate()).slice(-2)
].join('/');
},
hideEditLink : function() {
this._id("editLink").style.display = "none";
},
_id : function (id) {
return document.getElementById(id);
},
setContent : function(html) {
this._id('content').innerHTML = html;
},
getCurrentPageName : function() {
return document.title;
},
getInputValue : function() {
return this._id('textarea').value;
},
renderInputForm : function(pagename, content) {
var html =
"<form id='textform' onsubmit='wiki.savePage(\"" + pagename + "\"); return false;'>"
+ "<input type='submit' value='save'> "
+ "<input type='button' value='cancel' onclick='wiki.editCancel(\""+pagename+"\");'><br />"
+ "<textarea id='textarea' wrap='off'></textarea><br />"
+ "<input type='submit' value='save'><br />"
+ "</form>";
this.setContent(html);
var area = this._id('textarea');
area.focus();
area.value = content;
},
_e : function(str) {
var node;
if (str instanceof HTMLElement) {
node = str;
} else if (typeof str === 'string') {
node = document.createTextNode(str);
} else {
throw new Error('Error: invalid argument:' + str);
}
var wrapper = document.createElement('div');
wrapper.appendChild(node);
return wrapper.innerHTML;
},
renderIndexPage : function(pairs, prevKeyword) {
var links = [];
for(var k in pairs) {
if (! pairs.hasOwnProperty(k)) {
continue;
} else if (k == wiki.config.IndexPageName) {
continue;
}
var li = "<li class='search-index'>"
+ "<a href='#" + k + "'>" + k + "</a>"
+ "<!-- " + this._e((k + pairs[k]).toLowerCase()) + " -->"
+ "</li>";
links.push(li);
}
var html = "<input type='text' id='search' placeholder='search...' "
+ "oninput='wiki.searchPage(this.value);' "
+ "onchange='wiki.storeSearchKeyword(this.value);' "
+ "style='float:right' accessKey='/'>"
+ "<ul id='index'>" + links.join("") + "</ul>";
this.setContent(html);
var s = this._id('search');
s.value = prevKeyword;
s.dispatchEvent(new Event('input'));
s.select();
},
showAllLists : function() {
this._x('//li[@class="search-index"]').forEach(function(e) {
e.style.display = '';
});
},
hideUnmatchedLists : function(keyword) {
var path = '//li[@class="search-index" and not(contains(comment(), "' + keyword.toLowerCase() + '"))]';
this._x(path).forEach(function(e) {
e.style.display = 'none';
});
},
_x : function(xpath, root) {
var r = [], x, node;
root = root || document;
x = document.evaluate(xpath, root, null, XPathResult.ANY_TYPE, null);
while (node = x.iterateNext())
r.push(node);
return r;
},
renderBackupPage : function(content) {
var escaped = content.replace(/ /g, '\\u0020'); // space escape
// "<a href='data:application/json;charset=UTF-8,"+escaped+"' download='backup-"+timestamp+".json' >Dump</a>"
var a = document.createElement('a');
a.setAttribute('href', 'data:application/json;charset=UTF-8,' + escaped);
a.setAttribute('download', 'backup-' + Date.now() + '.json');
a.innerText = 'Dump';
var html = this._e(a)
+ " | <a href='#' onclick='return wiki.selectRestoreFile();'>Restore</a>"
+ "<input type='file' id='restore' onchange='wiki.restoreBackup();' style='display:none' accept='application/json'>";
this.setContent(html);
}
};
window.onload = function(){ wiki.main(); };
</script>
<script type="text/plain" id="help">
jswmとは
========================================
ようこそ `jswm` へ。 `jswm` はオープンソースの Wiki ツールです。
YukiWikiMini 1.0.2 を JavaScript で再実装した jsWikiMini2 に改良を加えたものです。
特長
========================================
インストール不要、サーバ不要
----------------------------------------
html ファイル単体で動作するため、ブラウザさえあれば利用できます。
ソフトがインストールできない制限された環境でも利用することができます。
超高速
----------------------------------------
html ファイル単体で動作するため、ブラウザさえあれば利用できます。
サーバとの通信が行われないため動作が軽快・高速です。
データのエクスポート・インポートに対応
----------------------------------------
データをファイルとしてエクポート、またはインポートできるので
バックアップやバージョン管理を行うことができます。
使い方
========================================
* WikiName のような単語を書くと、そこから新しいページを作ることができます。
* 2017/01/01 のような日付も wiki のリンクとなります。
* URL は自動でリンクになります。
* ページを消したいときは中身を空にして保存します。
* デフォルトでは FrontPage が最初に開きます。さっそく FrontPage を作ってみましょう!
ライセンス
========================================
Perl と同じ (GPL or Artistic License) です。
開発Webサイト
========================================
https://github.com/arai-ta/jswm
</script>
<style>
html, body, #content, #textform, #textarea {
height: 95%;
}
#textarea {
width: 100%;
margin-top: 5px;
}
body { font-family: "Courier New", monospace; }
pre { line-height:130%; }
a {
text-decoration: none;
color: mediumseagreen;
}
a:hover { text-decoration: underline }
/* external link */
a.ext:after { content: "^" }
/* internal link */
a.int:before { content: "[" }
a.int:after { content: "]" }
/* uncreated wiki page */
a.new:after { content: "?" }
table { width:100%; border:none; }
code {
display: block;
margin: 20px;
padding: 5px;
background-color: #E8E8E8;
}
header > h1 {
float: left;
}
nav {
float: right;
}
nav > ul > li {
display: inline;
}
nav > ul > li:before {
content: "|";
margin: 0 10px 0 0;
}
nav > ul > li:first-child:before {
content: "";
}
article {
clear: both;
}
@media print {
nav {
display: none;
}
}
@media (max-width: 700px) {
nav > ul {
text-align: right;
list-style: none;
}
nav > ul > li {
display: list-item;
margin: 0;
}
nav > ul > li:before {
content: "";
}
}
#content dl {
white-space: initial;
padding-left: 20px;
}
</style>
</head>
<body>
<header>
<h1 id="headerH1"></h1>
<nav>
<ul>
<li><a id="linkToHomePage" accesskey="F" href="#">FrontPage</a></li>
<li id="editLink">
<a id="prevPage" class="date-nav" accesskey="p" href="#"><</a>
<a id="linkToEditPage" accesskey="E" href="#">Edit</a>
<a id="nextPage" class="date-nav" accesskey="n" href="#">></a>
<a accesskey="r" href="#Index" onclick="wiki.storeSearchKeyword(wiki.getHashPage());" style="font-size: smaller">ref</a>
</li>
<li><a id="linkToIndexPage" accesskey="i" href="#">Index</a></li>
<li><a id="linkToHelpPage" accesskey="H" href="#">Help</a></li>
<li><a id="linkToBackupPage" accesskey="b" href="#">Backup</a></li>
</ul>
</nav>
</header>
<article id="content"></article>
</body>
</html>