wp-epub-gen 0.1.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/.eslintrc.js +24 -0
- package/.mocharc.json +3 -0
- package/.prettierrc.json +19 -0
- package/README.md +87 -0
- package/dist/downloadImage.d.ts +7 -0
- package/dist/downloadImage.js +82 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.js +14 -0
- package/dist/generateTempFile.d.ts +7 -0
- package/dist/generateTempFile.js +121 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +128 -0
- package/dist/makeCover.d.ts +7 -0
- package/dist/makeCover.js +63 -0
- package/dist/parseContent.d.ts +7 -0
- package/dist/parseContent.js +330 -0
- package/dist/render.d.ts +7 -0
- package/dist/render.js +90 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.js +48 -0
- package/package.json +66 -0
- package/src/downloadImage.ts +75 -0
- package/src/errors.ts +11 -0
- package/src/generateTempFile.ts +144 -0
- package/src/index.ts +152 -0
- package/src/makeCover.ts +52 -0
- package/src/parseContent.ts +350 -0
- package/src/render.ts +84 -0
- package/src/types.d.ts +79 -0
- package/src/utils.ts +39 -0
- package/templates/epub2/content.opf.ejs +57 -0
- package/templates/epub2/toc.xhtml.ejs +26 -0
- package/templates/epub3/content.opf.ejs +86 -0
- package/templates/epub3/toc.xhtml.ejs +38 -0
- package/templates/template.css +46 -0
- package/templates/toc.ncx.ejs +45 -0
- package/test/basic.test.js +28 -0
- package/test/child-page-image.test.js +21 -0
- package/test/data/1.json +21 -0
- package/test/tt.js +16 -0
- package/tsconfig.json +31 -0
- package/webpack.config.js +75 -0
|
@@ -0,0 +1,330 @@
|
|
|
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
|
+
chapter.href = `${index}_${titleSlug}.xhtml`;
|
|
19
|
+
chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', chapter.href);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
let is_xhtml = chapter.filename.endsWith('.xhtml');
|
|
23
|
+
chapter.href = is_xhtml ? chapter.filename : `${chapter.filename}.xhtml`;
|
|
24
|
+
if (is_xhtml) {
|
|
25
|
+
chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', chapter.filename);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
chapter.filePath = path.join(epubConfigs.dir, 'OEBPS', `${chapter.filename}.xhtml`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
chapter.id = `item_${index}`;
|
|
32
|
+
chapter.dir = path.dirname(chapter.filePath);
|
|
33
|
+
chapter.excludeFromToc = chapter.excludeFromToc || false;
|
|
34
|
+
chapter.beforeToc = chapter.beforeToc || false;
|
|
35
|
+
// fix author array
|
|
36
|
+
if (chapter.author && typeof chapter.author === 'string') {
|
|
37
|
+
chapter.author = [chapter.author];
|
|
38
|
+
}
|
|
39
|
+
else if (!chapter.author || !Array.isArray(chapter.author)) {
|
|
40
|
+
chapter.author = [];
|
|
41
|
+
}
|
|
42
|
+
let allowedAttributes = [
|
|
43
|
+
'content',
|
|
44
|
+
'alt',
|
|
45
|
+
'id',
|
|
46
|
+
'title',
|
|
47
|
+
'src',
|
|
48
|
+
'href',
|
|
49
|
+
'about',
|
|
50
|
+
'accesskey',
|
|
51
|
+
'aria-activedescendant',
|
|
52
|
+
'aria-atomic',
|
|
53
|
+
'aria-autocomplete',
|
|
54
|
+
'aria-busy',
|
|
55
|
+
'aria-checked',
|
|
56
|
+
'aria-controls',
|
|
57
|
+
'aria-describedat',
|
|
58
|
+
'aria-describedby',
|
|
59
|
+
'aria-disabled',
|
|
60
|
+
'aria-dropeffect',
|
|
61
|
+
'aria-expanded',
|
|
62
|
+
'aria-flowto',
|
|
63
|
+
'aria-grabbed',
|
|
64
|
+
'aria-haspopup',
|
|
65
|
+
'aria-hidden',
|
|
66
|
+
'aria-invalid',
|
|
67
|
+
'aria-label',
|
|
68
|
+
'aria-labelledby',
|
|
69
|
+
'aria-level',
|
|
70
|
+
'aria-live',
|
|
71
|
+
'aria-multiline',
|
|
72
|
+
'aria-multiselectable',
|
|
73
|
+
'aria-orientation',
|
|
74
|
+
'aria-owns',
|
|
75
|
+
'aria-posinset',
|
|
76
|
+
'aria-pressed',
|
|
77
|
+
'aria-readonly',
|
|
78
|
+
'aria-relevant',
|
|
79
|
+
'aria-required',
|
|
80
|
+
'aria-selected',
|
|
81
|
+
'aria-setsize',
|
|
82
|
+
'aria-sort',
|
|
83
|
+
'aria-valuemax',
|
|
84
|
+
'aria-valuemin',
|
|
85
|
+
'aria-valuenow',
|
|
86
|
+
'aria-valuetext',
|
|
87
|
+
'class',
|
|
88
|
+
'content',
|
|
89
|
+
'contenteditable',
|
|
90
|
+
'contextmenu',
|
|
91
|
+
'datatype',
|
|
92
|
+
'dir',
|
|
93
|
+
'draggable',
|
|
94
|
+
'dropzone',
|
|
95
|
+
'hidden',
|
|
96
|
+
'hreflang',
|
|
97
|
+
'id',
|
|
98
|
+
'inlist',
|
|
99
|
+
'itemid',
|
|
100
|
+
'itemref',
|
|
101
|
+
'itemscope',
|
|
102
|
+
'itemtype',
|
|
103
|
+
'lang',
|
|
104
|
+
'media',
|
|
105
|
+
'ns1:type',
|
|
106
|
+
'ns2:alphabet',
|
|
107
|
+
'ns2:ph',
|
|
108
|
+
'onabort',
|
|
109
|
+
'onblur',
|
|
110
|
+
'oncanplay',
|
|
111
|
+
'oncanplaythrough',
|
|
112
|
+
'onchange',
|
|
113
|
+
'onclick',
|
|
114
|
+
'oncontextmenu',
|
|
115
|
+
'ondblclick',
|
|
116
|
+
'ondrag',
|
|
117
|
+
'ondragend',
|
|
118
|
+
'ondragenter',
|
|
119
|
+
'ondragleave',
|
|
120
|
+
'ondragover',
|
|
121
|
+
'ondragstart',
|
|
122
|
+
'ondrop',
|
|
123
|
+
'ondurationchange',
|
|
124
|
+
'onemptied',
|
|
125
|
+
'onended',
|
|
126
|
+
'onerror',
|
|
127
|
+
'onfocus',
|
|
128
|
+
'oninput',
|
|
129
|
+
'oninvalid',
|
|
130
|
+
'onkeydown',
|
|
131
|
+
'onkeypress',
|
|
132
|
+
'onkeyup',
|
|
133
|
+
'onload',
|
|
134
|
+
'onloadeddata',
|
|
135
|
+
'onloadedmetadata',
|
|
136
|
+
'onloadstart',
|
|
137
|
+
'onmousedown',
|
|
138
|
+
'onmousemove',
|
|
139
|
+
'onmouseout',
|
|
140
|
+
'onmouseover',
|
|
141
|
+
'onmouseup',
|
|
142
|
+
'onmousewheel',
|
|
143
|
+
'onpause',
|
|
144
|
+
'onplay',
|
|
145
|
+
'onplaying',
|
|
146
|
+
'onprogress',
|
|
147
|
+
'onratechange',
|
|
148
|
+
'onreadystatechange',
|
|
149
|
+
'onreset',
|
|
150
|
+
'onscroll',
|
|
151
|
+
'onseeked',
|
|
152
|
+
'onseeking',
|
|
153
|
+
'onselect',
|
|
154
|
+
'onshow',
|
|
155
|
+
'onstalled',
|
|
156
|
+
'onsubmit',
|
|
157
|
+
'onsuspend',
|
|
158
|
+
'ontimeupdate',
|
|
159
|
+
'onvolumechange',
|
|
160
|
+
'onwaiting',
|
|
161
|
+
'prefix',
|
|
162
|
+
'property',
|
|
163
|
+
'rel',
|
|
164
|
+
'resource',
|
|
165
|
+
'rev',
|
|
166
|
+
'role',
|
|
167
|
+
'spellcheck',
|
|
168
|
+
'style',
|
|
169
|
+
'tabindex',
|
|
170
|
+
'target',
|
|
171
|
+
'title',
|
|
172
|
+
'type',
|
|
173
|
+
'typeof',
|
|
174
|
+
'vocab',
|
|
175
|
+
'xml:base',
|
|
176
|
+
'xml:lang',
|
|
177
|
+
'xml:space',
|
|
178
|
+
'colspan',
|
|
179
|
+
'rowspan',
|
|
180
|
+
'epub:type',
|
|
181
|
+
'epub:prefix',
|
|
182
|
+
];
|
|
183
|
+
let allowedXhtml11Tags = [
|
|
184
|
+
'div',
|
|
185
|
+
'p',
|
|
186
|
+
'h1',
|
|
187
|
+
'h2',
|
|
188
|
+
'h3',
|
|
189
|
+
'h4',
|
|
190
|
+
'h5',
|
|
191
|
+
'h6',
|
|
192
|
+
'ul',
|
|
193
|
+
'ol',
|
|
194
|
+
'li',
|
|
195
|
+
'dl',
|
|
196
|
+
'dt',
|
|
197
|
+
'dd',
|
|
198
|
+
'address',
|
|
199
|
+
'hr',
|
|
200
|
+
'pre',
|
|
201
|
+
'blockquote',
|
|
202
|
+
'center',
|
|
203
|
+
'ins',
|
|
204
|
+
'del',
|
|
205
|
+
'a',
|
|
206
|
+
'span',
|
|
207
|
+
'bdo',
|
|
208
|
+
'br',
|
|
209
|
+
'em',
|
|
210
|
+
'strong',
|
|
211
|
+
'dfn',
|
|
212
|
+
'code',
|
|
213
|
+
'samp',
|
|
214
|
+
'kbd',
|
|
215
|
+
'bar',
|
|
216
|
+
'cite',
|
|
217
|
+
'abbr',
|
|
218
|
+
'acronym',
|
|
219
|
+
'q',
|
|
220
|
+
'sub',
|
|
221
|
+
'sup',
|
|
222
|
+
'tt',
|
|
223
|
+
'i',
|
|
224
|
+
'b',
|
|
225
|
+
'big',
|
|
226
|
+
'small',
|
|
227
|
+
'u',
|
|
228
|
+
's',
|
|
229
|
+
'strike',
|
|
230
|
+
'basefont',
|
|
231
|
+
'font',
|
|
232
|
+
'object',
|
|
233
|
+
'param',
|
|
234
|
+
'img',
|
|
235
|
+
'table',
|
|
236
|
+
'caption',
|
|
237
|
+
'colgroup',
|
|
238
|
+
'col',
|
|
239
|
+
'thead',
|
|
240
|
+
'tfoot',
|
|
241
|
+
'tbody',
|
|
242
|
+
'tr',
|
|
243
|
+
'th',
|
|
244
|
+
'td',
|
|
245
|
+
'embed',
|
|
246
|
+
'applet',
|
|
247
|
+
'iframe',
|
|
248
|
+
'img',
|
|
249
|
+
'map',
|
|
250
|
+
'noscript',
|
|
251
|
+
'ns:svg',
|
|
252
|
+
'object',
|
|
253
|
+
'script',
|
|
254
|
+
'table',
|
|
255
|
+
'tt',
|
|
256
|
+
'var',
|
|
257
|
+
];
|
|
258
|
+
let $ = cheerio.load(chapter.data, {
|
|
259
|
+
lowerCaseTags: true,
|
|
260
|
+
recognizeSelfClosing: true,
|
|
261
|
+
});
|
|
262
|
+
// only body innerHTML is allowed
|
|
263
|
+
let body = $('body');
|
|
264
|
+
if (body.length) {
|
|
265
|
+
let html = body.html();
|
|
266
|
+
if (html) {
|
|
267
|
+
$ = cheerio.load(html, {
|
|
268
|
+
lowerCaseTags: true,
|
|
269
|
+
recognizeSelfClosing: true,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
$($('*').get().reverse()).each(function (elemIndex, elem) {
|
|
274
|
+
// @ts-ignore
|
|
275
|
+
let attrs = elem.attribs;
|
|
276
|
+
// @ts-ignore
|
|
277
|
+
let that = this;
|
|
278
|
+
let tags = ['img', 'br', 'hr'];
|
|
279
|
+
if (tags.includes(that.name)) {
|
|
280
|
+
if (that.name === 'img' && !$(that).attr('alt')) {
|
|
281
|
+
$(that).attr('alt', 'image-placeholder');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
Object.entries(attrs).map(([k, v]) => {
|
|
285
|
+
if (allowedAttributes.includes(k)) {
|
|
286
|
+
if (k === 'type' && that.name !== 'script') {
|
|
287
|
+
$(that).removeAttr(k);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
$(that).removeAttr(k);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
if (epubConfigs.version === 2) {
|
|
295
|
+
if (!allowedXhtml11Tags.includes(that.name)) {
|
|
296
|
+
if (epubConfigs.verbose) {
|
|
297
|
+
console.log('Warning (content[' + index + ']):', that.name, "tag isn't allowed on EPUB 2/XHTML 1.1 DTD.");
|
|
298
|
+
}
|
|
299
|
+
let child = $(that).html();
|
|
300
|
+
$(that).replaceWith($('<div>' + child + '</div>'));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
$('img').each((index, elem) => {
|
|
305
|
+
let url = $(elem).attr('src') || '';
|
|
306
|
+
let image = epubConfigs.images.find((el) => el.url === url);
|
|
307
|
+
let id;
|
|
308
|
+
let extension;
|
|
309
|
+
if (image) {
|
|
310
|
+
id = image.id;
|
|
311
|
+
extension = image.extension;
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
id = (0, uuid_1.v4)();
|
|
315
|
+
let mediaType = mime.getType(url.replace(/\?.*/, '')) || '';
|
|
316
|
+
extension = mime.getExtension(mediaType) || '';
|
|
317
|
+
let dir = chapter.dir || '';
|
|
318
|
+
let img = { id, url, dir, mediaType, extension };
|
|
319
|
+
epubConfigs.images.push(img);
|
|
320
|
+
}
|
|
321
|
+
$(elem).attr('src', `images/${id}.${extension}`);
|
|
322
|
+
});
|
|
323
|
+
chapter.data = $.xml();
|
|
324
|
+
if (Array.isArray(chapter.children)) {
|
|
325
|
+
chapter.children = chapter.children.map((content, idx) => parseContent(content, `${index}_${idx}`, epubConfigs));
|
|
326
|
+
}
|
|
327
|
+
return chapter;
|
|
328
|
+
}
|
|
329
|
+
exports.default = parseContent;
|
|
330
|
+
//# sourceMappingURL=parseContent.js.map
|
package/dist/render.d.ts
ADDED
package/dist/render.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utils
|
|
3
|
+
* @author: oldj
|
|
4
|
+
* @homepage: https://oldj.net
|
|
5
|
+
*/
|
|
6
|
+
/// <reference types="node" />
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
export declare const readFile: typeof fs.readFile.__promisify__;
|
|
9
|
+
export declare const writeFile: typeof fs.writeFile.__promisify__;
|
|
10
|
+
export declare const 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";
|
|
11
|
+
export declare const wait: (ms: number) => Promise<unknown>;
|
|
12
|
+
export declare function fileIsStable(filename: string, max_wait?: number): Promise<boolean>;
|
|
13
|
+
export declare function simpleMinifier(xhtml: string): string;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wp-epub-gen",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Epub generator.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"tsc": "tsc",
|
|
9
|
+
"test": "mocha -r espower-typescript/guess -r tsconfig-paths/register test/*.test.js",
|
|
10
|
+
"tt": "npm run build && node ./test/tt.js",
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"bt": "npm run build && npm run test",
|
|
13
|
+
"w": "tsc -w",
|
|
14
|
+
"p": "npm run build && npm run test && npm publish --access public"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/oldj/wp-epub-gen.git"
|
|
19
|
+
},
|
|
20
|
+
"author": "",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/oldj/wp-epub-gen/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/oldj/wp-epub-gen#readme",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@types/diacritics": "^1.3.1",
|
|
28
|
+
"@types/uslug": "^1.0.1",
|
|
29
|
+
"@types/uuid": "^8.3.4",
|
|
30
|
+
"archiver": "^5.3.0",
|
|
31
|
+
"cheerio": "^0.22.0",
|
|
32
|
+
"diacritics": "^1.3.0",
|
|
33
|
+
"ejs": "^3.1.6",
|
|
34
|
+
"entities": "^3.0.1",
|
|
35
|
+
"fs-extra": "^10.0.1",
|
|
36
|
+
"mime": "^3.0.0",
|
|
37
|
+
"superagent": "^7.1.1",
|
|
38
|
+
"uslug": "^1.0.4",
|
|
39
|
+
"uuid": "^8.3.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/archiver": "^5.3.1",
|
|
43
|
+
"@types/cheerio": "^0.22.31",
|
|
44
|
+
"@types/ejs": "^3.1.0",
|
|
45
|
+
"@types/fs-extra": "^9.0.13",
|
|
46
|
+
"@types/mime": "^2.0.3",
|
|
47
|
+
"@types/superagent": "^4.1.15",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^5.16.0",
|
|
49
|
+
"@typescript-eslint/parser": "^5.16.0",
|
|
50
|
+
"chai": "^4.3.6",
|
|
51
|
+
"cross-env": "^7.0.3",
|
|
52
|
+
"eslint": "^8.11.0",
|
|
53
|
+
"eslint-config-prettier": "^8.5.0",
|
|
54
|
+
"eslint-plugin-import": "^2.25.4",
|
|
55
|
+
"espower-typescript": "^10.0.0",
|
|
56
|
+
"mocha": "^9.2.2",
|
|
57
|
+
"prettier": "^2.6.1",
|
|
58
|
+
"terser-webpack-plugin": "^5.3.1",
|
|
59
|
+
"ts-loader": "^9.2.8",
|
|
60
|
+
"tsconfig-paths": "^3.14.1",
|
|
61
|
+
"typescript": "^4.6.3",
|
|
62
|
+
"webpack": "^5.70.0",
|
|
63
|
+
"webpack-cli": "^4.9.2",
|
|
64
|
+
"webpack-notifier": "^1.15.0"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
log(`[Copy 1] '${auxPath}' to '${filename}'`)
|
|
29
|
+
if (fs.existsSync(auxPath)) {
|
|
30
|
+
try {
|
|
31
|
+
fs.copySync(auxPath, filename)
|
|
32
|
+
} catch (e) {
|
|
33
|
+
log('[Copy 1 Error] ' + e.message)
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
log(`[Copy 1 Fail] '${url}' not exists!`)
|
|
37
|
+
}
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let requestAction: any
|
|
42
|
+
if (url.startsWith('http')) {
|
|
43
|
+
requestAction = request.get(url).set({ 'User-Agent': USER_AGENT })
|
|
44
|
+
requestAction.pipe(fs.createWriteStream(filename))
|
|
45
|
+
} else {
|
|
46
|
+
log(`[Copy 2] '${url}' to '${filename}'`)
|
|
47
|
+
requestAction = fs.createReadStream(path.join(options.dir || '', url))
|
|
48
|
+
requestAction.pipe(fs.createWriteStream(filename))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
requestAction.on('error', (err: any) => {
|
|
53
|
+
log('[Download Error] Error while downloading: ' + url)
|
|
54
|
+
log(err)
|
|
55
|
+
fs.unlinkSync(filename)
|
|
56
|
+
// reject(err)
|
|
57
|
+
resolve()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
requestAction.on('end', () => {
|
|
61
|
+
log('[Download Success] ' + url)
|
|
62
|
+
resolve()
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const downloadAllImages = async (epubData: IEpubData) => {
|
|
68
|
+
let { images } = epubData
|
|
69
|
+
if (images.length === 0) return
|
|
70
|
+
|
|
71
|
+
fs.ensureDirSync(path.join(epubData.dir, 'OEBPS', 'images'))
|
|
72
|
+
for (let image of images) {
|
|
73
|
+
await downloadImage(epubData, image)
|
|
74
|
+
}
|
|
75
|
+
}
|