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");
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"});
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() {
18 // render the html head using a template
22 blogName : ddoc.blog.title,
24 commentsFeed : commentsFeed
30 newPostPath : path.show("edit"),
31 assets : path.asset(),
32 posts : List.withRows(function(row) {
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();
45 link : path.list("index", "tags", {
56 return path.older(key);
59 "10" : path.limit(10),
62 return Mustache.to_html(ddoc.templates.index, stash, ddoc.templates.partials, List.send);
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");
72 // we load the first row to find the most recent change date
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),
83 // send the header to the client
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);
94 var html = Mustache.escape(row.value.html);
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,
101 updated : new Date(row.value.created_at),
102 author : row.value.author,
103 alternate : path.absolute(path.show('post', row.id))
105 // send the entry to client
107 } while (row = getRow());
110 // close the loop after all rows are rendered