-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassiterite.js
150 lines (118 loc) · 4.96 KB
/
cassiterite.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
var events = require('events');
var Mu = require('./mu/mu');
var tyrant = require('./tyrant/tyrant');
var showdown = require('./showdown');
Mu.templateRoot = './theme';
var cassiterite = {};
var converter = new showdown.converter();
// Closure
(function(){
cassiterite = {
getPosts: function(number, offset, callback) {
var promise = new events.Promise();
tyrant.connect();
tyrant.addListener('error', function(e) {
sys.puts('Error: ' + JSON.stringify(e));
});
tyrant.addListener('connect', function() {
tyrant.search(tyrant.is('type', 'blog'), tyrant.sort('time', 'desc')).addCallback(function(value) {
tyrant.getlist(value).addCallback(function(added_posts) {
var posts_processed = [];
var items = tyrant.dict(added_posts);
for (var raw_item in items) {
var item_mock = items[raw_item].split('\u0000');
var item = tyrant.dict(item_mock);
var post_date = new Date(parseInt(item.time));
if (!item.text) {
item.text = '';
}
posts_processed.push({
id: raw_item,
title: item.name,
link:'blog/' + raw_item,
date: post_date.getDate() + '.' + (post_date.getMonth() + 1) + '.' + post_date.getFullYear() + ' ' + post_date.getHours() + ':' + post_date.getMinutes() + ':' + post_date.getSeconds(),
text: converter.makeHtml(item.text),
tags: '',
num_of_comments: 0
});
}
promise.emitSuccess(posts_processed);
});
// res.respond(page_text);
}).addErrback(function(e){
promise.emitError(e);
});
});
if (callback) {
promise.addCallback(callback);
}
return promise;
},
render: function(type, response, page, callback) {
var promise = new events.Promise();
Mu.render(type, page, {chunkSize: 10}).addCallback(function (output) {
var buffer = '';
output
.addListener('data', function (c) {
buffer += c;
})
.addListener('eof', function () {
response.respond(buffer);
promise.emitSuccess(true);
});
}).addErrback(function (e) {
response.respond('Render Oops:' + JSON.stringify(e));
promise.emitError(e);
});
if (callback) {
promise.addCallback(callback);
}
return promise;
},
getPost: function(id, callback) {
var promise = new events.Promise();
tyrant.connect();
tyrant.addListener('connect', function() {
tyrant.get(id).addCallback(function(raw_item) {
var item = tyrant.dict(raw_item);
var post_date = new Date(parseInt(item.time));
var blog_post = {
'id': '0',
'title':item.name,
'text': converter.makeHtml(item.text),
'date': post_date.getDate() + '.' + (post_date.getMonth() + 1) + '.' + post_date.getFullYear() + ' ' + post_date.getHours() + ':' + post_date.getMinutes() + ':' + post_date.getSeconds(),
'tags': item.tags,
'num_of_comments': 0
};
promise.emitSuccess(blog_post);
}).addErrback(function(e) {
promise.emitError(e);
});
});
if (callback) {
promise.addCallback(callback);
}
return promise;
},
savePost: function(post) {
var promise = new events.Promise();
tyrant.connect();
tyrant.addListener('connect', function() {
tyrant.put('blogentry' + post.time, 'type', 'blog', 'name', post.title, 'text', post.body, 'time', post.time.toString());
promise.emitSuccess();
});
return promise;
},
renderPage: function(title, text, response) {
var page = {
'id': '0',
'title': title,
'text': text,
'tags': '',
'num_of_comments': 0
};
this.render('post', response, page);
}
};
})();
process.mixin(exports, cassiterite);