-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
582 lines (484 loc) · 16.3 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Markdown Editor</title>
<script src="node_modules/marked/marked.min.js"></script>
<script src="node_modules/vivliostyle/lib/vivliostyle.min.js"></script>
<script src="node_modules/prismjs/prism.js"></script>
<script src="emojify.js"></script>
<script src="codemirror/lib/codemirror.js"></script>
<script src="codemirror/overlay.js"></script>
<script src="codemirror/xml/xml.js"></script>
<script src="codemirror/markdown/markdown.js"></script>
<script src="codemirror/gfm/gfm.js"></script>
<script src="codemirror/javascript/javascript.js"></script>
<script src="codemirror/css/css.js"></script>
<script src="codemirror/htmlmixed/htmlmixed.js"></script>
<script src="codemirror/lib/util/continuelist.js"></script>
<script src="rawinflate.js"></script>
<script src="rawdeflate.js"></script>
<link rel="stylesheet" href="node_modules/prismjs/themes/prism.css">
<link rel="stylesheet" href="node_modules/vivliostyle/resources/vivliostyle-viewport.css">
<link rel="stylesheet" href="node_modules/vivliostyle/resources/vivliostyle-viewport-screen.css" media="screen">
<link rel="stylesheet" href="base16-light.css">
<link rel="stylesheet" href="codemirror/lib/codemirror.css">
<style>
body {margin: 0;}
.CodeMirror pre{
line-height: 16px;
}
#in{
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 50%;
height: auto;
overflow: auto;
font-size: 12px;
box-shadow: -10px 2px 6px 10px rgba(0,0,0,0.4);
}
.CodeMirror {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: auto;
height: auto;
}
.CodeMirror-scroll {
padding: 30px;
box-sizing: border-box;
}
.out-wrapper {
position: fixed;
top: 0;
right: 0;
left: 50%;
bottom: 0;
}
#out{
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
overflow: auto;
padding: 10px;
color: #444;
font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif;
font-size: 16px;
line-height: 1.5em
}
#page-navigation-left,
#page-navigation-right {
position: absolute;
top: 0;
width: 40px;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
color: #fff;
line-height: 100vh;
text-align: center;
vertical-align: middle;
cursor: pointer;
opacity: 0;
}
#page-navigation-left {
left: 0;
}
#page-navigation-right {
right: 0;
}
.out-wrapper:hover #page-navigation-left,
.out-wrapper:hover #page-navigation-right {
opacity: 1;
}
#menu {
display: none;
position: fixed;
background-color: #111;
border-radius: 5px;
top: 50%;
left: 50%;
width: 250px;
height: 150px;
margin-top: -75px;
margin-left: -125px;
z-index: 99;
text-align: center;
color: white;
}
#menu > span {
display: block;
font-size: 1.5em;
line-height: 1.3;
margin-top: 0.25em;
}
#menu > div {
display: inline-block;
width: 100px;
text-align: center;
vertical-align: top;
cursor: pointer;
opacity: 0.7;
}
#menu > div:hover {
opacity: 1;
}
#menu svg {
width: 64px;
height: 64px;
margin: 0 auto;
display: block;
}
#menu path {
fill: #fff;
}
#close-menu {
position: absolute;
top: 5px;
right: 9px;
color: white;
cursor: pointer;
}
.emoji {
width: 1em;
height: 1em;
vertical-align: baseline;
}
.cm-header-1 { font-size: 2em; }
.cm-header-2 { font-size: 1.75em; }
.cm-header-3 { font-size: 1.5em; }
.cm-header-4 { font-size: 1.3em; }
.cm-header-5 { font-size: 1.2em; }
.cm-header-6 { font-size: 1.15em; }
.cm-quote { color: #90a959; font-style: italic; }
.view #in {
display: none;
}
.view #out {
left: 0;
padding-left: 10px;
}
a{ color: #0645ad; text-decoration:none;}
a:visited{ color: #0b0080; }
a:hover{ color: #06e; }
a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }
p{margin:1em 0;}
img{max-width:100%;}
h1,h2,h3,h4,h5,h6{font-weight:normal;color:#111;line-height:1em;}
h4,h5,h6{ font-weight: bold; }
h1{ font-size:2.5em; }
h2{ font-size:2em; border-bottom:1px solid silver; padding-bottom: 5px; }
h3{ font-size:1.5em; }
h4{ font-size:1.2em; }
h5{ font-size:1em; }
h6{ font-size:0.9em; }
blockquote{color:#666666;margin:0;padding-left: 3em;border-left: 0.5em #EEE solid;}
hr { display: block; height: 2px; border: 0; border-top: 1px solid #aaa;border-bottom: 1px solid #eee; margin: 1em 0; padding: 0; }
pre, code{
color: #000;
font-family:Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 0.94em; /* 0.94 = 0.88 + (1.00 - 0.88) / 2 */
border-radius:3px;
background-color: #F8F8F8;
border: 1px solid #CCC;
}
pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; padding: 5px;}
pre code { border: 0px !important; background: transparent !important; line-height: 1.3em; }
code { padding: 0 3px 0 3px; }
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }
ul, ol { margin: 1em 0; padding: 0 0 0 2em; }
li p:last-child { margin:0 }
dd { margin: 0 0 0 2em; }
img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; }
table { border-collapse: collapse; border-spacing: 0; }
td, th { vertical-align: top; padding: 4px 10px; border: 1px solid #bbb; }
tr:nth-child(even) td, tr:nth-child(even) th { background: #eee; }
</style>
</head>
<body>
<div id="in"><form><textarea id="code">
# pentapod 技術書典3 原稿プレビューワ
## これはなに
技術書典3にてpentapodが発刊する,技術誌のレイアウトやページ数を確認するための
簡易プレビューワ&エディタです.
左ペインにMarkdownで原稿を書くと右ペインに原稿のプレビューが現れます.
* Ctrl+S / Cmd+S でMarkdownファイルをダウンロードします
* Ctrl+Shift+S / Cmd+Shift+S でMarkdownかHTMLを選んでダウンロードします
* ドラッグ&ドロップでファイルを読み込みます
* ファイル内容はハッシュとしてURLに保存されます
## レイアウトサンプル
レイアウトは編集中のため突如大胆に変わる可能性があります.
### 箇条書き
* サーバル
* かばん
* ハシビロコウ
- 絵文字が使えます:angel:
### スニペット
```javascript
$(function(){
$('div').html('I am a div.');
});
```
シンタックスハイライトに対応する言語は → http://prismjs.com/#languages-list
### 表
| 中央揃え | 左揃え |
|:----------:|:---------|
| うぇるかむ | とぅ |
| ざ | じゃぱりぱー |
### 図
![404](http://lohas.nicoseiga.jp/thumb/6477133i?1486829606)
## リポジトリ
https://github.com/pentapod/tbf03-previewer
</textarea></form></div>
<div class="out-wrapper">
<div id="out"></div>
<div id="page-navigation-left">←</div>
<div id="page-navigation-right">→</div>
</div>
<div id="menu">
<span>Save As</span>
<div id="saveas-markdown">
<svg height="64" width="64" xmlns="http://www.w3.org/2000/svg">
<g transform="scale(0.0625)">
<path d="M950.154 192H73.846C33.127 192 0 225.12699999999995 0 265.846v492.308C0 798.875 33.127 832 73.846 832h876.308c40.721 0 73.846-33.125 73.846-73.846V265.846C1024 225.12699999999995 990.875 192 950.154 192zM576 703.875L448 704V512l-96 123.077L256 512v192H128V320h128l96 128 96-128 128-0.125V703.875zM767.091 735.875L608 512h96V320h128v192h96L767.091 735.875z" />
</g>
</svg>
<span>Markdown</span>
</div>
<div id="saveas-html">
<svg height="64" width="64" xmlns="http://www.w3.org/2000/svg">
<g transform="scale(0.0625) translate(64,0)">
<path d="M608 192l-96 96 224 224L512 736l96 96 288-320L608 192zM288 192L0 512l288 320 96-96L160 512l224-224L288 192z" />
</g>
</svg>
<span>HTML</span>
</div>
<a id="close-menu">×</a>
</div>
<script type="text/javascript">
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob;
window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs;
// Because highlight.js is a bit awkward at times
var languageOverrides = {
js: 'javascript',
html: 'xml'
};
emojify.setConfig({
img_dir: window.location.origin + window.location.pathname + 'emoji'
});
var exLang = ["abap","actionscript","ada","apacheconf","apl","applescript","asciidoc","aspnet","autohotkey","autoit","bash","basic","batch","bison","brainfuck","bro","c","clike","coffeescript","core","cpp","crystal","csharp","css-extras","css","d","dart","diff","docker","eiffel","elixir","erlang","fortran","fsharp","gherkin","git","glsl","go","graphql","groovy","haml","handlebars","haskell","haxe","http","icon","inform7","ini","j","jade","java","javascript","jolie","json","jsx","julia","keyman","kotlin","latex","less","livescript","lolcode","lua","makefile","markdown","markup","matlab","mel","mizar","monkey","nasm","nginx","nim","nix","nsis","objectivec","ocaml","oz","parigp","parser","pascal","perl","php-extras","php","powershell","processing","prolog","properties","protobuf","puppet","pure","python","q","qore","r","reason","rest","rip","roboconf","ruby","rust","sas","sass","scala","scheme","scss","smalltalk","smarty","sql","stylus","swift","tcl","textile","twig","typescript","verilog","vhdl","vim","wiki","xojo","yaml",];
marked.setOptions({
highlight: function(code, lang, cb) {
if (!Prism.languages[lang] && exLang.indexOf(lang) >= 0) {
var script = document.createElement('script');
script.src = 'node_modules/prismjs/components/prism-' + lang + '.min.js';
script.onload = function() {
var highlighted = Prism.highlight(code, Prism.languages[lang]);
cb(null, highlighted);
};
document.getElementsByTagName('head')[0].appendChild(script);
} else {
var highlighted = (Prism.languages[lang])
? Prism.highlight(code, Prism.languages[lang])
: code;
cb(null, highlighted);
}
}
});
/* ====== Vivliostyle ====== */
function Viewer() {
this.viewer = new vivliostyle.viewer.Viewer({
userAgentRootURL: 'node_modules/vivliostyle/resources/',
viewportElement: document.getElementById('out'),
debug: false
});
}
Viewer.prototype.render = function(body) {
const url = window.URL.createObjectURL(new Blob(
[`<html>
<head>
<link rel="stylesheet" href="${window.location.origin}${window.location.pathname}source/dest/main.css">
</head>
<body>${body}</body>
</html>`],
{type: 'text/html'}
));
console.log(url);
this.viewer.loadDocument({
url: url,
startPage: undefined,
skipPagesBefore: undefined,
}, {
userStyleSheet: [{
text: '@page {size: ' + 'B5' + '}'
}]
}, {
fitToScreen: true
});
}
Viewer.prototype.navigateToLeft = function() {
this.viewer.navigateToPage('left');
}
Viewer.prototype.navigateToRight = function() {
this.viewer.navigateToPage('right');
}
/* ========================= */
var vv = new Viewer();
var timer;
var prevVal = null;
function update(e, duration){
duration = duration || 1;
clearTimeout(timer);
timer = setTimeout(function() {
var val = e.getValue();
if (prevVal !== val) {
prevVal = val;
setOutput(val);
updateHash();
}
}, duration);
}
function setOutput(val){
val = val.replace(/<equation>((.*?\n)*?.*?)<\/equation>/ig, function(a, b){
return '<img src="http://latex.codecogs.com/png.latex?' + encodeURIComponent(b) + '" />';
});
marked(val, function(err, body) {
if (err) throw err;
else {
body = emojify.replace(body);
vv.render(body);
}
});
}
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
mode: 'gfm',
lineNumbers: false,
matchBrackets: true,
lineWrapping: true,
theme: 'base16-light',
extraKeys: {"Enter": "newlineAndIndentContinueMarkdownList"}
});
editor.on('change', function(e) {
update(e, 1000);
});
document.getElementById('page-navigation-left').addEventListener('click', function() {
vv.navigateToLeft();
});
document.getElementById('page-navigation-right').addEventListener('click', function() {
vv.navigateToRight();
});
document.addEventListener('drop', function(e){
e.preventDefault();
e.stopPropagation();
var reader = new FileReader();
reader.onload = function(e){
editor.setValue(e.target.result);
};
reader.readAsText(e.dataTransfer.files[0]);
}, false);
function saveAsMarkdown(){
save(editor.getValue(), "untitled.md");
}
function saveAsHtml() {
save(document.getElementById('out').innerHTML, "untitled.html");
}
document.getElementById('saveas-markdown').addEventListener('click', function() {
saveAsMarkdown();
hideMenu();
});
document.getElementById('saveas-html').addEventListener('click', function() {
saveAsHtml();
hideMenu();
});
function save(code, name){
var blob = new Blob([code], { type: 'text/plain' });
if(window.saveAs){
window.saveAs(blob, name);
}else if(navigator.saveBlob){
navigator.saveBlob(blob, name);
}else{
url = URL.createObjectURL(blob);
var link = document.createElement("a");
link.setAttribute("href",url);
link.setAttribute("download",name);
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
link.dispatchEvent(event);
}
}
var menuVisible = false;
var menu = document.getElementById('menu');
function showMenu() {
menuVisible = true;
menu.style.display = 'block';
}
function hideMenu() {
menuVisible = false;
menu.style.display = 'none';
}
document.getElementById('close-menu').addEventListener('click', function(){
hideMenu();
});
document.addEventListener('keydown', function(e){
if(e.keyCode == 83 && (e.ctrlKey || e.metaKey)){
e.shiftKey ? showMenu() : saveAsMarkdown();
e.preventDefault();
return false;
}
if(e.keyCode === 27 && menuVisible){
hideMenu();
e.preventDefault();
return false;
}
// navigate page
if (e.keyCode === 37 && !editor.state.focused) {
vv.navigateToLeft();
}
if (e.keyCode === 39 && !editor.state.focused) {
vv.navigateToRight();
}
});
function updateHash(){
window.location.hash = btoa( // base64 so url-safe
RawDeflate.deflate( // gzip
unescape(encodeURIComponent( // convert to utf8
editor.getValue()
))
)
);
}
if(window.location.hash){
var h = window.location.hash.replace(/^#/, '');
if(h.slice(0,5) == 'view:'){
setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5))))));
document.body.className = 'view';
}else{
editor.setValue(
decodeURIComponent(escape(
RawDeflate.inflate(
atob(
h
)
)
))
);
update(editor);
editor.focus();
}
}else{
update(editor);
editor.focus();
}
</script>
</body>
</html>