forked from javierbyte/docs2epub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (106 loc) · 3.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var run = require('./src/run')
var _ = require('lodash')
var epub = require('epub-gen')
var fs = require('fs')
var async = require('async')
var slug = require('slug')
var moment = require('moment')
const tocObjToHtml = require('./src/tocObjToHtml')
function getEpubOptions (tocObj) {
return {
title: tocObj.title,
cover: tocObj.cover,
author: tocObj.author,
css: 'code,pre{font-size: 0.9em;background:#fafafa;padding:0.5em;display:block;margin:0.5rem 0}',
content: _.chain(tocObj.content).filter(el => el.result).map(tocEl => {
return {
title: tocEl.title,
data: tocEl.result ? tocEl.result.content : ''
}
}).value()
}
}
function execCommand (command, cb) {
var sys = require('sys')
var exec = require('child_process').exec
function puts (error, stdout, stderr) {
if (error) throw(error)
sys.puts(stdout)
cb()
}
exec(command, puts)
}
// generations trategies
function generateEpubFromHtml (tocArray) {
var epubOptions = getEpubOptions(tocArray)
new epub(epubOptions, 'docs/download/' + strategyToRun + '.epub')
}
function generateHtmlFromHtml (tocArray) {
fs.writeFile('docs/download/' + strategyToRun + '.html', tocObjToHtml(tocArray), (err, res) => {
// console.log({err, res})
})
}
function generateEpubMetadata (docObj) {
return new Promise(function (resolve, reject) {
var epubMeta = `
---
title:
- type: main
text: ${docObj.title}
creator:
- role: author
text: ${docObj.author}
rights: ${docObj.licenceUrl}
include-before: Documentation for <b>${docObj.title}</b> ([${docObj.docsUrl}](${docObj.docsUrl})) generated by <b>docs2epub</b> ([http://javier.xyz/docs2epub/](http://javier.xyz/docs2epub/)) on ${moment().format('YYYY/MM/DD')}.
date: ${moment().format('YYYY/MM/DD')}
description: Documentation for <b>${docObj.title}</b> ([${docObj.docsUrl}](${docObj.docsUrl})) generated by <b>docs2epub</b> ([http://javier.xyz/docs2epub/](http://javier.xyz/docs2epub/)) on ${moment().format('YYYY/MM/DD')}.
...
`
fs.writeFile(`_tmp/epub/${slug(docObj.title)}/meta.txt`, epubMeta, (err, res) => {
if (err) {
reject(err)
return
}
resolve(res)
})
})
}
function generateEpubFromMarkdown (docObj) {
return new Promise(function (resolve, reject) {
execCommand(`rm -rf _tmp/epub/${slug(docObj.title)} && mkdir -p _tmp/epub/${slug(docObj.title)}`, () => {
async.map(docObj.content, (tocEl, cb) => {
if (tocEl.result) {
fs.writeFile(`_tmp/epub/${slug(docObj.title)}/${tocEl.index}.md`, tocEl.result.content, cb)
}
}, (err, res) => {
var pandocCommand = `pandoc -s -o docs/download/${docObj.title.toLowerCase()}.epub _tmp/epub/${slug(docObj.title)}/meta.txt `
pandocCommand += _.map(docObj.content, tocEl => `_tmp/epub/${slug(docObj.title)}/${tocEl.index}.md `).join('')
if (docObj.cover) {
pandocCommand += `--epub-cover-image=${docObj.cover} `
}
if (docObj.epubStylesheet) {
pandocCommand += `--epub-stylesheet=${docObj.epubStylesheet} `
}
if (docObj.epubTOCDepth) {
pandocCommand += `--table-of-contents --toc-depth=${docObj.epubTOCDepth}`
}
generateEpubMetadata(docObj).then(() => {
execCommand(pandocCommand, () => {
console.log('Done.')
})
}, reject)
})
})
})
}
var strategyToRun = 'react'
run(strategyToRun).then(tocArray => {
if (tocArray.type === 'MARKDOWN') {
generateEpubFromMarkdown(tocArray).catch(err => console.log(err))
} else {
generateEpubFromHtml(tocArray)
generateHtmlFromHtml(tocArray)
}
}).catch(err => {
console.log(err.stack)
})