1 | function bookify() {
|
2 | const fsp = require('fs-promise');
|
3 | const path = require('path');
|
4 | const chalk = require('chalk');
|
5 | const wc = require('wordcount');
|
6 |
|
7 | const headerTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
|
8 |
|
9 | let book = {};
|
10 | let index = {};
|
11 |
|
12 | let page_count = 1;
|
13 |
|
14 |
|
15 | fsp.readJson(path.join('.', 'interim', 'tmp', '.prebook'))
|
16 | .then((prebook) => {
|
17 |
|
18 | page_count = parseInt(prebook.START_PAGE);
|
19 |
|
20 | var html = ''; // Assign p tags to it.
|
21 | var wordcount = 0;
|
22 | var linecount = 0;
|
23 | var indexer = 0;
|
24 |
|
25 |
|
26 | for (var key in prebook) {
|
27 | if (prebook.hasOwnProperty(key)) {
|
28 |
|
29 | let elem = prebook[key];
|
30 |
|
31 | for (var tag in elem) {
|
32 | if (elem.hasOwnProperty(tag)) {
|
33 |
|
34 | if (tag === 'img') {
|
35 | let image = '<' + tag + ' width = "100%" src = "' + elem[tag] + '" />';
|
36 | book[`page-${ page_count }`] = image;
|
37 | page_count += 1;
|
38 |
|
39 |
|
40 | } else if (headerTags.indexOf(tag) > -1) {
|
41 |
|
42 | if (html !== '') {
|
43 | book[`page-${ page_count }`] = html;
|
44 | page_count += 1;
|
45 | html = '';
|
46 | wordcount = 0;
|
47 | linecount = 0;
|
48 | }
|
49 |
|
50 |
|
51 | let heading = '<' + tag + '>' + elem[tag] + '</' + tag + '>';
|
52 | book[`page-${ page_count }`] = heading;
|
53 |
|
54 | if (tag === 'h3' || tag === 'h2' && elem[tag].substr(0, 5).trim() === 'CHAPT') {
|
55 |
|
56 | index[indexer] = `<li><a class = "page" href="${page_count}"> ${elem[tag]} <span>${page_count}</span></a> </li>`;
|
57 | indexer++;
|
58 | }
|
59 |
|
60 | page_count += 1;
|
61 |
|
62 |
|
63 |
|
64 | } else if (tag === 'p') {
|
65 |
|
66 | const LOWER_WORD_LIMIT = 120; // Full page
|
67 | const UPPER_WORD_LIMIT = 160; // Full page 16 lines = ideal.
|
68 | const LOWER_LINE_LIMIT = 18;
|
69 | const UPPER_LINE_LIMIT = 22; // 20 is ideal.
|
70 | // const MAX_CHAR_LIMIT = 850; // Based on visual readability tests
|
71 |
|
72 | let newcount = wc(elem[tag]);
|
73 | let newlines = Math.round(newcount / 7) + 1;
|
74 |
|
75 | if (wordcount + newcount <= LOWER_WORD_LIMIT) {
|
76 |
|
77 | html += '<' + tag + '>' + elem[tag] + '</' + tag + '>';
|
78 |
|
79 | wordcount += newcount;
|
80 |
|
81 | linecount += Math.round(newcount / 7) + 1;
|
82 |
|
83 |
|
84 | // if (linecount + newlines <= LOWER_LINE_LIMIT) {
|
85 |
|
86 | // html += '<' + tag + '>' + elem[tag] + '</' + tag + '>';
|
87 |
|
88 | // wordcount += newcount;
|
89 |
|
90 | // linecount += Math.round(newcount / 7) + 1;
|
91 |
|
92 | // } else if (linecount + newlines > LOWER_LINE_LIMIT) {
|
93 | // if (linecount + newlines <= UPPER_LINE_LIMIT) {
|
94 |
|
95 | // html += '<' + tag + '>' + elem[tag] + '</' + tag + '>';
|
96 |
|
97 | // wordcount += newcount;
|
98 |
|
99 | // linecount += Math.round(newcount / 7) + 1;
|
100 |
|
101 | // book[`page-${ page_count }`] = html;
|
102 |
|
103 | // page_count += 1;
|
104 |
|
105 | // html = '';
|
106 |
|
107 | // wordcount = 0;
|
108 |
|
109 | // linecount = 0;
|
110 |
|
111 | // } else if (linecount + newlines > UPPER_LINE_LIMIT) {
|
112 |
|
113 | // book[`page-${ page_count }`] = html;
|
114 |
|
115 | // page_count += 1;
|
116 |
|
117 | // html = '<' + tag + '>' + elem[tag] + '</' + tag + '>';
|
118 |
|
119 | // wordcount = wc(html);
|
120 |
|
121 | // linecount = Math.round(wordcount / 7) + 1;
|
122 |
|
123 |
|
124 | // }
|
125 |
|
126 | // }
|
127 |
|
128 |
|
129 |
|
130 | } else if (wordcount + newcount > LOWER_WORD_LIMIT) {
|
131 | if (wordcount + newcount <= UPPER_WORD_LIMIT) {
|
132 |
|
133 | html += '<' + tag + '>' + elem[tag] + '</' + tag + '>';
|
134 |
|
135 | wordcount += newcount;
|
136 |
|
137 | linecount += Math.round(newcount / 7) + 1;
|
138 |
|
139 | book[`page-${ page_count }`] = html;
|
140 |
|
141 | page_count += 1;
|
142 |
|
143 | html = '';
|
144 |
|
145 | wordcount = 0;
|
146 |
|
147 | linecount = 0;
|
148 |
|
149 |
|
150 | } else if (wordcount + newcount > UPPER_WORD_LIMIT) {
|
151 |
|
152 | let availableRoom = 160 - wordcount;
|
153 |
|
154 | if (elem[tag].indexOf('.') !== -1) {
|
155 | let nearestDot = elem[tag].indexOf('.', availableRoom);
|
156 |
|
157 | let leftPartSentence = elem[tag].substr(0, nearestDot + 1);
|
158 |
|
159 | html += '<' + tag + '>' + leftPartSentence + '</' + tag + '>';
|
160 |
|
161 | book[`page-${ page_count }`] = html;
|
162 |
|
163 | page_count += 1;
|
164 |
|
165 | html = '<' + tag + '>' + elem[tag].substr(nearestDot + 1) + '</' + tag + '>';
|
166 |
|
167 | wordcount = wc(elem[tag].substr(nearestDot + 1));
|
168 |
|
169 | linecount = Math.round(wordcount / 7) + 1;
|
170 |
|
171 | } else {
|
172 |
|
173 | let wordsArray = elem[tag].split(' ');
|
174 |
|
175 | let leftPartSentence = wordsArray.slice(0, availableRoom).join(' ');
|
176 |
|
177 | let rightPartSentence = wordsArray.slice(availableRoom).join(' ');
|
178 |
|
179 | html += '<' + tag + '>' + leftPartSentence + '</' + tag + '>';
|
180 |
|
181 | book[`page-${ page_count }`] = html;
|
182 |
|
183 | page_count += 1;
|
184 |
|
185 | html = '<' + tag + '>' + rightPartSentence + '</' + tag + '>';
|
186 |
|
187 | wordcount = wc(rightPartSentence);
|
188 |
|
189 | linecount = Math.round(wordcount / 7) + 1;
|
190 |
|
191 | }
|
192 | }
|
193 | }
|
194 | }
|
195 | }
|
196 | }
|
197 | }
|
198 | }
|
199 |
|
200 | function createPage(html, page_count, carryOverHtml) {
|
201 |
|
202 | book[`page-${ page_count }`] = html;
|
203 |
|
204 | page_count += 1;
|
205 |
|
206 | html = carryOverHtml;
|
207 | wordcount = 0;
|
208 | charcount = 0;
|
209 | linecount = 0;
|
210 |
|
211 |
|
212 | }
|
213 |
|
214 | }).then(() => {
|
215 | fsp.writeFile(path.join('.', 'interim', 'tmp', '.book'), JSON.stringify(book, null, 2))
|
216 | .then(() => {
|
217 | console.log(chalk.green(`Bookification… (.book) is ${chalk.blue('complete')}`));
|
218 | }).catch((err) => {
|
219 | if (err)
|
220 | return console.log(chalk.bold.red('Failed to write .book json', err));
|
221 | });
|
222 | }).then(() => {
|
223 | fsp.writeFile(path.join('.', 'interim', 'tmp', '.index'), JSON.stringify(index, null, 2))
|
224 | .then(() => {
|
225 | console.log(chalk.green(`book.index) is ${chalk.blue('ready')}`));
|
226 | }).catch((err) => {
|
227 | if (err)
|
228 | return console.log(chalk.bold.red('Failed to write index json', err));
|
229 | });
|
230 |
|
231 | }).catch((err) => {
|
232 | if (err)
|
233 | console.log(chalk.red('Could not read .book json', err));
|
234 | });
|
235 |
|
236 | }
|
237 |
|
238 | module.exports.bookify = bookify;
|