Add rss feed

This commit is contained in:
Las Zenow 2013-08-31 00:38:32 +02:00
parent c3529c3a8e
commit 56964ec45f
7 changed files with 58 additions and 3 deletions

BIN
img/feed.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B

View file

@ -62,5 +62,11 @@ func searchHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
if page > 0 {
data.Prev = "/search/?q=" + req + "&p=" + strconv.Itoa(page-1)
}
format := r.Form["fmt"]
if (len(format) > 0) && (format[0] == "rss") {
loadTxtTemplate(w, "search_rss.xml", data)
} else {
loadTemplate(w, "search", data)
}
}

View file

@ -5,7 +5,11 @@ import (
"net/http"
)
import txt_tmpl "text/template"
type Status struct {
BaseURL string
FullURL string
Search string
User string
IsAdmin bool
@ -22,6 +26,8 @@ func GetStatus(w http.ResponseWriter, r *http.Request) Status {
var s Status
sess := GetSession(r)
sess.Save(w, r)
s.BaseURL = "http://" + r.Host
s.FullURL = s.BaseURL + r.RequestURI
s.User = sess.User
s.IsAdmin = sess.IsAdmin()
s.Notif = sess.Notif
@ -53,3 +59,13 @@ func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
return
}
}
var txt_templates = txt_tmpl.Must(txt_tmpl.ParseFiles(TEMPLATE_PATH + "search_rss.xml"))
func loadTxtTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
err := txt_templates.ExecuteTemplate(w, tmpl, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

View file

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="/css/custom.css" rel="stylesheet">
<link rel="alternate" type="application/rss+xml" title="Imperial Library of Trantor" href="/search/?fmt=rss&q={{.Search}}" />
<title>Imperial Library of Trantor</title>
<script src="/js/jquery.js"></script>
</head>

View file

@ -8,7 +8,7 @@
<div class="row">
<div class="span8">
<h4>Last books added:</h4>
<h4>Last books added: <a href="/search/?fmt=rss&q="><img src="/img/feed.png"/></a></h4>
</div>
<div class="span4">
<p class="pull-right">{{.Count}} books</p>

View file

@ -1,6 +1,6 @@
{{template "header.html" .S}}
<p class="centered">Found {{.Found}} books. Page {{.Page}}</p>
<p class="centered">Found {{.Found}} books. Page {{.Page}} <a href="/search/?fmt=rss&q={{.S.Search}}"><img src="/img/feed.png"/></a></p>
<ul class="pager">
{{if .Prev}}

32
templates/search_rss.xml Normal file
View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
{{with .S}}
<title>Imperial Library of Trantor</title>
{{if .Search}}
<description>Book search: {{.Search}}</description>
{{else}}
<description>Last books added</description>
{{end}}
<link>{{.BaseURL}}</link>
{{end}}
{{$baseURL := .S.BaseURL}}
{{range .Books}}
<item>
<title>{{.Title}} - {{index .Author 0}}</title>
<description>{{.Description}}</description>
<link>{{$baseURL}}/book/{{.Id}}</link>
{{if .Isbn}}
<guid isPermaLink="false">ISBN: {{.Isbn}}</guid>
{{end}}
{{range .Subject}}
{{if .}}
<category>{{.}}</category>
{{end}}
{{end}}
</item>
{{end}}
</channel>
</rss>