-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcore.js
More file actions
748 lines (742 loc) · 33.9 KB
/
core.js
File metadata and controls
748 lines (742 loc) · 33.9 KB
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
/**
* core.js.
* User: q13
* Date: 12-9-15
* Time: 下午3:56
*/
var fs=require("fs"),
moment=require("moment"),
//jsdom=require("jsdom"),
//markdown=require( "markdown").markdown,
$=require("jQuery"),
metamd=require("metamd"),
ejs=require("ejs"),
path=require("path"),
crypto = require('crypto'),
utils=require("./lib/utils");
var tplPath=path.normalize(__dirname+"/tpl");
var core={
/**
* 前期准备,结构搭建
* @param basePath
*/
preConfig:function(basePath){
var todayDirName=moment().format("YYYY-MM-DD");
var layoutContent,
indexTpl; //首页模板
var files={
"dir":["source","source/"+todayDirName,"published","published/pages","published/pages/"+todayDirName,"published/cates","published/tags","published/theme"],
"file":["cate.json","tag.json","published/index.html","published/info.json","checksum.json"]
},
configPath=basePath+"/config.json",
themePath=basePath+"/published/theme";
var config;
var serverHref={
"BASE":"/",
"THEME":"",
"CATES":"/cates",
"TAGS":"/tags",
"PAGES":"/pages"
};
//创建config.json
if(!fs.existsSync(configPath)){
fs.writeFileSync(basePath+'/config.json', fs.readFileSync(tplPath+'/config.json', "utf8"), "utf8");
//stream方式文件拷贝,pipe传输是异步的不靠谱
//fs.createReadStream(path.normalize(tplPath+'/config.json')).pipe(fs.createWriteStream(path.normalize(basePath+'/config.json')));
}
config=JSON.parse(fs.readFileSync(configPath, "utf8"));
serverHref["THEME"]="/theme/"+config.theme;
//将serverHref揉杂到config中
utils.mix(config,serverHref);
//创建预设dir和file
layoutContent=fs.readFileSync(tplPath+'/'+config.theme+"/layout.ejs","utf8");
//用indexTpl模板构建index页面
if(fs.existsSync(tplPath+'/'+config.theme+"/index.ejs")){
indexTpl=fs.readFileSync(tplPath+'/'+config.theme+"/index.ejs","utf8");
}else{
indexTpl=layoutContent;
}
files.dir.forEach(function(v,i){
var dirPath= basePath+'/'+v;
if(!fs.existsSync(dirPath)){
fs.mkdirSync(dirPath);
}
});
files.file.forEach(function(v,i){
var filePath= basePath+'/'+ v,
fileContent;
if(!fs.existsSync(filePath)){
if(v=="cate.json"||v=="tag.json"){
fileContent='{"items":[]}';
}else if(v=="published/index.html"){
fileContent=ejs.render(indexTpl, {
page:{
content:'<ul id="page-list"></ul>',
tag:" ",
cate:" "
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
}else if(v=="checksum.json"){
fileContent='{"items":[]}';
}else if(v=="published/info.json"){
fileContent='{}';
}else{
fileContent=" ";
}
//同步方式创建
fs.writeFileSync(filePath,fileContent,"utf8");
}
});
//主题配置
themePath+="/"+config.theme;
if(!fs.existsSync(themePath)){
fs.mkdirSync(themePath);
}
utils.filePipe(tplPath+'/'+config.theme+'/resources',themePath);
//返回配置项
return config;
},
archive:function(basePath,pageMds){
var config,
layoutContent, //默认模板
pageTpl, //page模板
cateTpl, //分类模板
tagTpl, //标签模板
summaryTpl, //简介模板
metaTplData={
"cate":null,
"tag":null
},
catePath=basePath+'/published/cates',
tagPath=basePath+'/published/tags',
indexPath=basePath+'/published/index.html',
checksumPath=basePath+'/checksum.json';
var todayDirName=moment().format("YYYY-MM-DD");
//准备page path
if(pageMds){
if(!Array.isArray(pageMds)){
pageMds=[basePath+'/'+pageMds];
}else{
pageMds=pageMds.map(function(pagePath){
return basePath+'/'+pagePath;
});
}
}else{
//取得basePath下的所有page markdown
pageMds=utils.getAllFiles(basePath,1).filter(function(pagePath){
if(path.extname(pagePath,1).slice(1)=="markdown"){
return true;
}
});
}
//pre config
config=core.preConfig(basePath);
//输出模板
layoutContent=fs.readFileSync(tplPath+'/'+config.theme+"/layout.ejs","utf8");
//用pageTpl模板构建page页面
if(fs.existsSync(tplPath+'/'+config.theme+"/page.ejs")){
pageTpl=fs.readFileSync(tplPath+'/'+config.theme+"/page.ejs","utf8");
}else{
pageTpl=layoutContent;
}
//用cateTpl模板构建cate页面
if(fs.existsSync(tplPath+'/'+config.theme+"/cate.ejs")){
cateTpl=fs.readFileSync(tplPath+'/'+config.theme+"/cate.ejs","utf8");
}else{
cateTpl=layoutContent;
}
//用tagTpl模板构建tag页面
if(fs.existsSync(tplPath+'/'+config.theme+"/tag.ejs")){
tagTpl=fs.readFileSync(tplPath+'/'+config.theme+"/tag.ejs","utf8");
}else{
tagTpl=layoutContent;
}
//summary模板
if(fs.existsSync(tplPath+'/'+config.theme+"/page-summary.ejs")){
summaryTpl=fs.readFileSync(tplPath+'/'+config.theme+"/page-summary.ejs","utf8");
}else{
summaryTpl='<h3 class="page-title"><a href="<%- page.link %>"><%- page.title %></a></h3><div class="page-summary"><%- page.summary %></div>';
}
metaTplData.cate=JSON.parse(fs.readFileSync(basePath+'/'+"cate.json","utf8"));
metaTplData.tag=JSON.parse(fs.readFileSync(basePath+'/'+"tag.json","utf8"));
pageMds.forEach(function(pagePath){
var mdContent=fs.readFileSync(pagePath,"utf8"),
pageInfo=metamd(mdContent),
pageContent=pageInfo.getHtml(),
pageMeta=pageInfo.getData();
var pageName=path.basename(pagePath, '.markdown'),
pageId=todayDirName+'/'+pageName,
pageRenderStr;
var cateChanged=false,
tagChanged=false,
cateStr="",
tagStr="",
cateDocEl,
tagDocEl,
indexDocEl;
var archMdPath;
//page summary template
var pageSummaryTpl=ejs.render('<li class="page-item" pageid="'+pageId+'">'+summaryTpl+'</li>', {
page:{
title:pageMeta.title,
link:config.PAGES+'/'+todayDirName+'/'+pageName+'.html',
summary:pageMeta.summary
}
});
var curReadyPages=utils.getAllFiles(basePath+'/published/pages'); //现有的page
curReadyPages=curReadyPages.concat(utils.getAllFiles(catePath)); //add cates
curReadyPages=curReadyPages.concat(utils.getAllFiles(tagPath)); //add tags
curReadyPages.push(indexPath); //add index.html
//更新cate和tag导航
if(metaTplData.cate.items.indexOf(pageMeta.cate)==-1){
metaTplData.cate.items.push(pageMeta.cate);
//重新写入
fs.writeFileSync(basePath+'/'+"cate.json",JSON.stringify(metaTplData.cate),"utf8");
cateChanged=true;
}
if(metaTplData.tag.items.indexOf(pageMeta.tag)==-1){
metaTplData.tag.items.push(pageMeta.tag);
//重新写入
fs.writeFileSync(basePath+'/'+"tag.json",JSON.stringify(metaTplData.tag),"utf8");
tagChanged=true;
}
metaTplData.cate.items.forEach(function(item){
//filter掉default category
if(item!="default"){
cateStr+='<li cateid="'+item+'"><a href="'+config.CATES+'/'+item+'.html">'+item+'</a></li>';
}
});
cateStr='<ul id="cate-list">'+cateStr+'</ul>';
metaTplData.tag.items.forEach(function(item){
//filter掉default tag
if(item!="default"){
tagStr+='<li tagid="'+item+'"><a href="'+config.TAGS+'/'+item+'.html">'+item+'</a></li>';
}
});
tagStr='<ul id="tag-list">'+tagStr+'</ul>';
pageRenderStr=ejs.render(pageTpl, {
page:{
content:pageContent,
tag:tagStr,
cate:cateStr,
title:pageMeta.title
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
fs.writeFileSync(basePath+"/published/pages/"+todayDirName+'/'+pageName+".html",pageRenderStr,"utf8");
//将md放到source目录下,以时间分类
archMdPath= basePath+"/source/"+todayDirName+'/'+pageName+".markdown";
fs.renameSync(pagePath,archMdPath);
//添加checksum
core.addChecksum(archMdPath,checksumPath);
//替换已有文件的cate and tag
if(cateChanged||tagChanged){
//替换现有的html cate或tag部分
curReadyPages.forEach(function(curReadyPage){
//var docEl=$(fs.readFileSync(curReadyPage,"utf8"));
var docEl=core.getDocEl(fs.readFileSync(curReadyPage,"utf8"));
//重新设置cate
if(cateChanged){
docEl.find("#page-cate").html(cateStr);
}
if(tagChanged){
docEl.find("#page-tag").html(tagStr);
}
//写回文件
//fs.writeFileSync(curReadyPage,core.addDoctype(docEl.html()),"utf8");
fs.writeFileSync(curReadyPage,core.addDoctype(core.getDocHtmlStr(docEl)),"utf8");
});
}
//生成新的cate或更新现有cate索引文件
if(cateChanged){
pageRenderStr=ejs.render(cateTpl, {
page:{
content:'<ul id="page-list">'+pageSummaryTpl+'</ul>',
tag:tagStr,
cate:cateStr,
title:pageMeta.cate
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
fs.writeFileSync(catePath+'/'+pageMeta.cate+".html",pageRenderStr,"utf8");
}else{
//cateDocEl=$(fs.readFileSync(catePath+'/'+pageMeta.cate+".html","utf8"));
cateDocEl=core.getDocEl(fs.readFileSync(catePath+'/'+pageMeta.cate+".html","utf8"));
$(pageSummaryTpl).appendTo($('#page-list',cateDocEl));
//写回文件
//fs.writeFileSync(catePath+'/'+pageMeta.cate+".html",core.addDoctype(cateDocEl.html()),"utf8");
fs.writeFileSync(catePath+'/'+pageMeta.cate+".html",core.addDoctype(core.getDocHtmlStr(cateDocEl)),"utf8");
}
//生成新的tag或更新现有tag索引文件
if(tagChanged){
pageRenderStr=ejs.render(tagTpl, {
page:{
content:'<ul id="page-list">'+pageSummaryTpl+'</ul>',
tag:tagStr,
cate:cateStr,
title:pageMeta.tag
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
fs.writeFileSync(tagPath+'/'+pageMeta.tag+".html",pageRenderStr,"utf8");
}else{
//tagDocEl=$(fs.readFileSync(tagPath+'/'+pageMeta.tag+".html","utf8"));
tagDocEl=core.getDocEl(fs.readFileSync(tagPath+'/'+pageMeta.tag+".html","utf8"));
$(pageSummaryTpl).appendTo($('#page-list',tagDocEl));
//写回文件
//fs.writeFileSync(tagPath+'/'+pageMeta.tag+".html",core.addDoctype(tagDocEl.html()),"utf8");
fs.writeFileSync(tagPath+'/'+pageMeta.tag+".html",core.addDoctype(core.getDocHtmlStr(tagDocEl)),"utf8");
}
//更新index索引文件
//indexDocEl=$(fs.readFileSync(indexPath,"utf8"));
indexDocEl=core.getDocEl(fs.readFileSync(indexPath,"utf8"));
$(pageSummaryTpl).appendTo($('#page-list',indexDocEl));
//写回文件
//fs.writeFileSync(indexPath,core.addDoctype(indexDocEl.html()),"utf8");
fs.writeFileSync(indexPath,core.addDoctype(core.getDocHtmlStr(indexDocEl)),"utf8");
});
//添加cate和tag附加统计信息
core.updateCateExtInfo(catePath,basePath+'/cate.json');
core.updateTagExtInfo(tagPath,basePath+'/tag.json');
//添加发布附加信息
core.setInfo(basePath+'/published/info.json',basePath+'/cate.json',basePath+'/tag.json');
},
update:function(basePath,pageMds){
var sourcePath=basePath+"/source",
checksumPath=basePath+"/checksum.json",
checksum=JSON.parse(fs.readFileSync(checksumPath, "utf8"));
//pre config
var config=core.preConfig(basePath);
var layoutContent=fs.readFileSync(tplPath+'/'+config.theme+"/layout.ejs","utf8"),
pageTpl, //page模板
cateTpl, //分类模板
tagTpl, //标签模板
summaryTpl, //简介模板
metaTplData={
"cate":null,
"tag":null
},
catePath=basePath+'/published/cates',
tagPath=basePath+'/published/tags',
indexPath=basePath+'/published/index.html';
//准备page path
if(pageMds){
if(!Array.isArray(pageMds)){
pageMds=[sourcePath+'/'+pageMds];
}else{
pageMds=pageMds.map(function(pagePath){
return sourcePath+'/'+pagePath;
});
}
}else{
//取得sources下的所有已更改的page markdown
pageMds=utils.getAllFiles(sourcePath).filter(function(pagePath){
var md5;
if(path.extname(pagePath,1).slice(1)=="markdown"){
md5=core.getChecksum(pagePath);
return checksum.items.some(function(item){
if(item.path==pagePath&&item.md5!=md5){
return true;
}
});
}
});
}
metaTplData.cate=JSON.parse(fs.readFileSync(basePath+'/'+"cate.json","utf8"));
metaTplData.tag=JSON.parse(fs.readFileSync(basePath+'/'+"tag.json","utf8"));
//用pageTpl模板构建page页面
if(fs.existsSync(tplPath+'/'+config.theme+"/page.ejs")){
pageTpl=fs.readFileSync(tplPath+'/'+config.theme+"/page.ejs","utf8");
}else{
pageTpl=layoutContent;
}
//用cateTpl模板构建cate页面
if(fs.existsSync(tplPath+'/'+config.theme+"/cate.ejs")){
cateTpl=fs.readFileSync(tplPath+'/'+config.theme+"/cate.ejs","utf8");
}else{
cateTpl=layoutContent;
}
//用tagTpl模板构建tag页面
if(fs.existsSync(tplPath+'/'+config.theme+"/tag.ejs")){
tagTpl=fs.readFileSync(tplPath+'/'+config.theme+"/tag.ejs","utf8");
}else{
tagTpl=layoutContent;
}
//summary模板
if(fs.existsSync(tplPath+'/'+config.theme+"/page-summary.ejs")){
summaryTpl=fs.readFileSync(tplPath+'/'+config.theme+"/page-summary.ejs","utf8");
}else{
summaryTpl='<h3 class="page-title"><a href="<%- page.link %>"><%- page.title %></a></h3><div class="page-summary"><%- page.summary %></div>';
}
pageMds.forEach(function(pagePath){
//重新替换published page
var mdContent=fs.readFileSync(pagePath,"utf8"),
pageInfo=metamd(mdContent),
pageContent=pageInfo.getHtml(),
pageMeta=pageInfo.getData();
var pageName=path.basename(pagePath, '.markdown'),
fullPageName=path.basename(pagePath),
curDirName=pagePath.slice(-(fullPageName.length+11),-(fullPageName.length+1)),
pageId=curDirName+'/'+pageName,
pageRenderStr;
var cateChanged=false,
tagChanged=false,
cateStr="",
tagStr="",
cateDocEl,
tagDocEl,
indexDocEl;
//page summary template
var pageSummaryTpl=ejs.render('<li class="page-item" pageid="'+pageId+'">'+summaryTpl+'</li>', {
page:{
title:pageMeta.title,
link:config.PAGES+'/'+curDirName+'/'+pageName+'.html',
summary:pageMeta.summary
}
});
var pageItemEl,
catePagePaths,
tagPagesPaths;
var curReadyPages=utils.getAllFiles(basePath+'/published/pages'); //现有的page
curReadyPages=curReadyPages.concat(utils.getAllFiles(catePath)); //add cates
curReadyPages=curReadyPages.concat(utils.getAllFiles(tagPath)); //add tags
curReadyPages.push(indexPath); //add index.html
//更新cate和tag导航
if(metaTplData.cate.items.indexOf(pageMeta.cate)==-1){
metaTplData.cate.items.push(pageMeta.cate);
//重新写入
fs.writeFileSync(basePath+'/'+"cate.json",JSON.stringify(metaTplData.cate),"utf8");
cateChanged=true;
}
if(metaTplData.tag.items.indexOf(pageMeta.tag)==-1){
metaTplData.tag.items.push(pageMeta.tag);
//重新写入
fs.writeFileSync(basePath+'/'+"tag.json",JSON.stringify(metaTplData.tag),"utf8");
tagChanged=true;
}
metaTplData.cate.items.forEach(function(item){
cateStr+='<li cateid="'+item+'"><a href="'+config.CATES+'/'+item+'.html">'+item+'</a></li>';
});
cateStr='<ul id="cate-list">'+cateStr+'</ul>';
metaTplData.tag.items.forEach(function(item){
tagStr+='<li tagid="'+item+'"><a href="'+config.TAGS+'/'+item+'.html">'+item+'</a></li>';
});
tagStr='<ul id="tag-list">'+tagStr+'</ul>';
pageRenderStr=ejs.render(pageTpl, {
page:{
content:pageContent,
tag:tagStr,
cate:cateStr,
title:pageMeta.title
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
fs.writeFileSync(basePath+"/published/pages/"+curDirName+'/'+pageName+".html",pageRenderStr,"utf8");
//更新checksum
core.updateChecksum(pagePath,checksumPath);
//替换已有文件的cate and tag
if(cateChanged||tagChanged){
//替换现有的html cate或tag部分
curReadyPages.forEach(function(curReadyPage){
var docEl=core.getDocEl(fs.readFileSync(curReadyPage,"utf8"));
//重新设置cate
if(cateChanged){
docEl.find("#page-cate").html(cateStr);
}
if(tagChanged){
docEl.find("#page-tag").html(tagStr);
}
//写回文件
fs.writeFileSync(curReadyPage,core.addDoctype(core.getDocHtmlStr(docEl)),"utf8");
});
}
//生成新的cate或更新现有cate索引文件
if(cateChanged){
pageRenderStr=ejs.render(cateTpl, {
page:{
content:'<ul id="page-list">'+pageSummaryTpl+'</ul>',
tag:tagStr,
cate:cateStr,
title:pageMeta.cate
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
fs.writeFileSync(catePath+'/'+pageMeta.cate+".html",pageRenderStr,"utf8");
}else{
cateDocEl=core.getDocEl(fs.readFileSync(catePath+'/'+pageMeta.cate+".html","utf8"));
//pageItemEl=$('#page-list [pageid]="'+pageName+'"',cateDocEl);
pageItemEl=cateDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length==0){
$(pageSummaryTpl).appendTo($('#page-list',cateDocEl));
//删除原所属cate对应page item
catePagePaths=utils.getAllFiles(catePath).filter(function(catePagePath){
return catePagePath!=catePath+'/'+pageMeta.cate+".html";
});
catePagePaths.forEach(function(catePagePath){
var cateDocEl=core.getDocEl(fs.readFileSync(catePagePath,"utf8")),
//pageItemEl=$('[pageid]="'+pageName+'"',cateDocEl);
pageItemEl=cateDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length>0){
pageItemEl.remove();
//写回文件
fs.writeFileSync(catePagePath,core.addDoctype(core.getDocHtmlStr(cateDocEl)),"utf8");
}
});
}else{
$('.page-title a',pageItemEl).text(pageMeta.title);
$('.page-summary',pageItemEl).text(pageMeta.summary);
}
//写回文件
fs.writeFileSync(catePath+'/'+pageMeta.cate+".html",core.addDoctype(core.getDocHtmlStr(cateDocEl)),"utf8");
}
//生成新的tag或更新现有tag索引文件
if(tagChanged){
pageRenderStr=ejs.render(tagTpl, {
page:{
content:'<ul id="page-list">'+pageSummaryTpl+'</ul>',
tag:tagStr,
cate:cateStr,
title:pageMeta.tag
},
BASEHREF:config.BASE,
THEMEHREF:config.THEME
});
fs.writeFileSync(tagPath+'/'+pageMeta.tag+".html",pageRenderStr,"utf8");
}else{
tagDocEl=core.getDocEl(fs.readFileSync(tagPath+'/'+pageMeta.tag+".html","utf8"));
//pageItemEl=$('#page-list [pageid]="'+pageName+'"',tagDocEl);
pageItemEl=tagDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length==0){
$(pageSummaryTpl).appendTo($('#page-list',tagDocEl));
//删除原所属cate对应page item
tagPagePaths=utils.getAllFiles(tagPath).filter(function(tagPagePath){
return tagPagePath!=tagPath+'/'+pageMeta.tag+".html";
});
tagPagePaths.forEach(function(tagPagePath){
var tagDocEl=core.getDocEl(fs.readFileSync(tagPagePath,"utf8")),
//pageItemEl=$('[pageid]="'+pageName+'"',tagDocEl);
pageItemEl=tagDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length>0){
pageItemEl.remove();
//写回文件
fs.writeFileSync(tagPagePath,core.addDoctype(core.getDocHtmlStr(tagDocEl)),"utf8");
}
});
}else{
$('.page-title a',pageItemEl).text(pageMeta.title);
$('.page-summary',pageItemEl).text(pageMeta.summary);
}
//写回文件
fs.writeFileSync(tagPath+'/'+pageMeta.tag+".html",core.addDoctype(core.getDocHtmlStr(tagDocEl)),"utf8");
}
//更新index索引文件
indexDocEl=core.getDocEl(fs.readFileSync(indexPath,"utf8"));
//pageItemEl=$('[pageid]="'+pageName+'"',indexDocEl);
pageItemEl=indexDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length>0){
pageItemEl.find('.page-content').text(pageMeta.summary);
}else{
$(pageSummaryTpl).appendTo($('#page-list',indexDocEl));
}
//写回文件
fs.writeFileSync(indexPath,core.addDoctype(core.getDocHtmlStr(indexDocEl)),"utf8");
});
//添加cate和tag附加统计信息
core.updateCateExtInfo(catePath,basePath+'/cate.json');
core.updateTagExtInfo(tagPath,basePath+'/tag.json');
//添加发布附加信息
core.setInfo(basePath+'/published/info.json',basePath+'/cate.json',basePath+'/tag.json');
},
remove:function(basePath,pageMds){
var sourcePath=basePath+"/source",
pageHtmlPath=basePath+'/published/pages',
catePath=basePath+'/published/cates',
tagPath=basePath+'/published/tags',
indexPath=basePath+'/published/index.html',
checksumPath=basePath+"/checksum.json";
//准备page path
if(pageMds){
if(!Array.isArray(pageMds)){
pageMds=[sourcePath+'/'+pageMds];
}else{
pageMds=pageMds.map(function(pagePath){
return sourcePath+'/'+pagePath;
});
}
}else{
//取得sources下的所有已更改的page markdown
pageMds=utils.getAllFiles(sourcePath);
}
pageMds.forEach(function(pagePath){
var mdContent=fs.readFileSync(pagePath,"utf8"),
pageInfo=metamd(mdContent),
pageMeta=pageInfo.getData();
var cateDocEl,tagDocEl,indexDocEl,pageItemEl;
var pageName=path.basename(pagePath, '.markdown'),
fullPageName=path.basename(pagePath),
curDirName=pagePath.slice(-(fullPageName.length+11),-(fullPageName.length+1)),
pageHtmlRelPath=curDirName+'/'+pageName,
pageId=curDirName+'/'+pageName;
//删除对应的page html
fs.unlinkSync(pageHtmlPath+'/'+pageHtmlRelPath+'.html');
//删除 checksum.json中对应的配置
core.deleteChecksum(pagePath,checksumPath);
//删除cate,tag,index中对应的索引
cateDocEl=core.getDocEl(fs.readFileSync(catePath+'/'+pageMeta.cate+".html","utf8"));
pageItemEl=cateDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length>0){
pageItemEl.remove();
//写回文件
fs.writeFileSync(catePath+'/'+pageMeta.cate+".html",core.addDoctype(core.getDocHtmlStr(cateDocEl)),"utf8");
}
tagDocEl=core.getDocEl(fs.readFileSync(tagPath+'/'+pageMeta.tag+".html","utf8"));
pageItemEl=tagDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length>0){
pageItemEl.remove();
//写回文件
fs.writeFileSync(tagPath+'/'+pageMeta.tag+".html",core.addDoctype(core.getDocHtmlStr(tagDocEl)),"utf8");
}
indexDocEl=core.getDocEl(fs.readFileSync(indexPath,"utf8"));
pageItemEl=indexDocEl.find('#page-list [pageid="'+pageId+'"]');
if(pageItemEl.length>0){
pageItemEl.remove();
//写回文件
fs.writeFileSync(indexPath,core.addDoctype(core.getDocHtmlStr(indexDocEl)),"utf8");
}
});
//添加cate和tag附加统计信息
core.updateCateExtInfo(catePath,basePath+'/cate.json');
core.updateTagExtInfo(tagPath,basePath+'/tag.json');
//添加发布附加信息
core.setInfo(basePath+'/published/info.json',basePath+'/cate.json',basePath+'/tag.json');
},
theme:function(basePath,themeName){
var configPath=basePath+"/config.json",
cateJsonPath=basePath+"/cate.json",
tagJsonPath=basePath+"/tag.json",
checksumPath=basePath+"/checksum.json",
checksumJson,
config;
checksumJson=JSON.parse(fs.readFileSync(checksumPath, "utf8"));
config=JSON.parse(fs.readFileSync(configPath, "utf8"));
//设置config.json主题
config.theme=themeName;
//写回配置文件
fs.writeFileSync(configPath, JSON.stringify(config), "utf8");
//删除catetory/tag json
fs.existsSync(cateJsonPath)&&fs.unlinkSync(cateJsonPath);
fs.existsSync(tagJsonPath)&&fs.unlinkSync(tagJsonPath);
//reset checksum json
checksumJson.items.forEach(function(item){
item.md5="";
});
//写回checksum json文件
fs.writeFileSync(checksumPath, JSON.stringify(checksumJson), "utf8");
//删除index.html首页索引文件
fs.existsSync(basePath+"/published/index.html")&&fs.unlinkSync(basePath+"/published/index.html");
//更新所有source文件
core.update(basePath);
},
addDoctype:function(htmlStr){
return '<!DOCTYPE html><html>'+htmlStr+'</html>'; //<!DOCTYPE html>之间不能有空格,否则会解析错误<html>
},
getChecksum:function(filePath){
var buffer=fs.readFileSync(filePath);
var shasum = crypto.createHash('md5');
return shasum.update(buffer).digest('hex');
},
addChecksum:function(filePath,checksumPath){
var checksum=JSON.parse(fs.readFileSync(checksumPath, "utf8")),
code=core.getChecksum(filePath);
checksum.items.push({
"path":filePath,
"md5":code
});
//写回文件
fs.writeFileSync(checksumPath,JSON.stringify(checksum),"utf8");
},
updateChecksum:function(filePath,checksumPath){
var checksum=JSON.parse(fs.readFileSync(checksumPath, "utf8")),
code=core.getChecksum(filePath);
var exist=checksum.items.some(function(item){
if(item.path==filePath){
item.md5=code;
return true;
}
});
if(exist===true){
//写回文件
fs.writeFileSync(checksumPath,JSON.stringify(checksum),"utf8");
}else{
core.addChecksum(filePath,checksumPath);
}
},
deleteChecksum:function(filePath,checksumPath){
var checksum=JSON.parse(fs.readFileSync(checksumPath, "utf8"));
checksum.items=checksum.items.filter(function(item){
return (item.path!=filePath);
});
//写回文件
fs.writeFileSync(checksumPath,JSON.stringify(checksum),"utf8");
},
getDocEl:function(htmlStr){
var filterStr=htmlStr.replace(/<script/gi,'<!--%%%<script').replace(/<\/script>/gi,'</script>%%%-->');
filterStr=filterStr.replace(/<\!\[endif\]-->/gi,'<!--[endif]-->');
return $(filterStr);
},
getDocHtmlStr:function(docEl){
var cleanStr=docEl.html().replace(/<!--%%%<script/gi,'<script').replace(/<\/script>%%%-->/gi,'</script>');
cleanStr=cleanStr.replace(/<\!--\[endif\]-->/gi,'<![endif]-->');
return cleanStr;
//return docEl.html().replace(/<!--%%%<script/gi,'<script').replace(/<\/script>%%%-->/gi,'</script>');
},
updateCateExtInfo:function(catePath,cateJsonPath){
var cateJson=JSON.parse(fs.readFileSync(cateJsonPath, "utf8")),
extInfo=[];
var catePages=utils.getAllFiles(catePath);
catePages.forEach(function(catePagePath,i){
var pageName=path.basename(catePagePath, '.html');
var cateDocEl=core.getDocEl(fs.readFileSync(catePagePath,"utf8"));
extInfo[i]={
"cateName":pageName,
"pageNum":cateDocEl.find('#page-list .page-item').length
};
});
cateJson.extInfo=extInfo;
//写回json文件
fs.writeFileSync(cateJsonPath,JSON.stringify(cateJson),"utf8");
},
updateTagExtInfo:function(tagPath,tagJsonPath){
var tagJson=JSON.parse(fs.readFileSync(tagJsonPath, "utf8")),
extInfo=[];
var tagPages=utils.getAllFiles(tagPath);
tagPages.forEach(function(tagPagePath,i){
var pageName=path.basename(tagPagePath, '.html');
var tagDocEl=core.getDocEl(fs.readFileSync(tagPagePath,"utf8"));
extInfo[i]={
"tagName":pageName,
"pageNum":tagDocEl.find('#page-list .page-item').length
};
});
tagJson.extInfo=extInfo;
//写回json文件
fs.writeFileSync(tagJsonPath,JSON.stringify(tagJson),"utf8");
},
setInfo:function(infoPath,cateJsonPath,tagJsonPath){
var info=JSON.parse(fs.readFileSync(infoPath, "utf8")),
cateJson=JSON.parse(fs.readFileSync(cateJsonPath, "utf8")),
tagJson=JSON.parse(fs.readFileSync(tagJsonPath, "utf8"));
info.cate=cateJson.extInfo;
info.tag=tagJson.extInfo;
//写回info文件
fs.writeFileSync(infoPath,JSON.stringify(info),"utf8");
}
};
module.exports=exports=core;