Permalink
Browse files

Implement RSS.

  • Loading branch information...
1 parent 192aee8 commit d731ddc9800b48396cc7a3d62671da2ce7a4c092 @TheOnly92 committed Dec 11, 2011
Showing with 44 additions and 0 deletions.
  1. +1 −0 bloody.go
  2. +8 −0 controllers/index.go
  3. +19 −0 models/posts.go
  4. +1 −0 templates/layout.mustache
  5. +15 −0 templates/rss.mustache
View
@@ -138,5 +138,6 @@ func main() {
web.Get("/admin/page/del/(.*)", web.MethodHandler(a, "DelPage"))
web.Get("/admin/comment/del/(.*)/(.*)", web.MethodHandler(a, "DelComment"))
web.Get("/admin/bloody/restart", web.MethodHandler(a, "RestartBloody"))
+ web.Get("/rss", web.MethodHandler(i, "RSS"))
web.Run(config.Get("host")+":"+config.Get("port"))
}
@@ -18,6 +18,14 @@ func (c *Index) Index() string {
return render(output, "")
}
+func (c *Index) RSS(ctx *web.Context) string {
+ p := PostModelInit()
+ results := p.RSS()
+
+ ctx.ContentType("xml")
+ return mustache.RenderFile("templates/rss.mustache", map[string][]map[string]string {"posts":results})
+}
+
func (c *Index) NewComment(ctx *web.Context, postId string) {
p := PostModelInit()
p.InsertComment(postId,ctx.Params["comment"],ctx.Params["author"])
View
@@ -73,6 +73,25 @@ func (post *PostModel) FrontPage() []map[string]string {
return results
}
+func (post *PostModel) RSS() []map[string]string {
+ var result *Post
+ results := []map[string]string{}
+ err := post.c.Find(bson.M{"status":1}).Sort(bson.M{"timestamp":-1}).Limit(20).For(&result, func() error {
+ t := time.Unix(result.Created, 0)
+ if result.Type == 1 {
+ renderer := blackfriday.HtmlRenderer(post.html_flags,"","")
+ result.Content = string(blackfriday.Markdown([]byte(result.Content), renderer, post.extensions))
+ }
+ results = append(results, map[string]string {"Title":result.Title, "Content":result.Content, "Date":t.Format(time.RFC1123), "Id": objectIdHex(result.Id.String())})
+ return nil
+ })
+ if err != nil {
+ panic(err)
+ }
+
+ return results
+}
+
func (post *PostModel) RenderPost(postId string) *Post {
result := post.Get(postId)
renderer := blackfriday.HtmlRenderer(post.html_flags,"","")
@@ -3,6 +3,7 @@
<title>{{#Title}}{{Name}} - {{/Title}}Bloody.go</title>
<link rel="stylesheet" href="/style.css" />
<link rel="stylesheet" href="/jquery.cleditor.css">
+ <link rel="alternate" type="application/rss+xml" href="/rss" />
<script src="http://code.jquery.com/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="/jquery.cleditor.min.js" type="text/javascript"></script>
<script src="/jquery.cleditor.xhtml.min.js" type="text/javascript"></script>
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
+ <channel>
+ <atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/>
+ {{#posts}}
+ <item>
+ <title>{{Title}}</title>
+ <description>{{Content}}</description>
+ <link>/post/{{Id}}</link>
+ <guid>/post/{{Id}}</guid>
+ <pubDate>{{Date}}</pubDate>
+ </item>
+ {{/posts}}
+ </channel>
+</rss>

0 comments on commit d731ddc

Please sign in to comment.