Commit 62ce7b72 authored by Yannis Barlas's avatar Yannis Barlas

Merge branch 'styles-root-fix' into 'master'

Styles root fix

See merge request !3
parents c2236adb 9da001b8
......@@ -16,6 +16,7 @@ class HTMLEPUB {
this.xhtml = []
this.images = []
this.styles = []
this.fonts = []
}
load (items) {
......@@ -37,6 +38,18 @@ class HTMLEPUB {
stream (outputStream) {
const epub = this
if (epub.options.fontsRoot !== '') {
const fontsRoot = epub.options.fontsRoot.replace(/\/?$/, '/') // ensure trailing slash
fs.readdirSync(fontsRoot).forEach(file => {
const source = `${fontsRoot}/${file}`
const target = `styles/fonts/${file}`
const mimetype = 'application/vnd.ms-opentype'
const id = file
epub.fonts.push({ id, source, mimetype, target })
})
}
return new Promise((resolve, reject) => {
const archive = archiver('zip')
......@@ -99,7 +112,8 @@ class HTMLEPUB {
Promise.all(epub.images.map(appendFile)),
// EPUB/styles/* - the styles
Promise.all(epub.styles.map(appendFile))
Promise.all(epub.styles.map(appendFile)),
Promise.all(epub.fonts.map(appendFile))
]).then(() => {
archive.finalize()
......@@ -170,19 +184,37 @@ class HTMLEPUB {
$('head > link[rel="stylesheet"]').each((index, node) => {
const $node = $(node)
const id = `style-${chapter}-${index}`
const uri = $node.attr('href').replace(/^\//, '') // ensure no leading slash
// const id = `style-${chapter}-${index}`
const source = url.resolve(stylesRoot, uri)
const stylesRootExpanded = source.split('/')
const filenameWithExtension = stylesRootExpanded[stylesRootExpanded.length - 1]
const filenameWithoutExtension = filenameWithExtension.split('.')[0]
if (source.indexOf(stylesRoot) !== 0) {
throw new Error('Resource is outside the style root')
}
const id = filenameWithoutExtension
const target = `styles/${id}.css`
const mimetype = 'text/css'
if (epub.styles.length === 0) {
epub.styles.push({ id, source, mimetype, target })
} else {
let found = false
for (let i = 0; i < epub.styles.length; i++) {
if (epub.styles[i].id === id) {
found = true
break
}
}
if (!found) {
epub.styles.push({ id, source, mimetype, target })
}
}
epub.styles.push({ id, source, mimetype, target })
$node.attr('href', '../' + target)
})
......@@ -219,6 +251,7 @@ class HTMLEPUB {
}
get toc () {
const epub = this
return {
html: {
'@xmlns': 'http://www.w3.org/1999/xhtml',
......@@ -227,21 +260,33 @@ class HTMLEPUB {
meta: {
'@charset': 'utf-8'
},
title: this.book.title
title: this.book.title,
link: {
'@href': epub.styles[0].target,
'@rel': 'stylesheet',
'@type': epub.styles[0].mimetype
}
},
body: {
header: {
h1: 'Contents'
},
nav: {
'@epub:type': 'toc',
ol: {
li: this.xhtml.map(item => ({
'a': {
'@href': item.target,
'#text': item.title
}
}))
section: {
'@data-type': 'toc',
header: {
h1: {
'@class': 'ct',
'#text': 'Contents'
}
},
nav: {
'@epub:type': 'toc',
'@class': 'toc',
ol: {
li: this.xhtml.map(item => ({
'a': {
'@href': item.target,
'#text': item.title
}
}))
}
}
}
}
......@@ -303,6 +348,14 @@ class HTMLEPUB {
})
})
this.fonts.forEach(file => {
item.push({
'@id': file.id,
'@href': file.target,
'@media-type': file.mimetype
})
})
return { item }
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment