mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
Replace css text with archived assets links
This commit is contained in:
parent
13467128e5
commit
518c1599c3
8 changed files with 177 additions and 65 deletions
112
dist/epub.js
vendored
112
dist/epub.js
vendored
|
@ -4758,7 +4758,7 @@ module.exports = Continuous;
|
||||||
var RSVP = require('rsvp');
|
var RSVP = require('rsvp');
|
||||||
|
|
||||||
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||||
|
/*
|
||||||
//-- Parse the different parts of a url, returning a object
|
//-- Parse the different parts of a url, returning a object
|
||||||
function uri(url){
|
function uri(url){
|
||||||
var uri = {
|
var uri = {
|
||||||
|
@ -4838,7 +4838,7 @@ function folder(url){
|
||||||
return folder;
|
return folder;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
function isElement(obj) {
|
function isElement(obj) {
|
||||||
return !!(obj && obj.nodeType == 1);
|
return !!(obj && obj.nodeType == 1);
|
||||||
};
|
};
|
||||||
|
@ -5154,9 +5154,19 @@ function isXml(ext) {
|
||||||
return ['xml', 'opf', 'ncx'].indexOf(ext) > -1;
|
return ['xml', 'opf', 'ncx'].indexOf(ext) > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createBlobUrl(content, mime){
|
||||||
|
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||||
|
var tempUrl;
|
||||||
|
var blob = new Blob([content], {type : mime });
|
||||||
|
|
||||||
|
tempUrl = _URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
return tempUrl;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
'uri': uri,
|
// 'uri': uri,
|
||||||
'folder': folder,
|
// 'folder': folder,
|
||||||
'isElement': isElement,
|
'isElement': isElement,
|
||||||
'uuid': uuid,
|
'uuid': uuid,
|
||||||
'values': values,
|
'values': values,
|
||||||
|
@ -5176,7 +5186,8 @@ module.exports = {
|
||||||
'windowBounds': windowBounds,
|
'windowBounds': windowBounds,
|
||||||
'cleanStringForXpath': cleanStringForXpath,
|
'cleanStringForXpath': cleanStringForXpath,
|
||||||
'indexOfTextNode': indexOfTextNode,
|
'indexOfTextNode': indexOfTextNode,
|
||||||
'isXml': isXml
|
'isXml': isXml,
|
||||||
|
'createBlobUrl': createBlobUrl
|
||||||
};
|
};
|
||||||
|
|
||||||
},{"rsvp":4}],9:[function(require,module,exports){
|
},{"rsvp":4}],9:[function(require,module,exports){
|
||||||
|
@ -8159,7 +8170,9 @@ Rendition.prototype.triggerViewEvent = function(e){
|
||||||
};
|
};
|
||||||
|
|
||||||
Rendition.prototype.replacements = function(){
|
Rendition.prototype.replacements = function(){
|
||||||
|
// Wait for loading
|
||||||
return this.q.enqueue(function () {
|
return this.q.enqueue(function () {
|
||||||
|
// Get thes books manifest
|
||||||
var manifest = this.book.package.manifest;
|
var manifest = this.book.package.manifest;
|
||||||
var manifestArray = Object.keys(manifest).
|
var manifestArray = Object.keys(manifest).
|
||||||
map(function (key){
|
map(function (key){
|
||||||
|
@ -8178,17 +8191,23 @@ Rendition.prototype.replacements = function(){
|
||||||
// Only CSS
|
// Only CSS
|
||||||
var css = items.
|
var css = items.
|
||||||
filter(function (item){
|
filter(function (item){
|
||||||
if (item.type != "text/css") {
|
if (item.type === "text/css") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Css Urls
|
||||||
|
var cssUrls = css.map(function(item) {
|
||||||
|
return item.href;
|
||||||
|
});
|
||||||
|
|
||||||
|
// All Assets Urls
|
||||||
var urls = items.
|
var urls = items.
|
||||||
map(function(item) {
|
map(function(item) {
|
||||||
// return this.book.baseUrl + item.href;
|
|
||||||
return item.href;
|
return item.href;
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
// Create blob urls for all the assets
|
||||||
var processing = urls.
|
var processing = urls.
|
||||||
map(function(url) {
|
map(function(url) {
|
||||||
var absolute = URI(url).absoluteTo(this.book.baseUrl).toString();
|
var absolute = URI(url).absoluteTo(this.book.baseUrl).toString();
|
||||||
|
@ -8196,22 +8215,61 @@ Rendition.prototype.replacements = function(){
|
||||||
return this.book.archive.createUrl(absolute);
|
return this.book.archive.createUrl(absolute);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
// After all the urls are created
|
||||||
return RSVP.all(processing).
|
return RSVP.all(processing).
|
||||||
then(function(replacementUrls) {
|
then(function(replacementUrls) {
|
||||||
this.hooks.serialize.register(function(content, section) {
|
|
||||||
// resolve file urls
|
// Replace Asset Urls in the text of all css files
|
||||||
|
cssUrls.forEach(function(href) {
|
||||||
|
this.replaceCss(href, urls, replacementUrls);
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
// Replace Asset Urls in chapters
|
||||||
|
// by registering a hook after the sections contents has been serialized
|
||||||
|
this.hooks.serialize.register(function(output, section) {
|
||||||
|
this.replaceAssets(section, urls, replacementUrls);
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
}.bind(this)).catch(function(reason){
|
||||||
|
console.error(reason);
|
||||||
|
});
|
||||||
|
}.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
|
||||||
|
var newUrl;
|
||||||
|
var indexInUrls;
|
||||||
|
|
||||||
|
// Find the absolute url of the css file
|
||||||
|
var fileUri = URI(href);
|
||||||
|
var absolute = fileUri.absoluteTo(this.book.baseUrl).toString();
|
||||||
|
// Get the text of the css file from the archive
|
||||||
|
var text = this.book.archive.getText(absolute);
|
||||||
|
// Get asset links relative to css file
|
||||||
|
var relUrls = urls.
|
||||||
|
map(function(assetHref) {
|
||||||
|
var assetUri = URI(assetHref).absoluteTo(this.book.baseUrl);
|
||||||
|
var relative = assetUri.relativeTo(absolute).toString();
|
||||||
|
return relative;
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
// Replacements in the css text
|
||||||
|
text = replace.substitute(text, relUrls, replacementUrls);
|
||||||
|
|
||||||
|
// Get the new url
|
||||||
|
newUrl = core.createBlobUrl(text, 'text/css');
|
||||||
|
|
||||||
|
// switch the url in the replacementUrls
|
||||||
|
indexInUrls = urls.indexOf(href);
|
||||||
|
if (indexInUrls > -1) {
|
||||||
|
replacementUrls[indexInUrls] = newUrl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Rendition.prototype.replaceAssets = function(section, urls, replacementUrls){
|
||||||
var fileUri = URI(section.url);
|
var fileUri = URI(section.url);
|
||||||
// console.log(section.url);
|
// Get Urls relative to current sections
|
||||||
// var fileDirectory = fileUri.directory();
|
var relUrls = urls.
|
||||||
// // // package urls
|
|
||||||
// var packageUri = URI(this.book.packageUrl);
|
|
||||||
// var packageDirectory = packageUri.directory();
|
|
||||||
//
|
|
||||||
// // Need to compare the directory of the current file
|
|
||||||
// // with the directory of the package file.
|
|
||||||
|
|
||||||
|
|
||||||
urls = urls.
|
|
||||||
map(function(href) {
|
map(function(href) {
|
||||||
var assetUri = URI(href).absoluteTo(this.book.baseUrl);
|
var assetUri = URI(href).absoluteTo(this.book.baseUrl);
|
||||||
var relative = assetUri.relativeTo(fileUri).toString();
|
var relative = assetUri.relativeTo(fileUri).toString();
|
||||||
|
@ -8219,12 +8277,7 @@ Rendition.prototype.replacements = function(){
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
|
||||||
section.output = replace.substitute(content, urls, replacementUrls);
|
section.output = replace.substitute(section.output, relUrls, replacementUrls);
|
||||||
}.bind(this));
|
|
||||||
}.bind(this)).catch(function(reason){
|
|
||||||
console.error(reason);
|
|
||||||
});
|
|
||||||
}.bind(this));
|
|
||||||
};
|
};
|
||||||
//-- Enable binding events to Renderer
|
//-- Enable binding events to Renderer
|
||||||
RSVP.EventTarget.mixin(Rendition.prototype);
|
RSVP.EventTarget.mixin(Rendition.prototype);
|
||||||
|
@ -8306,7 +8359,9 @@ function links(view, renderer) {
|
||||||
|
|
||||||
function substitute(content, urls, replacements) {
|
function substitute(content, urls, replacements) {
|
||||||
urls.forEach(function(url, i){
|
urls.forEach(function(url, i){
|
||||||
|
if (url && replacements[i]) {
|
||||||
content = content.replace(new RegExp(url, 'g'), replacements[i]);
|
content = content.replace(new RegExp(url, 'g'), replacements[i]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
@ -8440,6 +8495,7 @@ module.exports = request;
|
||||||
|
|
||||||
},{"./core":8,"rsvp":4,"urijs":5}],21:[function(require,module,exports){
|
},{"./core":8,"rsvp":4,"urijs":5}],21:[function(require,module,exports){
|
||||||
var RSVP = require('rsvp');
|
var RSVP = require('rsvp');
|
||||||
|
var URI = require('urijs');
|
||||||
var core = require('./core');
|
var core = require('./core');
|
||||||
var EpubCFI = require('./epubcfi');
|
var EpubCFI = require('./epubcfi');
|
||||||
var Hook = require('./hook');
|
var Hook = require('./hook');
|
||||||
|
@ -8478,7 +8534,7 @@ Section.prototype.load = function(_request){
|
||||||
request(this.url)
|
request(this.url)
|
||||||
.then(function(xml){
|
.then(function(xml){
|
||||||
var base;
|
var base;
|
||||||
var directory = core.folder(this.url);
|
var directory = URI(this.url).directory();
|
||||||
|
|
||||||
this.document = xml;
|
this.document = xml;
|
||||||
this.contents = xml.documentElement;
|
this.contents = xml.documentElement;
|
||||||
|
@ -8589,7 +8645,7 @@ Section.prototype.cfiFromElement = function(el) {
|
||||||
|
|
||||||
module.exports = Section;
|
module.exports = Section;
|
||||||
|
|
||||||
},{"./core":8,"./epubcfi":9,"./hook":10,"./replacements":19,"./request":20,"rsvp":4}],22:[function(require,module,exports){
|
},{"./core":8,"./epubcfi":9,"./hook":10,"./replacements":19,"./request":20,"rsvp":4,"urijs":5}],22:[function(require,module,exports){
|
||||||
var RSVP = require('rsvp');
|
var RSVP = require('rsvp');
|
||||||
var core = require('./core');
|
var core = require('./core');
|
||||||
var EpubCFI = require('./epubcfi');
|
var EpubCFI = require('./epubcfi');
|
||||||
|
|
2
dist/epub.js.map
vendored
2
dist/epub.js.map
vendored
File diff suppressed because one or more lines are too long
8
dist/epub.min.js
vendored
8
dist/epub.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/epub.min.js.map
vendored
2
dist/epub.min.js.map
vendored
File diff suppressed because one or more lines are too long
21
src/core.js
21
src/core.js
|
@ -1,7 +1,7 @@
|
||||||
var RSVP = require('rsvp');
|
var RSVP = require('rsvp');
|
||||||
|
|
||||||
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||||
|
/*
|
||||||
//-- Parse the different parts of a url, returning a object
|
//-- Parse the different parts of a url, returning a object
|
||||||
function uri(url){
|
function uri(url){
|
||||||
var uri = {
|
var uri = {
|
||||||
|
@ -81,7 +81,7 @@ function folder(url){
|
||||||
return folder;
|
return folder;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
function isElement(obj) {
|
function isElement(obj) {
|
||||||
return !!(obj && obj.nodeType == 1);
|
return !!(obj && obj.nodeType == 1);
|
||||||
};
|
};
|
||||||
|
@ -397,9 +397,19 @@ function isXml(ext) {
|
||||||
return ['xml', 'opf', 'ncx'].indexOf(ext) > -1;
|
return ['xml', 'opf', 'ncx'].indexOf(ext) > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createBlobUrl(content, mime){
|
||||||
|
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||||
|
var tempUrl;
|
||||||
|
var blob = new Blob([content], {type : mime });
|
||||||
|
|
||||||
|
tempUrl = _URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
return tempUrl;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
'uri': uri,
|
// 'uri': uri,
|
||||||
'folder': folder,
|
// 'folder': folder,
|
||||||
'isElement': isElement,
|
'isElement': isElement,
|
||||||
'uuid': uuid,
|
'uuid': uuid,
|
||||||
'values': values,
|
'values': values,
|
||||||
|
@ -419,5 +429,6 @@ module.exports = {
|
||||||
'windowBounds': windowBounds,
|
'windowBounds': windowBounds,
|
||||||
'cleanStringForXpath': cleanStringForXpath,
|
'cleanStringForXpath': cleanStringForXpath,
|
||||||
'indexOfTextNode': indexOfTextNode,
|
'indexOfTextNode': indexOfTextNode,
|
||||||
'isXml': isXml
|
'isXml': isXml,
|
||||||
|
'createBlobUrl': createBlobUrl
|
||||||
};
|
};
|
||||||
|
|
|
@ -634,7 +634,9 @@ Rendition.prototype.triggerViewEvent = function(e){
|
||||||
};
|
};
|
||||||
|
|
||||||
Rendition.prototype.replacements = function(){
|
Rendition.prototype.replacements = function(){
|
||||||
|
// Wait for loading
|
||||||
return this.q.enqueue(function () {
|
return this.q.enqueue(function () {
|
||||||
|
// Get thes books manifest
|
||||||
var manifest = this.book.package.manifest;
|
var manifest = this.book.package.manifest;
|
||||||
var manifestArray = Object.keys(manifest).
|
var manifestArray = Object.keys(manifest).
|
||||||
map(function (key){
|
map(function (key){
|
||||||
|
@ -653,17 +655,23 @@ Rendition.prototype.replacements = function(){
|
||||||
// Only CSS
|
// Only CSS
|
||||||
var css = items.
|
var css = items.
|
||||||
filter(function (item){
|
filter(function (item){
|
||||||
if (item.type != "text/css") {
|
if (item.type === "text/css") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Css Urls
|
||||||
|
var cssUrls = css.map(function(item) {
|
||||||
|
return item.href;
|
||||||
|
});
|
||||||
|
|
||||||
|
// All Assets Urls
|
||||||
var urls = items.
|
var urls = items.
|
||||||
map(function(item) {
|
map(function(item) {
|
||||||
// return this.book.baseUrl + item.href;
|
|
||||||
return item.href;
|
return item.href;
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
// Create blob urls for all the assets
|
||||||
var processing = urls.
|
var processing = urls.
|
||||||
map(function(url) {
|
map(function(url) {
|
||||||
var absolute = URI(url).absoluteTo(this.book.baseUrl).toString();
|
var absolute = URI(url).absoluteTo(this.book.baseUrl).toString();
|
||||||
|
@ -671,22 +679,61 @@ Rendition.prototype.replacements = function(){
|
||||||
return this.book.archive.createUrl(absolute);
|
return this.book.archive.createUrl(absolute);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
// After all the urls are created
|
||||||
return RSVP.all(processing).
|
return RSVP.all(processing).
|
||||||
then(function(replacementUrls) {
|
then(function(replacementUrls) {
|
||||||
this.hooks.serialize.register(function(content, section) {
|
|
||||||
// resolve file urls
|
// Replace Asset Urls in the text of all css files
|
||||||
|
cssUrls.forEach(function(href) {
|
||||||
|
this.replaceCss(href, urls, replacementUrls);
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
// Replace Asset Urls in chapters
|
||||||
|
// by registering a hook after the sections contents has been serialized
|
||||||
|
this.hooks.serialize.register(function(output, section) {
|
||||||
|
this.replaceAssets(section, urls, replacementUrls);
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
}.bind(this)).catch(function(reason){
|
||||||
|
console.error(reason);
|
||||||
|
});
|
||||||
|
}.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
Rendition.prototype.replaceCss = function(href, urls, replacementUrls){
|
||||||
|
var newUrl;
|
||||||
|
var indexInUrls;
|
||||||
|
|
||||||
|
// Find the absolute url of the css file
|
||||||
|
var fileUri = URI(href);
|
||||||
|
var absolute = fileUri.absoluteTo(this.book.baseUrl).toString();
|
||||||
|
// Get the text of the css file from the archive
|
||||||
|
var text = this.book.archive.getText(absolute);
|
||||||
|
// Get asset links relative to css file
|
||||||
|
var relUrls = urls.
|
||||||
|
map(function(assetHref) {
|
||||||
|
var assetUri = URI(assetHref).absoluteTo(this.book.baseUrl);
|
||||||
|
var relative = assetUri.relativeTo(absolute).toString();
|
||||||
|
return relative;
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
// Replacements in the css text
|
||||||
|
text = replace.substitute(text, relUrls, replacementUrls);
|
||||||
|
|
||||||
|
// Get the new url
|
||||||
|
newUrl = core.createBlobUrl(text, 'text/css');
|
||||||
|
|
||||||
|
// switch the url in the replacementUrls
|
||||||
|
indexInUrls = urls.indexOf(href);
|
||||||
|
if (indexInUrls > -1) {
|
||||||
|
replacementUrls[indexInUrls] = newUrl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Rendition.prototype.replaceAssets = function(section, urls, replacementUrls){
|
||||||
var fileUri = URI(section.url);
|
var fileUri = URI(section.url);
|
||||||
// console.log(section.url);
|
// Get Urls relative to current sections
|
||||||
// var fileDirectory = fileUri.directory();
|
var relUrls = urls.
|
||||||
// // // package urls
|
|
||||||
// var packageUri = URI(this.book.packageUrl);
|
|
||||||
// var packageDirectory = packageUri.directory();
|
|
||||||
//
|
|
||||||
// // Need to compare the directory of the current file
|
|
||||||
// // with the directory of the package file.
|
|
||||||
|
|
||||||
|
|
||||||
urls = urls.
|
|
||||||
map(function(href) {
|
map(function(href) {
|
||||||
var assetUri = URI(href).absoluteTo(this.book.baseUrl);
|
var assetUri = URI(href).absoluteTo(this.book.baseUrl);
|
||||||
var relative = assetUri.relativeTo(fileUri).toString();
|
var relative = assetUri.relativeTo(fileUri).toString();
|
||||||
|
@ -694,12 +741,7 @@ Rendition.prototype.replacements = function(){
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
|
||||||
section.output = replace.substitute(content, urls, replacementUrls);
|
section.output = replace.substitute(section.output, relUrls, replacementUrls);
|
||||||
}.bind(this));
|
|
||||||
}.bind(this)).catch(function(reason){
|
|
||||||
console.error(reason);
|
|
||||||
});
|
|
||||||
}.bind(this));
|
|
||||||
};
|
};
|
||||||
//-- Enable binding events to Renderer
|
//-- Enable binding events to Renderer
|
||||||
RSVP.EventTarget.mixin(Rendition.prototype);
|
RSVP.EventTarget.mixin(Rendition.prototype);
|
||||||
|
|
|
@ -72,7 +72,9 @@ function links(view, renderer) {
|
||||||
|
|
||||||
function substitute(content, urls, replacements) {
|
function substitute(content, urls, replacements) {
|
||||||
urls.forEach(function(url, i){
|
urls.forEach(function(url, i){
|
||||||
|
if (url && replacements[i]) {
|
||||||
content = content.replace(new RegExp(url, 'g'), replacements[i]);
|
content = content.replace(new RegExp(url, 'g'), replacements[i]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
var RSVP = require('rsvp');
|
var RSVP = require('rsvp');
|
||||||
|
var URI = require('urijs');
|
||||||
var core = require('./core');
|
var core = require('./core');
|
||||||
var EpubCFI = require('./epubcfi');
|
var EpubCFI = require('./epubcfi');
|
||||||
var Hook = require('./hook');
|
var Hook = require('./hook');
|
||||||
|
@ -37,7 +38,7 @@ Section.prototype.load = function(_request){
|
||||||
request(this.url)
|
request(this.url)
|
||||||
.then(function(xml){
|
.then(function(xml){
|
||||||
var base;
|
var base;
|
||||||
var directory = core.folder(this.url);
|
var directory = URI(this.url).directory();
|
||||||
|
|
||||||
this.document = xml;
|
this.document = xml;
|
||||||
this.contents = xml.documentElement;
|
this.contents = xml.documentElement;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue