1 | (function() {
|
2 | var Post, PostSchema, eco, fs, helpers;
|
3 | fs = require('fs');
|
4 | helpers = require('./helpers');
|
5 | eco = require('eco');
|
6 | PostSchema = new app.mongoose.Schema({
|
7 | title: String,
|
8 | topic: String,
|
9 | body: String,
|
10 | slug: String,
|
11 | date: Date
|
12 | });
|
13 | PostSchema.method('uri', function(text) {
|
14 | return "" + app.conf.website_url + "/posts/" + this.slug;
|
15 | });
|
16 | PostSchema.method('update', function(text) {
|
17 | var lines;
|
18 | lines = text.split('\n');
|
19 | this.title = lines.first();
|
20 | this.topic = lines[1];
|
21 | this.date = lines.last().toDate();
|
22 | this.body = lines.slice(2, lines.length - 1).join('\n');
|
23 | return this.save();
|
24 | });
|
25 | PostSchema.static('update_or_create', function(file, callback) {
|
26 | var slug, text;
|
27 | slug = file.name.split('.')[0];
|
28 | text = fs.readFileSync(file.path, 'utf-8');
|
29 | return Post.findOne({
|
30 | slug: slug
|
31 | }, function(err, post) {
|
32 | if (!post) {
|
33 | post = new Post;
|
34 | post.slug = slug;
|
35 | }
|
36 | post.update(text);
|
37 | return callback();
|
38 | });
|
39 | });
|
40 | PostSchema.static('topics', function(callback) {
|
41 | return Post.find({}, function(err, posts) {
|
42 | var desc, topics;
|
43 | topics = [];
|
44 | Object.each(posts.groupBy('topic'), function(topic, list) {
|
45 | var desc;
|
46 | return topics.push({
|
47 | name: topic,
|
48 | posts: list.sortBy('date', desc = true)
|
49 | });
|
50 | });
|
51 | topics = topics.sortBy((function(t) {
|
52 | return t.posts.first().date;
|
53 | }), desc = true);
|
54 | return callback(topics);
|
55 | });
|
56 | });
|
57 | Post = app.mongoose.model('Post', PostSchema);
|
58 | module.exports = Post;
|
59 | app.post('/posts', function(req, res) {
|
60 | return req.form.complete(function(err, fields, files) {
|
61 | var file;
|
62 | if (err) {
|
63 | return next(err);
|
64 | } else {
|
65 | file = files.file;
|
66 | return Post.update_or_create(file, function() {
|
67 | return res.send('ok\n');
|
68 | });
|
69 | }
|
70 | });
|
71 | });
|
72 | app["delete"]('/posts/:slug', function(req, res) {
|
73 | return Post.findOne({
|
74 | slug: req.params.slug
|
75 | }, function(err, post) {
|
76 | if (post) {
|
77 | return post.remove(function() {
|
78 | return res.send("ok\n");
|
79 | });
|
80 | } else {
|
81 | return res.send("not found\n");
|
82 | }
|
83 | });
|
84 | });
|
85 | app.get('/', function(req, res) {
|
86 | return Post.topics(function(topics) {
|
87 | req.topics = topics;
|
88 | return res.render('home/main', req);
|
89 | });
|
90 | });
|
91 | app.get('/posts/:slug', function(req, res, next) {
|
92 | return Post.findOne({
|
93 | slug: req.params.slug
|
94 | }, function(err, post) {
|
95 | if (post != null) {
|
96 | req.title = post.title;
|
97 | post.body = helpers.html(post.body);
|
98 | req.post = post;
|
99 | return res.render('post/main', req);
|
100 | } else {
|
101 | return next();
|
102 | }
|
103 | });
|
104 | });
|
105 | app.get('/api/posts', function(req, res) {
|
106 | return Post.topics(function(topics) {
|
107 | res.contentType('application/json');
|
108 | return res.send(topics);
|
109 | });
|
110 | });
|
111 | app.get('/api/posts/:slug', function(req, res, next) {
|
112 | return Post.findOne({
|
113 | slug: req.params.slug
|
114 | }, function(err, post) {
|
115 | if (post != null) {
|
116 | post.body = helpers.html(post.body);
|
117 | return res.send(post);
|
118 | } else {
|
119 | return next();
|
120 | }
|
121 | });
|
122 | });
|
123 | app.get('/sitemap.xml', function(req, res, next) {
|
124 | return Post.find({}, function(err, posts) {
|
125 | return res.send(eco.render("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\">\n <!-- http://www.google.com/support/webmasters/bin/answer.py?answer=34648 -->\n <url><loc><%= @conf.website_url %></loc><mobile:mobile/></url>\n <% for p in @posts: %>\n <url><loc><%= p.uri() %></loc><mobile:mobile/></url><% end %>\n</urlset>", {
|
126 | posts: posts,
|
127 | conf: app.conf
|
128 | }));
|
129 | });
|
130 | });
|
131 | }).call(this);
|