forked from jashkenas/journo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
journo.js
346 lines (309 loc) · 9.9 KB
/
journo.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
337
338
339
340
341
342
343
344
345
346
// Generated by CoffeeScript 1.6.1
(function() {
var Highlight, Journo, catchErrors, detectDescription, detectTitle, exec, fatal, folderContents, fs, htmlPath, loadConfig, loadLayout, loadManifest, manifestPath, mapLink, marked, path, postName, postPath, postUrl, renderVariables, rsync, shared, sortedPosts, spawn, updateManifest, _, _ref,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Journo = module.exports = {};
marked = require('marked');
_ = require('underscore');
shared = {};
Journo.render = function(post, source) {
return catchErrors(function() {
var content, markdown, title, variables;
loadLayout();
source || (source = fs.readFileSync(postPath(post)));
variables = renderVariables(post);
markdown = _.template(source.toString())(variables);
title = detectTitle(markdown);
content = marked.parser(marked.lexer(markdown));
return shared.layout(_.extend(variables, {
title: title,
content: content
}));
});
};
loadLayout = function(force) {
var layout;
if (!force && (layout = shared.layout)) {
return layout;
}
return shared.layout = _.template(fs.readFileSync('layout.html').toString());
};
fs = require('fs');
path = require('path');
_ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
Journo.build = function() {
loadManifest();
if (!fs.existsSync('site')) {
fs.mkdirSync('site');
}
return exec("rsync -vur --delete public/ site", function(err, stdout, stderr) {
var file, html, post, _i, _len, _ref1;
if (err) {
throw err;
}
_ref1 = folderContents('posts');
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
post = _ref1[_i];
html = Journo.render(post);
file = htmlPath(post);
if (!fs.existsSync(path.dirname(file))) {
fs.mkdirSync(path.dirname(file));
}
fs.writeFileSync(file, html);
}
return fs.writeFileSync("site/feed.rss", Journo.feed());
});
};
loadConfig = function() {
var err;
if (shared.config) {
return;
}
try {
shared.config = JSON.parse(fs.readFileSync('config.json'));
} catch (_error) {
err = _error;
fatal("Unable to read config.json");
}
return shared.siteUrl = shared.config.url.replace(/\/$/, '');
};
Journo.publish = function() {
Journo.build();
return rsync('site/images/', path.join(shared.config.publish, 'images/'), function() {
return rsync('site/', shared.config.publish);
});
};
rsync = function(from, to, callback) {
var child, port;
port = "ssh -p " + (shared.config.publishPort || 22);
child = spawn("rsync", ['-vurz', '--delete', '-e', port, from, to]);
child.stdout.on('data', function(out) {
return console.log(out.toString());
});
child.stderr.on('data', function(err) {
return console.error(err.toString());
});
if (callback) {
return child.on('exit', callback);
}
};
manifestPath = 'journo-manifest.json';
loadManifest = function() {
loadConfig();
shared.manifest = fs.existsSync(manifestPath) ? JSON.parse(fs.readFileSync(manifestPath)) : {};
updateManifest();
return fs.writeFileSync(manifestPath, JSON.stringify(shared.manifest));
};
updateManifest = function() {
var content, entry, manifest, post, posts, stat, _i, _len;
manifest = shared.manifest;
posts = folderContents('posts');
for (post in manifest) {
if (__indexOf.call(posts, post) < 0) {
delete manifest[post];
}
}
for (_i = 0, _len = posts.length; _i < _len; _i++) {
post = posts[_i];
stat = fs.statSync(postPath(post));
entry = manifest[post];
if (!entry || entry.mtime !== stat.mtime) {
entry || (entry = {
pubtime: stat.ctime
});
entry.mtime = stat.mtime;
content = fs.readFileSync(postPath(post)).toString();
entry.title = detectTitle(content);
entry.description = detectDescription(content, post);
manifest[post] = entry;
}
}
return true;
};
Highlight = require('highlight').Highlight;
marked.setOptions({
highlight: function(code, lang) {
return Highlight(code);
}
});
Journo.feed = function() {
var RSS, config, entry, feed, post, _i, _len, _ref1;
RSS = require('rss');
loadConfig();
config = shared.config;
feed = new RSS({
title: config.title,
description: config.description,
feed_url: "" + shared.siteUrl + "/rss.xml",
site_url: shared.siteUrl,
author: config.author
});
_ref1 = sortedPosts().slice(0, 20);
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
post = _ref1[_i];
entry = shared.manifest[post];
feed.item({
title: entry.title,
description: entry.description,
url: postUrl(post),
date: entry.pubtime
});
}
return feed.xml();
};
Journo.init = function() {
var bootstrap, here;
here = fs.realpathSync('.');
if (fs.existsSync('posts')) {
fatal("A blog already exists in " + here);
}
bootstrap = path.join(__dirname, 'bootstrap/*');
return exec("rsync -vur --delete " + bootstrap + " .", function(err, stdout, stderr) {
if (err) {
throw err;
}
return console.log("Initialized new blog in " + here);
});
};
Journo.preview = function() {
var http, mime, server, url, util;
http = require('http');
mime = require('mime');
url = require('url');
util = require('util');
loadManifest();
server = http.createServer(function(req, res) {
var publicPath, rawPath;
rawPath = url.parse(req.url).pathname.replace(/(^\/|\/$)/g, '') || 'index';
if (rawPath === 'feed.rss') {
res.writeHead(200, {
'Content-Type': mime.lookup('.rss')
});
return res.end(Journo.feed());
} else {
publicPath = "public/" + rawPath;
return fs.exists(publicPath, function(exists) {
var post;
if (exists) {
res.writeHead(200, {
'Content-Type': mime.lookup(publicPath)
});
return fs.createReadStream(publicPath).pipe(res);
} else {
post = "posts/" + rawPath + ".md";
return fs.exists(post, function(exists) {
if (exists) {
loadLayout(true);
return fs.readFile(post, function(err, content) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
return res.end(Journo.render(post, content));
});
} else {
res.writeHead(404);
return res.end('404 Not Found');
}
});
}
});
}
});
server.listen(1234);
console.log("Journo is previewing at http://localhost:1234");
return exec("open http://localhost:1234");
};
Journo.run = function() {
var command;
command = process.argv[2] || 'preview';
if (Journo[command]) {
return Journo[command]();
}
return console.error("Journo doesn't know how to '" + command + "'");
};
Journo.help = Journo['--help'] = function() {
return console.log("Usage: journo [command]\n\nIf called without a command, `journo` will preview your blog.\n\ninit start a new blog in the current folder\nbuild build a static version of the blog into 'site'\npreview live preview the blog via a local server\npublish publish the blog to your remote server");
};
Journo.version = Journo['--version'] = function() {
return console.log("Journo 0.0.1");
};
postPath = function(post) {
return "posts/" + post;
};
htmlPath = function(post) {
var name;
name = postName(post);
if (name === 'index') {
return 'site/index.html';
} else {
return "site/" + name + "/index.html";
}
};
postName = function(post) {
return path.basename(post, '.md');
};
postUrl = function(post) {
return "" + shared.siteUrl + "/" + (postName(post)) + "/";
};
detectTitle = function(content) {
var _ref1;
return (_ref1 = _.find(marked.lexer(content), function(token) {
return token.type === 'heading';
})) != null ? _ref1.text : void 0;
};
detectDescription = function(content, post) {
var desc, _ref1;
desc = (_ref1 = _.find(marked.lexer(content), function(token) {
return token.type === 'paragraph';
})) != null ? _ref1.text : void 0;
return marked.parser(marked.lexer(_.template("" + desc + "...")(renderVariables(post))));
};
folderContents = function(folder) {
return fs.readdirSync(folder).filter(function(f) {
return f.charAt(0) !== '.';
});
};
sortedPosts = function() {
return _.sortBy(_.without(_.keys(shared.manifest), 'index.md'), function(post) {
return shared.manifest[post].pubtime;
});
};
renderVariables = function(post) {
return {
_: _,
fs: fs,
path: path,
mapLink: mapLink,
postName: postName,
folderContents: folderContents,
posts: sortedPosts(),
post: path.basename(post),
manifest: shared.manifest
};
};
mapLink = function(place, additional, zoom) {
var query;
if (additional == null) {
additional = '';
}
if (zoom == null) {
zoom = 15;
}
query = encodeURIComponent("" + place + ", " + additional);
return "<a href=\"https://maps.google.com/maps?q=" + query + "&t=h&z=" + zoom + "\">" + place + "</a>";
};
catchErrors = function(func) {
var err;
try {
return func();
} catch (_error) {
err = _error;
console.error(err.stack);
return "<pre>" + err.stack + "</pre>";
}
};
fatal = function(message) {
console.error(message);
return process.exit(1);
};
}).call(this);