wp-epub-gen 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{dist → build}/downloadImage.d.ts +0 -0
- package/{dist → build}/errors.d.ts +0 -0
- package/{dist → build}/generateTempFile.d.ts +0 -0
- package/{dist → build}/index.d.ts +1 -0
- package/build/index.js +752 -0
- package/{dist → build}/makeCover.d.ts +0 -0
- package/{dist → build}/parseContent.d.ts +0 -0
- package/{dist → build}/render.d.ts +0 -0
- package/{src → build}/types.d.ts +1 -1
- package/{dist → build}/utils.d.ts +0 -0
- package/cpi.epub +0 -0
- package/package.json +22 -22
- package/test.epub +0 -0
- package/tsconfig.json +11 -3
- package/vite.config.ts +40 -0
- package/dist/downloadImage.js +0 -88
- package/dist/errors.js +0 -14
- package/dist/generateTempFile.js +0 -121
- package/dist/index.js +0 -128
- package/dist/makeCover.js +0 -63
- package/dist/parseContent.js +0 -331
- package/dist/render.js +0 -90
- package/dist/utils.js +0 -48
- package/src/downloadImage.ts +0 -83
- package/src/errors.ts +0 -11
- package/src/generateTempFile.ts +0 -144
- package/src/index.ts +0 -152
- package/src/makeCover.ts +0 -52
- package/src/parseContent.ts +0 -351
- package/src/render.ts +0 -84
- package/src/utils.ts +0 -39
- package/test/basic.test.js +0 -28
- package/test/child-page-image.test.js +0 -21
- package/test/data/1.json +0 -21
- package/test/tt.js +0 -16
- package/webpack.config.js +0 -75
package/dist/parseContent.js
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* parseContent
|
|
4
|
-
* @author: oldj
|
|
5
|
-
* @homepage: https://oldj.net
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
const cheerio = require("cheerio");
|
|
9
|
-
const diacritics_1 = require("diacritics");
|
|
10
|
-
const mime = require("mime");
|
|
11
|
-
const path = require("path");
|
|
12
|
-
const uslug = require("uslug");
|
|
13
|
-
const uuid_1 = require("uuid");
|
|
14
|
-
function parseContent(content, index, epubConfigs) {
|
|
15
|
-
let chapter = Object.assign({}, content);
|
|
16
|
-
if (!chapter.filename) {
|
|
17
|
-
let titleSlug = uslug((0, diacritics_1.remove)(chapter.title || 'no title'));
|
|
18
|
-
titleSlug = titleSlug.replace(/[\/\\]/g, '_');
|
|
19
|
-
chapter.href = `${index}_${titleSlug}.xhtml`;
|
|
20
|
-
chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', chapter.href);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
let is_xhtml = chapter.filename.endsWith('.xhtml');
|
|
24
|
-
chapter.href = is_xhtml ? chapter.filename : `${chapter.filename}.xhtml`;
|
|
25
|
-
if (is_xhtml) {
|
|
26
|
-
chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', chapter.filename);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', `${chapter.filename}.xhtml`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
chapter.id = `item_${index}`;
|
|
33
|
-
chapter.dir = path.dirname(chapter.filePath);
|
|
34
|
-
chapter.excludeFromToc = chapter.excludeFromToc || false;
|
|
35
|
-
chapter.beforeToc = chapter.beforeToc || false;
|
|
36
|
-
// fix author array
|
|
37
|
-
if (chapter.author && typeof chapter.author === 'string') {
|
|
38
|
-
chapter.author = [chapter.author];
|
|
39
|
-
}
|
|
40
|
-
else if (!chapter.author || !Array.isArray(chapter.author)) {
|
|
41
|
-
chapter.author = [];
|
|
42
|
-
}
|
|
43
|
-
let allowedAttributes = [
|
|
44
|
-
'content',
|
|
45
|
-
'alt',
|
|
46
|
-
'id',
|
|
47
|
-
'title',
|
|
48
|
-
'src',
|
|
49
|
-
'href',
|
|
50
|
-
'about',
|
|
51
|
-
'accesskey',
|
|
52
|
-
'aria-activedescendant',
|
|
53
|
-
'aria-atomic',
|
|
54
|
-
'aria-autocomplete',
|
|
55
|
-
'aria-busy',
|
|
56
|
-
'aria-checked',
|
|
57
|
-
'aria-controls',
|
|
58
|
-
'aria-describedat',
|
|
59
|
-
'aria-describedby',
|
|
60
|
-
'aria-disabled',
|
|
61
|
-
'aria-dropeffect',
|
|
62
|
-
'aria-expanded',
|
|
63
|
-
'aria-flowto',
|
|
64
|
-
'aria-grabbed',
|
|
65
|
-
'aria-haspopup',
|
|
66
|
-
'aria-hidden',
|
|
67
|
-
'aria-invalid',
|
|
68
|
-
'aria-label',
|
|
69
|
-
'aria-labelledby',
|
|
70
|
-
'aria-level',
|
|
71
|
-
'aria-live',
|
|
72
|
-
'aria-multiline',
|
|
73
|
-
'aria-multiselectable',
|
|
74
|
-
'aria-orientation',
|
|
75
|
-
'aria-owns',
|
|
76
|
-
'aria-posinset',
|
|
77
|
-
'aria-pressed',
|
|
78
|
-
'aria-readonly',
|
|
79
|
-
'aria-relevant',
|
|
80
|
-
'aria-required',
|
|
81
|
-
'aria-selected',
|
|
82
|
-
'aria-setsize',
|
|
83
|
-
'aria-sort',
|
|
84
|
-
'aria-valuemax',
|
|
85
|
-
'aria-valuemin',
|
|
86
|
-
'aria-valuenow',
|
|
87
|
-
'aria-valuetext',
|
|
88
|
-
'class',
|
|
89
|
-
'content',
|
|
90
|
-
'contenteditable',
|
|
91
|
-
'contextmenu',
|
|
92
|
-
'datatype',
|
|
93
|
-
'dir',
|
|
94
|
-
'draggable',
|
|
95
|
-
'dropzone',
|
|
96
|
-
'hidden',
|
|
97
|
-
'hreflang',
|
|
98
|
-
'id',
|
|
99
|
-
'inlist',
|
|
100
|
-
'itemid',
|
|
101
|
-
'itemref',
|
|
102
|
-
'itemscope',
|
|
103
|
-
'itemtype',
|
|
104
|
-
'lang',
|
|
105
|
-
'media',
|
|
106
|
-
'ns1:type',
|
|
107
|
-
'ns2:alphabet',
|
|
108
|
-
'ns2:ph',
|
|
109
|
-
'onabort',
|
|
110
|
-
'onblur',
|
|
111
|
-
'oncanplay',
|
|
112
|
-
'oncanplaythrough',
|
|
113
|
-
'onchange',
|
|
114
|
-
'onclick',
|
|
115
|
-
'oncontextmenu',
|
|
116
|
-
'ondblclick',
|
|
117
|
-
'ondrag',
|
|
118
|
-
'ondragend',
|
|
119
|
-
'ondragenter',
|
|
120
|
-
'ondragleave',
|
|
121
|
-
'ondragover',
|
|
122
|
-
'ondragstart',
|
|
123
|
-
'ondrop',
|
|
124
|
-
'ondurationchange',
|
|
125
|
-
'onemptied',
|
|
126
|
-
'onended',
|
|
127
|
-
'onerror',
|
|
128
|
-
'onfocus',
|
|
129
|
-
'oninput',
|
|
130
|
-
'oninvalid',
|
|
131
|
-
'onkeydown',
|
|
132
|
-
'onkeypress',
|
|
133
|
-
'onkeyup',
|
|
134
|
-
'onload',
|
|
135
|
-
'onloadeddata',
|
|
136
|
-
'onloadedmetadata',
|
|
137
|
-
'onloadstart',
|
|
138
|
-
'onmousedown',
|
|
139
|
-
'onmousemove',
|
|
140
|
-
'onmouseout',
|
|
141
|
-
'onmouseover',
|
|
142
|
-
'onmouseup',
|
|
143
|
-
'onmousewheel',
|
|
144
|
-
'onpause',
|
|
145
|
-
'onplay',
|
|
146
|
-
'onplaying',
|
|
147
|
-
'onprogress',
|
|
148
|
-
'onratechange',
|
|
149
|
-
'onreadystatechange',
|
|
150
|
-
'onreset',
|
|
151
|
-
'onscroll',
|
|
152
|
-
'onseeked',
|
|
153
|
-
'onseeking',
|
|
154
|
-
'onselect',
|
|
155
|
-
'onshow',
|
|
156
|
-
'onstalled',
|
|
157
|
-
'onsubmit',
|
|
158
|
-
'onsuspend',
|
|
159
|
-
'ontimeupdate',
|
|
160
|
-
'onvolumechange',
|
|
161
|
-
'onwaiting',
|
|
162
|
-
'prefix',
|
|
163
|
-
'property',
|
|
164
|
-
'rel',
|
|
165
|
-
'resource',
|
|
166
|
-
'rev',
|
|
167
|
-
'role',
|
|
168
|
-
'spellcheck',
|
|
169
|
-
'style',
|
|
170
|
-
'tabindex',
|
|
171
|
-
'target',
|
|
172
|
-
'title',
|
|
173
|
-
'type',
|
|
174
|
-
'typeof',
|
|
175
|
-
'vocab',
|
|
176
|
-
'xml:base',
|
|
177
|
-
'xml:lang',
|
|
178
|
-
'xml:space',
|
|
179
|
-
'colspan',
|
|
180
|
-
'rowspan',
|
|
181
|
-
'epub:type',
|
|
182
|
-
'epub:prefix',
|
|
183
|
-
];
|
|
184
|
-
let allowedXhtml11Tags = [
|
|
185
|
-
'div',
|
|
186
|
-
'p',
|
|
187
|
-
'h1',
|
|
188
|
-
'h2',
|
|
189
|
-
'h3',
|
|
190
|
-
'h4',
|
|
191
|
-
'h5',
|
|
192
|
-
'h6',
|
|
193
|
-
'ul',
|
|
194
|
-
'ol',
|
|
195
|
-
'li',
|
|
196
|
-
'dl',
|
|
197
|
-
'dt',
|
|
198
|
-
'dd',
|
|
199
|
-
'address',
|
|
200
|
-
'hr',
|
|
201
|
-
'pre',
|
|
202
|
-
'blockquote',
|
|
203
|
-
'center',
|
|
204
|
-
'ins',
|
|
205
|
-
'del',
|
|
206
|
-
'a',
|
|
207
|
-
'span',
|
|
208
|
-
'bdo',
|
|
209
|
-
'br',
|
|
210
|
-
'em',
|
|
211
|
-
'strong',
|
|
212
|
-
'dfn',
|
|
213
|
-
'code',
|
|
214
|
-
'samp',
|
|
215
|
-
'kbd',
|
|
216
|
-
'bar',
|
|
217
|
-
'cite',
|
|
218
|
-
'abbr',
|
|
219
|
-
'acronym',
|
|
220
|
-
'q',
|
|
221
|
-
'sub',
|
|
222
|
-
'sup',
|
|
223
|
-
'tt',
|
|
224
|
-
'i',
|
|
225
|
-
'b',
|
|
226
|
-
'big',
|
|
227
|
-
'small',
|
|
228
|
-
'u',
|
|
229
|
-
's',
|
|
230
|
-
'strike',
|
|
231
|
-
'basefont',
|
|
232
|
-
'font',
|
|
233
|
-
'object',
|
|
234
|
-
'param',
|
|
235
|
-
'img',
|
|
236
|
-
'table',
|
|
237
|
-
'caption',
|
|
238
|
-
'colgroup',
|
|
239
|
-
'col',
|
|
240
|
-
'thead',
|
|
241
|
-
'tfoot',
|
|
242
|
-
'tbody',
|
|
243
|
-
'tr',
|
|
244
|
-
'th',
|
|
245
|
-
'td',
|
|
246
|
-
'embed',
|
|
247
|
-
'applet',
|
|
248
|
-
'iframe',
|
|
249
|
-
'img',
|
|
250
|
-
'map',
|
|
251
|
-
'noscript',
|
|
252
|
-
'ns:svg',
|
|
253
|
-
'object',
|
|
254
|
-
'script',
|
|
255
|
-
'table',
|
|
256
|
-
'tt',
|
|
257
|
-
'var',
|
|
258
|
-
];
|
|
259
|
-
let $ = cheerio.load(chapter.data, {
|
|
260
|
-
lowerCaseTags: true,
|
|
261
|
-
recognizeSelfClosing: true,
|
|
262
|
-
});
|
|
263
|
-
// only body innerHTML is allowed
|
|
264
|
-
let body = $('body');
|
|
265
|
-
if (body.length) {
|
|
266
|
-
let html = body.html();
|
|
267
|
-
if (html) {
|
|
268
|
-
$ = cheerio.load(html, {
|
|
269
|
-
lowerCaseTags: true,
|
|
270
|
-
recognizeSelfClosing: true,
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
$($('*').get().reverse()).each(function (elemIndex, elem) {
|
|
275
|
-
// @ts-ignore
|
|
276
|
-
let attrs = elem.attribs;
|
|
277
|
-
// @ts-ignore
|
|
278
|
-
let that = this;
|
|
279
|
-
let tags = ['img', 'br', 'hr'];
|
|
280
|
-
if (tags.includes(that.name)) {
|
|
281
|
-
if (that.name === 'img' && !$(that).attr('alt')) {
|
|
282
|
-
$(that).attr('alt', 'image-placeholder');
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
Object.entries(attrs).map(([k, v]) => {
|
|
286
|
-
if (allowedAttributes.includes(k)) {
|
|
287
|
-
if (k === 'type' && that.name !== 'script') {
|
|
288
|
-
$(that).removeAttr(k);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
$(that).removeAttr(k);
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
if (epubConfigs.version === 2) {
|
|
296
|
-
if (!allowedXhtml11Tags.includes(that.name)) {
|
|
297
|
-
if (epubConfigs.verbose) {
|
|
298
|
-
console.log('Warning (content[' + index + ']):', that.name, "tag isn't allowed on EPUB 2/XHTML 1.1 DTD.");
|
|
299
|
-
}
|
|
300
|
-
let child = $(that).html();
|
|
301
|
-
$(that).replaceWith($('<div>' + child + '</div>'));
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
$('img').each((index, elem) => {
|
|
306
|
-
let url = $(elem).attr('src') || '';
|
|
307
|
-
let image = epubConfigs.images.find((el) => el.url === url);
|
|
308
|
-
let id;
|
|
309
|
-
let extension;
|
|
310
|
-
if (image) {
|
|
311
|
-
id = image.id;
|
|
312
|
-
extension = image.extension;
|
|
313
|
-
}
|
|
314
|
-
else {
|
|
315
|
-
id = (0, uuid_1.v4)();
|
|
316
|
-
let mediaType = mime.getType(url.replace(/\?.*/, '')) || '';
|
|
317
|
-
extension = mime.getExtension(mediaType) || '';
|
|
318
|
-
let dir = chapter.dir || '';
|
|
319
|
-
let img = { id, url, dir, mediaType, extension };
|
|
320
|
-
epubConfigs.images.push(img);
|
|
321
|
-
}
|
|
322
|
-
$(elem).attr('src', `images/${id}.${extension}`);
|
|
323
|
-
});
|
|
324
|
-
chapter.data = $.xml();
|
|
325
|
-
if (Array.isArray(chapter.children)) {
|
|
326
|
-
chapter.children = chapter.children.map((content, idx) => parseContent(content, `${index}_${idx}`, epubConfigs));
|
|
327
|
-
}
|
|
328
|
-
return chapter;
|
|
329
|
-
}
|
|
330
|
-
exports.default = parseContent;
|
|
331
|
-
//# sourceMappingURL=parseContent.js.map
|
package/dist/render.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* render
|
|
4
|
-
* @author: oldj
|
|
5
|
-
* @homepage: https://oldj.net
|
|
6
|
-
*/
|
|
7
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.render = void 0;
|
|
18
|
-
const archiver = require("archiver");
|
|
19
|
-
const fs = require("fs-extra");
|
|
20
|
-
const path = require("path");
|
|
21
|
-
const downloadImage_1 = require("./downloadImage");
|
|
22
|
-
const generateTempFile_1 = require("./generateTempFile");
|
|
23
|
-
const makeCover_1 = require("./makeCover");
|
|
24
|
-
const utils_1 = require("./utils");
|
|
25
|
-
function render(data) {
|
|
26
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
-
let { log } = data;
|
|
28
|
-
log('Generating Template Files...');
|
|
29
|
-
yield (0, generateTempFile_1.generateTempFile)(data);
|
|
30
|
-
log('Downloading Images...');
|
|
31
|
-
yield (0, downloadImage_1.downloadAllImages)(data);
|
|
32
|
-
log('Making Cover...');
|
|
33
|
-
yield (0, makeCover_1.default)(data);
|
|
34
|
-
log('Generating Epub Files...');
|
|
35
|
-
yield genEpub(data);
|
|
36
|
-
if (fs.existsSync(data.output)) {
|
|
37
|
-
log('Output: ' + data.output);
|
|
38
|
-
log('Done.');
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
log('Output fail!');
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
exports.render = render;
|
|
46
|
-
function genEpub(epubData) {
|
|
47
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
-
let { log, dir, output } = epubData;
|
|
49
|
-
let archive = archiver('zip', { zlib: { level: 9 } });
|
|
50
|
-
let outputStream = fs.createWriteStream(epubData.output);
|
|
51
|
-
log('Zipping temp dir to ' + output);
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
archive.on('end', () => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
log('Done zipping, clearing temp dir...');
|
|
55
|
-
// log(fs.statSync(epubData.output).size)
|
|
56
|
-
// setTimeout(() => log(fs.statSync(epubData.output).size), 1)
|
|
57
|
-
// setTimeout(() => log(fs.statSync(epubData.output).size), 100)
|
|
58
|
-
// setTimeout(() => log(fs.statSync(epubData.output).size), 1000)
|
|
59
|
-
let stable = yield (0, utils_1.fileIsStable)(epubData.output);
|
|
60
|
-
if (!stable) {
|
|
61
|
-
log('Output epub file is not stable!');
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
fs.removeSync(dir);
|
|
65
|
-
resolve();
|
|
66
|
-
}
|
|
67
|
-
catch (e) {
|
|
68
|
-
log('[Error] ' + e.message);
|
|
69
|
-
reject(e);
|
|
70
|
-
}
|
|
71
|
-
}));
|
|
72
|
-
archive.on('close', () => {
|
|
73
|
-
log('Zip close..');
|
|
74
|
-
});
|
|
75
|
-
archive.on('finish', () => {
|
|
76
|
-
log('Zip finish..');
|
|
77
|
-
});
|
|
78
|
-
archive.on('error', (err) => {
|
|
79
|
-
log('[Archive Error] ' + err.message);
|
|
80
|
-
reject(err);
|
|
81
|
-
});
|
|
82
|
-
archive.pipe(outputStream);
|
|
83
|
-
archive.append('application/epub+zip', { store: true, name: 'mimetype' });
|
|
84
|
-
archive.directory(path.join(dir, 'META-INF'), 'META-INF');
|
|
85
|
-
archive.directory(path.join(dir, 'OEBPS'), 'OEBPS');
|
|
86
|
-
archive.finalize();
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
//# sourceMappingURL=render.js.map
|
package/dist/utils.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* utils
|
|
4
|
-
* @author: oldj
|
|
5
|
-
* @homepage: https://oldj.net
|
|
6
|
-
*/
|
|
7
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.simpleMinifier = exports.fileIsStable = exports.wait = exports.USER_AGENT = exports.writeFile = exports.readFile = void 0;
|
|
18
|
-
const fs = require("fs");
|
|
19
|
-
const util_1 = require("util");
|
|
20
|
-
exports.readFile = (0, util_1.promisify)(fs.readFile);
|
|
21
|
-
exports.writeFile = (0, util_1.promisify)(fs.writeFile);
|
|
22
|
-
exports.USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36';
|
|
23
|
-
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
24
|
-
exports.wait = wait;
|
|
25
|
-
function fileIsStable(filename, max_wait = 30000) {
|
|
26
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
-
let start_time = new Date().getTime();
|
|
28
|
-
let last_size = fs.statSync(filename).size;
|
|
29
|
-
while (new Date().getTime() - start_time <= max_wait) {
|
|
30
|
-
yield (0, exports.wait)(1000);
|
|
31
|
-
let size = fs.statSync(filename).size;
|
|
32
|
-
if (size === last_size)
|
|
33
|
-
return true;
|
|
34
|
-
last_size = size;
|
|
35
|
-
}
|
|
36
|
-
return false;
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
exports.fileIsStable = fileIsStable;
|
|
40
|
-
function simpleMinifier(xhtml) {
|
|
41
|
-
xhtml = xhtml
|
|
42
|
-
.replace(/\s+<\/a>/gi, '</a>')
|
|
43
|
-
.replace(/\n\s+</g, '\n<')
|
|
44
|
-
.replace(/\n+/g, '\n');
|
|
45
|
-
return xhtml;
|
|
46
|
-
}
|
|
47
|
-
exports.simpleMinifier = simpleMinifier;
|
|
48
|
-
//# sourceMappingURL=utils.js.map
|
package/src/downloadImage.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* downloadImage
|
|
3
|
-
* @author: oldj
|
|
4
|
-
* @homepage: https://oldj.net
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as fs from 'fs-extra'
|
|
8
|
-
import * as path from 'path'
|
|
9
|
-
import * as request from 'superagent'
|
|
10
|
-
import { IEpubData, IEpubImage } from './types'
|
|
11
|
-
import { USER_AGENT } from './utils'
|
|
12
|
-
|
|
13
|
-
const downloadImage = async (epubData: IEpubData, options: IEpubImage): Promise<void> => {
|
|
14
|
-
let { url } = options
|
|
15
|
-
let { log } = epubData
|
|
16
|
-
let epub_dir = epubData.dir
|
|
17
|
-
|
|
18
|
-
if (!url) {
|
|
19
|
-
return
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let image_dir = path.join(epub_dir, 'OEBPS', 'images')
|
|
23
|
-
fs.ensureDirSync(image_dir)
|
|
24
|
-
|
|
25
|
-
let filename = path.join(image_dir, options.id + '.' + options.extension)
|
|
26
|
-
if (url.startsWith('file://') || url.startsWith('/')) {
|
|
27
|
-
let auxPath = url.replace(/^file:\/\//i, '')
|
|
28
|
-
|
|
29
|
-
if (process.platform === 'win32') {
|
|
30
|
-
// Windows 下,把 /C:/ 转换成 C:/ 这样的形式
|
|
31
|
-
if (auxPath.match(/^\/[a-zA-Z]:/)) {
|
|
32
|
-
auxPath = auxPath.replace(/^\//, '')
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
log(`[Copy 1] '${auxPath}' to '${filename}'`)
|
|
37
|
-
if (fs.existsSync(auxPath)) {
|
|
38
|
-
try {
|
|
39
|
-
fs.copySync(auxPath, filename)
|
|
40
|
-
} catch (e) {
|
|
41
|
-
log('[Copy 1 Error] ' + e.message)
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
log(`[Copy 1 Fail] '${url}' not exists!`)
|
|
45
|
-
}
|
|
46
|
-
return
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let requestAction: any
|
|
50
|
-
if (url.startsWith('http')) {
|
|
51
|
-
requestAction = request.get(url).set({ 'User-Agent': USER_AGENT })
|
|
52
|
-
requestAction.pipe(fs.createWriteStream(filename))
|
|
53
|
-
} else {
|
|
54
|
-
log(`[Copy 2] '${url}' to '${filename}'`)
|
|
55
|
-
requestAction = fs.createReadStream(path.join(options.dir || '', url))
|
|
56
|
-
requestAction.pipe(fs.createWriteStream(filename))
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
requestAction.on('error', (err: any) => {
|
|
61
|
-
log('[Download Error] Error while downloading: ' + url)
|
|
62
|
-
log(err)
|
|
63
|
-
fs.unlinkSync(filename)
|
|
64
|
-
// reject(err)
|
|
65
|
-
resolve()
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
requestAction.on('end', () => {
|
|
69
|
-
log('[Download Success] ' + url)
|
|
70
|
-
resolve()
|
|
71
|
-
})
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export const downloadAllImages = async (epubData: IEpubData) => {
|
|
76
|
-
let { images } = epubData
|
|
77
|
-
if (images.length === 0) return
|
|
78
|
-
|
|
79
|
-
fs.ensureDirSync(path.join(epubData.dir, 'OEBPS', 'images'))
|
|
80
|
-
for (let image of images) {
|
|
81
|
-
await downloadImage(epubData, image)
|
|
82
|
-
}
|
|
83
|
-
}
|