-
Notifications
You must be signed in to change notification settings - Fork 0
/
iceblerg.js
173 lines (151 loc) · 6.32 KB
/
iceblerg.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
// The MIT License (MIT)
//
// Copyright (c) 2015 Joshua Bemenderfer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/* Disable certain jsHint warnings */
// Allow inline ifs which return null
/* jshint -W030 */
var fse = require('fs-extra');
var extend = require('extend');
var path = require('path');
var modelGenerator = require('./scripts/model-generator');
var defaultOptions = {
'post-dir': './posts',
'template-dir': './templates',
'output-dir': './out',
'post-extensions': ['.md', '.markdown', '.txt'],
'template-extension': '.jade',
'preview-length': 70,
'preview-separator': '==[END PREVIEW]==',
/** Render function used for every file. Override this in the options to
* implement your own template engines and markup renderers.
* @param {string} templatePath - The full path to the template used.
* @param {string} data - An object containing the iceblerg model as well as page-specific data.
* @param {string} type - The page type, eg. postPage, tagPage, authorPage, overviewPage.
* @returns {string} - The generated HTML string.
*/
'render': function(templatePath, data, type) {
try {
// iceblerg.contentRenderer is used to render the body of a post to HTML
data.iceblerg.contentRenderer = require('marked');
// Render the template with Jade using the data supplied.
return require('jade').renderFile(templatePath, data);
} catch (e) {
return e.message;
}
}
};
/**
* Returns a new instance of iceblerg which can be used to generate a blog.
* @param {object} userOptions - Options which can be set by the user. See !TODO!
*/
var iceblerg = function(userOptions) {
this.options = defaultOptions;
extend(true, this.options, userOptions);
/**
* Set the options of the iceblerg instance.
* @param {object} newUserOptions - Options which can be set by the user. See !TODO!
*/
this.setOptions = function(newUserOptions) {
extend(true, this.options, newUserOptions);
};
this.buildModel = function(callback) {
modelGenerator(this.options, function(model) {
callback ? callback(model) : null;
});
};
this.generate = function(model, callback) {
// Build the post pages. (Using byDate is simpler than using Object.keys())
for (var i = 0; i < model.byDate.length; i++) {
var postData = model.posts[model.byDate[i].post];
var outputPath = path.join(this.options['output-dir'], 'posts', postData.path+".html");
// Write the rendered page.
fse.outputFile(outputPath, this.generatePostPage(model, postData));
}
// Build the tag pages.
/* jshint -W004 */
for (var i = 0; i < Object.keys(model.tags).length; i++) {
var tag = Object.keys(model.tags)[i];
var outputPath = path.join(this.options['output-dir'], 'tags', tag+'.html');
fse.outputFile(outputPath, this.generateTagPage(model, tag));
}
// Build the author pages.
for (var i = 0; i < Object.keys(model.authors).length; i++) {
var author = Object.keys(model.authors)[i];
var outputPath = path.join(this.options['output-dir'], 'authors', author+'.html');
fse.outputFile(outputPath, this.generateAuthorPage(model, author));
}
/* jshint +W004 */
// Build the overview page.
fse.outputFile(path.join(this.options['output-dir'], 'main', 'overview.html'),
this.generateOverviewPage(model));
console.log("[iceblerg] Sucessfully Generated Blog. Output: "+this.options['output-dir']);
};
this.generatePostPage = function(model, postData) {
// Set the page data for the model. Modifying the model avoids extra extends
// but is easier to create bugs if we forget to unset it afterwards.
model.page = postData;
return this.options.render(
path.join(
this.options['template-dir'],
"post"+this.options['template-extension']
),
{'iceblerg': model},
'postPage'
);
};
this.generateTagPage = function(model, tagName) {
model.page = {
tag: tagName,
};
return this.options.render(
path.join(
this.options['template-dir'],
"tag"+this.options['template-extension']
),
{'iceblerg': model},
'tagPage'
);
};
this.generateAuthorPage = function(model, authorName) {
model.page = {
author: authorName,
};
return this.options.render(
path.join(
this.options['template-dir'],
"author"+this.options['template-extension']
),
{'iceblerg': model},
'authorPage'
);
};
this.generateOverviewPage = function(model) {
return this.options.render(
path.join(
this.options['template-dir'],
"overview"+this.options['template-extension']
),
{'iceblerg': model},
'overviewPage'
);
};
};
module.exports = iceblerg;