From 93003534e9209956feb330e50d7c7bc000f0e377 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 21 Aug 2012 18:15:21 +0200 Subject: [PATCH] Simple epub reader --- reader.go | 106 ++++++++++++++++++++++++++++++++++++++++++++ template.go | 1 + templates/read.html | 31 +++++++++++++ trantor.go | 1 + 4 files changed, 139 insertions(+) create mode 100644 reader.go create mode 100644 templates/read.html diff --git a/reader.go b/reader.go new file mode 100644 index 0000000..d1004e7 --- /dev/null +++ b/reader.go @@ -0,0 +1,106 @@ +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 +} + +func parseUrl(url string) (string, string, string) { + exp, _ := regexp.Compile("^\\/read\\/([^\\/]*)\\/?(.*\\.([^\\.]*))?$") + res := exp.FindStringSubmatch(url) + title := res[1] + file := "" + ext := "" + if len(res) == 4 { + file = res[2] + ext = res[3] + } + return title, file, ext +} + +func cleanHtml(html string) string { + str := strings.Split(html, "", 2) + if len(str) < 2 { + return str[0] + } + + return "
" + strings.Split(str[1], "")[0] + "
" +} + +/* return next and prev urls from document */ +func nextPrev(e *epub.Epub, file string, title string) (string, string) { + 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 != "" { + prev = "/read/" + title + "/" + prev + } + if next != "" { + next = "/read/" + title + "/" + next + } + return next, prev +} + +func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + title, file, ext := parseUrl(r.URL.Path) + books, _, err := GetBook(coll, bson.M{"title": title}) + 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() + http.Redirect(w, r, "/read/" + title + "/" + it.CurrUrl(), 307) + return + } + + if ext == "html" || ext == "htm" { + var data readData + data.S = GetStatus(w, r) + data.Book = book + data.Next, data.Prev = nextPrev(e, file, title) + page := string(e.Data(file)) + data.Txt = template.HTML(cleanHtml(page)) + loadTemplate(w, "read", data) + } else { + w.Write(e.Data(file)) + } + } +} diff --git a/template.go b/template.go index ea0848b..e64cd16 100644 --- a/template.go +++ b/template.go @@ -37,6 +37,7 @@ func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { TEMPLATE_DIR+"search.html", TEMPLATE_DIR+"upload.html", TEMPLATE_DIR+"new.html", + TEMPLATE_DIR+"read.html", TEMPLATE_DIR+"edit.html", )) diff --git a/templates/read.html b/templates/read.html new file mode 100644 index 0000000..5b45ed9 --- /dev/null +++ b/templates/read.html @@ -0,0 +1,31 @@ +{{template "header.html" .S}} + + + +{{.Txt}} + + + +{{template "footer.html"}} diff --git a/trantor.go b/trantor.go index 03fef9e..68dee14 100644 --- a/trantor.go +++ b/trantor.go @@ -136,6 +136,7 @@ func main() { http.HandleFunc("/new/", newHandler(newColl)) http.HandleFunc("/delnew/", deleteHandler(newColl, "/new/")) http.HandleFunc("/store/", storeHandler(newColl, coll)) + http.HandleFunc("/read/", readHandler(coll)) http.HandleFunc("/edit/", editHandler(coll)) http.HandleFunc("/save/", saveHandler(coll)) http.HandleFunc("/delete/", deleteHandler(coll, "/"))