summaryrefslogtreecommitdiffstats
path: root/lib/page
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-02-12 23:57:43 +0100
committerSamy Pesse <samypesse@gmail.com>2016-02-12 23:57:43 +0100
commit4c6717e23488656686f276aa2b40ce1d1c7641f8 (patch)
treec728f106224cedc836b83b06609011990eac4f5a /lib/page
parent39b6562d1445e9a6c43a377d2a978eefa6458755 (diff)
downloadgitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.zip
gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.tar.gz
gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.tar.bz2
Add conversion of svg to png for assets inliner
Diffstat (limited to 'lib/page')
-rw-r--r--lib/page/html.js21
-rw-r--r--lib/page/index.js18
2 files changed, 36 insertions, 3 deletions
diff --git a/lib/page/html.js b/lib/page/html.js
index 948462a..255bf48 100644
--- a/lib/page/html.js
+++ b/lib/page/html.js
@@ -19,11 +19,12 @@ function HTMLPipeline(htmlString, opts) {
_.bindAll(this);
this.opts = _.defaults(opts || {}, {
- convertImages: true,
-
// Calcul new href for a relative link
onRelativeLink: _.identity,
+ // Output an image
+ onImage: _.identity,
+
// Output a svg, if returns null the svg is kept inlined
onOutputSVG: _.constant(null)
});
@@ -56,7 +57,22 @@ HTMLPipeline.prototype.normalizeLinks = function() {
// External links
$a.attr('target', '_blank');
}
+ });
+};
+
+// Normalize images
+HTMLPipeline.prototype.normalizeImages = function() {
+ var that = this;
+
+ var $imgs = this.$('img');
+
+ return Promise.serie($imgs, function(img) {
+ var $img = that.$(img);
+ return Promise(that.opts.onImage($img.attr('src')))
+ .then(function(filename) {
+ $img.attr('src', filename);
+ });
});
};
@@ -101,6 +117,7 @@ HTMLPipeline.prototype.output = function() {
return Promise()
.then(this.normalizeLinks)
+ .then(this.normalizeImages)
.then(this.addHeadingIDs)
.then(this.outlineSVG)
.then(function() {
diff --git a/lib/page/index.js b/lib/page/index.js
index 282cffe..e96f89b 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -4,6 +4,7 @@ var parsers = require('gitbook-parsers');
var error = require('../utils/error');
var pathUtil = require('../utils/path');
+var location = require('../utils/location');
var HTMLPipeline = require('./html');
/*
@@ -62,6 +63,21 @@ Page.prototype.outputPath = function(ext) {
return output;
};
+// Resolve a filename relative to this page
+// It returns a path relative to the book root folder
+Page.prototype.resolveLocal = function() {
+ var dir = path.dirname(this.path);
+ var file = path.join.apply(path, _.toArray(arguments));
+
+ return location.toAbsolute(file, dir, '');
+};
+
+// Resolve a filename relative to this page
+// It returns an absolute path for the FS
+Page.prototype.resolve = function() {
+ return this.book.resolve(this.resolveLocal.apply(this, arguments));
+};
+
// Update content of the page
Page.prototype.update = function(content) {
this.content = content;
@@ -118,8 +134,8 @@ Page.prototype.parse = function(output) {
// Normalize HTML output
.then(function() {
var pipelineOpts = {
- // Replace links to page of summary
onRelativeLink: _.partial(output.onRelativeLink, that),
+ onImage: _.partial(output.onOutputImage, that),
onOutputSVG: _.partial(output.onOutputSVG, that)
};
var pipeline = new HTMLPipeline(that.content, pipelineOpts);