forked from jdan/cleaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaver.js
336 lines (280 loc) · 7.89 KB
/
cleaver.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
var Q = require('q');
var debug = require('debug')('cleaver');
var path = require('path');
var marked = require('marked');
var hljs = require('highlight.js');
var yaml = require('js-yaml');
var mustache = require('mustache');
var helper;
/**
* Constructor accepts a document (as a string) and a path from which to
* include external dependencies (stylesheets, scripts, etc)
*
* @param {string} document The input document
* @param {!string} includePath Optional resource path (default: '.')
* @constructor
*/
function Cleaver(document, includePath) {
this.document = document.toString();
this.path = path.resolve(includePath || '.');
helper = require('./helper')(this.path);
this.templates = {
layout: 'templates/layout.mustache',
author: 'templates/author.mustache',
slides: 'templates/default.mustache'
};
this.resources = {
style: 'resources/default.css',
githubStyle: 'resources/github.css',
script: 'resources/script.js'
};
this.external = {
style: []
};
this.metadata = null;
this.slides = [];
this.override = false;
marked.setOptions({
gfm: true,
highlight: function (code, lang) {
return (lang) ? hljs.highlight(lang, code).value : code;
}
});
}
/**
* Loads the document and required templates.
*
* @return {Promise}
*/
Cleaver.prototype.loadDocument = function () {
return Q.all([
helper.load(this.external, { external: true }).then(function () {
debug('loaded input document');
}),
helper.load(this.templates).then(function () {
debug('loaded templates');
})
]);
};
/**
* Parses the metadata and renders the slides.
*
* @return {Promise}
*/
Cleaver.prototype.renderSlides = function () {
var slices = this.slice(this.document);
var i;
this.metadata = yaml.safeLoad(slices[0].content) || {};
debug('parsed metadata');
for (i = 1; i < slices.length; i++) {
this.slides.push({
id: i,
hidden: i !== 1, // first slide is visible
classList: slices[i].classList,
content: this.renderSlide(slices[i].content)
});
}
// insert an author slide (if necessary) at the end
if (this.metadata.author) {
if (this.metadata.author.twitter &&
!this.metadata.author.twitter.match(/^@/)) {
this.metadata.author.twitter = '@' + this.metadata.author.twitter;
}
this.slides.push({
id: i,
hidden: true,
content: this.renderAuthorSlide(this.metadata.author)
});
}
return Q.resolve(true);
};
/**
* Populates `slides` and some extra loaded content, based on the metadata
* listed in the document.
*
* @return {Promise}
*/
Cleaver.prototype.populateResources = function () {
var promises = [];
// maybe load an external stylesheet
if (this.metadata.style) {
promises.push(helper.populateSingle(this.metadata.style, this.external, 'style')
.then(function () {
debug('loaded user stylesheet');
}));
}
// maybe load an external template
if (this.metadata.template) {
promises.push(helper.populateSingle(this.metadata.template, this.templates, 'slides')
.then(function () {
debug('loaded user template');
}));
}
// maybe load an external layout
if (this.metadata.layout) {
promises.push(helper.populateSingle(this.metadata.layout, this.templates, 'layout')
.then(function () {
debug('loaded user layout');
}));
}
return Q.all(promises);
};
/**
* Populates a given theme's resources. Occurs _after_ populateResources.
*
* @return {Promise}
*/
Cleaver.prototype.populateThemeResources = function () {
// maybe load a theme
if (this.metadata.theme) {
return helper.loadTheme(this.metadata.theme, this).then(function () {
debug('loaded theme');
});
}
};
/**
* Loads the templates, default stylesheet, and any external stylesheets
* as defined by the user.
*
* @return {Promise.<Array.<Object>>}
*/
Cleaver.prototype.loadStaticAssets = function () {
return Q.all([
helper.load(this.resources)
]).then(function () {
debug('loaded static assets');
});
};
/**
* Renders the slideshow.
*
* @return {Promise}
*/
Cleaver.prototype.renderSlideshow = function () {
var putControls = this.metadata.controls || (this.metadata.controls === undefined);
var putProgress = this.metadata.progress || (this.metadata.progress === undefined);
var style, script, output;
// Render the slides in a template (maybe as specified by the user)
var slideshow = mustache.render(this.templates.loaded.slides, {
slides: this.slides,
controls: putControls,
progress: putProgress
});
debug('rendered slides');
// TODO: handle defaults gracefully
var title = this.metadata.title || 'Untitled';
var encoding = this.metadata.encoding || 'utf-8';
if (this.override && this.external.loaded.style) {
style = this.external.loaded.style && this.external.loaded.style.join('\n');
} else {
style = [
this.resources.loaded.style,
this.resources.loaded.githubStyle,
// External styles are represented with an array
this.external.loaded.style && this.external.loaded.style.join('\n')
].join('\n');
}
if (this.override && this.external.loaded.script) {
script = this.external.loaded.script;
} else {
script = [
this.resources.loaded.script,
this.external.loaded.script
].join('\n');
}
var layoutData = {
slideshow: slideshow,
title: title,
encoding: encoding,
style: style,
author: this.metadata.author,
script: script,
metadata: this.metadata
};
/* Return the rendered slideshow */
output = mustache.render(this.templates.loaded.layout, layoutData);
debug('rendered presentation');
return output;
};
/**
* Renders a slide.
*
* @param {string} content Markdown content to be rendered as a slide
* @return {string} The formatted slide
*/
Cleaver.prototype.renderSlide = function (content) {
return marked(content);
};
/**
* Renders the author slide.
*
* @param {string} content The author field of the slideshow metadata
* @return {string} The formatted author slide
*/
Cleaver.prototype.renderAuthorSlide = function (content) {
return mustache.render(this.templates.loaded.author, content);
};
/**
* Returns a chopped up document that's easy to parse.
*
* @param {string} The full document
* @return {Array.<string>} A list of all slides
*/
Cleaver.prototype.slice = function(document) {
var cuts = document.split(/\n(?=\-\-)/);
var slices = [];
var nlIndex;
for (var i = 0; i < cuts.length; i++) {
/**
* The first slide does not get the following treatment, so we just
* add it as content.
*
* Otherwise, we slice off the `--` at the beginning.
*/
if (!cuts[i].match(/^--/)) {
slices.push({
content: cuts[i].trim()
});
continue;
} else {
/* If we leave out metadata, add an empty slide at the beginning */
if (i === 0) {
slices.push({ content: '' });
}
cuts[i] = cuts[i].slice(2);
}
/**
* Slices at this point will contain class names, followed by several
* newlines
*/
nlIndex = cuts[i].indexOf('\n');
if (nlIndex === -1) {
// Just to be safe...
nlIndex = 0;
}
/* Push the classList and markdown content */
slices.push({
classList: cuts[i].slice(0, nlIndex),
content: cuts[i].slice(nlIndex).trim()
});
}
return slices;
};
/**
* Method to run the whole show.
*
* @return {Promise}
*/
Cleaver.prototype.run = function () {
var self = this;
// Load document -> Parse Metadata / Render Slides -> Populate Resources
var documentChain = this.loadDocument()
.then(self.renderSlides.bind(self))
.then(self.populateThemeResources.bind(self))
.then(self.populateResources.bind(self));
// Load static assets
var assetChain = this.loadStaticAssets();
return Q.all([documentChain, assetChain])
.then(self.renderSlideshow.bind(self));
};
module.exports = Cleaver;