forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_images_to_epub.js
More file actions
24 lines (22 loc) · 833 Bytes
/
add_images_to_epub.js
File metadata and controls
24 lines (22 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var fs = require("fs"), path = require("path");
var images = [];
function scanDir(dir) {
fs.readdirSync(dir).forEach(function(file) {
var full = path.resolve(dir, file), type;
if (fs.lstatSync(full).isDirectory())
return scanDir(full);
if (/\.svg$/.test(file))
type = "image/svg+xml";
else if (/\.png$/.test(file))
type = "image/png";
else if (/\.jpg$/.test(file))
type = "image/jpeg";
else
throw new Error("Unknown image type: " + full);
var local = full.slice(full.indexOf("/img/") + 1);
images.push(" <item id=\"image." + file + "\" href=\"" + local + "\" media-type=\"" + type + "\"/>");
});
}
scanDir("epub/img");
var out = fs.readFileSync("epub/content.opf.src", "utf8").replace("{{images}}", images.join("\n"));
fs.writeFileSync("epub/content.opf", out);