2016-08-15 08:42:57 +12:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-03-14 02:01:18 +13:00
|
|
|
const args = require('commander')
|
2018-03-14 04:23:45 +13:00
|
|
|
.command('fimfic2epub <story> [filename]')
|
2018-03-14 02:01:18 +13:00
|
|
|
.description(require('../package.json').description)
|
|
|
|
.version(require('../package.json').version)
|
2018-03-14 04:23:45 +13:00
|
|
|
.option('-d, --dir <path>', 'Directory to store ebook in. Is prepended to filename')
|
2018-03-14 02:01:18 +13:00
|
|
|
.option('-t, --title <value>', 'Set the title of the story')
|
|
|
|
.option('-a, --author <value>', 'Set the author of the story')
|
|
|
|
.option('-c, --no-comments-link', 'Don\'t add link to online comments')
|
|
|
|
.option('-h, --no-headings', 'Don\'t add headings to chapters')
|
|
|
|
.option('-e, --no-external', 'Don\'t embed external resources, such as images (breaks EPUB spec)')
|
|
|
|
.option('-n, --no-notes', 'Don\'t include author notes')
|
|
|
|
.option('-i, --notes-index', 'Create an index with all author notes at the end of the ebook')
|
|
|
|
.option('-p, --paragraphs <style>', 'Select a paragraph style [spaced|indented|indentedall|both]', 'spaced')
|
|
|
|
.option('-j, --join-subjects', 'Join subjects to a single value')
|
|
|
|
.option('-r, --reading-ease', 'Calculate Flesch reading ease (slow for long stories)')
|
|
|
|
.option('-C, --cover <url>', 'Set cover image url')
|
|
|
|
.parse(process.argv)
|
|
|
|
|
|
|
|
if (args.args.length < 1) {
|
|
|
|
console.error('Error: No story id/url provided')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:03:58 +13:00
|
|
|
// use a mock DOM so we can run mithril on the server
|
|
|
|
require('mithril/test-utils/browserMock')(global)
|
2017-10-19 02:02:07 +13:00
|
|
|
|
2018-03-13 10:03:58 +13:00
|
|
|
const FimFic2Epub = require('../dist/fimfic2epub')
|
2016-08-15 21:11:20 +12:00
|
|
|
const fs = require('fs')
|
2018-03-14 04:23:45 +13:00
|
|
|
const path = require('path')
|
2016-08-15 08:42:57 +12:00
|
|
|
|
2018-03-14 02:01:18 +13:00
|
|
|
const STORY_ID = args.args[0]
|
2016-08-15 08:42:57 +12:00
|
|
|
|
2018-03-14 02:01:18 +13:00
|
|
|
const ffc = new FimFic2Epub(STORY_ID, {
|
|
|
|
addCommentsLink: !!args.commentsLink,
|
|
|
|
includeAuthorNotes: !!args.notes,
|
|
|
|
useAuthorNotesIndex: !!args.notesIndex,
|
|
|
|
addChapterHeadings: !!args.headings,
|
|
|
|
includeExternal: !!args.external,
|
|
|
|
paragraphStyle: args.paragraphs,
|
|
|
|
joinSubjects: !!args.joinSubjects,
|
|
|
|
calculateReadingEase: !!args.readingEase
|
|
|
|
})
|
|
|
|
ffc.coverUrl = args.cover
|
2016-08-15 08:42:57 +12:00
|
|
|
|
2018-03-14 02:01:18 +13:00
|
|
|
const outputStdout = args.args[1] === '-' || args.args[1] === '/dev/stdout'
|
2016-08-15 21:11:20 +12:00
|
|
|
|
|
|
|
if (outputStdout) {
|
|
|
|
console.log = console.error
|
|
|
|
console.log('Outputting to stdout')
|
|
|
|
}
|
|
|
|
|
2018-03-14 02:01:18 +13:00
|
|
|
/*
|
2016-08-25 00:47:48 +12:00
|
|
|
ffc.on('progress', (percent, status) => {
|
|
|
|
if (status) {
|
|
|
|
console.log('fimfic2epub: ' + status)
|
|
|
|
}
|
|
|
|
})
|
2018-03-14 02:01:18 +13:00
|
|
|
*/
|
2016-08-25 00:47:48 +12:00
|
|
|
|
2018-03-14 02:01:18 +13:00
|
|
|
ffc.fetchMetadata()
|
|
|
|
.then(() => {
|
|
|
|
if (args.title) {
|
|
|
|
ffc.setTitle(args.title)
|
|
|
|
}
|
|
|
|
if (args.author) {
|
|
|
|
ffc.setAuthorName(args.author)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(ffc.fetchAll.bind(ffc))
|
2016-08-24 02:32:55 +12:00
|
|
|
.then(ffc.build.bind(ffc))
|
2016-08-15 21:11:20 +12:00
|
|
|
.then(() => {
|
2018-03-14 02:01:18 +13:00
|
|
|
let filename = (args.args[1] || '').replace('%id%', ffc.storyInfo.id) || ffc.filename
|
2016-08-15 21:11:20 +12:00
|
|
|
let stream
|
|
|
|
|
2018-03-14 04:23:45 +13:00
|
|
|
if (args.dir) {
|
|
|
|
filename = path.join(args.dir, filename)
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:11:20 +12:00
|
|
|
if (outputStdout) {
|
|
|
|
stream = process.stdout
|
|
|
|
} else {
|
|
|
|
stream = fs.createWriteStream(filename)
|
|
|
|
}
|
2017-10-19 02:02:07 +13:00
|
|
|
ffc.streamFile(null)
|
2016-08-15 21:11:20 +12:00
|
|
|
.pipe(stream)
|
|
|
|
.on('finish', () => {
|
|
|
|
if (!outputStdout) {
|
|
|
|
console.log('Saved story as ' + filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2016-08-30 02:20:20 +12:00
|
|
|
if (err && err.stack) {
|
|
|
|
console.error(err.stack)
|
|
|
|
} else {
|
|
|
|
console.error('Error: ' + (err || 'Unknown error'))
|
|
|
|
}
|
2016-08-15 21:11:20 +12:00
|
|
|
process.exit(1)
|
2016-08-15 08:42:57 +12:00
|
|
|
})
|