fimfic2epub/src/templates.js

411 lines
16 KiB
JavaScript
Raw Normal View History

2016-06-24 01:26:01 +12:00
import m from 'mithril'
import render from 'mithril-node-render'
2016-06-24 01:26:01 +12:00
import { pd as pretty } from 'pretty-data'
import zeroFill from 'zero-fill'
import { NS } from './constants'
function nth (d) {
if (d > 3 && d < 21) return 'th'
switch (d % 10) {
case 1: return 'st'
case 2: return 'nd'
case 3: return 'rd'
default: return 'th'
2016-06-24 01:26:01 +12:00
}
}
function prettyDate (d) {
// format: 27th Oct 2011
let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
return d.getDate() + nth(d) + ' ' + months[d.getMonth()].substring(0, 3) + ' ' + d.getFullYear()
}
export function createChapter (ch) {
let {content, notes, notesFirst, title, link, linkNotes} = ch
let sections = [
m.trust(content || ''),
notes ? m('div#author_notes', {className: notesFirst ? 'top' : 'bottom'}, [
m('p', m('b', 'Author\'s Note:')),
m.trust(notes)]) : null
]
// if author notes are a the beginning of the chapter
if (notes && notesFirst) {
sections.reverse()
}
2018-03-13 10:09:08 +13:00
const tokenContent = '%%HTML_CONTENT_' + Math.random() + '%%'
return Promise.all([
render(
m('html', {xmlns: NS.XHTML, 'xmlns:epub': NS.OPS}, [
m('head', [
m('meta', {charset: 'utf-8'}),
m('link', {rel: 'stylesheet', type: 'text/css', href: '../Styles/style.css'}),
m('title', title)
]),
2018-03-13 10:09:08 +13:00
m('body', {'epub:type': 'bodymatter chapter'}, m('section', [
title ? m('.chapter-title', [
m('h1', title),
2017-06-07 20:48:59 +12:00
m('hr.old')
2016-08-24 09:49:27 +12:00
]) : null,
2018-03-13 10:09:08 +13:00
tokenContent,
(link || linkNotes) ? m('p.double', {style: 'text-align: center; clear: both;'},
link ? m('a.chaptercomments', {href: link + '#comment_list'}, 'Read chapter comments online') : null,
linkNotes ? m('a.chaptercomments', {href: linkNotes}, 'Read author\'s note') : null
) : null
2018-03-13 10:09:08 +13:00
]))
])
2018-03-13 10:09:08 +13:00
, {strict: true}),
render(sections)
]).then(([chapterPage, sectionsData]) => {
2018-03-13 10:09:08 +13:00
chapterPage = '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n' + chapterPage
chapterPage = chapterPage.replace(tokenContent, '\n' + sectionsData + '\n')
return chapterPage
})
}
2016-08-19 22:35:58 +12:00
// some eReaders doesn't understand linear=no, so push those items to the end of the spine/book.
function sortSpineItems (items) {
let count = items.length
for (let i = 0; i < count; i++) {
let item = items[i]
2016-08-24 02:32:55 +12:00
if (!item) {
continue
}
2016-08-19 22:35:58 +12:00
if (item.attrs.linear === 'no') {
// push it to the end
items.splice(i, 1)
items.push(item)
count--
i--
}
}
2016-08-19 22:35:58 +12:00
return items
}
export function createOpf (ffc) {
2016-06-24 01:26:01 +12:00
let remotes = []
2016-08-29 22:12:04 +12:00
// let remoteCounter = 0
ffc.remoteResources.forEach((r, url) => {
2016-08-29 22:12:04 +12:00
// remoteCounter++
if (!ffc.options.includeExternal) {
// hack-ish, but what can I do?
// turns out only video and audio can be remote resources.. :I
/*
if (url.indexOf('//') === 0) {
url = 'http:' + url
}
if (url.indexOf('/') === 0) {
url = 'http://www.fimfiction.net' + url
}
let mime = null
if (url.toLowerCase().lastIndexOf('.png')) {
mime = 'image/png'
} else if (url.toLowerCase().lastIndexOf('.jpg')) {
mime = 'image/jpeg'
}
if (mime) {
remotes.push(m('item', {id: 'remote_' + zeroFill(3, remoteCounter), href: url, 'media-type': mime}))
}
*/
return
}
2016-06-24 01:26:01 +12:00
if (!r.dest) {
return
}
remotes.push(m('item', {id: r.filename, href: r.dest, 'media-type': r.type}))
2016-06-24 01:26:01 +12:00
})
let manifestChapters = ffc.storyInfo.chapters.map((ch, num) =>
m('item', {id: 'chapter_' + zeroFill(3, num + 1), href: 'Text/chapter_' + zeroFill(3, num + 1) + '.xhtml', 'media-type': 'application/xhtml+xml', properties: ch.remote ? 'remote-resources' : null})
)
let spineChapters = ffc.storyInfo.chapters.map((ch, num) =>
m('itemref', {idref: 'chapter_' + zeroFill(3, num + 1)})
)
let manifestNotes = []
let spineNotes = []
if (ffc.options.includeAuthorNotes && ffc.options.useAuthorNotesIndex && ffc.hasAuthorNotes) {
spineNotes.push(m('itemref', {idref: 'notesnav'}))
ffc.chaptersWithNotes.forEach((num) => {
let id = 'note_' + zeroFill(3, num + 1)
manifestNotes.push(m('item', {id: id, href: 'Text/' + id + '.xhtml', 'media-type': 'application/xhtml+xml'}))
spineNotes.push(m('itemref', {idref: id}))
})
}
let subjects = ffc.subjects
if (ffc.options.joinSubjects) {
subjects = [subjects.join(', ')]
}
return render(
2016-06-24 01:26:01 +12:00
m('package', {xmlns: NS.OPF, version: '3.0', 'unique-identifier': 'BookId'}, [
m('metadata', {'xmlns:dc': NS.DC, 'xmlns:opf': NS.OPF}, [
m('dc:identifier#BookId', ffc.storyInfo.uuid),
m('dc:title', ffc.storyInfo.title),
m('dc:creator#cre', ffc.storyInfo.author.name),
2016-06-24 01:26:01 +12:00
m('meta', {refines: '#cre', property: 'role', scheme: 'marc:relators'}, 'aut'),
m('dc:date', new Date((ffc.storyInfo.publishDate || ffc.storyInfo.date_modified) * 1000).toISOString().substring(0, 10)),
2016-06-24 01:26:01 +12:00
m('dc:publisher', 'Fimfiction'),
2018-03-13 10:09:08 +13:00
ffc.storyInfo.short_description ? m('dc:description', ffc.storyInfo.short_description) : null,
m('dc:source', ffc.storyInfo.url),
2016-06-24 01:26:01 +12:00
m('dc:language', 'en'),
2016-08-24 02:32:55 +12:00
ffc.coverImage ? m('meta', {name: 'cover', content: 'cover'}) : null,
m('meta', {property: 'dcterms:modified'}, new Date(ffc.storyInfo.date_modified * 1000).toISOString().replace('.000', ''))
].concat(subjects.map((s) =>
m('dc:subject', s)
), m('meta', {name: 'fimfic2epub version', content: FIMFIC2EPUB_VERSION}))),
2016-06-24 01:26:01 +12:00
m('manifest', [
2016-08-24 02:32:55 +12:00
ffc.coverImage ? m('item', {id: 'cover', href: ffc.coverFilename, 'media-type': ffc.coverType, properties: 'cover-image'}) : null,
2016-06-24 01:26:01 +12:00
m('item', {id: 'ncx', href: 'toc.ncx', 'media-type': 'application/x-dtbncx+xml'}),
2018-03-13 10:09:08 +13:00
m('item', {id: 'nav', 'href': 'nav.xhtml', 'media-type': 'application/xhtml+xml', properties: 'nav'}),
ffc.options.includeAuthorNotes && ffc.options.useAuthorNotesIndex && ffc.hasAuthorNotes ? m('item', {id: 'notesnav', 'href': 'notesnav.xhtml', 'media-type': 'application/xhtml+xml'}) : null,
2016-08-24 02:32:55 +12:00
m('item', {id: 'style', href: 'Styles/style.css', 'media-type': 'text/css'}),
m('item', {id: 'coverstyle', href: 'Styles/coverstyle.css', 'media-type': 'text/css'}),
2016-08-24 02:32:55 +12:00
m('item', {id: 'titlestyle', href: 'Styles/titlestyle.css', 'media-type': 'text/css'}),
2018-03-13 10:09:08 +13:00
m('item', {id: 'navstyle', href: 'Styles/navstyle.css', 'media-type': 'text/css'}),
ffc.iconsFont ? m('item', {id: 'font-awesome', href: 'Fonts/fontawesome-webfont-subset.ttf', 'media-type': 'application/x-font-ttf'}) : null,
2016-08-24 02:32:55 +12:00
m('item', {id: 'coverpage', href: 'Text/cover.xhtml', 'media-type': 'application/xhtml+xml', properties: ffc.coverImage ? 'svg' : undefined}),
m('item', {id: 'titlepage', href: 'Text/title.xhtml', 'media-type': 'application/xhtml+xml', properties: ffc.hasRemoteResources.titlePage ? 'remote-resources' : null})
].concat(manifestChapters, manifestNotes, remotes)),
2016-06-24 01:26:01 +12:00
2016-08-19 22:35:58 +12:00
m('spine', {toc: 'ncx'}, sortSpineItems([
2016-06-24 01:26:01 +12:00
m('itemref', {idref: 'coverpage'}),
2016-08-24 02:32:55 +12:00
m('itemref', {idref: 'titlepage'}),
m('itemref', {idref: 'nav', linear: ffc.storyInfo.chapters.length <= 1 && !(ffc.options.includeAuthorNotes && ffc.options.useAuthorNotesIndex && ffc.hasAuthorNotes) ? 'no' : undefined})
].concat(
spineChapters,
spineNotes
))),
m('guide', [
m('reference', {type: 'cover', title: 'Cover', href: 'Text/cover.xhtml'}),
2018-03-13 10:09:08 +13:00
m('reference', {type: 'toc', title: 'Contents', href: 'nav.xhtml'})
])
2016-06-24 01:26:01 +12:00
])
2018-03-13 10:09:08 +13:00
, {strict: true}).then((contentOpf) => {
contentOpf = '<?xml version="1.0" encoding="utf-8"?>\n' + pretty.xml(contentOpf)
return contentOpf
})
2016-06-24 01:26:01 +12:00
}
function navPoints (list) {
let arr = []
for (let i = 0; i < list.length; i++) {
if (!list[i]) continue
2016-06-24 01:26:01 +12:00
arr.push(m('navPoint', {id: 'navPoint-' + (i + 1), playOrder: i + 1}, [
m('navLabel', m('text', list[i][0])),
m('content', {src: list[i][1]})
]))
}
return arr
}
export function createNcx (ffc) {
return render(
2016-06-24 01:26:01 +12:00
m('ncx', {version: '2005-1', xmlns: NS.DAISY}, [
m('head', [
m('meta', {content: ffc.storyInfo.uuid, name: 'dtb:uid'}),
2016-06-24 01:26:01 +12:00
m('meta', {content: 0, name: 'dtb:depth'}),
m('meta', {content: 0, name: 'dtb:totalPageCount'}),
m('meta', {content: 0, name: 'dtb:maxPageNumber'})
]),
m('docTitle', m('text', ffc.storyInfo.title)),
2016-06-24 01:26:01 +12:00
m('navMap', navPoints([
2018-03-13 10:09:08 +13:00
['Cover', 'Text/cover.xhtml'],
['Title Page', 'Text/title.xhtml'],
['Contents', 'nav.xhtml']
].concat(ffc.storyInfo.chapters.map((ch, num) =>
[ch.title, 'Text/chapter_' + zeroFill(3, num + 1) + '.xhtml']
2018-03-13 10:09:08 +13:00
), ffc.options.includeAuthorNotes && ffc.options.useAuthorNotesIndex && ffc.hasAuthorNotes ? [['Author\'s Notes', 'notesnav.xhtml']] : null)))
2016-06-24 01:26:01 +12:00
])
2018-03-13 10:09:08 +13:00
, {strict: true}).then((tocNcx) => {
tocNcx = '<?xml version="1.0" encoding="utf-8" ?>\n' + pretty.xml(tocNcx)
return tocNcx
})
2016-06-24 01:26:01 +12:00
}
2018-03-13 10:09:08 +13:00
export function createNav (ffc) {
let list = [
m('li', {hidden: ''}, m('a', {href: 'Text/cover.xhtml'}, 'Cover')),
m('li', {hidden: ''}, m('a', {href: 'Text/title.xhtml'}, 'Title Page')),
ffc.storyInfo.chapters.length > 0 ? m('li', {hidden: ''}, m('a', {href: 'nav.xhtml'}, 'Contents')) : null
].concat(ffc.storyInfo.chapters.map((ch, num) =>
m('li', [
m('a.leftalign', {href: 'Text/chapter_' + zeroFill(3, num + 1) + '.xhtml'}, ch.title)
])
))
let prettyList = ffc.storyInfo.chapters.map((ch, num) =>
m('.item', [
m('.floatbox', m('span.wordcount', ch.realWordCount.toLocaleString('en-GB'))),
m('a.leftalign', {href: 'Text/chapter_' + zeroFill(3, num + 1) + '.xhtml'}, ch.title),
m('span.date', [m('b', ' · '), prettyDate(new Date(ch.date_modified * 1000))])
])
)
if (ffc.options.includeAuthorNotes && ffc.options.useAuthorNotesIndex && ffc.hasAuthorNotes) {
list.push(m('li', m('a', {href: 'notesnav.xhtml'}, 'Author\'s Notes')))
prettyList.push(m('.item.double', m('a.leftalign', {href: 'notesnav.xhtml'}, 'Author\'s Notes')))
}
return render(
m('html', {xmlns: NS.XHTML, 'xmlns:epub': NS.OPS}, [
2016-06-24 01:26:01 +12:00
m('head', [
m('meta', {charset: 'utf-8'}),
2018-03-13 10:09:08 +13:00
m('link', {rel: 'stylesheet', type: 'text/css', href: 'Styles/style.css'}),
m('link', {rel: 'stylesheet', type: 'text/css', href: 'Styles/navstyle.css'}),
m('title', 'Contents')
2016-06-24 01:26:01 +12:00
]),
2018-03-13 10:09:08 +13:00
m('body', {'epub:type': 'frontmatter toc'}, m('section', [
m('h3', 'Contents'),
m('#toc.hidden', prettyList),
m('nav.invisible', {'epub:type': 'toc'}, m('ol', list))
]))
2016-06-24 01:26:01 +12:00
])
2018-03-13 10:09:08 +13:00
, {strict: true}).then((navDocument) => {
navDocument = '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n' + pretty.xml(navDocument)
return navDocument
})
}
export function createNotesNav (ffc) {
let list = ffc.chaptersWithNotes.map((num) => {
let ch = ffc.storyInfo.chapters[num]
return m('.item', m('a.leftalign', {href: 'Text/note_' + zeroFill(3, num + 1) + '.xhtml'}, ch.title))
})
2018-03-13 10:09:08 +13:00
return render(
m('html', {xmlns: NS.XHTML, 'xmlns:epub': NS.OPS}, [
m('head', [
m('meta', {charset: 'utf-8'}),
m('link', {rel: 'stylesheet', type: 'text/css', href: 'Styles/style.css'}),
m('link', {rel: 'stylesheet', type: 'text/css', href: 'Styles/navstyle.css'}),
m('title', 'Author\'s Notes')
]),
m('body#navpage', {'epub:type': 'frontmatter toc'}, m('section', [
m('h3', 'Author\'s Notes'),
m('#toc', list)
]))
])
, {strict: true}).then((navDocument) => {
navDocument = '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n' + pretty.xml(navDocument)
return navDocument
})
2016-06-24 01:26:01 +12:00
}
2016-08-24 02:32:55 +12:00
export function createCoverPage (ffc) {
let body
2016-08-24 02:32:55 +12:00
let {width, height} = ffc.coverImageDimensions
if (ffc.coverImage) {
body = m('svg#cover', {xmlns: NS.SVG, 'xmlns:xlink': NS.XLINK, version: '1.1', viewBox: '0 0 ' + width + ' ' + height},
m('image', {width: width, height: height, 'xlink:href': '../' + ffc.coverFilename})
)
} else {
body = [
m('h1', ffc.storyInfo.title),
m('h2', ffc.storyInfo.author.name)
]
}
return render(
2016-06-24 01:26:01 +12:00
m('html', {xmlns: NS.XHTML, 'xmlns:epub': NS.OPS}, [
m('head', [
2016-08-24 02:32:55 +12:00
ffc.coverImage ? m('meta', {name: 'viewport', content: 'width=' + width + ', height=' + height}) : null,
2016-06-24 01:26:01 +12:00
m('title', 'Cover'),
m('link', {rel: 'stylesheet', type: 'text/css', href: '../Styles/coverstyle.css'})
2016-06-24 01:26:01 +12:00
]),
2018-03-13 10:09:08 +13:00
m('body#coverpage', {'epub:type': 'frontmatter cover'}, m('section', body))
2016-06-24 01:26:01 +12:00
])
2018-03-13 10:09:08 +13:00
, {strict: true}).then((coverPage) => {
coverPage = '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n' + pretty.xml(coverPage)
return coverPage
})
2016-06-24 01:26:01 +12:00
}
function infoBox (heading, data) {
return m('.infobox', m('.wrap', [
m('span.heading', heading),
m('br'),
m('span.data', data)
]))
}
function calcWordCount (chapters) {
let count = 0
for (let i = 0; i < chapters.length; i++) {
let ch = chapters[i]
count += ch.realWordCount
}
return count
}
export function createTitlePage (ffc) {
2018-03-13 10:09:08 +13:00
const tokenContent = '%%HTML_CONTENT_' + Math.random() + '%%'
const completedIcon = {
complete: 'check',
incomplete: 'pencil',
hiatus: 'pause',
cancelled: 'ban'
}
return render(
m('html', {xmlns: NS.XHTML, 'xmlns:epub': NS.OPS}, [
m('head', [
m('meta', {charset: 'utf-8'}),
m('link', {rel: 'stylesheet', type: 'text/css', href: '../Styles/style.css'}),
m('link', {rel: 'stylesheet', type: 'text/css', href: '../Styles/titlestyle.css'}),
m('title', ffc.storyInfo.title)
]),
2018-03-13 10:09:08 +13:00
m('body#titlepage', {'epub:type': 'frontmatter titlepage'}, m('section', [
2016-06-28 23:59:39 +12:00
m('.title', [
2018-03-13 10:09:08 +13:00
m('div', {className: 'content-rating content-rating-' + ffc.storyInfo.content_rating_text.toLowerCase()}, ffc.storyInfo.content_rating_text.charAt(0).toUpperCase()),
2016-06-28 23:59:39 +12:00
m('.story_name', ffc.storyInfo.title + ' '),
m('.author', ['by ', m('b', ffc.storyInfo.author.name)])
]),
// m('hr'),
2018-03-13 10:09:08 +13:00
m('.tags', [
ffc.tags.filter((tag) => tag.type !== 'character').map((tag) =>
[m('div', {className: tag.className}, tag.name), ' ']
2016-06-28 23:59:39 +12:00
)
]),
2018-03-13 10:09:08 +13:00
m('.readlink', m('a', {href: ffc.storyInfo.url}, 'Story on Fimfiction')),
m('br'),
// m('hr'),
ffc.storyInfo.prequel ? [m('div', [
'This story is a sequel to ',
m('a', {href: ffc.storyInfo.prequel.url}, ffc.storyInfo.prequel.title)
]), m('hr')] : null,
2018-03-13 10:09:08 +13:00
m('#description', tokenContent),
m('.bottom', [
2018-03-13 10:09:08 +13:00
m('div', {className: 'completed-status completed-status-' + ffc.storyInfo.status.toLowerCase()}, [
m('i.fa.fa-fw.fa-' + completedIcon[ffc.storyInfo.status.toLowerCase()]),
m('span', ffc.storyInfo.status)
]),
ffc.storyInfo.publishDate && infoBox('First Published', prettyDate(new Date(ffc.storyInfo.publishDate * 1000))),
infoBox('Last Modified', prettyDate(new Date(ffc.storyInfo.date_modified * 1000))),
infoBox('Word Count', calcWordCount(ffc.storyInfo.chapters).toLocaleString('en-GB'))
]),
// m('hr'),
2018-03-13 10:09:08 +13:00
m('.tags', [
ffc.tags.filter((tag) => tag.type === 'character').map((tag) =>
[m('div', {className: tag.className}, tag.name), ' ']
)
])
2018-03-13 10:09:08 +13:00
]))
])
2018-03-13 10:09:08 +13:00
, {strict: true}).then((titlePage) => {
titlePage = '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n' + titlePage
titlePage = titlePage.replace(tokenContent, '\n' + ffc.storyInfo.description + '\n')
return titlePage
})
}