diff --git a/packages/Epub/EpubBackend.js b/packages/Epub/EpubBackend.js index 946ea9b4bd11254e5779138b2b920a54dc5d4f0f..2a997e888692f133079d4ca6d978a63f1831ee10 100644 --- a/packages/Epub/EpubBackend.js +++ b/packages/Epub/EpubBackend.js @@ -1,10 +1,12 @@ const HTMLEPUB = require('html-epub') const fs = require('fs') +const _ = require('lodash') const sorter = require('./sorter') const converters = require('./converters') const processFragment = require('./process') const output = require('./output') +const config = require('config') const EpubBackend = function (app) { app.use('/api/collections/:id/epub', async function (req, res, next) { @@ -35,19 +37,37 @@ const EpubBackend = function (app) { stylesRoot = `${__dirname}/themes` } + let fontsRoot = process.cwd() + config.epub.fontsPath + console.log('opath', fontsRoot) + if (!fs.existsSync(fontsRoot)) { + fontsRoot = '' + } + // converters const activeConverters = [req.query.converter] .filter(name => name && converters[name]) .map(name => converters[name]) - const parts = fragments.sort(sorter).map( + const sortedFragments = fragments.sort(sorter) + const partsIds = sortedFragments + .filter(fragment => fragment.division === 'body' && fragment.subCategory === 'part') + .map(fragment => fragment.id) + + sortedFragments.forEach(fragment => { + let found = _.indexOf(partsIds, fragment.id) + if (found !== -1) { + fragment.number = found + 1 + } + }) + + const parts = sortedFragments.map( processFragment({ styles, activeConverters, book }) ) // TODO: read the path to the uploads folder from config const resourceRoot = process.cwd() + '/uploads' - const epub = new HTMLEPUB(book, {resourceRoot, stylesRoot}) + const epub = new HTMLEPUB(book, {resourceRoot, stylesRoot, fontsRoot}) await epub.load(parts) diff --git a/packages/Epub/converters/wax.js b/packages/Epub/converters/wax.js index 6e857e4a9b330dae904d915757ef0b0e303cefa1..71c996eb34a1b4a1838dcc0dbec630301fb09447 100644 --- a/packages/Epub/converters/wax.js +++ b/packages/Epub/converters/wax.js @@ -1,4 +1,4 @@ -module.exports = ($, fragmentTitle, bookTitle, fragmentDivision, fragmentSubcategory) => { +module.exports = ($, fragmentTitle, bookTitle, fragmentDivision, fragmentSubcategory, fragmentNumber) => { const body = $('body') let outerContainer = $('<div/>').attr('class', fragmentDivision) @@ -15,8 +15,13 @@ module.exports = ($, fragmentTitle, bookTitle, fragmentDivision, fragmentSubcate $('<p/>').attr('class', 'ch-start').html('beginning').appendTo(innerContainer) $('<div/>').attr('class', 'folio').appendTo(innerContainer) $('<div/>').attr('class', 'booktitle').html(bookTitle).appendTo(innerContainer) - $('<h1/>').attr('class', 'ct').html(fragmentTitle).appendTo(innerContainer) $('<div/>').attr('class', 'dup').html(fragmentTitle).appendTo(innerContainer) + if (fragmentSubcategory === 'part') { + $('<p/>').attr('class', 'part-number').html(fragmentNumber).appendTo(innerContainer) + } else if (fragmentSubcategory === 'chapter') { + $('<p/>').attr('class', 'chapter-number').html(fragmentNumber).appendTo(innerContainer) + } + $('<h1/>').attr('class', 'ct').html(fragmentTitle).appendTo(innerContainer) const replaceWithBlockquote = className => (i, elem) => { const $elem = $(elem) diff --git a/packages/Epub/process.js b/packages/Epub/process.js index 7aee80450c9a3566efa0b1819df27635921a07e8..a3dd2c16614a016084e891e18fa483795d62e546 100644 --- a/packages/Epub/process.js +++ b/packages/Epub/process.js @@ -6,8 +6,9 @@ module.exports = ({ styles, activeConverters, book }) => fragment => { const bookTitle = book.title const fragmentDivision = fragment.division const fragmentSubcategory = fragment.subCategory + const fragmentNumber = fragment.hasOwnProperty('number') ? fragment.number : -1 - activeConverters.forEach(converter => converter($, fragmentTitle, bookTitle, fragmentDivision, fragmentSubcategory)) + activeConverters.forEach(converter => converter($, fragmentTitle, bookTitle, fragmentDivision, fragmentSubcategory, fragmentNumber)) styles.forEach(uri => { $('<link rel="stylesheet"/>').attr('href', uri).appendTo('head') diff --git a/packages/Epub/process.test.js b/packages/Epub/process.test.js index eafbdd96908a809df0579b530829546efe0c7da1..533c02e05353b2824b3e2a8914f2904f84054fcf 100644 --- a/packages/Epub/process.test.js +++ b/packages/Epub/process.test.js @@ -7,6 +7,7 @@ test('converts source to html', () => { title: 'A Test', division: 'body', subCategory: 'part', + number: 3, source: ` <div> <h1>A Test</h1> diff --git a/packages/Epub/themes/default.css b/packages/Epub/themes/default.css index 6370b454136ddce17c75d3d0fd2f73e06d12ab93..9c898165bf27779f9b840f13a54c9e154cafc6d4 100644 --- a/packages/Epub/themes/default.css +++ b/packages/Epub/themes/default.css @@ -612,7 +612,7 @@ hr { } hr:after { - content: "• • •"; + content: "• • •"; height: 17px; display: block; text-align: center; @@ -838,7 +838,11 @@ html { text-indent: 0; } +<<<<<<< HEAD .pt , [data-type="part"] p.ct, [data-type="part"] h1.ct , .back [data-type="bm-body"] .ct , [data-type="toc"] h1 { +======= +.pt , [data-type="part"] p.ct, [data-type="part"] h1.ct { +>>>>>>> e61feefec32231764d419957742e76fa1eaf1885 -webkit-column-break-before: page; page-break-before: page; break-before: page; @@ -1851,6 +1855,7 @@ img { float-defer: last;*/ /*float: block-end; */ } +/* hack the chapter debut */ .cst + p { margin-top: 0px; @@ -1937,12 +1942,6 @@ p + .cst { height: 17px; } - @-epubx-partition runningheader-right { - -epubx-flow-from: booktitle; - -epubx-enabled: -epubx-expr(page-number % 2 == 1); - right: 73px; - top: 51px; - height: 17px; /* width: 400px; */ text-align: right;