mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-05 15:32:55 +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
|
@ -634,7 +634,9 @@ Rendition.prototype.triggerViewEvent = function(e){
|
|||
};
|
||||
|
||||
Rendition.prototype.replacements = function(){
|
||||
// Wait for loading
|
||||
return this.q.enqueue(function () {
|
||||
// Get thes books manifest
|
||||
var manifest = this.book.package.manifest;
|
||||
var manifestArray = Object.keys(manifest).
|
||||
map(function (key){
|
||||
|
@ -653,17 +655,23 @@ Rendition.prototype.replacements = function(){
|
|||
// Only CSS
|
||||
var css = items.
|
||||
filter(function (item){
|
||||
if (item.type != "text/css") {
|
||||
if (item.type === "text/css") {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Css Urls
|
||||
var cssUrls = css.map(function(item) {
|
||||
return item.href;
|
||||
});
|
||||
|
||||
// All Assets Urls
|
||||
var urls = items.
|
||||
map(function(item) {
|
||||
// return this.book.baseUrl + item.href;
|
||||
return item.href;
|
||||
}.bind(this));
|
||||
|
||||
// Create blob urls for all the assets
|
||||
var processing = urls.
|
||||
map(function(url) {
|
||||
var absolute = URI(url).absoluteTo(this.book.baseUrl).toString();
|
||||
|
@ -671,36 +679,70 @@ Rendition.prototype.replacements = function(){
|
|||
return this.book.archive.createUrl(absolute);
|
||||
}.bind(this));
|
||||
|
||||
// After all the urls are created
|
||||
return RSVP.all(processing).
|
||||
then(function(replacementUrls) {
|
||||
this.hooks.serialize.register(function(content, section) {
|
||||
// resolve file urls
|
||||
var fileUri = URI(section.url);
|
||||
// console.log(section.url);
|
||||
// var fileDirectory = fileUri.directory();
|
||||
// // // 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.
|
||||
|
||||
// Replace Asset Urls in the text of all css files
|
||||
cssUrls.forEach(function(href) {
|
||||
this.replaceCss(href, urls, replacementUrls);
|
||||
}.bind(this));
|
||||
|
||||
urls = urls.
|
||||
map(function(href) {
|
||||
var assetUri = URI(href).absoluteTo(this.book.baseUrl);
|
||||
var relative = assetUri.relativeTo(fileUri).toString();
|
||||
return relative;
|
||||
}.bind(this));
|
||||
|
||||
|
||||
section.output = replace.substitute(content, urls, replacementUrls);
|
||||
// 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);
|
||||
// Get Urls relative to current sections
|
||||
var relUrls = urls.
|
||||
map(function(href) {
|
||||
var assetUri = URI(href).absoluteTo(this.book.baseUrl);
|
||||
var relative = assetUri.relativeTo(fileUri).toString();
|
||||
return relative;
|
||||
}.bind(this));
|
||||
|
||||
|
||||
section.output = replace.substitute(section.output, relUrls, replacementUrls);
|
||||
};
|
||||
//-- Enable binding events to Renderer
|
||||
RSVP.EventTarget.mixin(Rendition.prototype);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue