diff options
-rw-r--r-- | lib/utils/images.js | 6 | ||||
-rw-r--r-- | lib/utils/page.js | 9 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/ebook.js | 37 | ||||
-rw-r--r-- | test/fixtures/test4/README.md | 4 | ||||
-rw-r--r-- | test/fixtures/test4/sub/PAGE.md | 4 |
6 files changed, 35 insertions, 27 deletions
diff --git a/lib/utils/images.js b/lib/utils/images.js index 61a52dc..1e90317 100644 --- a/lib/utils/images.js +++ b/lib/utils/images.js @@ -22,7 +22,11 @@ var convertSVG = function(source, dest, options) { if (error.code == 127) error = new Error("Need to install 'svgexport' using 'npm install svgexport -g'"); return d.reject(error); } - d.resolve(); + if (fs.existsSync(dest)) { + d.resolve(); + } else { + d.reject(new Error("Error converting "+source)); + } }); return d.promise; diff --git a/lib/utils/page.js b/lib/utils/page.js index ccf5dfa..20caf79 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -4,6 +4,7 @@ var path = require('path'); var cheerio = require('cheerio'); var domSerializer = require('dom-serializer'); var request = require('request'); +var crc = require("crc"); var links = require('./links'); var imgUtils = require('./images'); @@ -123,7 +124,7 @@ function normalizeHtml(src, options) { // If image is external and ebook, then downlaod the images if (isExternal) { origin = src; - src = "/"+fs.getUniqueFilename(outputRoot, path.basename(src)); + src = "/"+crc.crc32(origin).toString(16)+path.extname(origin); src = links.toAbsolute(src, options.base, options.output); isExternal = false; } @@ -226,6 +227,7 @@ function normalizeHtml(src, options) { function convertImages(images, options) { if (!options.convertImages) return Q(); + var downloaded = []; options.book.log.debug.ln("convert ", images.length, "images to png"); return _.reduce(images, function(prev, image) { @@ -235,8 +237,9 @@ function convertImages(images, options) { // Write image if need to be download .then(function() { - if (!image.origin) return; + if (!image.origin && !_.contains(downloaded, image.origin)) return; options.book.log.debug("download image", image.origin, "..."); + downloaded.push(image.origin); return options.book.log.debug.promise(fs.writeStream(imgin, request(image.origin))); }) @@ -247,7 +250,7 @@ function convertImages(images, options) { }) // Convert - .then(function(){ + .then(function() { if (!image.dest) return; var imgout = path.resolve(options.book.options.output, image.dest); options.book.log.debug("convert image", image.source, "to", image.dest, "..."); diff --git a/package.json b/package.json index 1076a5a..f5f3e05 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "URIjs": "1.14.1", "request": "2.51.0", "npm": "2.4.1", - "dom-serializer": "git+https://github.com/SamyPesse/dom-serializer.git#57ed9a2c3dfa964022ce7d71859c558ba7721785" + "dom-serializer": "0.1.0" }, "devDependencies": { "mocha": "1.18.2", diff --git a/test/ebook.js b/test/ebook.js index 9432afa..c3e29d9 100644 --- a/test/ebook.js +++ b/test/ebook.js @@ -1,6 +1,7 @@ var path = require('path'); var _ = require('lodash'); var assert = require('assert'); +var cheerio = require('cheerio'); var fs = require("fs"); var fsUtil = require("../lib/utils/fs"); @@ -15,26 +16,22 @@ describe('eBook Generator', function () { it('should correctly convert svg images to png', function(done) { testGeneration(books[4], "ebook", function(output) { - var readmeContent = fs.readFileSync(path.join(output, "index.html"), {encoding: "utf8"}); - var pageContent = fs.readFileSync(path.join(output, "sub/PAGE.html"), {encoding: "utf8"}); - - // Remote image - assert(pageContent.indexOf('src="../Tux.png"') >= 0); - assert(fs.existsSync(path.join(output, "Tux.png"))); - - assert(fs.existsSync(path.join(output, "test.png"))); - assert(fs.existsSync(path.join(output, "NewTux.png"))); - - assert(!fs.existsSync(path.join(output, "test_0.png"))); - assert(!fs.existsSync(path.join(output, "sub/test.png"))); - assert(!fs.existsSync(path.join(output, "sub/NewTux.png"))); - - assert(pageContent.indexOf('src="../test.png"') >= 0); - assert(pageContent.indexOf('src="../NewTux.png"') >= 0); - assert(pageContent.indexOf('<svg') < 0); - - assert(readmeContent.indexOf('src="test.png"') >= 0); - assert(readmeContent.indexOf('src="NewTux.png"') >= 0); + // Check that all images exists + _.each([ + "index.html", + "sub/PAGE.html" + ], function(pageName) { + var pageFile = path.join(output, pageName); + var pageFolder = path.dirname(pageFile); + var pageContent = fs.readFileSync(pageFile, {encoding: "utf8"}); + var $ = cheerio.load(pageContent); + + $("img").each(function() { + var src = $(this).attr("src"); + console.log(path.resolve(pageFolder, src)); + assert(fs.existsSync(path.resolve(pageFolder, src)), src+" not found for page "+pageName); + }) + }); }, done); }); }); diff --git a/test/fixtures/test4/README.md b/test/fixtures/test4/README.md index 19892a5..d73be69 100644 --- a/test/fixtures/test4/README.md +++ b/test/fixtures/test4/README.md @@ -5,3 +5,7 @@ A description    + +# Test with youtube videos that have the same filename: + + diff --git a/test/fixtures/test4/sub/PAGE.md b/test/fixtures/test4/sub/PAGE.md index 6de478e..a98a942 100644 --- a/test/fixtures/test4/sub/PAGE.md +++ b/test/fixtures/test4/sub/PAGE.md @@ -8,8 +8,8 @@ ## Inline svg {% html %} -<svg width="400" height="110"> - <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)"> +<svg xmlns="http://www.w3.org/2000/svg"> + <path d="M97.008198003228,6.103238498249268A97.2,97.2 0 0,1 87.9491894996971,41.385747140125076L54.289623147961166,25.546757493904366A60,60 0 0,0 59.88160370569629,3.7674311717588074Z"></path> </svg> {% endhtml %} |