forked from Hfimy/markdownTohtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.html
923 lines (784 loc) · 17.3 KB
/
README.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markdown to Html</title>
<style>@font-face {
font-family: octicons-anchor;
src: url(data:font/woff;charset=utf-8;base64,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) format('woff');
}
* {
box-sizing: border-box;
}
html{
padding: 0 10px;
}
body {
max-width: 1024px;
margin-right: auto;
margin-left: auto;
}
body .vs {
padding: 45px;
border: 1px solid #ddd;
border-radius: 3px;
word-wrap: break-word;
}
pre {
font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
.vs {
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
color: #333;
font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Microsoft YaHei Light";
font-size: 16px;
line-height: 1.6;
word-wrap: break-word;
background-color: #fff;
}
.vs a {
background-color: transparent;
}
.vs a:active,
.vs a:hover {
outline: 0;
}
.vs strong {
font-weight: bold;
}
.vs h1 {
font-size: 2em;
margin: 0.67em 0;
}
.vs img {
border: 0;
}
.vs hr {
box-sizing: content-box;
height: 0;
}
.vs pre {
overflow: auto;
}
.vs code,
.vs kbd,
.vs pre {
font-family: monospace, monospace;
font-size: 1em;
}
.vs input {
color: inherit;
font: inherit;
margin: 0;
}
.vs html input[disabled] {
cursor: default;
}
.vs input {
line-height: normal;
}
.vs input[type="checkbox"] {
box-sizing: border-box;
padding: 0;
}
.vs table {
border-collapse: collapse;
border-spacing: 0;
}
.vs td,
.vs th {
padding: 0;
}
.vs input {
font: 13px / 1.4 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
.vs a {
color: #4078c0;
text-decoration: none;
}
.vs a:hover,
.vs a:active {
text-decoration: underline;
}
.vs hr {
height: 0;
margin: 50px 0;
overflow: hidden;
background: transparent;
border: 0;
border-bottom: 1px solid #ddd;
}
.vs hr:before {
display: table;
content: "";
}
.vs hr:after {
display: table;
clear: both;
content: "华丽的分割线";
color: #ccc;
position: absolute;
left: 50%;
margin-left: -38px;
margin-top: -9px;
background-color: #fff;
padding: 0 2px;
font-size: 12px;
}
.vs h1,
.vs h2,
.vs h3,
.vs h4,
.vs h5,
.vs h6 {
margin-top: 35px;
margin-bottom: 25px;
line-height: 1.4;
}
.vs h1 {
font-size: 30px;
}
.vs h2 {
font-size: 21px;
}
.vs h3 {
font-size: 16px;
}
.vs h4 {
font-size: 14px;
}
.vs h5 {
font-size: 12px;
}
.vs h6 {
font-size: 11px;
}
.vs blockquote {
margin: 0;
}
.vs ul,
.vs ol {
padding: 0;
margin-top: 0;
margin-bottom: 0;
}
.vs ol ol,
.vs ul ol {
list-style-type: lower-roman;
}
.vs ul ul ol,
.vs ul ol ol,
.vs ol ul ol,
.vs ol ol ol {
list-style-type: lower-alpha;
}
.vs dd {
margin-left: 0;
}
.vs code {
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px;
}
.vs pre {
margin-top: 0;
margin-bottom: 0;
font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
.vs .select::-ms-expand {
opacity: 0;
}
.vs .octicon {
font: normal normal normal 16px/1 octicons-anchor;
display: inline-block;
text-decoration: none;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.vs .octicon-link:before {
content: '\f05c';
}
.vs:before {
display: table;
content: "";
}
.vs:after {
display: table;
clear: both;
content: "";
}
.vs>*:first-child {
margin-top: 0 !important;
}
.vs>*:last-child {
margin-bottom: 0 !important;
}
.vs a:not([href]) {
color: inherit;
text-decoration: none;
}
.vs .anchor {
display: inline-block;
padding-right: 2px;
margin-left: -18px;
}
.vs .anchor:focus {
outline: none;
}
.vs h1,
.vs h2,
.vs h3,
.vs h4,
.vs h5,
.vs h6 {
margin-top: 1em;
margin-bottom: 16px;
font-weight: bold;
line-height: 1.4;
}
.vs h1 .octicon-link,
.vs h2 .octicon-link,
.vs h3 .octicon-link,
.vs h4 .octicon-link,
.vs h5 .octicon-link,
.vs h6 .octicon-link {
color: #000;
vertical-align: middle;
visibility: hidden;
}
.vs h1:hover .anchor,
.vs h2:hover .anchor,
.vs h3:hover .anchor,
.vs h4:hover .anchor,
.vs h5:hover .anchor,
.vs h6:hover .anchor {
text-decoration: none;
}
.vs h1:hover .anchor .octicon-link,
.vs h2:hover .anchor .octicon-link,
.vs h3:hover .anchor .octicon-link,
.vs h4:hover .anchor .octicon-link,
.vs h5:hover .anchor .octicon-link,
.vs h6:hover .anchor .octicon-link {
visibility: visible;
}
.vs h1 {
padding-bottom: 0.3em;
font-size: 2.25em;
line-height: 1.2;
border-bottom: 1px solid #eee;
}
.vs h1 .anchor {
line-height: 1;
}
.vs h2 {
padding-bottom: 0.3em;
font-size: 1.75em;
line-height: 1.225;
border-bottom: 1px solid #eee;
}
.vs h2 .anchor {
line-height: 1;
}
.vs h3 {
font-size: 1.5em;
line-height: 1.43;
}
.vs h3 .anchor {
line-height: 1.2;
}
.vs h4 {
font-size: 1.25em;
}
.vs h4 .anchor {
line-height: 1.2;
}
.vs h5 {
font-size: 1em;
}
.vs h5 .anchor {
line-height: 1.1;
}
.vs h6 {
font-size: 1em;
color: #777;
}
.vs h6 .anchor {
line-height: 1.1;
}
.vs p,
.vs blockquote,
.vs ul,
.vs ol,
.vs dl,
.vs table,
.vs pre {
margin-top: 0;
margin-bottom: 16px;
}
.vs hr {
height: 1px;
padding: 0;
margin: 100px 0;
background-color: #e7e7e7;
border: 0 none;
}
.vs ul,
.vs ol {
padding-left: 2em;
}
.vs ul ul,
.vs ul ol,
.vs ol ol,
.vs ol ul {
margin-top: 0;
margin-bottom: 0;
}
.vs li>p {
margin-top: 16px;
}
.vs dl {
padding: 0;
}
.vs dl dt {
padding: 0;
margin-top: 16px;
font-size: 1em;
font-style: italic;
font-weight: bold;
}
.vs dl dd {
padding: 0 16px;
margin-bottom: 16px;
}
.vs blockquote {
padding: 0 15px;
color: #777;
border-left: 4px solid #ddd;
}
.vs blockquote>:first-child {
margin-top: 0;
}
.vs blockquote>:last-child {
margin-bottom: 0;
}
.vs table {
display: block;
width: 100%;
overflow: auto;
word-break: normal;
word-break: keep-all;
}
.vs table th {
font-weight: bold;
}
.vs table th,
.vs table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
.vs table tr {
background-color: #fff;
border-top: 1px solid #ccc;
}
.vs table tr:nth-child(2n) {
background-color: #f8f8f8;
}
.vs img {
max-width: 100%;
box-sizing: content-box;
background-color: #fff;
}
.vs code {
padding: 0;
padding-top: 0.2em;
padding-bottom: 0.2em;
margin: 0;
font-size: 85%;
background-color: rgba(0, 0, 0, 0.04);
border-radius: 3px;
}
.vs code:before,
.vs code:after {
letter-spacing: -0.2em;
content: "\00a0";
}
.vs pre>code {
padding: 0;
margin: 0;
font-size: 100%;
word-break: normal;
white-space: pre;
background: transparent;
border: 0;
}
.vs .highlight {
margin-bottom: 16px;
}
.vs .highlight pre,
.vs pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #f7f7f7;
border-radius: 3px;
}
.vs .highlight pre {
margin-bottom: 0;
word-break: normal;
}
.vs pre {
word-wrap: normal;
}
.vs pre code {
display: inline;
max-width: initial;
padding: 0;
margin: 0;
overflow: initial;
line-height: inherit;
word-wrap: normal;
background-color: transparent;
border: 0;
}
.vs pre code:before,
.vs pre code:after {
content: normal;
}
.vs kbd {
display: inline-block;
padding: 3px 5px;
font-size: 11px;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb;
}
.vs .pl-c {
color: #969896;
}
.vs .pl-c1,
.vs .pl-s .pl-v {
color: #0086b3;
}
.vs .pl-e,
.vs .pl-en {
color: #795da3;
}
.vs .pl-s .pl-s1,
.vs .pl-smi {
color: #333;
}
.vs .pl-ent {
color: #63a35c;
}
.vs .pl-k {
color: #a71d5d;
}
.vs .pl-pds,
.vs .pl-s,
.vs .pl-s .pl-pse .pl-s1,
.vs .pl-sr,
.vs .pl-sr .pl-cce,
.vs .pl-sr .pl-sra,
.vs .pl-sr .pl-sre {
color: #183691;
}
.vs .pl-v {
color: #ed6a43;
}
.vs .pl-id {
color: #b52a1d;
}
.vs .pl-ii {
background-color: #b52a1d;
color: #f8f8f8;
}
.vs .pl-sr .pl-cce {
color: #63a35c;
font-weight: bold;
}
.vs .pl-ml {
color: #693a17;
}
.vs .pl-mh,
.vs .pl-mh .pl-en,
.vs .pl-ms {
color: #1d3e81;
font-weight: bold;
}
.vs .pl-mq {
color: #008080;
}
.vs .pl-mi {
color: #333;
font-style: italic;
}
.vs .pl-mb {
color: #333;
font-weight: bold;
}
.vs .pl-md {
background-color: #ffecec;
color: #bd2c00;
}
.vs .pl-mi1 {
background-color: #eaffea;
color: #55a532;
}
.vs .pl-mdr {
color: #795da3;
font-weight: bold;
}
.vs .pl-mo {
color: #1d3e81;
}
.vs kbd {
display: inline-block;
padding: 3px 5px;
font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb;
}
.vs .plan-price-unit {
color: #767676;
font-weight: normal;
}
.vs .task-list-item {
list-style-type: none;
}
.vs .task-list-item+.task-list-item {
margin-top: 3px;
}
.vs .task-list-item input {
margin: 0 0.35em 0.25em -1.6em;
vertical-align: middle;
}
.vs .plan-choice {
padding: 15px;
padding-left: 40px;
display: block;
border: 1px solid #e0e0e0;
position: relative;
font-weight: normal;
background-color: #fafafa;
}
.vs .plan-choice.open {
background-color: #fff;
}
.vs .plan-choice.open .plan-choice-seat-breakdown {
display: block;
}
.vs .plan-choice-free {
border-radius: 3px 3px 0 0;
}
.vs .plan-choice-paid {
border-radius: 0 0 3px 3px;
border-top: 0;
margin-bottom: 20px;
}
.vs .plan-choice-radio {
position: absolute;
left: 15px;
top: 18px;
}
.vs .plan-choice-exp {
color: #999;
font-size: 12px;
margin-top: 5px;
}
.vs .plan-choice-seat-breakdown {
margin-top: 10px;
display: none;
}
.vs:checked+.radio-label {
z-index: 1;
position: relative;
border-color: #4078c0;
}
</style>
</head>
<body>
<div class='vs'>
<h1 id="node-">Node服务器端开发第四天</h1>
<blockquote>
<p>文件流、网络操作、服务端Web开发基础</p>
</blockquote>
<h2 id="-">文件操作</h2>
<h3 id="-">文件监视</h3>
<h4 id="-markdown-">利用文件监视实现自动 markdown 文件转换</h4>
<ul>
<li><p>相关链接:</p>
<ol>
<li><a href="https://github.com/chjj/marked">https://github.com/chjj/marked</a></li>
<li><a href="https://github.com/Browsersync/browser-sync">https://github.com/Browsersync/browser-sync</a> </li>
</ol>
</li>
</ul>
<ul>
<li>实现思路:<ol>
<li>利用<code>fs</code>模块的文件监视功能监视指定MD文件</li>
<li>当文件发生变化后,借助<code>marked</code>包提供的<code>markdown</code> to <code>html</code>功能将改变后的MD文件转换为HTML</li>
<li>再将得到的HTML替换到模版中</li>
<li>最后利用BrowserSync模块实现浏览器自动刷新
browsersync需要用到Python</li>
</ol>
</li>
</ul>
<pre><code class="lang-javascript">const fs = require('fs');
const path = require('path');
var marked = require('marked');
var bs = require('browser-sync').create();
var target = path.join(__dirname, process.argv[2] || './README.md');
var filename = path.basename(target, path.extname(target)) + '.html';
var targetHtml = path.join(path.dirname(target), filename);
bs.init({
server: path.dirname(target),
index: filename,
notify: false
});
bs.reload(filename);
var template = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>{{{styles}}}</style>
</head>
<body>
<article class="markdown">
{{{body}}}
</article>
</body>
</html>
`;
fs.readFile(path.join(__dirname, './markdown.css'), 'utf8', (error, css) => {
if (error) throw error;
template = template.replace('{{{styles}}}', css);
var handler = (current, previous) => {
fs.readFile(target, 'utf8', (error, content) => {
var html = template.replace('{{{body}}}', marked(content));
fs.writeFile(targetHtml, html, (error) => {
if (!error) {
console.log(`updated@${new Date()}`);
bs.reload(filename);
}
});
});
};
handler();
fs.watchFile(target, { interval: 100 }, handler);
});
</code></pre>
<hr>
<h2 id="-">文件流</h2>
<h3 id="-">什么是流</h3>
<p><img src="./img/01.png" alt="二进制的世界"></p>
<ul>
<li><p>现实过程的流</p>
<ul>
<li>水流,人流</li>
</ul>
</li>
<li><p>在程序开发的概念中</p>
<ul>
<li>流是程序输入或输出的一个连续的字节序列</li>
<li>文件流、网络流</li>
<li>设备(例如鼠标、键盘、磁盘、屏幕、调制解调器和打印机)的输入和输出都是用流来处理的。</li>
</ul>
</li>
</ul>
<h3 id="node-">Node中的流操作</h3>
<p><img src="./img/02.png" alt="流的操作"></p>
<p>在 Node 核心模块 fs 中定义了一些与流相关的 API</p>
<ul>
<li>fs.createReadStream()<ul>
<li>=> 得到一个ReadableStream</li>
</ul>
</li>
<li>fs.createWriteStream()<ul>
<li>=> 得到一个WritableStream</li>
</ul>
</li>
</ul>
<h3 id="-api">读取流常用 API</h3>
<h4 id="event-">Event:</h4>
<ul>
<li>data</li>
<li>end</li>
<li>error</li>
</ul>
<h4 id="method-">Method:</h4>
<ul>
<li>read([size])、</li>
<li>pause()、</li>
<li>isPause()、</li>
<li>resume()、</li>
<li>setEncoding(encoding)、</li>
<li>pipe(destination[, options])、</li>
<li>unpipe([destination])</li>
</ul>
<h3 id="-api">写入流常用 API</h3>
<h4 id="event-">Event:</h4>
<ul>
<li>error</li>
<li>pipe</li>
</ul>
<h4 id="method-">Method:</h4>
<ul>
<li>write(chunk[, encoding][, callback])</li>
<li>end([chunk][, encoding][, callback])</li>
<li>setDefaultEncoding(encoding)</li>
</ul>
<hr>
<h2 id="-">网络操作</h2>
<p>如何计算循环节长度呢?</p>
<p>想想我们手算,如果余数比除数小,我们会在后面补0然后再除。也就是说补零之后的数是下一次的被除数。如果被除数重复出现,除数确定的,那么商和余数也就是一样的。这时,循环节就出现了。</p>
<p>下面的函数分为两部分,</p>
<p>补零操作
查找有没有同样的被除数存在,如果有,就找到了循环节,计算循环节长度并返回。在没有找到的前提下,把当前被除数记录下来,并得到余数作为下一次的被除数。
这里需要注意,如果某一次出现了除尽的情况,说明该分数是有限小数,循环节长度是0。
JavaScript</p>
<p>console.log(foo(11, 3));<br>console.log(foo(1, 7));;<br>console.log(foo(2, 10));;<br>function foo(a, b) {<br> if (!(a % b)) {
return <code>${a}÷${b}:${a / b}</code>;
}
var all = (a / b).toString().split('.');
var i = getCycleSection(a, b);
if(i)
return <code>${a}÷${b}: ${all[0]}.{${all[1].substr(0, i) }}</code>;
return <code>${a}÷${b}: ${a / b}</code>;</p>
<p>}
function getCycleSection(n, m) {<br> var temp = [];
while (true) {
while (n < m) {
n *= 10;
}
var index = temp.indexOf(n);
if (index >= 0) {
return temp.length - index;
}
temp.push(n);
n %= m;
if (!n)
return 0;
}
}</p>
</div>
</body>
</html>