Read books on moderation

This commit is contained in:
Las Zenow 2012-08-21 20:54:57 +02:00
parent 9f99bf3ee3
commit 5d9093700d
4 changed files with 25 additions and 16 deletions

View file

@ -16,19 +16,21 @@ type readData struct {
Txt template.HTML Txt template.HTML
Next string Next string
Prev string Prev string
Back string
} }
func parseUrl(url string) (string, string, string) { func parseUrl(url string) (string, string, string, string) {
exp, _ := regexp.Compile("^\\/read\\/([^\\/]*)\\/?(.*\\.([^\\.]*))?$") exp, _ := regexp.Compile("^(\\/read[^\\/]*\\/)([^\\/]*)\\/?(.*\\.([^\\.]*))?$")
res := exp.FindStringSubmatch(url) res := exp.FindStringSubmatch(url)
title := res[1] base := res[1]
title := res[2]
file := "" file := ""
ext := "" ext := ""
if len(res) == 4 { if len(res) == 5 {
file = res[2] file = res[3]
ext = res[3] ext = res[4]
} }
return title, file, ext return base, title, file, ext
} }
func cleanHtml(html string) string { func cleanHtml(html string) string {
@ -45,7 +47,7 @@ func cleanHtml(html string) string {
} }
/* return next and prev urls from document */ /* return next and prev urls from document */
func nextPrev(e *epub.Epub, file string, title string) (string, string) { func nextPrev(e *epub.Epub, file string, title string, base string) (string, string) {
it := e.Iterator(epub.EITERATOR_LINEAR) it := e.Iterator(epub.EITERATOR_LINEAR)
defer it.Close() defer it.Close()
@ -65,17 +67,17 @@ func nextPrev(e *epub.Epub, file string, title string) (string, string) {
} }
} }
if prev != "" { if prev != "" {
prev = "/read/" + title + "/" + prev prev = base + title + "/" + prev
} }
if next != "" { if next != "" {
next = "/read/" + title + "/" + next next = base + title + "/" + next
} }
return next, prev return next, prev
} }
func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
title, file, ext := parseUrl(r.URL.Path) base, title, file, ext := parseUrl(r.URL.Path)
books, _, err := GetBook(coll, bson.M{"title": title}) books, _, err := GetBook(coll, bson.M{"title": title})
if err != nil || len(books) == 0 { if err != nil || len(books) == 0 {
http.NotFound(w, r) http.NotFound(w, r)
@ -87,7 +89,7 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
if file == "" { if file == "" {
it := e.Iterator(epub.EITERATOR_LINEAR) it := e.Iterator(epub.EITERATOR_LINEAR)
defer it.Close() defer it.Close()
http.Redirect(w, r, "/read/" + title + "/" + it.CurrUrl(), 307) http.Redirect(w, r, base + title + "/" + it.CurrUrl(), 307)
return return
} }
@ -95,7 +97,12 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
var data readData var data readData
data.S = GetStatus(w, r) data.S = GetStatus(w, r)
data.Book = book data.Book = book
data.Next, data.Prev = nextPrev(e, file, title) data.Next, data.Prev = nextPrev(e, file, title, base)
if base == "/readnew/" {
data.Back = "/new/"
} else {
data.Back = "/book/" + title
}
page := string(e.Data(file)) page := string(e.Data(file))
data.Txt = template.HTML(cleanHtml(page)) data.Txt = template.HTML(cleanHtml(page))
loadTemplate(w, "read", data) loadTemplate(w, "read", data)

View file

@ -20,7 +20,8 @@
</div> </div>
<div class="span2"> <div class="span2">
<div class="row"> <div class="row">
<p><a href="/{{.Path}}" class="btn btn-inverse pull-right"><i class="icon-download-alt icon-white"></i> download</a></p> <p><a href="/readnew/{{.Title}}" class="btn btn-warning pull-right"><i class="icon-eye-open icon-white"></i> read it!</a>
<a href="/{{.Path}}" class="btn btn-inverse pull-right"><i class="icon-download-alt icon-white"></i> download</a></p>
</div> </div>
<div class="row"><p></p></div> <div class="row"><p></p></div>
<div class="row btn-group pull-right"> <div class="row btn-group pull-right">

View file

@ -7,7 +7,7 @@
</li> </li>
{{end}} {{end}}
<li class=""> <li class="">
<a href="/book/{{.Book.Title}}">Book description</a> <a href="{{.Back}}">Back</a>
</li> </li>
{{if .Next}} {{if .Next}}
<li class="next"> <li class="next">
@ -25,7 +25,7 @@
</li> </li>
{{end}} {{end}}
<li class=""> <li class="">
<a href="/book/{{.Book.Title}}">Book description</a> <a href="{{.Back}}">Back</a>
</li> </li>
{{if .Next}} {{if .Next}}
<li class="next"> <li class="next">

View file

@ -137,6 +137,7 @@ func main() {
http.HandleFunc("/delnew/", deleteHandler(newColl, "/new/")) http.HandleFunc("/delnew/", deleteHandler(newColl, "/new/"))
http.HandleFunc("/store/", storeHandler(newColl, coll)) http.HandleFunc("/store/", storeHandler(newColl, coll))
http.HandleFunc("/read/", readHandler(coll)) http.HandleFunc("/read/", readHandler(coll))
http.HandleFunc("/readnew/", readHandler(newColl))
http.HandleFunc("/edit/", editHandler(coll)) http.HandleFunc("/edit/", editHandler(coll))
http.HandleFunc("/save/", saveHandler(coll)) http.HandleFunc("/save/", saveHandler(coll))
http.HandleFunc("/delete/", deleteHandler(coll, "/")) http.HandleFunc("/delete/", deleteHandler(coll, "/"))