From e7e723f1cf18fab82adb2a79d72abf80800f0647 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Wed, 16 Jan 2019 01:18:14 +0000 Subject: [PATCH] Multiple levels of TOC + meta info. --- src/epub.js | 131 +++++++++++++++++++++++++++++++++++++------------ src/sidebar.js | 46 +++++++++++------ 2 files changed, 129 insertions(+), 48 deletions(-) diff --git a/src/epub.js b/src/epub.js index a916d7b..7984b9d 100644 --- a/src/epub.js +++ b/src/epub.js @@ -101,15 +101,30 @@ class EpubOpf{ Please read the legal notice included in this e-book and/or check the copyright status in your country. ${m.metadata.book_id} ${m.metadata.title} - ${m.metadata.language}\n` + ${m.metadata.language} +` + if (m.metadata.description){ + cnt += ` ${m.metadata.description}\n` + } + if (m.metadata.isbn){ + cnt += ` ${m.metadata.isbn}\n` + let onixcode = m.metadata.isbn.length > 10 ? '15' : '02' + cnt += ` ${onixcode}\n` + } if (m.metadata.publisher){ cnt += ` ${m.metadata.publisher}\n` } if (m.metadata.cover_image){ cnt += ` \n` } - // Lauren Baratz-Logsted + for (let s in m.metadata.subject){ + cnt += ` ${m.metadata.subject[s]}\n` + } + for (let a in m.metadata.author){ + cnt += ` ${m.metadata.author[a]}\n` + cnt += ` aut\n` + } cnt +=' \n \n' @@ -141,11 +156,37 @@ class EpubNcx{ generate(){ let m = this.meta - let cnt = ` + + let max_depth = 0; + let toc = '' + for (let i = 0; i < m.toc.length; ++i) { + let toc_item = m.toc[i] + toc += + ` \n` + +` ${toc_item.label}\n` + +` \n` + + if (max_depth < toc_item.depth) { + max_depth = toc_item.depth + } + + let next_depth = 1 + if (i+1 < m.toc.length) { + next_depth = m.toc[i+1].depth + } + + for (let current_dept = toc_item.depth; + current_dept >= next_depth; + current_dept--) { + toc += ' \n' + } + } + + return ` - + @@ -153,18 +194,10 @@ class EpubNcx{ ${m.metadata.title} +${toc} + + ` - for (let i in this.meta.toc){ - let toc_item = this.meta.toc[i] - cnt += - ` \n` - +` ${toc_item.label}\n` - +` \n` - +' \n' - } - - cnt += ' \n\n' - return cnt } } @@ -177,7 +210,35 @@ class EpubNav{ generate(){ let m = this.meta - let cnt = ` + + let toc = '' + for (let i = 0; i < m.toc.length; ++i){ + let toc_item = m.toc[i] + toc += `
  • ${toc_item.label}` + + let next_depth = 1 + if (i+1 < m.toc.length){ + next_depth = m.toc[i+1].depth + } + let current_dept = toc_item.depth + + if (current_dept == next_depth){ + toc += '
  • \n' + } + else if (current_dept < next_depth){ + for (; current_dept < next_depth; current_dept++){ + toc += '
      \n' + } + } + else if (current_dept > next_depth){ + for (; current_dept > next_depth; current_dept--){ + toc += '
    \n' + } + toc += ' \n' + } + } + + return ` @@ -189,14 +250,12 @@ class EpubNav{ + + ` - for (let i in this.meta.toc){ - let toc_item = this.meta.toc[i] - cnt += `
  • ${toc_item.label}
  • \n` - } - - cnt += ' \n \n\n\n' - return cnt } } @@ -204,11 +263,14 @@ class EpubMeta{ constructor() { this.metadata = { book_id: 'book_id', + isbn: null, title: 'title', language: 'en', - author: 'author', - publisher: 'publisher', - cover_image: 'cover.img' + author: [], + publisher: null, + cover_image: null, + subject: [], + description: null } this.manifest = {} this.spine = [] @@ -228,14 +290,23 @@ class EpubMeta{ this.metadata.title = title } addAuthor(author){ - this.metadata.author = author + this.metadata.author.push(author) } addPublisher(publisher){ this.metadata.publisher = publisher } + addSubject(subject){ + this.metadata.subject.push(subject) + } addLanguage(lang){ this.metadata.language = lang } + addDescription(description){ + this.metadata.description = description + } + addIsbn(isbn){ + this.metadata.isbn = isbn + } addMetaData(){ } @@ -275,11 +346,8 @@ class EpubMeta{ } addToc(filename, label, depth){ - if (filename in this.override){ - filename = this.override[filename].filename - } this.toc.push({ - filename: filename, + href: filename, label: label, depth: depth }) @@ -327,7 +395,6 @@ class EpubWriter{ if (filename in this.meta.override){ let o = this.meta.override[filename] - filename = o.filename content = o.convert(content) } diff --git a/src/sidebar.js b/src/sidebar.js index 56830be..ab9a21e 100644 --- a/src/sidebar.js +++ b/src/sidebar.js @@ -210,21 +210,30 @@ function fillMetadata(epub, book) { epub.meta.addTitle(book.book_info.title) epub.meta.addLanguage(book.book_info.language) - epub.meta.addBookId(book.book_info.isbn) + if (book.book_info.isbn){ + epub.meta.addBookId(book.book_info.isbn) + } + if (book.book_info.description){ + epub.meta.addDescription(book.book_info.description) + } + epub.meta.addIsbn(book.book_info.isbn) for (let i in book.book_info.authors){ let author = book.book_info.authors[i] - epub.meta.addAuthor(author) + epub.meta.addAuthor(author.name) } for (let i in book.book_info.publishers){ let publisher = book.book_info.publishers[i] epub.meta.addPublisher(publisher.name) } + for (let i in book.book_info.subjects){ + let subject = book.book_info.subjects[i] + epub.meta.addSubject(subject.name) + } + // # The metadata element or deprecated dc-metadata element contains // # at least one identifier element, at least one title element, // # and at least one language element drawn from the Dublin Core tag // # set. - // epub.set_title('Test Title') - // epub.set_language('en') // epub.set_direction('ltr') // # epub.set_cover(file_name, content, create_page=True): // # epub.add_author(author, file_as=None, role=None, uid='creator'): @@ -259,13 +268,19 @@ function fillHtmlComponents(epub, book) function fillToc(epub, book) { - for (let i in book.book_toc){ - let toc_item = book.book_toc[i] - epub.meta.addToc( - toc_item.filename, - toc_item.label, - toc_item.depth) + let helper = function(epub, book_toc){ + for (let i in book_toc){ + let toc_item = book_toc[i] + epub.meta.addToc( + toc_item.href, + toc_item.label, + toc_item.depth) + if ('children' in toc_item) { + helper(epub, toc_item.children) + } + } } + helper(epub,book.book_toc) } function fillSpine(epub, book) @@ -277,24 +292,22 @@ function fillSpine(epub, book) } } -function fillGuide(epub, book) -{} - -function createEpub(book, epub){ +function createEpubStructure(book, epub){ epub.addMetaInfoFile("book.json", JSON.stringify(book, null, '\t')) // OPF file info. fillManifest(epub, book) fillSpine(epub, book) - fillGuide(epub, book) fillMetadata(epub, book) // NCX and NAV files. fillToc(epub, book) fillHtmlComponents(epub, book) +} - for (let url in book.book_files){ +function createEpub(book, epub){ + for (let url in book.book_files){ file = book.book_files[url] epub.addFile(file.filename, file.body) } @@ -316,6 +329,7 @@ function onDownloadBookClicked(){ // .then(() => { return page.renderChapterList(book); }) .then(() => { return book.downloadMetaContent(); }) .then(() => { return book.downloadContent(); }) + .then(() => { return createEpubStructure(book, epub); }) .then(() => { return createEpub(book, epub); }) .then(() => { return epub.generateAsync(); }) .then((file) => {