2018-03-13 10:09:26 +13:00
|
|
|
/* global chrome */
|
2016-06-28 09:19:01 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
import path from 'path'
|
2016-06-28 09:19:01 +12:00
|
|
|
import JSZip from 'jszip'
|
|
|
|
import escapeStringRegexp from 'escape-string-regexp'
|
|
|
|
import zeroFill from 'zero-fill'
|
|
|
|
import { XmlEntities } from 'html-entities'
|
2016-08-15 21:11:20 +12:00
|
|
|
import sanitize from 'sanitize-filename'
|
2016-08-16 01:12:20 +12:00
|
|
|
import URL from 'url'
|
2016-06-28 09:19:01 +12:00
|
|
|
import isNode from 'detect-node'
|
2016-08-22 21:50:11 +12:00
|
|
|
import fileType from 'file-type'
|
2017-06-12 23:53:17 +12:00
|
|
|
import isSvg from 'is-svg'
|
2016-08-24 02:32:55 +12:00
|
|
|
import sizeOf from 'image-size'
|
|
|
|
import Emitter from 'es6-event-emitter'
|
2016-06-28 09:19:01 +12:00
|
|
|
|
|
|
|
import { cleanMarkup } from './cleanMarkup'
|
2016-08-23 19:19:01 +12:00
|
|
|
import fetch from './fetch'
|
2016-06-28 09:19:01 +12:00
|
|
|
import fetchRemote from './fetchRemote'
|
|
|
|
import * as template from './templates'
|
2018-03-13 10:09:26 +13:00
|
|
|
import { styleCss, coverstyleCss, titlestyleCss, iconsCss, navstyleCss, paragraphsCss } from './styles'
|
|
|
|
import * as utils from './utils'
|
|
|
|
import subsetFont from './subsetFont'
|
|
|
|
import fontAwesomeCodes from '../build/font-awesome-codes.json'
|
2016-06-28 09:19:01 +12:00
|
|
|
|
2016-08-22 21:50:11 +12:00
|
|
|
import { containerXml } from './constants'
|
2016-06-28 09:19:01 +12:00
|
|
|
|
|
|
|
const entities = new XmlEntities()
|
|
|
|
|
2017-06-12 23:53:17 +12:00
|
|
|
const trimWhitespace = /^\s*(<br\s*\/?\s*>)+|(<br\s*\/?\s*>)+\s*$/ig
|
|
|
|
|
2016-08-24 08:04:38 +12:00
|
|
|
class FimFic2Epub extends Emitter {
|
2016-08-17 02:33:52 +12:00
|
|
|
static getStoryId (id) {
|
|
|
|
if (isNaN(id)) {
|
|
|
|
let url = URL.parse(id, false, true)
|
2016-08-16 01:12:20 +12:00
|
|
|
if (url.hostname === 'www.fimfiction.net' || url.hostname === 'fimfiction.net') {
|
|
|
|
let m = url.pathname.match(/^\/story\/(\d+)/)
|
|
|
|
if (m) {
|
2016-08-17 02:33:52 +12:00
|
|
|
id = m[1]
|
2016-08-16 01:12:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 02:33:52 +12:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFilename (storyInfo) {
|
|
|
|
return sanitize(storyInfo.title + ' by ' + storyInfo.author.name + '.epub')
|
|
|
|
}
|
|
|
|
|
2016-08-19 21:24:20 +12:00
|
|
|
static fetchStoryInfo (storyId, raw = false) {
|
2016-08-17 02:33:52 +12:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
storyId = FimFic2Epub.getStoryId(storyId)
|
2016-08-23 19:19:01 +12:00
|
|
|
let url = '/api/story.php?story=' + storyId
|
|
|
|
fetch(url).then((content) => {
|
2016-08-17 02:33:52 +12:00
|
|
|
let data
|
|
|
|
try {
|
2016-08-19 21:24:20 +12:00
|
|
|
data = JSON.parse(content)
|
2016-08-17 02:33:52 +12:00
|
|
|
} catch (e) {}
|
|
|
|
if (!data) {
|
2017-10-19 01:59:30 +13:00
|
|
|
reject(new Error('Unable to fetch story info'))
|
2016-08-17 02:33:52 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (data.error) {
|
2018-03-14 01:58:03 +13:00
|
|
|
reject(new Error(data.error + ' (id: ' + storyId + ')'))
|
2016-08-17 02:33:52 +12:00
|
|
|
return
|
|
|
|
}
|
2016-08-17 20:37:41 +12:00
|
|
|
let story = data.story
|
2016-08-19 21:24:20 +12:00
|
|
|
if (raw) {
|
|
|
|
resolve(story)
|
|
|
|
return
|
|
|
|
}
|
2016-08-17 20:37:41 +12:00
|
|
|
// this is so the metadata can be cached.
|
|
|
|
if (!story.chapters) story.chapters = []
|
|
|
|
delete story.likes
|
|
|
|
delete story.dislikes
|
|
|
|
delete story.views
|
|
|
|
delete story.total_views
|
|
|
|
delete story.comments
|
|
|
|
story.chapters.forEach((ch) => {
|
|
|
|
delete ch.views
|
|
|
|
})
|
2016-08-19 21:24:20 +12:00
|
|
|
// Add version number
|
|
|
|
story.FIMFIC2EPUB_VERSION = FIMFIC2EPUB_VERSION
|
|
|
|
resolve(story)
|
2016-08-17 02:33:52 +12:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
constructor (storyId, options = {}) {
|
2016-08-24 02:32:55 +12:00
|
|
|
super()
|
|
|
|
|
2016-08-17 02:33:52 +12:00
|
|
|
this.storyId = FimFic2Epub.getStoryId(storyId)
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
this.defaultOptions = {
|
2016-08-24 09:49:27 +12:00
|
|
|
addCommentsLink: true,
|
|
|
|
includeAuthorNotes: true,
|
2016-08-30 02:20:20 +12:00
|
|
|
useAuthorNotesIndex: false,
|
2016-08-25 00:47:48 +12:00
|
|
|
addChapterHeadings: true,
|
|
|
|
includeExternal: true,
|
2017-06-07 08:15:05 +12:00
|
|
|
paragraphStyle: 'spaced',
|
2018-03-13 23:10:04 +13:00
|
|
|
joinSubjects: false,
|
|
|
|
calculateReadingEase: false
|
2016-08-24 09:49:27 +12:00
|
|
|
}
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
this.options = Object.assign(this.defaultOptions, options)
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
// promise cache
|
|
|
|
this.pcache = {
|
|
|
|
metadata: null,
|
|
|
|
chapters: null,
|
|
|
|
remoteResources: null,
|
|
|
|
coverImage: null,
|
|
|
|
fetchAll: null
|
|
|
|
}
|
2016-08-23 07:57:19 +12:00
|
|
|
|
|
|
|
this.storyInfo = null
|
2016-08-24 02:32:55 +12:00
|
|
|
this.description = ''
|
2016-08-24 08:04:38 +12:00
|
|
|
this.subjects = []
|
2016-08-23 19:19:01 +12:00
|
|
|
this.chapters = []
|
2016-08-25 00:47:48 +12:00
|
|
|
this.chaptersHtml = []
|
2016-08-30 02:20:20 +12:00
|
|
|
this.notesHtml = []
|
|
|
|
this.hasAuthorNotes = false
|
|
|
|
this.chaptersWithNotes = []
|
2018-03-13 10:09:26 +13:00
|
|
|
this.pages = {}
|
2016-08-25 00:47:48 +12:00
|
|
|
this.remoteResourcesCached = false
|
2016-06-28 09:19:01 +12:00
|
|
|
this.remoteResources = new Map()
|
2018-03-13 10:09:26 +13:00
|
|
|
this.usedIcons = new Set()
|
|
|
|
this.iconsFont = null
|
2016-08-24 08:04:38 +12:00
|
|
|
this.coverUrl = ''
|
2016-08-24 02:32:55 +12:00
|
|
|
this.coverImage = null
|
|
|
|
this.coverFilename = ''
|
|
|
|
this.coverType = ''
|
|
|
|
this.coverImageDimensions = {width: 0, height: 0}
|
2016-08-23 07:57:19 +12:00
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
this.hasRemoteResources = {
|
|
|
|
titlePage: false
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:11:20 +12:00
|
|
|
this.cachedFile = null
|
2016-08-24 02:32:55 +12:00
|
|
|
this.tags = []
|
2016-06-28 09:19:01 +12:00
|
|
|
|
2016-08-24 02:32:55 +12:00
|
|
|
this.zip = null
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
fetchAll () {
|
|
|
|
if (this.pcache.fetchAll) {
|
|
|
|
return this.pcache.fetchAll
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
2016-08-25 00:47:48 +12:00
|
|
|
|
2018-03-14 01:58:03 +13:00
|
|
|
this.progress(0, 0, '')
|
2016-08-25 00:47:48 +12:00
|
|
|
|
|
|
|
this.pcache.fetchAll = this.fetchMetadata()
|
|
|
|
.then(this.fetchChapters.bind(this))
|
|
|
|
.then(this.fetchCoverImage.bind(this))
|
2016-08-25 01:57:05 +12:00
|
|
|
.then(this.buildChapters.bind(this))
|
2018-03-13 23:10:04 +13:00
|
|
|
.then(this.buildPages.bind(this))
|
2018-03-13 10:09:26 +13:00
|
|
|
.then(this.findIcons.bind(this))
|
2016-08-25 00:47:48 +12:00
|
|
|
.then(this.fetchRemoteFiles.bind(this))
|
|
|
|
.then(() => {
|
|
|
|
this.progress(0, 0.95)
|
|
|
|
this.pcache.fetchAll = null
|
|
|
|
})
|
|
|
|
|
|
|
|
return this.pcache.fetchAll
|
2016-06-28 19:39:31 +12:00
|
|
|
}
|
2016-06-28 09:19:01 +12:00
|
|
|
|
2016-08-24 08:04:38 +12:00
|
|
|
fetchMetadata () {
|
2016-08-25 00:47:48 +12:00
|
|
|
if (this.pcache.metadata) {
|
|
|
|
return this.pcache.metadata
|
|
|
|
}
|
|
|
|
if (this.storyInfo) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
2016-08-24 08:04:38 +12:00
|
|
|
this.storyInfo = null
|
|
|
|
this.description = ''
|
2016-08-25 00:47:48 +12:00
|
|
|
this.subjects = []
|
2016-08-24 08:04:38 +12:00
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
this.progress(0, 0, 'Fetching metadata...')
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
this.pcache.metadata = FimFic2Epub.fetchStoryInfo(this.storyId)
|
|
|
|
.then((storyInfo) => {
|
|
|
|
this.storyInfo = storyInfo
|
2018-03-13 23:11:17 +13:00
|
|
|
this.storyInfo.uuid = 'url:' + this.storyInfo.url
|
2018-03-13 10:09:26 +13:00
|
|
|
this.filename = FimFic2Epub.getFilename(this.storyInfo)
|
|
|
|
this.storyInfo.chapters.forEach((chapter) => {
|
|
|
|
if (chapter.date_modified > this.storyInfo.date_modified) {
|
|
|
|
this.storyInfo.date_modified = chapter.date_modified
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.progress(0, 0.5)
|
|
|
|
})
|
|
|
|
.then(this.fetchTitlePage.bind(this))
|
|
|
|
.then(() => {
|
|
|
|
this.progress(0, 1)
|
|
|
|
})
|
|
|
|
.then(() => cleanMarkup(this.description)).then((html) => {
|
|
|
|
this.storyInfo.description = html
|
|
|
|
}).then(() => {
|
|
|
|
this.pcache.metadata = null
|
|
|
|
})
|
2016-08-25 00:47:48 +12:00
|
|
|
return this.pcache.metadata
|
2016-08-24 08:04:38 +12:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
fetchChapters () {
|
|
|
|
if (this.pcache.chapters) {
|
|
|
|
return this.pcache.chapters
|
|
|
|
}
|
|
|
|
// chapters have already been fetched
|
|
|
|
if (this.chapters.length !== 0) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
this.chapters.length = 0
|
|
|
|
this.chaptersHtml.length = 0
|
2016-08-30 02:20:20 +12:00
|
|
|
this.hasAuthorNotes = false
|
|
|
|
this.chaptersWithNotes.length = 0
|
2016-08-25 00:47:48 +12:00
|
|
|
|
|
|
|
this.progress(0, 0, 'Fetching chapters...')
|
2017-06-12 23:53:17 +12:00
|
|
|
|
|
|
|
let chapterCount = this.storyInfo.chapters.length
|
2018-03-13 10:09:26 +13:00
|
|
|
let url = 'https://fimfiction.net/story/download/' + this.storyInfo.id + '/html'
|
2017-06-12 23:53:17 +12:00
|
|
|
|
|
|
|
this.pcache.chapters = fetch(url).then((html) => {
|
|
|
|
let p = Promise.resolve()
|
|
|
|
let matchChapter = /<article class="chapter">[\s\S]*?<\/header>([\s\S]*?)<\/article>/g
|
|
|
|
for (let ma, i = 0; (ma = matchChapter.exec(html)); i++) {
|
|
|
|
let chapterContent = ma[1]
|
|
|
|
chapterContent = chapterContent.replace(/<footer>[\s\S]*?<\/footer>/g, '').trim()
|
|
|
|
|
|
|
|
let authorNotesPos = chapterContent.indexOf('<aside ')
|
|
|
|
let notesContent = ''
|
|
|
|
let notesFirst = authorNotesPos === 0
|
|
|
|
if (authorNotesPos !== -1) {
|
|
|
|
chapterContent = chapterContent.replace(/<aside class="authors-note">([\s\S]*?)<\/aside>/, (match, content, pos) => {
|
|
|
|
content = content.replace(/<header><h1>.*?<\/h1><\/header>/, '')
|
|
|
|
notesContent = content.trim().replace(trimWhitespace, '')
|
|
|
|
return ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
chapterContent = chapterContent.trim().replace(trimWhitespace, '')
|
|
|
|
let chapter = {content: chapterContent, notes: notesContent, notesFirst}
|
|
|
|
p = p.then(cleanMarkup(chapter.content).then((content) => {
|
|
|
|
chapter.content = content
|
|
|
|
}))
|
|
|
|
if (notesContent) {
|
|
|
|
p = p.then(cleanMarkup(chapter.notes).then((notes) => {
|
|
|
|
chapter.notes = notes
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
p = p.then(() => {
|
2018-03-14 01:58:03 +13:00
|
|
|
this.progress(0, (i + 1) / chapterCount, 'Parsed chapter ' + (i + 1) + ' / ' + chapterCount)
|
2017-06-12 23:53:17 +12:00
|
|
|
if (chapter.notes) {
|
|
|
|
this.hasAuthorNotes = true
|
|
|
|
this.chaptersWithNotes.push(i)
|
|
|
|
}
|
|
|
|
this.chapters[i] = chapter
|
2018-03-13 23:10:04 +13:00
|
|
|
}).then(() => new Promise((resolve, reject) => setTimeout(resolve, 20)))
|
2017-06-12 23:53:17 +12:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}).then(() => {
|
|
|
|
this.pcache.chapters = null
|
|
|
|
})
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
return this.pcache.chapters
|
2016-08-24 09:49:27 +12:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
fetchRemoteFiles () {
|
2018-03-14 01:58:03 +13:00
|
|
|
if (!this.options.includeExternal || this.remoteResources.size === 0) {
|
2016-08-25 00:47:48 +12:00
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
if (this.pcache.remoteResources) {
|
|
|
|
return this.pcache.remoteResources
|
|
|
|
}
|
|
|
|
if (this.remoteResourcesCached) {
|
|
|
|
return Promise.resolve()
|
2016-08-23 07:57:19 +12:00
|
|
|
}
|
2016-06-28 19:39:31 +12:00
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
this.progress(0, 0, 'Fetching remote files...')
|
|
|
|
this.pcache.remoteResources = new Promise((resolve, reject) => {
|
|
|
|
let iter = this.remoteResources.entries()
|
|
|
|
let completeCount = 0
|
2016-08-23 19:19:01 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
let next = (r) => {
|
|
|
|
completeCount++
|
|
|
|
if (r.data) {
|
|
|
|
this.progress(0, completeCount / this.remoteResources.size, 'Fetched remote file ' + completeCount + ' / ' + this.remoteResources.size)
|
|
|
|
} else {
|
|
|
|
this.progress(0, completeCount / this.remoteResources.size, 'Fetching remote files...')
|
|
|
|
}
|
|
|
|
recursive()
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
let recursive = () => {
|
|
|
|
let r = iter.next().value
|
|
|
|
if (!r) {
|
|
|
|
if (completeCount === this.remoteResources.size) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let url = r[0]
|
|
|
|
r = r[1]
|
2018-03-13 10:09:26 +13:00
|
|
|
if (r.data) {
|
|
|
|
next(r)
|
|
|
|
return
|
|
|
|
}
|
2016-06-28 19:39:31 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
fetchRemote(url, 'arraybuffer').then(async (data) => {
|
2016-08-25 00:47:48 +12:00
|
|
|
r.dest = null
|
|
|
|
let info = fileType(isNode ? data : new Uint8Array(data))
|
2018-03-13 10:09:26 +13:00
|
|
|
if (!info || info.mime === 'application/xml') {
|
2017-06-12 23:53:17 +12:00
|
|
|
// file-type doesn't support SVG, extra check:
|
|
|
|
if (isSvg(Buffer.from(data).toString('utf8'))) {
|
|
|
|
info = {
|
|
|
|
mime: 'image/svg+xml',
|
|
|
|
ext: 'svg'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 00:47:48 +12:00
|
|
|
if (info) {
|
2018-03-13 10:09:26 +13:00
|
|
|
if (info.mime === 'image/webp') {
|
|
|
|
data = await utils.webp2png(isNode ? data : new Uint8Array(data))
|
|
|
|
info = fileType(data)
|
|
|
|
}
|
2016-08-25 00:47:48 +12:00
|
|
|
let type = info.mime
|
|
|
|
r.type = type
|
2018-03-13 10:09:26 +13:00
|
|
|
let isImage = type.startsWith('image/')
|
2016-08-25 00:47:48 +12:00
|
|
|
let folder = isImage ? 'Images' : 'Misc'
|
|
|
|
let dest = folder + '/*.' + info.ext
|
|
|
|
r.dest = dest.replace('*', r.filename)
|
|
|
|
r.data = data
|
|
|
|
}
|
2018-03-13 10:09:26 +13:00
|
|
|
next(r)
|
|
|
|
}).catch((err) => { console.error(err) })
|
2016-08-25 00:47:48 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// concurrent downloads!
|
|
|
|
recursive()
|
|
|
|
recursive()
|
|
|
|
recursive()
|
|
|
|
recursive()
|
|
|
|
}).then(() => {
|
|
|
|
this.pcache.remoteResources = null
|
|
|
|
})
|
|
|
|
return this.pcache.remoteResources
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
async buildPages () {
|
|
|
|
this.pages.cover = await template.createCoverPage(this)
|
|
|
|
this.pages.title = await template.createTitlePage(this)
|
|
|
|
this.findRemoteResources('titlepage', 'titlepage', this.pages.title)
|
|
|
|
this.pages.nav = await template.createNav(this)
|
|
|
|
delete this.pages.notesnav
|
|
|
|
if (this.options.includeAuthorNotes && this.options.useAuthorNotesIndex && this.hasAuthorNotes) {
|
|
|
|
this.pages.notesnav = await template.createNotesNav(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
buildChapters () {
|
|
|
|
let chain = Promise.resolve()
|
|
|
|
this.chaptersHtml.length = 0
|
2016-08-30 02:20:20 +12:00
|
|
|
this.notesHtml.length = 0
|
2016-08-25 00:47:48 +12:00
|
|
|
|
|
|
|
for (let i = 0; i < this.chapters.length; i++) {
|
|
|
|
let ch = this.storyInfo.chapters[i]
|
|
|
|
let chapter = this.chapters[i]
|
2016-08-30 02:20:20 +12:00
|
|
|
chain = chain.then(template.createChapter.bind(null, {
|
|
|
|
title: this.options.addChapterHeadings ? ch.title : null,
|
|
|
|
link: this.options.addCommentsLink ? ch.link : null,
|
|
|
|
linkNotes: this.options.includeAuthorNotes && this.options.useAuthorNotesIndex && chapter.notes ? 'note_' + zeroFill(3, i + 1) + '.xhtml' : null,
|
|
|
|
content: chapter.content,
|
|
|
|
notes: !this.options.useAuthorNotesIndex ? chapter.notes : '',
|
|
|
|
notesFirst: chapter.notesFirst
|
|
|
|
})).then((html) => {
|
|
|
|
this.findRemoteResources('ch_' + zeroFill(3, i + 1), {chapter: i}, html)
|
2016-08-25 00:47:48 +12:00
|
|
|
this.chaptersHtml[i] = html
|
2016-08-23 07:57:19 +12:00
|
|
|
})
|
2016-08-30 02:20:20 +12:00
|
|
|
if (this.options.includeAuthorNotes && this.options.useAuthorNotesIndex && chapter.notes) {
|
|
|
|
chain = chain.then(template.createChapter.bind(null, {
|
|
|
|
title: 'Author\'s Note: ' + ch.title,
|
|
|
|
content: chapter.notes
|
|
|
|
})).then((html) => {
|
|
|
|
this.findRemoteResources('note_' + zeroFill(3, i + 1), {note: i}, html)
|
|
|
|
this.notesHtml[i] = html
|
|
|
|
})
|
|
|
|
}
|
2018-03-13 23:10:04 +13:00
|
|
|
chain = chain.then(() => {
|
|
|
|
if (!ch.realWordCount) {
|
|
|
|
ch.realWordCount = utils.htmlWordCount(chapter.content)
|
|
|
|
}
|
|
|
|
if (this.options.calculateReadingEase && !ch.readingEase) {
|
2018-03-13 23:40:10 +13:00
|
|
|
let text = utils.htmlToText(chapter.content)
|
|
|
|
text = text.replace(/\s+/g, ' ').trim()
|
|
|
|
ch.readingEase = utils.readingEase(text)
|
2018-03-13 23:10:04 +13:00
|
|
|
}
|
|
|
|
this.progress(0, (i + 1) / this.chapters.length, 'Processed chapter ' + (i + 1) + ' / ' + this.chapters.length)
|
|
|
|
}).then(() => new Promise((resolve) => setTimeout(resolve, 20)))
|
2016-08-25 00:47:48 +12:00
|
|
|
}
|
2016-08-15 21:11:20 +12:00
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
return chain
|
2016-08-23 07:57:19 +12:00
|
|
|
}
|
2016-06-28 19:39:31 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
async build () {
|
2016-08-24 02:32:55 +12:00
|
|
|
this.cachedFile = null
|
|
|
|
this.zip = null
|
|
|
|
|
2016-08-25 01:57:05 +12:00
|
|
|
this.replaceRemoteResources()
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-25 01:57:05 +12:00
|
|
|
this.zip = new JSZip()
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-25 01:57:05 +12:00
|
|
|
this.zip.file('mimetype', 'application/epub+zip')
|
|
|
|
this.zip.file('META-INF/container.xml', containerXml)
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
this.zip.file('OEBPS/content.opf', Buffer.from(await template.createOpf(this), 'utf8'))
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-25 01:57:05 +12:00
|
|
|
if (this.coverImage) {
|
2018-03-13 10:09:26 +13:00
|
|
|
this.zip.file('OEBPS/' + this.coverFilename, Buffer.from(this.coverImage))
|
2016-08-25 01:57:05 +12:00
|
|
|
}
|
2018-03-13 10:09:26 +13:00
|
|
|
this.zip.file('OEBPS/Text/cover.xhtml', Buffer.from(this.pages.cover, 'utf8'))
|
2017-10-19 02:02:07 +13:00
|
|
|
this.zip.file('OEBPS/Styles/coverstyle.css', Buffer.from(coverstyleCss, 'utf8'))
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
this.zip.file('OEBPS/Text/title.xhtml', Buffer.from(this.pages.title, 'utf8'))
|
2017-10-19 02:02:07 +13:00
|
|
|
this.zip.file('OEBPS/Styles/titlestyle.css', Buffer.from(titlestyleCss, 'utf8'))
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
this.zip.file('OEBPS/nav.xhtml', Buffer.from(this.pages.nav, 'utf8'))
|
|
|
|
this.zip.file('OEBPS/toc.ncx', Buffer.from(await template.createNcx(this), 'utf8'))
|
|
|
|
this.zip.file('OEBPS/Styles/navstyle.css', Buffer.from(navstyleCss, 'utf8'))
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-25 01:57:05 +12:00
|
|
|
for (let i = 0; i < this.chapters.length; i++) {
|
|
|
|
let filename = 'OEBPS/Text/chapter_' + zeroFill(3, i + 1) + '.xhtml'
|
|
|
|
let html = this.chaptersHtml[i]
|
2017-10-19 02:02:07 +13:00
|
|
|
this.zip.file(filename, Buffer.from(html, 'utf8'))
|
2016-08-25 01:57:05 +12:00
|
|
|
}
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-30 02:20:20 +12:00
|
|
|
if (this.options.includeAuthorNotes && this.options.useAuthorNotesIndex && this.hasAuthorNotes) {
|
2018-03-13 10:09:26 +13:00
|
|
|
this.zip.file('OEBPS/notesnav.xhtml', Buffer.from(this.pages.notesnav, 'utf8'))
|
2016-08-30 02:20:20 +12:00
|
|
|
|
|
|
|
for (let i = 0; i < this.chapters.length; i++) {
|
|
|
|
if (!this.chapters[i].notes) continue
|
|
|
|
let filename = 'OEBPS/Text/note_' + zeroFill(3, i + 1) + '.xhtml'
|
|
|
|
let html = this.notesHtml[i]
|
2017-10-19 02:02:07 +13:00
|
|
|
this.zip.file(filename, Buffer.from(html, 'utf8'))
|
2016-08-30 02:20:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
if (this.iconsFont) {
|
|
|
|
this.zip.file('OEBPS/Fonts/fontawesome-webfont-subset.ttf', this.iconsFont)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.zip.file('OEBPS/Styles/style.css', Buffer.from(
|
|
|
|
styleCss + '\n\n' + this.iconsStyle() + '\n\n' +
|
|
|
|
(paragraphsCss[this.options.paragraphStyle] || '')
|
|
|
|
, 'utf8'))
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-25 01:57:05 +12:00
|
|
|
this.remoteResources.forEach((r) => {
|
|
|
|
if (r.dest) {
|
|
|
|
this.zip.file('OEBPS/' + r.dest, r.data)
|
|
|
|
}
|
2016-08-24 02:32:55 +12:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// for node, resolve a Buffer, in browser resolve a Blob
|
|
|
|
getFile () {
|
|
|
|
if (!this.zip) {
|
2017-10-19 01:59:30 +13:00
|
|
|
return Promise.reject(new Error('Not downloaded.'))
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
|
|
|
if (this.cachedFile) {
|
|
|
|
return Promise.resolve(this.cachedFile)
|
|
|
|
}
|
2017-10-19 02:02:07 +13:00
|
|
|
this.progress(0, 0, 'Compressing...')
|
|
|
|
|
|
|
|
let lastPercent = -1
|
2016-08-24 02:32:55 +12:00
|
|
|
|
|
|
|
return this.zip
|
|
|
|
.generateAsync({
|
|
|
|
type: isNode ? 'nodebuffer' : 'blob',
|
|
|
|
mimeType: 'application/epub+zip',
|
|
|
|
compression: 'DEFLATE',
|
|
|
|
compressionOptions: {level: 9}
|
2017-10-19 02:02:07 +13:00
|
|
|
}, (metadata) => { // onUpdate
|
|
|
|
let currentPercent = Math.round(metadata.percent / 10) * 10
|
|
|
|
if (lastPercent !== currentPercent) {
|
|
|
|
lastPercent = currentPercent
|
|
|
|
this.progress(0, currentPercent / 100, 'Compressing...')
|
|
|
|
}
|
2016-08-24 02:32:55 +12:00
|
|
|
})
|
|
|
|
.then((file) => {
|
2016-08-25 00:47:48 +12:00
|
|
|
this.progress(0, 1, 'Complete!')
|
2016-08-24 02:32:55 +12:00
|
|
|
this.cachedFile = file
|
|
|
|
return file
|
2016-08-23 19:19:01 +12:00
|
|
|
})
|
2018-03-13 10:09:26 +13:00
|
|
|
.catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|
|
|
|
|
2016-08-24 02:32:55 +12:00
|
|
|
// example usage: .pipe(fs.createWriteStream(filename))
|
2017-10-19 02:02:07 +13:00
|
|
|
streamFile (onUpdate) {
|
2016-08-24 02:32:55 +12:00
|
|
|
if (!this.zip) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return this.zip
|
|
|
|
.generateNodeStream({
|
|
|
|
type: 'nodebuffer',
|
|
|
|
streamFiles: false,
|
|
|
|
mimeType: 'application/epub+zip',
|
|
|
|
compression: 'DEFLATE',
|
|
|
|
compressionOptions: {level: 9}
|
2017-10-19 02:02:07 +13:00
|
|
|
}, onUpdate)
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
setTitle (title) {
|
|
|
|
this.storyInfo.title = title.trim()
|
|
|
|
this.filename = FimFic2Epub.getFilename(this.storyInfo)
|
|
|
|
}
|
|
|
|
setAuthorName (name) {
|
|
|
|
this.storyInfo.author.name = name.trim()
|
|
|
|
this.filename = FimFic2Epub.getFilename(this.storyInfo)
|
|
|
|
}
|
|
|
|
setCoverImage (buffer) {
|
2018-03-13 10:09:26 +13:00
|
|
|
buffer = isNode ? buffer : new Uint8Array(buffer)
|
|
|
|
let info = fileType(buffer)
|
|
|
|
if (!info || !info.mime.startsWith('image/')) {
|
2016-08-25 00:47:48 +12:00
|
|
|
throw new Error('Invalid image')
|
|
|
|
}
|
|
|
|
this.coverImage = buffer
|
|
|
|
this.coverFilename = 'Images/cover.' + info.ext
|
|
|
|
this.coverType = info.mime
|
2017-10-19 01:59:30 +13:00
|
|
|
this.coverImageDimensions = sizeOf(Buffer.from(buffer))
|
2016-08-25 00:47:48 +12:00
|
|
|
}
|
|
|
|
|
2016-08-24 02:32:55 +12:00
|
|
|
// Internal/private methods
|
2017-06-08 02:17:40 +12:00
|
|
|
progress (part, percent, status = '') {
|
2016-08-25 00:47:48 +12:00
|
|
|
// let parts = 6.3
|
|
|
|
// let partsize = 1 / parts
|
|
|
|
// percent = (part / parts) + percent * partsize
|
2017-06-08 02:17:40 +12:00
|
|
|
try {
|
|
|
|
this.trigger('progress', percent, status)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
if (status) {
|
|
|
|
console.log(status)
|
|
|
|
}
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
findRemoteResources (prefix, where, html) {
|
|
|
|
let remoteCounter = 1
|
|
|
|
let matchUrl = /<img.*?src="([^">]*\/([^">]*?))".*?>/g
|
|
|
|
let emoticonUrl = /static\.fimfiction\.net\/images\/emoticons\/([a-z_]*)\.[a-z]*$/
|
|
|
|
|
|
|
|
for (let ma; (ma = matchUrl.exec(html));) {
|
|
|
|
let url = ma[1]
|
2017-10-19 02:22:55 +13:00
|
|
|
let cleanurl = entities.decode(url)
|
2016-08-24 02:32:55 +12:00
|
|
|
if (this.remoteResources.has(cleanurl)) {
|
|
|
|
let r = this.remoteResources.get(cleanurl)
|
|
|
|
if (r.where.indexOf(where) === -1) {
|
|
|
|
r.where.push(where)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let filename = prefix + '_' + remoteCounter
|
|
|
|
let emoticon = url.match(emoticonUrl)
|
|
|
|
if (emoticon) {
|
|
|
|
filename = 'emoticon_' + emoticon[1]
|
|
|
|
}
|
|
|
|
remoteCounter++
|
|
|
|
this.remoteResources.set(cleanurl, {filename: filename, where: [where], originalUrl: url})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:09:26 +13:00
|
|
|
async findIcons () {
|
|
|
|
let matchIcon = /<i class="fa fa-fw fa-(.*?)"(><\/i|\/)>/g
|
|
|
|
this.usedIcons.clear()
|
|
|
|
|
|
|
|
const scan = (html) => {
|
|
|
|
if (!html) return
|
|
|
|
for (let ma; (ma = matchIcon.exec(html));) {
|
|
|
|
if (ma[1] in fontAwesomeCodes) {
|
|
|
|
this.usedIcons.add(ma[1])
|
|
|
|
} else {
|
|
|
|
console.warn('Unknown icon:', ma[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scan(this.pages.title)
|
|
|
|
this.chaptersHtml.forEach(scan)
|
|
|
|
this.notesHtml.forEach(scan)
|
|
|
|
|
|
|
|
if (this.usedIcons.size === 0) {
|
|
|
|
this.iconsFont = null
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let glyphs = [...this.usedIcons].map((name) => {
|
|
|
|
return fontAwesomeCodes[name].charCodeAt(0)
|
|
|
|
})
|
2018-03-13 10:31:08 +13:00
|
|
|
let fontPath
|
2018-03-13 10:09:26 +13:00
|
|
|
if (!isNode) {
|
|
|
|
fontPath = chrome.extension.getURL('build/fonts/fontawesome-webfont.ttf')
|
2018-03-13 10:31:08 +13:00
|
|
|
} else {
|
|
|
|
// TODO: Fix better font detection
|
|
|
|
const fs = require('fs')
|
|
|
|
fontPath = path.join(__dirname, '../node_modules/', 'font-awesome/fonts/fontawesome-webfont.ttf')
|
|
|
|
if (!fs.existsSync(fontPath)) {
|
|
|
|
fontPath = path.join(__dirname, '../../', 'font-awesome/fonts/fontawesome-webfont.ttf')
|
|
|
|
}
|
2018-03-13 10:09:26 +13:00
|
|
|
}
|
|
|
|
this.iconsFont = await subsetFont(fontPath, glyphs, {local: isNode})
|
|
|
|
}
|
|
|
|
|
|
|
|
iconsStyle () {
|
|
|
|
if (this.usedIcons.size === 0) return ''
|
|
|
|
let style = iconsCss.trim() + '\n'
|
|
|
|
this.usedIcons.forEach((name) => {
|
|
|
|
style += '.fa-' + name + ':before { content: "\\' + fontAwesomeCodes[name].charCodeAt(0).toString(16) + '"; }\n'
|
|
|
|
})
|
|
|
|
return style
|
|
|
|
}
|
|
|
|
|
2016-08-24 02:32:55 +12:00
|
|
|
fetchCoverImage () {
|
2016-08-25 00:47:48 +12:00
|
|
|
if (this.pcache.coverImage) {
|
|
|
|
return this.pcache.coverImage
|
|
|
|
}
|
2016-08-24 02:32:55 +12:00
|
|
|
if (this.coverImage) {
|
2016-08-25 00:47:48 +12:00
|
|
|
return Promise.resolve(this.coverImage)
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
|
|
|
this.coverImage = null
|
2016-08-24 08:04:38 +12:00
|
|
|
let url = this.coverUrl || this.storyInfo.full_image
|
2016-08-24 02:32:55 +12:00
|
|
|
if (!url) {
|
2016-08-25 00:47:48 +12:00
|
|
|
return Promise.resolve(null)
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
this.progress(0, 0, 'Fetching cover image...')
|
2016-08-24 02:32:55 +12:00
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
this.pcache.coverImage = fetchRemote(url, 'arraybuffer').then((data) => {
|
2018-03-13 10:09:26 +13:00
|
|
|
data = isNode ? data : new Uint8Array(data)
|
|
|
|
let info = fileType(data)
|
2016-08-24 02:32:55 +12:00
|
|
|
if (info) {
|
|
|
|
let type = info.mime
|
2018-03-13 10:09:26 +13:00
|
|
|
let isImage = type.startsWith('image/')
|
2016-08-24 02:32:55 +12:00
|
|
|
if (!isImage) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let filename = 'Images/cover.' + info.ext
|
|
|
|
this.coverFilename = filename
|
|
|
|
this.coverType = type
|
|
|
|
|
2017-10-19 01:59:30 +13:00
|
|
|
this.coverImageDimensions = sizeOf(Buffer.from(data))
|
2016-08-24 02:32:55 +12:00
|
|
|
this.coverImage = data
|
|
|
|
this.coverFilename = filename
|
|
|
|
return this.coverImage
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2016-08-25 00:47:48 +12:00
|
|
|
}).then(() => {
|
|
|
|
this.pcache.coverImage = null
|
2016-08-24 02:32:55 +12:00
|
|
|
})
|
2016-08-25 00:47:48 +12:00
|
|
|
return this.pcache.coverImage
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
|
|
|
|
2016-08-23 07:57:19 +12:00
|
|
|
fetchTitlePage () {
|
2018-03-13 10:09:26 +13:00
|
|
|
let viewMature = true
|
|
|
|
let isStoryMature = this.storyInfo.content_rating === 2
|
|
|
|
if (!isNode) {
|
|
|
|
viewMature = document.cookie.split('; ').includes('view_mature=true')
|
|
|
|
if (!viewMature && isStoryMature) {
|
|
|
|
if (window.setCookie) {
|
|
|
|
window.setCookie('view_mature', true, 365)
|
|
|
|
} else {
|
|
|
|
document.cookie = 'view_mature=true; path=/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fetch(this.storyInfo.url).then((data) => {
|
|
|
|
if (!viewMature && isStoryMature) {
|
|
|
|
// Delete cookie
|
|
|
|
document.cookie = 'view_mature=false; path=/; Max-Age=0'
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}).then(this.extractTitlePageInfo.bind(this))
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:39:31 +12:00
|
|
|
extractTitlePageInfo (html) {
|
2017-06-07 08:15:05 +12:00
|
|
|
let startTagsPos = html.indexOf('<div class="story_content_box"')
|
|
|
|
startTagsPos += html.substring(startTagsPos).indexOf('<ul class="story-tags">') + 23
|
|
|
|
let tagsHtml = html.substring(startTagsPos)
|
|
|
|
|
|
|
|
let endTagsPos = tagsHtml.indexOf('</ul>')
|
|
|
|
tagsHtml = tagsHtml.substring(0, endTagsPos)
|
2016-08-23 19:19:01 +12:00
|
|
|
|
2017-06-07 08:15:05 +12:00
|
|
|
let tags = []
|
2018-03-13 10:09:26 +13:00
|
|
|
let c
|
2017-06-07 08:15:05 +12:00
|
|
|
tags.byImage = {}
|
2016-08-25 06:18:01 +12:00
|
|
|
this.subjects.length = 0
|
2016-08-24 08:04:38 +12:00
|
|
|
this.subjects.push('Fimfiction')
|
2016-08-25 06:18:01 +12:00
|
|
|
this.subjects.push(this.storyInfo.content_rating_text)
|
2018-03-13 10:09:26 +13:00
|
|
|
|
|
|
|
let matchTag = /<a href="([^"]*?)" class="([^"]*?)" title="[^"]*?" data-tag="([^"]*?)".*?>(.*?)<\/a>/g
|
|
|
|
for (;(c = matchTag.exec(tagsHtml));) {
|
2017-06-07 18:02:43 +12:00
|
|
|
let cat = {
|
2018-03-13 10:09:26 +13:00
|
|
|
url: 'https://fimfiction.net' + c[1],
|
|
|
|
className: 'story-tag ' + c[2],
|
|
|
|
name: entities.decode(c[4]),
|
|
|
|
type: c[2].replace('tag-', '')
|
2017-06-07 18:02:43 +12:00
|
|
|
}
|
2018-03-13 10:09:26 +13:00
|
|
|
tags.push(cat)
|
2017-06-07 18:02:43 +12:00
|
|
|
this.subjects.push(cat.name)
|
|
|
|
}
|
2018-03-13 10:09:26 +13:00
|
|
|
this.tags = tags
|
2016-08-23 19:19:01 +12:00
|
|
|
|
2017-06-07 08:15:05 +12:00
|
|
|
html = html.substring(endTagsPos + 5)
|
|
|
|
html = html.substring(html.indexOf('<span class="description-text bbcode">') + 38)
|
|
|
|
|
|
|
|
let ma = html.match(/This story is a sequel to <a href="([^"]*)">(.*?)<\/a>/)
|
2016-08-23 19:19:01 +12:00
|
|
|
if (ma) {
|
|
|
|
this.storyInfo.prequel = {
|
2018-03-13 10:09:26 +13:00
|
|
|
url: 'https://fimfiction.net' + ma[1],
|
2016-08-23 19:19:01 +12:00
|
|
|
title: entities.decode(ma[2])
|
2016-06-28 19:39:31 +12:00
|
|
|
}
|
2016-08-23 19:19:01 +12:00
|
|
|
html = html.substring(html.indexOf('<hr />') + 6)
|
|
|
|
}
|
2017-06-07 08:15:05 +12:00
|
|
|
|
|
|
|
let endDescPos = html.indexOf('</span>\n')
|
2016-08-23 19:19:01 +12:00
|
|
|
let description = html.substring(0, endDescPos).trim()
|
2016-08-24 02:32:55 +12:00
|
|
|
this.description = description
|
2016-08-23 19:19:01 +12:00
|
|
|
|
|
|
|
html = html.substring(endDescPos + 7)
|
|
|
|
let extraPos = html.indexOf('<div class="extra_story_data">')
|
|
|
|
html = html.substring(extraPos + 30)
|
|
|
|
|
2018-03-13 23:10:54 +13:00
|
|
|
ma = html.match(/<span class="approved-date">.*?data-time="(.*?)".*?<\/span>/)
|
2016-08-23 19:19:01 +12:00
|
|
|
if (ma) {
|
2018-03-13 23:10:54 +13:00
|
|
|
this.storyInfo.publishDate = +ma[1]
|
2016-08-23 19:19:01 +12:00
|
|
|
}
|
2016-06-28 09:19:01 +12:00
|
|
|
|
2016-08-23 19:19:01 +12:00
|
|
|
html = html.substring(0, html.indexOf('<div class="button-group"'))
|
|
|
|
}
|
2016-06-28 09:19:01 +12:00
|
|
|
|
2016-08-24 09:49:27 +12:00
|
|
|
parseChapterPage (html) {
|
|
|
|
let trimWhitespace = /^\s*(<br\s*\/?\s*>)+|(<br\s*\/?\s*>)+\s*$/ig
|
|
|
|
|
|
|
|
let authorNotesPos = html.indexOf('<div class="authors-note"')
|
|
|
|
let authorNotes = ''
|
2016-08-25 00:47:48 +12:00
|
|
|
if (authorNotesPos !== -1) {
|
2016-08-24 09:49:27 +12:00
|
|
|
authorNotesPos = authorNotesPos + html.substring(authorNotesPos).indexOf('<b>Author\'s Note:</b>')
|
|
|
|
authorNotes = html.substring(authorNotesPos + 22)
|
|
|
|
authorNotes = authorNotes.substring(0, authorNotes.indexOf('\t\n\t</div>'))
|
|
|
|
authorNotes = authorNotes.trim()
|
|
|
|
authorNotes = authorNotes.replace(trimWhitespace, '')
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:15:05 +12:00
|
|
|
let chapterPos = html.indexOf('<div class="bbcode">')
|
|
|
|
let chapter = html.substring(chapterPos + 20)
|
2016-08-24 09:49:27 +12:00
|
|
|
|
2017-06-12 23:53:17 +12:00
|
|
|
let pos = chapter.indexOf('\t\t</div>\n\t</div>\t\t\n\t\t\t\t\t</div>\n')
|
2016-08-24 09:49:27 +12:00
|
|
|
|
|
|
|
chapter = chapter.substring(0, pos).trim()
|
|
|
|
|
|
|
|
// remove leading and trailing <br /> tags and whitespace
|
|
|
|
chapter = chapter.replace(trimWhitespace, '')
|
|
|
|
return {content: chapter, notes: authorNotes, notesFirst: authorNotesPos < chapterPos}
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
replaceRemoteResources () {
|
|
|
|
if (!this.options.includeExternal) {
|
|
|
|
this.remoteResources.forEach((r, url) => {
|
|
|
|
if (r.originalUrl && r.where) {
|
|
|
|
let ourl = new RegExp(escapeStringRegexp(r.originalUrl), 'g')
|
|
|
|
for (var i = 0; i < r.where.length; i++) {
|
|
|
|
let w = r.where[i]
|
|
|
|
if (typeof w === 'number') {
|
|
|
|
if (ourl.test(this.chapters[w])) {
|
|
|
|
this.storyInfo.chapters[w].remote = true
|
|
|
|
}
|
2018-03-13 10:09:26 +13:00
|
|
|
} else if (w === 'titlepage') {
|
|
|
|
if (ourl.test(this.pages.title)) {
|
2016-08-25 00:47:48 +12:00
|
|
|
this.hasRemoteResources.titlePage = true
|
|
|
|
}
|
2016-08-24 02:32:55 +12:00
|
|
|
}
|
2016-08-23 19:19:01 +12:00
|
|
|
}
|
2016-08-23 07:57:19 +12:00
|
|
|
}
|
2016-08-25 00:47:48 +12:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.remoteResources.forEach((r, url) => {
|
|
|
|
let dest = '../' + r.dest
|
|
|
|
if (r.dest && r.originalUrl && r.where) {
|
|
|
|
let ourl = new RegExp(escapeStringRegexp(r.originalUrl), 'g')
|
|
|
|
for (var i = 0; i < r.where.length; i++) {
|
|
|
|
let w = r.where[i]
|
2016-08-30 02:20:20 +12:00
|
|
|
if (typeof w === 'object' && w.chapter !== undefined && this.chaptersHtml[w.chapter]) {
|
|
|
|
this.chaptersHtml[w.chapter] = this.chaptersHtml[w.chapter].replace(ourl, dest)
|
|
|
|
} else if (typeof w === 'object' && w.note !== undefined && this.notesHtml[w.note]) {
|
|
|
|
this.notesHtml[w.note] = this.notesHtml[w.note].replace(ourl, dest)
|
2018-03-13 10:09:26 +13:00
|
|
|
} else if (w === 'titlepage') {
|
|
|
|
this.pages.title = this.pages.title.replace(ourl, dest)
|
2016-08-25 00:47:48 +12:00
|
|
|
}
|
2016-08-24 08:04:38 +12:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 00:47:48 +12:00
|
|
|
})
|
|
|
|
}
|
2016-08-24 08:04:38 +12:00
|
|
|
}
|
2016-06-28 09:19:01 +12:00
|
|
|
}
|
2016-08-24 08:04:38 +12:00
|
|
|
|
|
|
|
module.exports = FimFic2Epub
|