-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagemaki.js
179 lines (140 loc) · 4.35 KB
/
pagemaki.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
var fs = require("fs");
var yaml = require("js-yaml");
var _ = require("lodash");
var path = require("path");
var marked = require("marked");
marked.setOptions({
gfm: true,
tables: true,
breaks: true,
highlight: function (code) {
return require('highlight.js').highlightAuto(code).value;
}
})
/**
* Defaults object
*
*/
var defaults = {
optionsCheck: true,
optionsDelimiter: "---",
optionsParse: function (string) {
return yaml.safeLoad(string);
},
contentParse: function (string, ext) {
if (typeof ext === "string" && (ext.toLowerCase() === "markdown" || ext.toLowerCase() === "md")) {
return marked(string);
} else {
return string;
}
},
templatesDir: path.join(process.cwd(), "src", "layouts"),
templateCompile: function (string) {
return _.template(string);
},
templateData: {}
};
/**
* Constructor for the Pagemaki class
*
* @param {[type]} config [description]
*/
var Pagemaki = function (config) {
this.globals = config.globals || {};
this.templates = {};
this.config = _.extend({}, defaults, config);
this.config.optionsRegex = new RegExp("^" + this.config.optionsDelimiter + "([\\S\\s]+)" + this.config.optionsDelimiter + "([\\S\\s]+)");
};
/**
* Find and return the template string to be compiled
*
* Note: this can be overridden by passing a 'getTemplateString'
* function to the Pagemaki constructor, esp for testing
*
* @param {[type]} name [description]
* @return {[type]} [description]
*/
Pagemaki.prototype.getTemplateString = function (name) {
return fs.readFileSync(path.join(this.config.templatesDir, name + ".html")).toString();
}
/**
* Get a cached template by name, or from disk and then cache for later
*
* @param {[type]} name [description]
* @return {[type]} [description]
*/
Pagemaki.prototype.getTemplate = function (name) {
var getTemplateString = (this.config.getTemplateString || this.getTemplateString).bind(this);
if (!this.templates[name]) {
this.templates[name] = this.config.templateCompile(getTemplateString(name).trim());
}
return this.templates[name];
};
/**
* Parse out the string and look for options, content
*
* @param {Object} input File object with string 'contents' and optional extension
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Pagemaki.prototype.parse = function (input, callback) {
if (!input) {
callback(new Error("input.contents is required"));
return;
}
if (typeof input.contents !== "string") {
callback(new Error("input.contents must be a string, instead " + typeof input.contents));
return;
}
var data = {};
var matches = this.config.optionsCheck && this.config.optionsRegex.exec(input.contents);
if (matches) {
data.options = this.config.optionsParse(matches[1]);
data.content = this.config.contentParse(matches[2].trim(), input.extension);
} else {
data.options = {};
data.content = this.config.contentParse(input.contents.trim(), input.extension);
}
// Merge in global config values to be available in all templates
data.options = _.defaults({}, data.options, this.globals);
callback(null, data);
};
/**
* Use the rendering function to create static string
*
* @param {[type]} err [description]
* @param {[type]} parsed [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Pagemaki.prototype.render = function (err, parsed, callback) {
if (err) {
callback(err);
return;
}
if (!parsed.content) {
callback(null, "");
return;
}
var layout = (parsed.options && parsed.options.layout) || "default";
var render = this.getTemplate(layout);
parsed.content = this.config.templateCompile(parsed.content)(parsed.options);
this.config.templateData.page = parsed;
this.config.templateData.site = parsed.options.site;
callback(null, render(this.config.templateData).trim());
};
/**
* Putting it all together using the parsed options, content,
* and templates to compile static HTML
*
* @param {Object} input [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Pagemaki.prototype.make = function (input, callback) {
var self = this;
this.parse(input, function (err, parsed) {
self.render(err, parsed, callback);
});
};
module.exports = Pagemaki;