/
post.js
105 lines (94 loc) · 3.38 KB
/
post.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
var fs = require('fs')
, sys = require('sys')
, path = require('path')
, md2html = require('showdown').md2html
, prettyfy = require('highlight').Highlight
, postsPath = path.join(__dirname, '../posts')
, sorter = function(a,b) { return b.substr(0,10).replace(/-/g,'') - a.substr(0,10).replace(/-/g,'') }
var Post = function (filename) { this.filename = filename }
Post.all = function (files) {
return files.map(function(f) {
return new Post(f)
})
}
Post.page = function (page) {
var self = this
, files = fs.readdirSync(postsPath).sort(sorter)
, posts = Post.all(files)
, count = 5
, max = Math.ceil(posts.length/count)
, cur = ~~page || 1
, next = cur + 1
, prev = cur - 1
, start = cur == 1 ? 0 : prev*count
, end = start >= count ? start+count : count;
return {
posts: posts.slice(start, end)
, page: cur
, max: max
, nextPage: next > max ? max : next
, prevPage: prev == 0 ? 1 : prev
}
}
// FIXME - this whole method is an embarassing gross. wtf!
Post.rss = function () {
var title = 'wtfjs'
, desc = 'wtfjs'
, domain = 'http://wtfjs.com'
s = '<rss version="2.0">';
s += '<channel>';
s += '<description>' + desc + '</description>';
s += '<title>' + title + '</title>';
s += '<generator>http://github.com/brianleroux/node-code-blog</generator>';
s += '<link>http://' + domain + '/</link>';
var posts = Post.all(fs.readdirSync(postsPath).sort(sorter));
for (var i = 0; i < 5; i++) {
s += '<item>';
s += '<title>' + posts[i].title() + '</title>';
s += '<description>' + posts[i].html() + '</description>';
s += '<link>http://' + domain + posts[i].url() + '</link>';
s += '<guid>http://' + domain + posts[i].url() + '</guid>';
s += '<pubDate>' + posts[i].created() + '</pubDate>';
s += '</item>';
}
s += '</channel></rss>';
return s.replace(/ /g, ' '); // oh the irony! fixes webkit bug w/ in xml documents
};
Post.prototype = {
created: function() {
var e = this.filename.split('-').slice(0,3)
, y = e[0]
, m = e[1] - 1 //wtfdate!
, d = e[2]
, c = new Date(y, m, d)
return c
},
title: function() {
var a = this.filename.split('-')
, l = a.length
, t = a.slice(3,l).join(' ').replace('.md','');
return t;
},
url: function() {
var everything = this.filename.replace('.md','').split('-')
, length = everything.length
, theDate = everything.slice(0,3).join('/')
, article = everything.slice(3,length).join('-');
return '/' + theDate + '/' + article;
},
anchor: function() {
return '<a href="' + this.url() + '">' + this.title() + '</a>';
},
html: function() {
// read in the post text
var p = path.join(__dirname, "../posts", this.filename)
, t = fs.readFileSync(p).toString()
// replace the raw code blocks with prettyfied html
t = t.replace(/<code>[^<]+<\/code>/g, function(code) {
return "<pre><code>" + prettyfy(code.match(/<code>([\s\S]+)<\/code>/)[1]) + "</code></pre>";
})
// return markdown to html
return md2html(t)
}
};
exports.Post = Post