-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1029 lines (912 loc) · 41.9 KB
/
index.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
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==Headers==
// @Name: index
// @Description: index
// @Version: 1.0.1264
// @Author: dodying
// @Created: 2020-01-11 13:06:39
// @Modified: 2022-09-10 20:11:25
// @Namespace: https://github.com/dodying/Nodejs
// @SupportURL: https://github.com/dodying/Nodejs/issues
// @Require: archiver,chardet,iconv-lite
// ==/Headers==
/* global $ */
/* global tranStr */
let CONFIG = {
workDir: process.cwd(),
// defaultAuthor
// nativeCover
};
let THIS = {
// files 工作路径下的txt文件集
// title 标题
// content 工作路径下的首个txt文件的内容,或选择的文件的内容
};
const numberLib = {
1: '\\.0-9',
'1': '\\.0-9',
一: '点零一二两三四五六七八九十卅百千万廿卅上中下',
壹: '点點零壹贰叁参肆伍陆柒捌玖拾佰仟萬上中下',
'①': '〇①-⒛',
};
numberLib['11'] = numberLib['1'] + numberLib['1'];
numberLib['一壹'] = numberLib['一'] + numberLib['壹'];
numberLib['-1'] = numberLib['11'] + numberLib['一壹'] + numberLib['①'];
const suffixLib = {
'-1': '章回节集卷部话篇季',
};
const prefixLib = {};
// 导入原生模块
const fs = require('fs');
const path = require('path');
// 导入第三方模块
const archiver = require('archiver');
const chardet = require('chardet');
const iconv = require('iconv-lite');
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
// const numberFormat = new Intl.NumberFormat();
// Function
function wordSection(mode, word) { // 文本强制分段-测试功能
const symbol = {
lineEnd: '。?!”」』', // 句子结尾
lineStart: '“「『', // 句子开头
unbreak: '…,、—()()·《 》〈 〉._;:', // 不包括作为句子开头的标点 //作用是找到【需要断句的标点】后,不断判断之后的字符是否为标点,是则继续找,不是则断句
};
const reLineEnd = new RegExp(`[${symbol.lineEnd}]`);
const reLineStart = new RegExp(`[${symbol.lineStart}]`);
const reUnbreak = new RegExp(`[${symbol.unbreak}]`);
if (mode.includes('all')) word = word.replace(/([\r\n]+\s+)/g, '').replace(/[\r\n]+/g, '');
const arr = word.split(/[\r\n]+/);
if (mode.includes('end')) {
for (let i = 0; i < arr.length; i++) {
let j = i;
let lastWord = arr[j].substr(-1, 1);
while (!reLineEnd.test(lastWord) && arr[j].trim() && j < arr.length - 1) {
j++;
lastWord = arr[j].substr(-1, 1);
}
arr.splice(i, j - i + 1, arr.slice(i, j + 1).map((i) => i.trim()).join(''));
}
}
if (mode.includes('long')) {
for (let i = 0; i < arr.length;) {
// if (arr[i].length <= 30) continue;
const arrNew = arr[i].split('');
for (let j = 1; j < arrNew.length; j++) {
const lastWord = arrNew[j - 1].substr(-1, 1); // 查找上一个元素的最后一个字符
if (reUnbreak.test(arrNew[j]) || reLineEnd.test(arrNew[j]) || (!reLineEnd.test(lastWord) && !reLineStart.test(arrNew[j]))) {
arrNew[j - 1] += arrNew[j];
arrNew.splice(j, 1);
j--;
}
}
arr.splice(i, 1);
for (let j = 0; j < arrNew.length; j++) {
arr.splice(i, 0, arrNew[j]);
i++;
}
}
}
return arr.join('\r\n').replace(/\r\n\s+/g, '\r\n').replace(/[\r\n]+/g, '\r\n ');
}
function regexpEscape(text) {
return text.replace(/[\^$*+?.|(){}[\]]/g, '\\$&');
}
const deleteFolderRecursive = function (filepath) {
if (fs.existsSync(filepath)) {
fs.readdirSync(filepath).forEach((file, index) => {
const curPath = path.join(filepath, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(filepath);
}
};
const makeFolderRecursive = function (filepath) {
if (!fs.existsSync(filepath)) {
makeFolderRecursive(path.dirname(filepath));
fs.mkdirSync(filepath);
}
};
const writeFile = (filepath, content) => {
makeFolderRecursive(path.dirname(filepath));
if (content.match(/^data:.*?;base64,/)) {
fs.writeFileSync(filepath, content.replace(/^data:.*?;base64,/, ''), 'base64');
} else {
fs.writeFileSync(filepath, content);
}
};
const funcStart = () => {
// console.log('start')
// 重置全局参数
THIS = {};
// 设置全局元素
$('.bottomBar [name="prev"]').attr('disabled', 'disabled');
// 设置本地参数
let files;
files = fs.existsSync(CONFIG.workDir) ? fs.readdirSync(CONFIG.workDir).filter((file) => ['.txt'].includes(path.extname(file).toLowerCase())).sort(collator.compare) : [];
// 设置本地元素属性
if (files.length) {
$('.tabContent[name="start"]').find('[name="currentWorkStatus"]').addClass('actived').attr('files', files.length);
$('.bottomBar [name="next"]').removeAttr('disabled');
} else {
$('.tabContent[name="start"]').find('[name="currentWorkStatus"]').removeClass('actived');
}
files = files.slice(0, 10);
const currentWorkFileList = files.map((file) => `<li>${file}</li>`).join('');
$('.tabContent[name="start"]').find('[name="currentWorkDir"]').val(CONFIG.workDir);
$('.tabContent[name="start"]').find('[name="currentWorkFiles"]').html(currentWorkFileList);
// 设置本地元素事件
$('.tabContent[name="start"]').find('[name="selectFile"]').off('change').on('change', (e) => {
if (!e.target.files || !e.target.files.length) {
e.target.value = null;
return;
}
THIS.title = path.parse(e.target.value).name.trim();
const fr = new window.FileReader();
fr.onload = (e) => {
const content = Buffer.from(e.target.result);
const charset = chardet.detect(content);
console.log(charset);
THIS.content = iconv.decode(content, charset);
$('.tabContent[name="start"]').find('[name="selectFile"]').val(null);
$('.bottomBar [name="next"]').removeAttr('disabled');
$('.bottomBar [name="next"]').click();
};
fr.readAsArrayBuffer(e.target.files[0]);
});
$('.tabContent[name="start"]').find('input[name="list"]').off('click').on('click', (e) => {
let folder = fs.readdirSync(CONFIG.workDir).map((i) => path.resolve(CONFIG.workDir, i)).filter((i) => fs.statSync(i).isDirectory()).sort(collator.compare);
folder = folder.find((i) => fs.readdirSync(i).filter((file) => ['.txt'].includes(path.extname(file).toLowerCase())).length);
const files = fs.readdirSync(folder).filter((file) => ['.txt'].includes(path.extname(file).toLowerCase())).map((file) => path.join(folder, file)).sort(collator.compare);
if (files.length) {
$('.bottomBar [name="next"]').removeAttr('disabled');
} else {
return;
}
THIS.title = path.basename(folder);
THIS.list = [];
for (const file of files) {
let charset = chardet.detectFileSync(file, { sampleSize: 1024 * 10 });
if (!['GB18030', 'UTF-8'].includes(charset)) charset = chardet.detectFileSync(file);
console.log(charset);
const content = fs.readFileSync(file);
THIS.list.push({
title: path.parse(file).name.replace(/^\d+-(.*)$/, '$1'),
content: iconv.decode(content, charset),
});
}
THIS.content = THIS.list.map((i) => `${i.title}\r\n${i.content}`).join('\r\n\r\n');
$('.bottomBar [name="next"]').click();
THIS.files = files;
});
$('.tabContent[name="start"]').find('input[name="nextEvent"]').off('click').on('click', (e) => {
if (!THIS.content) {
THIS.files = files.map((file) => path.join(CONFIG.workDir, file));
THIS.title = path.parse(THIS.files[0]).name.trim();
let charset = chardet.detectFileSync(THIS.files[0], { sampleSize: 1024 * 10 });
if (!['GB18030', 'UTF-8'].includes(charset)) charset = chardet.detectFileSync(THIS.files[0]);
console.log(charset);
const content = fs.readFileSync(THIS.files[0]);
THIS.content = iconv.decode(content, charset);
}
if (THIS.title.match(/^《(.*?)》/)) THIS.title = THIS.title.match(/^《(.*?)》/)[1];
if (THIS.title.match(/^【(.*?)】/)) THIS.title = THIS.title.match(/^【(.*?)】/)[1];
THIS.title = THIS.title.replace(/\(.*?\)|(.*?)|【.*?】|作者:.*|全本|全集|完本|(无|未)删?减.*$/g, '').trim();
THIS.content = THIS.content.replace(/^\s*(\*+|\*\s+.*?(更多好书请访问|shuchong8).*?\s*\*)\s*$/mg, '\r\n').replace(/([^\S\r\n]|[*]){10,}/g, '\r\n').replace(//g, '').replace(/<\/?br>/gi, '\r\n')
.replace(/<\/?font>/gi, '')
.replace(/^.*(书虫包小说网).*$/mig, '')
.trim();
if (THIS.list) {
for (let i = 0; i < THIS.list.length; i++) {
THIS.list[0].content = THIS.list[0].content.replace(/^\s*(\*+|\*\s+.*?(更多好书请访问|shuchong8).*?\s*\*)\s*$/mg, '').replace(/[\s*]{10,}/g, '\r\n').replace(//g, '').replace(/<\/?br>/gi, '\r\n')
.replace(/<\/?font>/gi, '')
.replace(/^.*(书虫包小说网).*$/mig, '')
.trim();
}
}
});
};
const funcPretreat = () => {
// console.log('pretreat')
// 重置全局参数
// THIS = {}
// 设置全局元素
$('.bottomBar [name="prev"]').removeAttr('disabled');
// 设置本地参数
// 设置本地元素属性
$('.tabContent[name="pretreat"]').find('[name="content"]').text(THIS.content.replace(/\r/g, '').split(/\r*\n/).slice(0, 30).join('\n'));
// 设置本地元素事件
$('.tabContent[name="pretreat"]').find('input[type="button"][name]').off('click').on('click', (e) => {
e.stopPropagation();
const name = $(e.target).attr('name');
if (name === 'none') {
} else {
THIS.content = wordSection(name.split(','), THIS.content);
}
$('.bottomBar [name="next"]').click();
});
};
const funcInfo = () => {
// console.log('info')
// 重置全局参数
THIS.author = THIS.author || CONFIG.defaultAuthor;
THIS.cover = THIS.cover || null;
// 设置全局元素
// $('.bottomBar [name="next"]').attr('disabled', 'disabled')
// 设置本地参数
// 设置本地元素属性
$('.tabContent[name="info"]').find('[name="title"]').val(THIS.title);
$('.tabContent[name="info"]').find('[name="author"]').val(THIS.author);
// 设置本地元素事件
$('.tabContent[name="info"]').find('[name="selectCover"]').off('change').on('change', (e) => {
if (!e.target.files || !e.target.files.length) {
e.target.value = null;
return;
}
const fr = new window.FileReader();
fr.onload = (e) => {
THIS.cover = e.target.result;
};
fr.readAsDataURL(e.target.files[0]);
});
$('.tabContent[name="info"]').find('input[name="nextEvent"]').off('click').on('click', (e) => {
THIS.title = $('.tabContent[name="info"]').find('[name="title"]').val().trim();
THIS.author = $('.tabContent[name="info"]').find('[name="author"]').val().trim();
const fontSize = 20;
const width = 180;
const height = 240;
const color = '#000';
const lineHeight = 10;
const maxlen = width / fontSize - 2;
const txtArray = THIS.title.split(new RegExp(`(.{${maxlen}})`));
let i = 1;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
context.fillStyle = color;
context.strokeRect(0, 0, width, height);
context.font = `${fontSize}px sans-serif`;
context.textBaseline = 'top';
let fLeft; let
fTop;
for (let j = 0; j < txtArray.length; j++) {
if (txtArray[j] === '') continue;
fLeft = fontSize * ((maxlen - txtArray[j].length) / 2 + 1);
fTop = fontSize / 4 + fontSize * i + lineHeight * i;
context.fillText(txtArray[j], fLeft, fTop);
context.fillText('\n', fLeft, fTop);
i++;
}
THIS.cover = canvas.toDataURL();
});
};
const funcChapter = () => {
// console.log('chapter')
// 重置全局参数
THIS.chapters = THIS.chapters || THIS.list || [{
title: THIS.title,
content: THIS.content,
}];
// 设置全局元素
// 设置本地参数
let index;
const isWarnChapter = (chapter) => {
const wordCount = $('.tabContent[name="chapter"]').find('[name="wordCount"]').val() * 1;
return !chapter.title || !chapter.content || chapter.content.length < wordCount * 0.1 || chapter.content.length >= wordCount * 4;
};
const elemPattern = [
'<div class="{class}">',
'<span></span>',
'<span count="{length}">{length}</span>',
'<span>{title}</span>',
'<input type="button" key="a" title="(&a)向上移动" name="moveup" value="▲">',
'<input type="button" key="s" title="(&s)按标记分割章节" name="split" value="拆">',
'<input type="button" key="d" title="(&d)按字数分割章节" name="cut" value="分">',
'<input type="button" key="f" title="(&f)按章节字数分割章节" name="chapter" value="章">',
'<input type="button" key="g" title="(&g)合并到上一章节" name="combine" value="合">',
'<input type="button" key="h" title="(&h)新增章节" name="add" value="增">',
'<input type="button" key="j" title="(&j)插入章节" name="insert" value="插">',
'<input type="button" key="k" title="(&k)移除章节" name="delete" value="减">',
'</div>',
].join('');
const elemGen = (chapter) => {
let html = elemPattern;
html = html.replace(/\{title\}/g, $('<div>').text(chapter.title.substr(0, 30)).html());
html = html.replace(/\{length\}/g, chapter.content.length);
html = html.replace(/\{class\}/g, isWarnChapter(chapter) ? 'warn' : '');
return html;
};
const updateRegExp = () => {
let number = $('.tabContent[name="chapter"]').find('[name="patternNumber"]').val();
if (number in numberLib) {
number = numberLib[number];
number = `[${number}]+`;
}
let prefix = $('.tabContent[name="chapter"]').find('[name="patternPre"]').val();
if (prefix in prefixLib) {
prefix = prefixLib[prefix];
prefix = `[${prefix}]+`;
}
let suffix = $('.tabContent[name="chapter"]').find('[name="patternSuf"]').val();
if (suffix in suffixLib) {
suffix = suffixLib[suffix];
suffix = `[${suffix}]+`;
}
const regexp = `([\\r\\n]|^)(.*?${prefix}${number}${suffix}.*?)([\\r\\n]|$)`;
$('.tabContent[name="chapter"]').find('[name="pattern"]').val(regexp);
};
let filtered = false;
let chs = false;
const regenChapterElements = () => {
$('.tabContent[name="chapter"]').find('[name="chapterList"]').empty();
THIS.chapters.forEach((chapter) => {
$(elemGen(chapter)).appendTo('.tabContent[name="chapter"] [name="chapterList"]');
});
};
window.regenChapterElements = regenChapterElements;
let store = window.localStorage.getItem('value-store');
if (store) {
store = JSON.parse(store);
for (const name in store) {
const list = $('.tabContent[name="chapter"]').find('input[list]').filter(`[name="${name}"]`).prop('list');
$(list).append(store[name].map((value) => `<option value="${value}">${value}</option>`).join(''));
}
}
// 设置本地元素属性
regenChapterElements();
// 设置本地元素事件
$('.tabContent[name="chapter"]').find('[name="patternPre"],[name="patternNumber"],[name="patternSuf"],[name="patternGroup"],[name="titlePattern"]').off('dblclick').on('dblclick', (e) => {
e.target.value = '';
updateRegExp();
});
$('.tabContent[name="chapter"]').find('[name="patternPre"],[name="patternNumber"],[name="patternSuf"]').off('change').on('change', (e) => {
updateRegExp();
})
.change();
$('.tabContent[name="chapter"]').find('[name="patternGroup"]').off('change').on('change', (e) => {
const { value } = e.target;
if (value.split('|').length === 3) {
const [pre, number, suf] = value.split('|');
if (pre !== 'null') $('.tabContent[name="chapter"]').find('[name="patternPre"]').val(pre);
if (number !== 'null') $('.tabContent[name="chapter"]').find('[name="patternNumber"]').val(number);
if (suf !== 'null') $('.tabContent[name="chapter"]').find('[name="patternSuf"]').val(suf);
updateRegExp();
}
});
$('.tabContent[name="chapter"]').find('input[list]').on('change', (e) => {
const value = $(e.target).val();
const name = $(e.target).attr('name');
const list = $(e.target).prop('list');
const options = $(list.options).map((i, e) => $(e).val()).toArray();
if (!options.includes(value)) {
const store = JSON.parse(window.localStorage.getItem('value-store') || '{}');
if (!(name in store)) store[name] = [];
store[name].push(value);
window.localStorage.setItem('value-store', JSON.stringify(store));
$(`<option value="${value}">${value}</option>`).appendTo(list);
}
});
$('.tabContent[name="chapter"]').find('[name="filterChapter"]').off('click').on('click', (e) => {
if (filtered) {
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div.hide').removeClass('hide');
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div.filter').removeClass('filter');
} else {
const showIndex = [];
const titlePattern = $('.tabContent[name="chapter"]').find('[name="titlePattern"]').val();
const titlePatternRe = new RegExp(titlePattern, 'gim');
const isMatch = $('.tabContent[name="chapter"]').find('[name="titleMatch"]')[0].checked;
for (let i = 1; i < THIS.chapters.length; i++) {
const chapter = THIS.chapters[i];
if (titlePattern) {
const matched = THIS.chapters[i].title.match(titlePatternRe);
if ((isMatch && matched) || (!isMatch && !matched)) {
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div').eq(i).addClass('filter');
showIndex.push(i, i - 1, i + 1);
}
} else if (isWarnChapter(chapter)) {
showIndex.push(i, i - 1, i + 1);
}
}
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div').filter((i) => !showIndex.includes(i)).addClass('hide');
}
filtered = !filtered;
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div.actived').get(0).scrollIntoView();
});
$('.tabContent[name="chapter"]').find('[name="resetChapter"]').off('click').on('click', (e) => {
THIS.chapters = THIS.list || [{
title: THIS.title,
content: THIS.content,
}];
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="removeEmptyChapter"]').off('click').on('click', (e) => {
for (let i = 0; i < THIS.chapters.length; i++) {
if (THIS.chapters[i].content.length === 0) {
THIS.chapters.splice(i, 1);
i--;
}
}
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="combineEmptyChapter"]').off('click').on('click', (e) => {
for (let i = 1; i < THIS.chapters.length; i++) {
if (THIS.chapters[i].content.length === 0) {
THIS.chapters[i - 1].content += `\r\n${THIS.chapters[i].title}`;
THIS.chapters.splice(i, 1);
i--;
}
}
regenChapterElements();
});
// 合并重复名称章节
// for (let i = 0; i < THIS.chapters.length - 1; i++) {
// if (THIS.chapters[i].title === THIS.chapters[i + 1].title) {
// THIS.chapters[i].content += '\r\n' + THIS.chapters[i + 1].content;
// THIS.chapters.splice(i + 1, 1);
// i--;
// }
// }
$('.tabContent[name="chapter"]').find('[name="combineEmptyChapter2"]').off('click').on('click', (e) => {
for (let i = 0; i < THIS.chapters.length - 1; i++) {
if (THIS.chapters[i].content.length === 0) {
THIS.chapters[i].content = `${THIS.chapters[i + 1].title}\r\n${THIS.chapters[i + 1].content}`;
THIS.chapters.splice(i + 1, 1);
i--;
}
}
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="removeTitle"]').off('click').on('click', (e) => {
for (let i = 0; i < THIS.chapters.length; i++) {
THIS.chapters[i].content = THIS.chapters[i].content.replace(THIS.chapters[i].title, '');
}
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="autoSplit"]').off('click').on('click', (e) => {
const index = $('[name="chapterList"]>.actived:not(.hide)').length ? $('[name="chapterList"]>.actived:not(.hide)').index() : $('[name="chapterList"]>div:not(.hide)').index();
if (index === -1) return;
const text = THIS.chapters[index].content.substr(0, 10 * 10000);
const lib = [
// -1 = [\.0-9\.0-9点零一二两三四五六七八九十卅百千万廿卅上中下点點零壹贰叁参肆伍陆柒捌玖拾佰仟萬上中下〇①-⒛]+
// [匹配, 至少匹配次数]
[/^\s*[〇①-⒛]+/, 1, '^\\s*|①|'],
[/^\s*第.{0,5}[卷章]/, 2, '^\\s*第|-1|[卷章]'],
[/^\s*\d+[、.]/, 2, '^\\s*|\\d+|[、.]'],
[/^\s*\d{3,}/, 20, '^\\s*|\\d{3,}|'],
[/^\s*☆、/, 1, '^\\s*☆、||'],
[/^\s*正文/, 2, '^\\s*正文||'],
[/^\s*(.*?)\s*$/, 2, '^\\s*(|.*?|)\\s*$'],
[/^\s*【.*?】\s*$/, 2, '^\\s*【|.*?|】\\s*$'],
[/^\s*《.*?》\s*$/, 2, '^\\s*《|.*?|》\\s*$'],
[/^\s*「.*?」\s*$/, 2, '^\\s*「|.*?|」\\s*$'],
[/^\s*\[.*?\]\s*$/, 2, '^\\s*\\[|.*?|\\]\\s*$'],
[/^\s*\(.*?\}\s*$/, 2, '^\\s*\\(|.*?|\\}\\s*$'],
[/[(【《(][.0-90-9点零一二两三四五六七八九十卅百千万廿卅上中下点點零壹贰叁参肆伍陆柒捌玖拾佰仟萬上中下〇①-⒛]+[)】》)]/, 2, '[(【《\\(]-1[)】》\\)]'],
[/第.{0,5}[卷章]/, 2, '第|-1|[卷章]'],
[/^\s*序[章幕]?/, 1, '^\\s*序[章幕]?||'],
[/.{0,20}番外/, 1, '||番外'],
[/.{0,20}外传/, 1, '||外传'],
[/.{0,20}本卷/, 1, '||本卷'],
[/.{0,20}后日谈/, 1, '||后日谈'],
[/.{0,20}尾声/, 1, '||尾声'],
[/.{0,20}幕间/, 1, '||幕间'],
[/.{0,20}终章/, 1, '||终章'],
[/.{0,20}卷尾/, 1, '||卷尾'],
// “哟!小哥,你醒了?”忽然一道声音从门外传来,罗凡循声望去,只见一个身着青灰道袍的青年道士端着一碗黑乎乎的药汤走了过来,“小哥你运气真不错,在蒙古人屠村之后居然能活下来,正好我师父路过发现还有活人,于是把你给带了回来。”
];
for (const arr of lib) {
if (text.match(new RegExp(arr[0], 'gim')) && text.match(new RegExp(arr[0], 'gim')).length >= arr[1]) {
if (arr.length === 3) {
$('[name="patternGroup"]').val(arr[2]).trigger('change');
} else if (arr.length > 3) {
$('[name="patternPre"]').val(arr[2] || '');
$('[name="patternNumber"]').val(arr[3] || '');
$('[name="patternSuf"]').val(arr[4] || '');
updateRegExp();
}
$('[name="chapterList"]>div').eq(index).find('[name="split"]').click();
if (THIS.chapters.length >= 500) $('[name="filterChapter"]').click();
break;
}
}
// regenChapterElements();
});
// 第二行
$('.tabContent[name="chapter"]').find('[name="removeEmptyLine"]').off('click').on('click', (e) => {
for (let i = 0; i < THIS.chapters.length; i++) {
THIS.chapters[i].content = THIS.chapters[i].content.replace(/(\r?\n){2,}/g, '\r\n');
}
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="sectionLine"]').off('click').on('click', (e) => {
const action = $(e.target).attr('action');
for (let i = 0; i < THIS.chapters.length; i++) {
THIS.chapters[i].content = wordSection([action], THIS.chapters[i].content);
}
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="changeChineseST"]').off('click').on('click', (e) => {
for (let i = 0; i < THIS.chapters.length; i++) {
THIS.chapters[i].title = tranStr(THIS.chapters[i].title, chs);
THIS.chapters[i].content = tranStr(THIS.chapters[i].content, chs);
}
chs = !chs;
regenChapterElements();
});
$('.tabContent[name="chapter"]').find('[name="dealLongChapter"]').off('click').on('click', (e) => {
const action = $(e.target).attr('action');
const wordCount = $('.tabContent[name="chapter"]').find('[name="wordCount"]').val() * 1;
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div').filter((i, e) => $(e).find('[count]').attr('count') * 1 >= wordCount * 2).find(`[type="button"][name="${action}"]`)
.click();
regenChapterElements();
});
// 每章
$('.tabContent[name="chapter"]').find('[name="chapterList"]').off('click').on('click', 'span', (e) => {
$('.tabContent[name="chapter"]').find('[name="editable"]').prop('checked', false);
$('.tabContent[name="chapter"]').find('[name="content"]').attr('disabled', 'disabled');
const wordCount = $('.tabContent[name="chapter"]').find('[name="wordCount"]').val() * 1;
const target = $(e.target).parent();
target.siblings().removeClass('actived');
target.addClass('actived');
index = target.index();
$('.tabContent[name="chapter"]').find('[name="title"]').val(THIS.chapters[index].title);
$('.tabContent[name="chapter"]').find('[name="length"]').val(THIS.chapters[index].content.length);
$('.tabContent[name="chapter"]').find('[name="content"]').val(THIS.chapters[index].content.substr(0, wordCount * 10));
$('.tabContent[name="chapter"]').find('[name="content"]')[0].scrollTop = 0;
});
$('.tabContent[name="chapter"]').find('[name="chapterList"]').on('click', 'input:button', (e) => {
const target = $(e.target).parent();
target.addClass('hide');
index = target.index();
const name = $(e.target).attr('name');
if (name === 'split') {
let regexp = $('.tabContent[name="chapter"]').find('[name="pattern"]').val();
regexp = new RegExp(regexp, 'gim');
const chapter = THIS.chapters[index];
const result = chapter.content.split(regexp);
const chapters = [];
// if (result[0].trim()) {
chapters.push({
title: chapter.title,
content: result[0].trim(),
});
// }
for (let i = 1; i < result.length; i = i + 4) {
let title = result[i + 1].trim();
let content = result[i + 3].trim();
if (!title) {
content = content.split(/[\n]/).map((i) => i.replace(/[\r]/g, ''));
for (let i = 0; i < content.length;) {
if (content[i].trim()) break;
content.splice(i, 1);
}
if (content.length) {
title = content.splice(0, 1)[0].trim();
content = content.join('\r\n');
} else {
continue;
}
}
chapters.push({ title, content });
}
const elems = chapters.map((chapter) => elemGen(chapter)).join('');
$(target).replaceWith(elems);
THIS.chapters.splice(index, 1, ...chapters);
} else if (name === 'cut') {
const wordCount = $('.tabContent[name="chapter"]').find('[name="wordCount"]').val() * 1;
const lineEnd = '。?!”」』\n';
const searchMaxCount = 100;
const chapter = THIS.chapters[index];
let chapters = [];
let { content } = chapter;
const { title } = chapter;
while (content.length) {
if (content.length <= wordCount && chapters.length) {
if (content.length * 2 < wordCount) {
chapters[chapters.length - 1] += `\r\n ${content.trim()}`;
} else {
chapters.push(content);
}
break;
}
let i;
for (i = wordCount - 1; i > wordCount - searchMaxCount + 1; i = i - 1) {
if (lineEnd.includes(content[i])) break;
}
if (i === wordCount - searchMaxCount + 1) {
for (i = wordCount - 1; i < wordCount + searchMaxCount - 1; i = i + 1) {
if (i >= content.length || lineEnd.includes(content[i])) break;
}
}
if (i === wordCount + searchMaxCount - 1) i = wordCount - 1;
chapters.push(content.substr(0, i + 1));
content = content.substr(i + 1).trim();
}
chapters = chapters.map((i, o) => ({ title: `${title} - 第${o + 1}部分`, content: i }));
const elems = chapters.map((chapter) => elemGen(chapter)).join('');
$(target).replaceWith(elems);
THIS.chapters.splice(index, 1, ...chapters);
} else if (name === 'combine' && index > 0) {
const chapter = THIS.chapters[index];
const chapterBefore = THIS.chapters[index - 1];
let title;
if (chapter.title.match(/ - 第(\d+)部分$/)) {
title = chapter.title.match(/ - 第(\d+)部分$/)[1] * 1 === 1 ? `\r\n\r\n${chapter.title.replace(/ - 第(\d+)部分$/, '')}` : '';
} else {
title = `\r\n\r\n${chapter.title.trim()}`;
}
const chapterNew = {
title: chapterBefore.title.replace(/ - 第(\d+)部分$/, ''),
content: `${chapterBefore.content + title}\r\n${chapter.content.trim()}`,
};
index = index - 1;
$(target).prev().replaceWith(elemGen(chapterNew));
$(target).remove();
THIS.chapters.splice(index, 2, chapterNew);
} else if (name === 'add') {
const chapterNew = {
title: '',
content: '',
};
index = index + 1;
$(target).after(elemGen(chapterNew));
THIS.chapters.splice(index, 0, chapterNew);
} else if (name === 'insert') {
const chapterNew = {
title: '',
content: '',
};
$(target).before(elemGen(chapterNew));
THIS.chapters.splice(index, 0, chapterNew);
} else if (name === 'delete') {
$(target).remove();
THIS.chapters.splice(index, 1);
if (index === THIS.chapters.length) index = index - 1;
} else if (name === 'chapter') {
const chapterCount = $('.tabContent[name="chapter"]').find('[name="chapterCount"]').val() * 1;
const chapter = THIS.chapters[index];
const chapters = [{
title: chapter.title,
content: '',
}];
const lines = chapter.content.split(/[\n]/).map((i) => i.replace(/[\r]/g, ''));
for (const line of lines) {
if (line.trim() && line.trim().length <= chapterCount) {
chapters.push({
title: line.trim(),
content: '',
});
} else {
chapters[chapters.length - 1].content = `${chapters[chapters.length - 1].content.trim()}\r\n${line}`;
}
}
const elems = chapters.map((chapter) => elemGen(chapter)).join('');
$(target).replaceWith(elems);
THIS.chapters.splice(index, 1, ...chapters);
} else if (name === 'moveup' && index > 0) {
const chapter = THIS.chapters[index];
const chapterBefore = THIS.chapters[index - 1];
index = index - 1;
$(target).prev().replaceWith(elemGen(chapter));
$(target).replaceWith(elemGen(chapterBefore));
THIS.chapters.splice(index, 2, chapter, chapterBefore);
}
target.removeClass('hide');
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div>span:nth-child(3)').eq(index).click();
});
const keyupFunc = (e) => {
if (e.ctrlKey || e.shiftKey) return;
if ($(e.target).is('body') && $('.tabContent.actived[name="chapter"]').length) {
if (['ArrowDown'].includes(e.key)) {
index++;
} else if (['ArrowUp'].includes(e.key)) {
index--;
} else if (['ArrowRight'].includes(e.key)) {
index = index + 20;
} else if (['ArrowLeft'].includes(e.key)) {
index = index - 20;
} else if ($('.tabContent[name="chapter"]').find('[name="chapterList"]>div.actived').find(`input:button[key="${e.key}"]`).length) {
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div.actived').find(`input:button[key="${e.key}"]`).click();
} else if ($('.tabContent[name="chapter"]').find(`input:button[key="${e.key}"]`).length) {
$('.tabContent[name="chapter"]').find(`input:button[key="${e.key}"]`).click();
} else {
console.log(e.key);
}
if (['ArrowDown', 'ArrowUp', 'ArrowRight', 'ArrowLeft'].includes(e.key)) {
if (index >= THIS.chapters.length) index = index - THIS.chapters.length;
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div>span:nth-child(3)').eq(index).click();
}
if ($('.tabContent[name="chapter"]').find('[name="chapterList"]>div.actived').length) $('.tabContent[name="chapter"]').find('[name="chapterList"]>div.actived').get(0).scrollIntoView();
}
};
$(window).off('keyup', keyupFunc).on('keyup', keyupFunc);
$('.tabContent[name="chapter"]').find('[name="editable"]').off('change').on('change', (e) => {
const checked = $(e.target).prop('checked');
if (checked) {
$('.tabContent[name="chapter"]').find('[name="content"]').val(THIS.chapters[index].content);
$('.tabContent[name="chapter"]').find('[name="content"]').attr('disabled', null);
} else {
$('.tabContent[name="chapter"]').find('[name="content"]').attr('disabled', 'disabled');
THIS.chapters[index].content = $('.tabContent[name="chapter"]').find('[name="content"]').val();
const elem = $(elemGen(THIS.chapters[index])).addClass('actived');
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div').eq(index).replaceWith(elem);
}
});
$('.tabContent[name="chapter"]').find('[name="title"]').off('change').on('change', (e) => {
THIS.chapters[index].title = e.target.value;
const elem = $(elemGen(THIS.chapters[index])).addClass('actived');
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div').eq(index).replaceWith(elem);
});
// if ($('.tabContent[name="chapter"]').find('[name="chapterList"]>div').length === 1) $('.tabContent[name="chapter"]').find('[name="chapterList"]>div>input[type="button"][name="split"]').eq(0).click()
$('.tabContent[name="chapter"]').find('[name="chapterList"]>div>span:nth-child(3)').eq(0).click();
};
const funcExport = () => {
// console.log('export')
// 重置全局参数
// 设置全局元素
// $('.bottomBar [name="next"]').attr('disabled', 'disabled')
// 设置本地参数
const tempDir = path.resolve(__dirname, './../temp');
// 设置本地元素属性
// 设置本地元素事件
$('.tabContent[name="export"]').find('input[name="nextEvent"]').off('click').on('click', (e) => {
const { length } = String(THIS.chapters.length);
const uuid = `nd${new Date().getTime().toString()}`;
const files = {
mimetype: 'application/epub+zip',
'META-INF/container.xml': '<?xml version="1.0" encoding="UTF-8"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml" /></rootfiles></container>',
'OEBPS/stylesheet.css': CONFIG.css,
'OEBPS/cover.jpg': THIS.cover,
'OEBPS/content.opf': `<?xml version="1.0" encoding="UTF-8"?><package version="2.0" unique-identifier="${uuid}" xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"><dc:title>${THIS.title}</dc:title><dc:creator>${THIS.author}</dc:creator><dc:publisher>epubBuilder</dc:publisher><dc:identifier id="${uuid}">urn:uuid:${uuid}</dc:identifier><dc:language>zh-CN</dc:language><meta name="cover" content="cover-image" /></metadata><manifest>`,
'OEBPS/toc.ncx': `<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"><ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1"><head><meta name="dtb:uid" content="urn:uuid:${uuid}"/><meta name="dtb:depth" content="1"/><meta name="dtb:totalPageCount" content="0"/><meta name="dtb:maxPageNumber" content="0"/></head><docTitle><text>${THIS.title}</text></docTitle><navMap><navPoint id="navpoint-1" playOrder="1"><navLabel><text>首页</text></navLabel><content src="cover.html"/></navPoint>`,
'OEBPS/cover.html': `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>${THIS.title}</title><link type="text/css" rel="stylesheet" href="stylesheet.css" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><h1>${THIS.title}</h1><h2>本电子书由用户脚本${THIS.author}制作</h2></body></html>`,
};
let item = '<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/><item id="cover" href="cover.html" media-type="application/xhtml+xml"/><item id="css" href="stylesheet.css" media-type="text/css"/>';
let itemref = '<itemref idref="cover" linear="yes"/>';
for (let i = 0; i < THIS.chapters.length; i++) {
const chapter = THIS.chapters[i];
const chapterName = chapter.title;
const chapterOrder = String(i + 1).padStart(length, '0');
const chapterContent = chapter.content.replace(/\r/g, '').replace(/\n/g, '</p><p>').replace(/<p>\s+/g, '<p>');
files['OEBPS/toc.ncx'] = `${files['OEBPS/toc.ncx']}<navPoint id="chapter${chapterOrder}" playOrder="${i + 2}"><navLabel><text>${chapterName}</text></navLabel><content src="${chapterOrder}.html"/></navPoint>`;
item = `${item}<item id="chapter${chapterOrder}" href="${chapterOrder}.html" media-type="application/xhtml+xml"/>`;
itemref = `${itemref}<itemref idref="chapter${chapterOrder}" linear="yes"/>`;
files[`OEBPS/${chapterOrder}.html`] = `<html xmlns="http://www.w3.org/1999/xhtml"><head><title>${chapterName}</title><link type="text/css" rel="stylesheet" media="all" href="stylesheet.css" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><h3>${chapterName}</h3><div><p>${chapterContent}</p></div></body></html>`;
}
files['OEBPS/content.opf'] = `${files['OEBPS/content.opf']}${item}<item id="cover-image" href="cover.jpg" media-type="image/jpeg"/></manifest><spine toc="ncx">${itemref}</spine><guide><reference href="cover.html" type="cover" title="Cover"/></guide></package>`;
files['OEBPS/toc.ncx'] = `${files['OEBPS/toc.ncx']}</navMap></ncx>`;
// console.log(files)
for (const file in files) {
const filepath = path.resolve(tempDir, file);
writeFile(filepath, files[file]);
}
let output = path.resolve(CONFIG.workDir, `${THIS.title}.epub`);
let count = 1;
while (fs.existsSync(output)) {
output = path.resolve(CONFIG.workDir, `${THIS.title}-${count++}.epub`);
}
output = fs.createWriteStream(output);
const archive = archiver('zip', {
zlib: {
level: 9,
},
});
output.on('close', () => {
if (THIS.list) {
for (const file of THIS.files) {
fs.unlinkSync(file);
}
const dir = path.dirname(THIS.files[0]);
if (path.relative(dir, CONFIG.workDir) !== '' && fs.readdirSync(dir).length === 0) {
fs.rmdirSync(dir);
}
} else if (THIS.files && THIS.files.length) {
fs.unlinkSync(THIS.files[0]);
}
$('.bottomBar span[name="reset"]').click();
});
archive.on('error', (err) => {
throw err;
});
archive.pipe(output);
archive.directory(tempDir, false);
archive.finalize();
});
};
const funcConfig = () => {
const elems = $('.tabContent[name="config"]').find('input:not([type="button"]):not([type="file"]),select,textarea').toArray();
elems.forEach((i) => {
const key = i.getAttribute('id').replace('config-', '');
if (!(key in CONFIG)) return;
const value = CONFIG[key];
if (i.type === 'text' || i.type === 'hidden' || i.type === 'select-one' || i.type === 'number' || i.type === 'textarea') {
i.value = value;
} else if (i.type === 'checkbox') {
i.checked = value;
} else if (i.type === 'radio') {
i.checked = (i.value === value);
}
});
$('#config-btnSave').on('click', () => {
const config = {};
elems.forEach((i) => {
const key = i.getAttribute('id').replace('config-', '');
let value;
if (i.type === 'number') {
value = (i.value || i.placeholder) * 1;
if (isNaN(value)) return;
} else if (i.type === 'text' || i.type === 'hidden' || i.type === 'textarea') {
value = i.value || i.placeholder;
} else if (i.type === 'checkbox') {
value = i.checked;
} else if (i.type === 'select-one') {
value = i.value;
} else if (i.type === 'radio') {
if (!i.checked) return;
value = i.value;
}
config[key] = value;
});
CONFIG = config;
fs.writeFileSync('./config.json', JSON.stringify(config, null, 2));
});
};
// Main
const main = async () => {
$('.navBar>span[name="start"]').on('click', funcStart);
$('.navBar>span[name="pretreat"]').on('click', funcPretreat);
$('.navBar>span[name="info"]').on('click', funcInfo);
$('.navBar>span[name="chapter"]').on('click', funcChapter);
$('.navBar>span[name="export"]').on('click', funcExport);
$('.navBar>span').on('click', (e) => {
if ($(e.target).attr('disabled')) return;
$('.navBar>span').removeClass('actived');
$(e.target).addClass('actived');
const name = $(e.target).attr('name');
$('.tabContent').removeClass('actived');
$(`.tabContent[name="${name}"]`).addClass('actived');
});
$('.bottomBar span[name="reset"]').on('click', () => {
window.location.reload();
});
$('.bottomBar [name="prev"]').on('click', (e) => {
if ($(e.target).attr('disabled')) return;
$('.navBar>span.actived').prev().click();
});
$('.bottomBar [name="next"]').on('click', (e) => {
if ($(e.target).attr('disabled')) return;
$('.tabContent.actived input[name="nextEvent"]').click();
$('.navBar>span.actived').next().removeAttr('disabled').click();
});
if (!fs.existsSync('./config.json')) {
$('.navBar>span[name="config"]').click();
} else {