This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/reader.go

114 lines
2.3 KiB
Go
Raw Normal View History

2012-08-21 18:15:21 +02:00
package main
import (
"html/template"
"git.gitorious.org/go-pkg/epub.git"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/http"
"regexp"
"strings"
)
type readData struct {
S Status
Book Book
Txt template.HTML
Next string
Prev string
2012-08-21 20:54:57 +02:00
Back string
2012-08-21 18:15:21 +02:00
}
2012-08-21 20:54:57 +02:00
func parseUrl(url string) (string, string, string, string) {
exp, _ := regexp.Compile("^(\\/read[^\\/]*\\/)([^\\/]*)\\/?(.*\\.([^\\.]*))?$")
2012-08-21 18:15:21 +02:00
res := exp.FindStringSubmatch(url)
2012-08-21 20:54:57 +02:00
base := res[1]
2012-08-21 21:22:56 +02:00
id := res[2]
2012-08-21 18:15:21 +02:00
file := ""
ext := ""
2012-08-21 20:54:57 +02:00
if len(res) == 5 {
file = res[3]
ext = res[4]
2012-08-21 18:15:21 +02:00
}
2012-08-21 21:22:56 +02:00
return base, id, file, ext
2012-08-21 18:15:21 +02:00
}
func cleanHtml(html string) string {
str := strings.Split(html, "<body")
if len(str) < 2 {
return html
}
str = strings.SplitN(str[1], ">", 2)
if len(str) < 2 {
return str[0]
}
return "<div " + str[0] + ">" + strings.Split(str[1], "</body>")[0] + "</div>"
}
/* return next and prev urls from document */
2012-08-21 21:22:56 +02:00
func nextPrev(e *epub.Epub, file string, id string, base string) (string, string) {
2012-08-21 18:15:21 +02:00
it := e.Iterator(epub.EITERATOR_LINEAR)
defer it.Close()
prev := ""
next := ""
for it.CurrUrl() != file {
prev = it.CurrUrl()
_, err := it.Next()
if err != nil {
break
}
}
if it.CurrUrl() == file {
_, err := it.Next()
if err == nil {
next = it.CurrUrl()
}
}
if prev != "" {
2012-08-21 21:22:56 +02:00
prev = base + id + "/" + prev
2012-08-21 18:15:21 +02:00
}
if next != "" {
2012-08-21 21:22:56 +02:00
next = base + id + "/" + next
2012-08-21 18:15:21 +02:00
}
return next, prev
}
func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
2012-08-21 21:22:56 +02:00
base, id, file, ext := parseUrl(r.URL.Path)
books, _, err := GetBook(coll, bson.M{"_id": bson.ObjectIdHex(id)})
2012-08-21 18:15:21 +02:00
if err != nil || len(books) == 0 {
http.NotFound(w, r)
return
}
book := books[0]
e, _ := epub.Open(book.Path, 0)
defer e.Close()
if file == "" {
it := e.Iterator(epub.EITERATOR_LINEAR)
defer it.Close()
2012-08-21 21:22:56 +02:00
http.Redirect(w, r, base + id + "/" + it.CurrUrl(), 307)
2012-08-21 18:15:21 +02:00
return
}
2012-08-21 18:30:44 +02:00
if ext == "html" || ext == "htm" || ext == "xhtml" || ext == "xml" {
2012-08-21 18:15:21 +02:00
var data readData
data.S = GetStatus(w, r)
data.Book = book
2012-08-21 21:22:56 +02:00
data.Next, data.Prev = nextPrev(e, file, id, base)
2012-08-21 20:54:57 +02:00
if base == "/readnew/" {
data.Back = "/new/"
} else {
2012-08-21 21:22:56 +02:00
data.Back = "/book/" + id
2012-08-21 20:54:57 +02:00
}
2012-08-21 18:15:21 +02:00
page := string(e.Data(file))
data.Txt = template.HTML(cleanHtml(page))
loadTemplate(w, "read", data)
} else {
w.Write(e.Data(file))
}
}
}