Add scripts to create myops-getqueryview:
[myops.git] / web / query / lists / index.js
1 function(head, req) {
2   var ddoc = this;
3   var Mustache = require("lib/mustache");
4   var List = require("vendor/couchapp/lib/list");
5   var path = require("vendor/couchapp/lib/path").init(req);
6   var Atom = require("vendor/couchapp/lib/atom");
7
8   var indexPath = path.list('index','recent-posts',{descending:true, limit:10});
9   var feedPath = path.list('index','recent-posts',{descending:true, limit:10, format:"atom"});
10   var commentsFeed = path.list('comments','comments',{descending:true, limit:10, format:"atom"});
11
12   var path_parts = req.path;
13   // The provides function serves the format the client requests.
14   // The first matching format is sent, so reordering functions changes 
15   // thier priority. In this case HTML is the preferred format, so it comes first.
16   provides("html", function() {
17     var key = "";
18     // render the html head using a template
19     var stash = {
20       header : {
21         index : indexPath,
22         blogName : ddoc.blog.title,
23         feedPath : feedPath,
24         commentsFeed : commentsFeed
25       },
26       scripts : {},
27       db : req.path[0],
28       design : req.path[2],
29       feedPath : feedPath,
30       newPostPath : path.show("edit"),
31       assets : path.asset(),
32       posts : List.withRows(function(row) {
33         var post = row.value;
34         key = row.key;
35         return {
36           title : post.title,
37           author : post.author,
38           date : post.created_at,
39           link : path.list('post','post-page', {startkey : [row.id]}),
40           has_tags : post.tags ? true : false,
41           tags : post.tags && post.tags.map ? post.tags.map(function(tag) {
42             var t = tag.toLowerCase();
43             return {
44               tag : tag,
45               link : path.list("index", "tags", {
46                 descending : true, 
47                 reduce : false, 
48                 startkey : [t, {}], 
49                 endkey : [t]
50               })
51             }
52           }) : []
53         };
54       }),
55       older : function() {
56         return path.older(key);
57       },
58       "5" : path.limit(5),
59       "10" : path.limit(10),
60       "25" : path.limit(25)
61     };
62     return Mustache.to_html(ddoc.templates.index, stash, ddoc.templates.partials, List.send);
63   });
64
65   // if the client requests an atom feed and not html, 
66   // we run this function to generate the feed.
67   provides("atom", function() {    
68     var path = require("vendor/couchapp/lib/path").init(req);
69     var markdown = require("vendor/couchapp/lib/markdown");
70     var textile = require("vendor/textile/textile");
71
72     // we load the first row to find the most recent change date
73     var row = getRow();
74     
75     // generate the feed header
76     var feedHeader = Atom.header({
77       updated : (row ? new Date(row.value.created_at) : new Date()),
78       title : ddoc.blog.title,
79       feed_id : path.absolute(indexPath),
80       feed_link : path.absolute(feedPath),
81     });
82     
83     // send the header to the client
84     send(feedHeader);
85
86     // loop over all rows
87     if (row) {
88       do {
89         if (row.value.format == "markdown") {
90           var html = markdown.encode(row.value.body);
91         } else if (row.value.format == "textile") {
92           var html = textile.encode(row.value.body);
93         } else {
94           var html = Mustache.escape(row.value.html);
95         }
96         // generate the entry for this row
97         var feedEntry = Atom.entry({
98           entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)),
99           title : row.value.title,
100           content : html,
101           updated : new Date(row.value.created_at),
102           author : row.value.author,
103           alternate : path.absolute(path.show('post', row.id))
104         });
105         // send the entry to client
106         send(feedEntry);
107       } while (row = getRow());
108     }
109
110     // close the loop after all rows are rendered
111     return "</feed>";
112   });
113 };