diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-04-27 22:18:33 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-04-27 22:18:33 +0200 |
commit | 9bb0a3fbd355b6dda2fe33e3a83884baa8f30917 (patch) | |
tree | 567c1f02e05704039c87e42e10d4d5c9fb0ed9bf | |
parent | 9a1a6f2625612c18745957c4331be27dfdc2fb34 (diff) | |
download | gitbook-9bb0a3fbd355b6dda2fe33e3a83884baa8f30917.zip gitbook-9bb0a3fbd355b6dda2fe33e3a83884baa8f30917.tar.gz gitbook-9bb0a3fbd355b6dda2fe33e3a83884baa8f30917.tar.bz2 |
Add tests for svg inliner
-rw-r--r-- | lib/output/getModifiers.js | 5 | ||||
-rw-r--r-- | lib/output/modifiers/__tests__/svgToImg.js | 4 | ||||
-rw-r--r-- | lib/output/modifiers/__tests__/svgToPng.js | 32 | ||||
-rw-r--r-- | lib/output/modifiers/inlineAssets.js | 8 | ||||
-rw-r--r-- | lib/output/modifiers/svgToImg.js | 28 | ||||
-rw-r--r-- | lib/output/modifiers/svgToPng.js | 21 | ||||
-rw-r--r-- | lib/utils/fs.js | 2 |
7 files changed, 78 insertions, 22 deletions
diff --git a/lib/output/getModifiers.js b/lib/output/getModifiers.js index 1dd62ba..1979063 100644 --- a/lib/output/getModifiers.js +++ b/lib/output/getModifiers.js @@ -20,6 +20,9 @@ function getModifiers(output, page) { var entries = glossary.getEntries(); var file = page.getFile(); + // Current file path + var currentFilePath = file.getPath(); + // Get TemplateBlock for highlighting var blocks = Plugins.listBlocks(plugins); var code = blocks.get(CODEBLOCK) || defaultBlocks.get(CODEBLOCK); @@ -33,7 +36,7 @@ function getModifiers(output, page) { // Resolve links (.md -> .html) Modifiers.resolveLinks.bind(null, - file.getPath(), + currentFilePath, resolveFileToURL.bind(null, output) ), diff --git a/lib/output/modifiers/__tests__/svgToImg.js b/lib/output/modifiers/__tests__/svgToImg.js index 762a02e..793395e 100644 --- a/lib/output/modifiers/__tests__/svgToImg.js +++ b/lib/output/modifiers/__tests__/svgToImg.js @@ -12,10 +12,10 @@ describe('svgToImg', function() { pit('should write svg as a file', function() { var $ = cheerio.load('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1"><rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/></svg>'); - return svgToImg(dir.name, $) + return svgToImg(dir.name, 'index.html', $) .then(function() { var $img = $('img'); - var src = '.' + $img.attr('src'); + var src = $img.attr('src'); expect(dir.name).toHaveFile(src); }); diff --git a/lib/output/modifiers/__tests__/svgToPng.js b/lib/output/modifiers/__tests__/svgToPng.js new file mode 100644 index 0000000..163d72e --- /dev/null +++ b/lib/output/modifiers/__tests__/svgToPng.js @@ -0,0 +1,32 @@ +var cheerio = require('cheerio'); +var tmp = require('tmp'); +var path = require('path'); + +describe('svgToPng', function() { + var dir; + var svgToImg = require('../svgToImg'); + var svgToPng = require('../svgToPng'); + + beforeEach(function() { + dir = tmp.dirSync(); + }); + + pit('should write svg as png file', function() { + var $ = cheerio.load('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1"><rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/></svg>'); + var fileName = 'index.html'; + + return svgToImg(dir.name, fileName, $) + .then(function() { + return svgToPng(dir.name, fileName, $); + }) + .then(function() { + var $img = $('img'); + var src = $img.attr('src'); + + expect(dir.name).toHaveFile(src); + expect(path.extname(src)).toBe('.png'); + }); + }); +}); + + diff --git a/lib/output/modifiers/inlineAssets.js b/lib/output/modifiers/inlineAssets.js index 044953b..4106d69 100644 --- a/lib/output/modifiers/inlineAssets.js +++ b/lib/output/modifiers/inlineAssets.js @@ -9,12 +9,12 @@ var Promise = require('../../utils/promise'); @param {String} rootFolder */ -function inlineAssets(rootFolder) { +function inlineAssets(rootFolder, currentFile) { return function($) { return Promise() - .then(fetchRemoteImages.bind(null, rootFolder)) - .then(svgToImg.bind(null, rootFolder)) - .then(svgToPng.bind(null, rootFolder)); + .then(fetchRemoteImages.bind(null, rootFolder, currentFile)) + .then(svgToImg.bind(null, rootFolder, currentFile)) + .then(svgToPng.bind(null, rootFolder, currentFile)); }; } diff --git a/lib/output/modifiers/svgToImg.js b/lib/output/modifiers/svgToImg.js index d088e3e..f31b06d 100644 --- a/lib/output/modifiers/svgToImg.js +++ b/lib/output/modifiers/svgToImg.js @@ -1,8 +1,10 @@ var path = require('path'); +var crc = require('crc'); var domSerializer = require('dom-serializer'); var editHTMLElement = require('./editHTMLElement'); var fs = require('../../utils/fs'); +var LocationUtils = require('../../utils/location'); /** Render a cheerio DOM as html @@ -26,23 +28,27 @@ function renderDOM($, dom, options) { @param {String} baseFolder @param {HTMLDom} $ */ -function svgToImg(baseFolder, $) { +function svgToImg(baseFolder, currentFile, $) { + var currentDirectory = path.dirname(currentFile); + return editHTMLElement($, 'svg', function($svg) { var content = '<?xml version="1.0" encoding="UTF-8"?>' + renderDOM($, $svg); - // Generate a filename - return fs.uniqueFilename(baseFolder, 'image.svg') - .then(function(fileName) { - var filePath = path.join(baseFolder, fileName); + // We avoid generating twice the same PNG + var hash = crc.crc32(content).toString(16); + var fileName = hash + '.svg'; + var filePath = path.join(baseFolder, fileName); - // Write the svg to the file - return fs.writeFile(filePath, content, 'utf8') + // Write the svg to the file + return fs.assertFile(filePath, function() { + return fs.writeFile(filePath, content, 'utf8'); + }) - // Return as image - .then(function() { - $svg.replaceWith('<img src="/' + fileName + '" />'); - }); + // Return as image + .then(function() { + var src = LocationUtils.relative(currentDirectory, fileName); + $svg.replaceWith('<img src="' + src + '" />'); }); }); } diff --git a/lib/output/modifiers/svgToPng.js b/lib/output/modifiers/svgToPng.js index 4f8644a..1093106 100644 --- a/lib/output/modifiers/svgToPng.js +++ b/lib/output/modifiers/svgToPng.js @@ -3,6 +3,7 @@ var path = require('path'); var imagesUtil = require('../../utils/images'); var fs = require('../../utils/fs'); +var LocationUtils = require('../../utils/location'); var editHTMLElement = require('./editHTMLElement'); @@ -13,23 +14,37 @@ var editHTMLElement = require('./editHTMLElement'); @param {HTMLDom} $ @return {Promise} */ -function svgToPng(rootFolder, $) { +function svgToPng(rootFolder, currentFile, $) { + var currentDirectory = path.dirname(currentFile); + return editHTMLElement($, 'img', function($img) { var src = $img.attr('src'); if (path.extname(src) !== '.svg') { return; } + // Calcul absolute path for this + src = LocationUtils.toAbsolute(src, currentDirectory, '.'); + // We avoid generating twice the same PNG var hash = crc.crc32(src).toString(16); var fileName = hash + '.png'; + + // Input file path + var inputPath = path.join(rootFolder, src); + + // Result file path var filePath = path.join(rootFolder, fileName); return fs.assertFile(filePath, function() { - return imagesUtil.convertSVGToPNG(src, filePath); + return imagesUtil.convertSVGToPNG(inputPath, filePath); }) .then(function() { - $img.replaceWith('<img src="/' + fileName + '" />'); + // Convert filename to a relative filename + fileName = LocationUtils.relative(currentDirectory, fileName); + + // Replace src + $img.attr('src', fileName); }); }); } diff --git a/lib/utils/fs.js b/lib/utils/fs.js index 96a0e49..008dd40 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -109,7 +109,7 @@ function assertFile(filePath, generator) { .then(function(exists) { if (exists) return; - generator(); + return generator(); }); } |