-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublican.js
291 lines (265 loc) · 11.6 KB
/
publican.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
function exportBody() {
if (!data) {
return null;
}
var toc = table(data.chapters.map(function (chapter, i) {
return tr(td(chapter.name),
td(a({ href: "#chapter" + i }, "view")));
}));
toc.style.width = "100%";
var chapters = data.chapters.map(function (chapter, i) {
return article(
h2(a({ name: "chapter" + i },
chapter.name + " ",
small(a({ href: "#TOC" }, "(top)")))),
chapter.doc
.replace(/\r\n/g, "\n")
.replace(/\n{2,}/g, "\n")
.split(/\n/g).map(function (para) {
return p(para);
}));
});
return [div({ id: "table-of-contents" },
h2(a({ name: "TOC" }, "Table of Contents")),
toc),
div({ id: "chapters" }, chapters)];
}
function exportHTML() {
var doc = html(head(
title("My Book"),
meta({ charset: "utf8" }),
style(
["@page {margin-top: 3cm;margin-bottom:3cm;}",
"@page :left {margin-left: 4cm; margin-right: 3cm;}",
"@page :left :header {content:\"pp. \" decimal(pageno), ,;}",
"@page :left :footer {content:\"pp. \" decimal(pageno), first(chapter),;}",
"@page :right {margin-left: 3cm;margin-right: 4cm;}",
"@page :right :header {content:, , \"pp. \" decimal(pageno);}",
"@page :right :footer {content:, first(chapter), \"pp. \" decimal(pageno);}",
"article{page-break-before:always;}",
"h2{running-head:chapter;}",
"p{text-align:justify}"].join("\n"))),
body(exportBody()));
saveFileToDesktop("book.html", "text/html", utf8_to_b64(doc.outerHTML));
ga('send', 'event', 'report', "publish");
}
function exportEPUB() {
data.title = pubTitle.getValue();
data.authorFirstName = pubAuthFirstName.getValue();
data.authorLastName = pubAuthLastName.getValue();
saveFile();
if (data.title && data.authorFirstName && data.authorLastName) {
ePub();
ga('send', 'event', 'report', "publish");
}
else {
var message = "Before generating a publishable document, please enter a value the following fields:<ul>";
if (!data.title) {
message += "<li>Title</li>";
}
if (!data.authorFirstName) {
message += "<li>Author (first name)</li>";
}
if (!data.authorLastName) {
message += "<li>Author (last name)</li>";
}
message += "</ul>";
msg("publish-error-message", message, 0, forever);
}
}
function ePub() {
var styleFileName = "style";
var navFileName = "epub3toc";
var ncxFileName = "epub2toc";
var coverFileName = "cover";
var fileName = data.title.replace(/ /g, "-");
var zip = new JSZip();
zip.file("mimetype", "application/epub+zip");
zip.file(styleFileName + ".css", "body{}");
zip.file(fileName + ".opf", ePubPackageDoc("pubid", guid(), "en", navFileName, ncxFileName, styleFileName, coverFileName));
zip.file(navFileName + ".xhtml", ePubNavigationDoc(styleFileName, navFileName));
zip.file(ncxFileName + ".ncx", ePub2NCX(guid(), navFileName));
zip.file(coverFileName + ".xhtml", ePubCover());
if(data.pubImage){
zip.file(data.pubImage.name, data.pubImage.data.substring(data.pubImage.data.indexOf("base64,")+"base64,".length), {base64:true});
}
data.chapters.forEach(function (chapter, i) {
var name = chapter.name.replace(/ /g, "-");
zip.file(fmt("chapter$1.xhtml", i + 1), ePubContentDoc(styleFileName, chapter));
});
var metainf = zip.folder("META-INF");
metainf.file("container.xml", ePubContainer(fileName));
var exported = false;
try {
if (window.Blob) {
saveAs(zip.generate({ type: "blob" }), fileName + ".epub");
exported = true;
}
}
catch (err) {
console.error(err);
}
if (!exported) {
saveFileToDesktop(fileName + ".epub", "application/epub+zip", zip.generate({ type: "base64" }));
}
}
function ePubCover(){
return fmt("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
+"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+"\t<head>\n"
+"\t\t<title>Cover</title>\n"
+"\t\t<style type=\"text/css\"> img { max-width: 100%; } </style>\n"
+"\t</head>"
+"\t<body>"
+"\t\t<div id=\"cover-image\">\n"
+"\t\t\t<img src=\"$1\" alt=\"Cover Image\"/>\n"
+"\t\t</div>\n"
+"\t</body>\n"
+"</html>", data.pubImage.name);
}
function ePubNavigationDoc(styleFileName, navFileName) {
var chaptersChunk = data.chapters.map(function (chapter, i) {
return fmt("<li id=\"chapter$1\"><a href=\"chapter$1.xhtml\">$2</a></li>", i + 1, chapter.name);
}).join("\n\t\t\t\t");
return fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE html>\n"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"en\" lang=\"en\">\n"
+ "\t<head>\n"
+ "\t\t<meta charset=\"utf-8\" />\n"
+ "\t\t<title>$1</title>\n"
+ "\t\t<link rel=\"stylesheet\" href=\"$2.css\" type=\"text/css\"/>\n"
+ "\t</head>\n"
+ "\t<body>\n"
+ "\t\t<h1>$1</h1>\n"
+ "\t\t<nav id=\"toc\" epub:type=\"toc\">\n"
+ "\t\t\t<h2>Table of Contents</h2>\n"
+ "\t\t\t<ol>\n"
+ "\t\t\t\t<li id=\"toc\"><a href=\"$3.xhtml\">Table of Contents</a></li>\n"
+ "\t\t\t\t$4\n"
+ "\t\t\t</ol>\n"
+ "\t\t</nav>\n"
+ "\t</body>\n"
+ "</html>", data.title, styleFileName, navFileName, chaptersChunk);
}
function ePub2NCX(ncxUID, navFileName) {
var chaptersChunk = data.chapters.map(function (chapter, i) {
return fmt("<navPoint class=\"h1\" id=\"ncx-$1\"><navLabel><text>$2</text></navLabel><content src=\"chapter$1.xhtml\"/></navPoint>", i + 1, chapter.name);
}).join("\n\t\t");
return fmt("<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\" xml:lang=\"en\">\n"
+ "\t<head><meta name=\"dtb:uid\" content=\"urn:uuid:$1\"/></head>\n"
+ "\t<docTitle><text>$2</text></docTitle>\n"
+ "\t<docAuthor><text>$3 $4</text></docAuthor>\n"
+ "\t<navMap>\n"
+ "\t\t<navPoint class=\"h1\" id=\"ncx-toc\"><navLabel><text>Table of Contents</text></navLabel><content src=\"$5.xhtml\"/></navPoint>\n"
+ "\t\t$6\n"
+ "\t</navMap>\n"
+ "</ncx>",
ncxUID, data.title, data.authorFirstName, data.authorLastName, navFileName, chaptersChunk);
}
function ePubContentDoc(styleFileName, chapter) {
var chapterBody = chapter.doc
.replace(/\r\n/g, "\n")
.replace(/\n{2,}/g, "\n")
.split(/\n/g).map(function (para) {
return p(para).outerHTML;
})
.join("\n\t\t\t");
return fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE html>\n"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"en\" lang=\"en\">\n"
+ "\t<head>\n"
+ "\t\t<meta charset=\"utf-8\" />\n"
+ "\t\t<title>$1: $2</title>\n"
+ "\t\t<link rel=\"stylesheet\" href=\"$3.css\" type=\"text/css\"/>\n"
+ "\t</head>\n"
+ "\t<body>\n"
+ "\t\t<section epub:type=\"chapter\">\n"
+ "\t\t\t<h1 epub:type=\"title\">$2</h1>\n"
+ "\t\t\t$4\n"
+ "\t\t</section>\n"
+ "\t</body>\n"
+ "</html>",
data.title, chapter.name, styleFileName, chapterBody);
}
function ePubPackageDoc(pubID, uuid, lang, navFileName, ncxFileName, styleFileName, coverFileName) {
var now = new Date();
var chapters = data.chapters.map(function (chapter, i) {
return {
item: fmt("<item id=\"chapter$1\" href=\"chapter$1.xhtml\" media-type=\"application/xhtml+xml\"/>", i + 1),
itemref: fmt("<itemref idref=\"chapter$1\" />", i + 1)
};
});
var manifestChapters = chapters.map(function (c) {
return c.item;
}).join("\n\t\t");
var spineChapters = chapters.map(function (c) {
return c.itemref;
}).join("\n\t\t");
return fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<package version=\"3.0\" unique-identifier=\"$1\" xml:lang=\"en\" xmlns=\"http://www.idpf.org/2007/opf\" >\n"
+ "\t<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
+ "\t\t<dc:identifier id=\"$1\">urn:uuid:$2</dc:identifier>\n"
+ "\t\t<dc:title>$3</dc:title>\n"
+ "\t\t<dc:language>$4</dc:language>\n"
+ "\t\t<meta property=\"dcterms:modified\">$5</meta>\n"
+ "\t\t<dc:creator id=\"creator01\">$6 $7</dc:creator>\n"
+ "\t\t<meta refines=\"#creator01\" property=\"file-as\">$7, $6</meta>\n"
+ "\t\t<meta refines=\"#creator01\" property=\"display-seq\">1</meta>\n"
+ "\t\t<meta refines=\"#creator01\" property=\"role\" scheme=\"marc:relators\">aut</meta>\n"
+ (data.pubImage ? "\t\t<meta name=\"cover\" content=\"cover-image\" />\n" : "")
+ "\t\t<dc:date>$5</dc:date>\n"
+ "\t\t<dc:rights>Copyright © $8 $6 $7</dc:rights>\n"
+ "\t</metadata>\n"
+ "\t<manifest>\n"
+ "\t\t<item id=\"epub3nav\" properties=\"nav\" href=\"$9.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
+ "\t\t<item id=\"epub2nav\" href=\"$10.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"
+ "\t\t<item id=\"main-style-sheet\" href=\"$11.css\" media-type=\"text/css\"/>\n"
+ (data.pubImage ? "\t\t<item id=\"cover-image\" href=\"$14\" properties=\"cover-image\" media-type=\"$15\"/>\n" : "")
+ (data.pubImage ? "\t\t<item id=\"cover-page\" href=\"$16.xhtml\" media-type=\"application/xhtml+xml\"/>\n" : "")
+ "\t\t$12\n"
+ "\t</manifest>\n"
+ "\t<spine toc=\"epub2nav\">\n"
+ "\t\t<itemref idref=\"epub3nav\" />\n"
+ (data.pubImage ? "\t\t<itemref idref=\"cover-page\" />\n" : "")
+ "\t\t$13\n"
+ "\t</spine>\n"
+ "</package>",
pubID, uuid, data.title, lang, now.toISOString(), data.authorFirstName, data.authorLastName, now.getFullYear(), navFileName, ncxFileName, styleFileName, manifestChapters, spineChapters, data.pubImage && data.pubImage.name, data.pubImage && data.pubImage.type, coverFileName);
}
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function ePubContainer(pubName) {
return fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
+ "\t<rootfiles>\n"
+ "\t\t<rootfile full-path=\"$1.opf\" media-type=\"application/oebps-package+xml\" />\n"
+ "\t</rootfiles>\n"
+ "</container>\n",
pubName);
}
function loadCoverImage(){
if(pubImage.files.length == 1){
var file = pubImage.files[0];
var reader = new FileReader();
reader.addEventListener("load", function (evt){
pubImageThumb.src = evt.target.result;
data.pubImage = {
data: evt.target.result,
name: file.name,
type: file.type
};
pubImageThumb.style.display = "block";
saveFile();
});
reader.addEventListener("error", console.error);
reader.readAsDataURL(file);
}
else{
pubImageThumb.style.display = "none";
console.log("Help!");
}
}