Add scripts to create myops-getqueryview:
[myops.git] / web / query / lists / post.js
1 function(head, req) {
2   var Mustache = require("lib/mustache");
3   var ddoc = this;
4   var List = require("vendor/couchapp/lib/list");
5   var path = require("vendor/couchapp/lib/path").init(req);
6   var markdown = require("vendor/couchapp/lib/markdown");
7   var textile = require("vendor/textile/textile");
8
9   var indexPath = path.list('index','recent-posts',{descending:true, limit:10});
10   var feedPath = path.list('index','recent-posts',{descending:true, limit:10, format:"atom"});
11   var commentsFeed = path.list('comments','comments',{descending:true, limit:10, format:"atom"});
12   
13   provides("html", function() {
14     // get the first row and make sure it's a post
15     var post = getRow().value;
16     if (post.type != "post") {
17       throw(["error", "not_found", "not a post"]);
18     } else {
19       if (post.format == "markdown") {
20         var html = markdown.encode(post.body);
21       } else if (post.format == "textile") {
22         var html = textile.encode(post.body);
23       } else {
24         var html = Mustache.escape(post.html);
25       }
26
27       var stash = {
28         header : {
29           index : indexPath,
30           blogName : ddoc.blog.title,
31           feedPath : feedPath,
32           commentsFeed : commentsFeed
33         },
34         scripts : {},
35         title : post.title,
36         post_id : post._id,
37         date : post.created_at,
38         html : html,
39         comments : List.withRows(function(row) {
40           var v = row.value;
41           if (v.type != "comment") {
42             return;
43           }
44           // keep getting comments until we get to the next post...
45           return {
46             comment : {
47               name : v.commenter.nickname || v.commenter.name,
48               url : v.commenter.url,
49               avatar : v.commenter.gravatar_url || 'http://www.gravatar.com/avatar/'+v.commenter.gravatar+'.jpg?s=40&d=identicon',
50               html : markdown.encode(Mustache.escape(v.comment)),
51               created_at : v.created_at
52             }
53           };
54         })
55       };
56       return Mustache.to_html(ddoc.templates.post, stash, ddoc.templates.partials, List.send);   
57     }
58   });
59 }