forked from vkarpov15/mastering-async-await
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.js
More file actions
173 lines (147 loc) · 5.53 KB
/
compile.js
File metadata and controls
173 lines (147 loc) · 5.53 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const Epub = require('epub-gen');
const acquit = require('acquit');
const archiver = require('archiver');
const cheerio = require('cheerio');
const highlight = require('highlight.js');
const fs = require('fs');
const marked = require('marked');
const nightmare = require('nightmare');
const transform = require('acquit-require');
require('acquit-ignore')();
marked.setOptions({
highlight: function(code) {
return highlight.highlight('JavaScript', code).value;
}
});
run().catch(error => console.error(error.stack));
async function run() {
const blank = fs.readFileSync('./content/blank.html', 'utf8');
const cover = fs.readFileSync('./content/cover.html', 'utf8');
const dedication = fs.readFileSync('./content/dedication.html', 'utf8');
const intro = fs.readFileSync('./content/intro.md', 'utf8');
const outro = fs.readFileSync('./content/outro.md', 'utf8');
const toc = fs.readFileSync('./content/toc.md', 'utf8');
const examples = [1, 2, 3, 4].
map(c => fs.readFileSync(`./examples/chapter${c}.test.js`, 'utf8').toString());
const chapters = [1, 2, 3, 4].
map(c => fs.readFileSync(`./content/chapter${c}.md`, 'utf8').toString()).
map((c, i) => transform(c, examples[i]));
await compileEpub(intro, chapters, outro);
const css = {
content: fs.readFileSync('./content/content.css', 'utf8'),
cover: fs.readFileSync('./content/cover.css', 'utf8'),
dedication: fs.readFileSync('./content/dedication.css', 'utf8')
};
const coverHtml = `
<link href='http://fonts.googleapis.com/css?family=Titillium+Web' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>
<style>
${css.cover}
</style>
<div id="content">
${cover}
</div>
`;
const contentHtml = `
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Titillium+Web' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
${css.content}
</style>
</head>
<body>
<div class="toc">
${marked(toc)}
</div>
<div class="chapter">
${marked(intro)}
</div>
<div class="chapter">
${marked(chapters[0])}
</div>
<div class="chapter">
${marked(chapters[1])}
</div>
<div class="chapter">
${marked(chapters[2])}
</div>
<div class="chapter">
${marked(chapters[3])}
</div>
<div class="chapter">
${marked(outro)}
</div>
</body>
</html>
`;
const dedicationHtml = `
<link href='http://fonts.googleapis.com/css?family=Titillium+Web' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>
<style>
${css.dedication}
</style>
<div id="content">
${dedication}
</div>
`;
fs.writeFileSync('./bin/cover.html', coverHtml);
fs.writeFileSync('./bin/content.html', contentHtml);
fs.writeFileSync('./bin/dedication.html', dedicationHtml);
let browser = nightmare({ show: false });
await browser.goto(`file://${__dirname}/bin/cover.html`).
pdf('./bin/cover.pdf', { marginsType: 0 });
await browser.goto(`file://${__dirname}/bin/content.html`).
pdf('./bin/content.pdf', { marginsType: 0 });
await browser.goto(`file://${__dirname}/bin/dedication.html`).
pdf('./bin/dedication.pdf', { marginsType: 0 });
console.log('Done');
process.exit(0);
}
async function compileEpub(intro, chapters, conclusion) {
intro = marked(stripFirstLine(intro));
chapters = chapters.
map(stripFirstLine).
map(ch => marked(ch));
conclusion = marked(stripFirstLine(conclusion));
chapters[1] = chapters[1].replace(/<svg[\s\S]+<\/svg>/m,
'<img src="https://i.imgur.com/wemS4Ws.png" />');
chapters[2] = chapters[2].replace('../images/flow.png',
'https://i.imgur.com/UyRLFTS.jpg');
for (let i = 0; i < chapters.length; ++i) {
const $ = cheerio.load(chapters[i]);
$('.example-header-wrap').next('pre').children().
append(i => `<div class="example-footer">${$('.example-header').eq(i).html()}</div>`);
chapters[i] = $.html();
}
const options = {
title: 'Mastering Async/Await',
author: 'Valeri Karpov',
output: `${process.cwd()}/bin/mastering-async-await.epub`,
cover: `${process.cwd()}/images/cover.jpg`,
content: [
{ title: 'How To Use This Book', data: intro },
{ title: 'Async/Await: The Good Parts', data: chapters[0] },
{ title: 'Promises From The Ground Up', data: chapters[1] },
{ title: 'Async/Await Internals', data: chapters[2] },
{ title: 'Async/Await in the Wild', data: chapters[3] },
{ title: 'Moving On', data: conclusion }
],
css: fs.readFileSync('./content/epub.css', 'utf8')
};
await new Epub(options).promise;
}
function stripFirstLine(str) {
const firstNewLine = str.indexOf('\n');
if (firstNewLine === -1) {
return '';
}
return str.substr(firstNewLine + 1);
}