Using iframes for reading books

This commit is contained in:
Las Zenow 2012-08-30 20:49:58 +02:00
parent c81992de2c
commit 45b2d47bd5
3 changed files with 65 additions and 26 deletions

View file

@ -2,7 +2,6 @@ package main
import ( import (
"git.gitorious.org/go-pkg/epub.git" "git.gitorious.org/go-pkg/epub.git"
"html/template"
"labix.org/v2/mgo" "labix.org/v2/mgo"
"labix.org/v2/mgo/bson" "labix.org/v2/mgo/bson"
"net/http" "net/http"
@ -23,25 +22,23 @@ type chapter struct {
type readData struct { type readData struct {
S Status S Status
Book Book Book Book
Txt template.HTML Content string
Chapters []chapter Chapters []chapter
Next string Next string
Prev string Prev string
Back string Back string
} }
func parseUrl(url string) (string, string, string, string) { func parseUrl(url string) (string, string, string) {
exp, _ := regexp.Compile("^(\\/read[^\\/]*\\/)([^\\/]*)\\/?(.*\\.([^\\.]*))?$") exp, _ := regexp.Compile("^(\\/[^\\/]*\\/)([^\\/]*)\\/?(.*)?$")
res := exp.FindStringSubmatch(url) res := exp.FindStringSubmatch(url)
base := res[1] base := res[1]
id := res[2] id := res[2]
file := "" file := ""
ext := "" if len(res) == 4 {
if len(res) == 5 {
file = res[3] file = res[3]
ext = res[4]
} }
return base, id, file, ext return base, id, file
} }
func cleanHtml(html string) string { func cleanHtml(html string) string {
@ -121,7 +118,7 @@ func chapterList(e *epub.Epub, file string, id string, base string) (string, str
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) {
base, id, file, ext := parseUrl(r.URL.Path) base, id, file := parseUrl(r.URL.Path)
if base == "/readnew/" { if base == "/readnew/" {
sess := GetSession(r) sess := GetSession(r)
if sess.User == "" { if sess.User == "" {
@ -144,21 +141,47 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
return return
} }
if ext == "html" || ext == "htm" || ext == "xhtml" || ext == "xml" { 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, data.Chapters = chapterList(e, file, id, base)
data.Next, data.Prev, data.Chapters = chapterList(e, file, id, base) if base == "/readnew/" {
if base == "/readnew/" { data.Back = "/new/"
data.Back = "/new/"
} else {
data.Back = "/book/" + id
}
page := string(e.Data(file))
data.Txt = template.HTML(cleanHtml(page))
loadTemplate(w, "read", data)
} else { } else {
w.Write(e.Data(file)) data.Back = "/book/" + id
} }
baseContent := "/content/"
if base == "/readnew/" {
base = "/contentnew/"
}
data.Content = genLink(id, baseContent, file)
loadTemplate(w, "read", data)
}
}
func contentHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
base, id, file := parseUrl(r.URL.Path)
if base == "/contentnew/" {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
}
books, _, err := GetBook(coll, bson.M{"_id": bson.ObjectIdHex(id)})
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 == "" {
http.NotFound(w, r)
return
}
w.Write(e.Data(file))
} }
} }

View file

@ -32,9 +32,23 @@
{{end}} {{end}}
</div> </div>
<div class="span8"> <script type="text/javascript">
{{.Txt}} function iframedoc(aID){
</div> var framecont = null;
var frame=document.getElementById(aID);
if(frame.contentDocument)
framecont = frame.contentDocument;
else
framecont = document.frames[aID].document;
return framecont;
}
function iframe_resize(){
var myframe = document.getElementById('txt');
var frameDoc = iframedoc('txt');
myframe.height = frameDoc.body.offsetHeight+20;
}
</script>
<iframe class="span8" id="txt" onload="iframe_resize();" scrolling="no" src="{{.Content}}" seamless="seamless"></iframe>
</div> </div>
<ul class="pager"> <ul class="pager">

View file

@ -127,7 +127,9 @@ 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("/content/", contentHandler(coll))
http.HandleFunc("/readnew/", readHandler(newColl)) http.HandleFunc("/readnew/", readHandler(newColl))
http.HandleFunc("/contentnew/", contentHandler(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, "/"))