From 6b0c8caed3d5af1d986ae5b64ed8e3d76d6c959e Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 28 Aug 2012 10:38:00 +0200 Subject: [PATCH] Add chapters to book reader --- reader.go | 39 +++++++++++++++++++++++++++++++++++++++ templates/read.html | 12 ++++++++++++ 2 files changed, 51 insertions(+) diff --git a/reader.go b/reader.go index 0b6ab26..b6b81c5 100644 --- a/reader.go +++ b/reader.go @@ -8,12 +8,21 @@ import ( "net/http" "regexp" "strings" + "strconv" ) +type chapter struct { + Label string + Link string + Depth int + Active bool +} + type readData struct { S Status Book Book Txt template.HTML + Chapters []chapter Next string Prev string Back string @@ -46,6 +55,10 @@ func cleanHtml(html string) string { return "
" + strings.Split(str[1], "")[0] + "
" } +func genLink(id string, base string, link string) string { + return base + id + "/" + link +} + /* return next and prev urls from document */ func nextPrev(e *epub.Epub, file string, id string, base string) (string, string) { it := e.Iterator(epub.EITERATOR_LINEAR) @@ -73,6 +86,31 @@ func nextPrev(e *epub.Epub, file string, id string, base string) (string, string return next, prev } +func cleanLink(link string) string { + for i := 0; i < len(link); i++ { + if link[i] == '%' { + c, _ := strconv.ParseInt(link[i+1:i+3], 16, 0) + link = link[:i] + string(c) + link[i+3:] + } + } + return link +} + +func listChapters(e *epub.Epub, file string, id string, base string) []chapter { + chapters := make([]chapter, 0) + tit := e.Titerator(epub.TITERATOR_NAVMAP) + defer tit.Close() + for ; tit.Valid(); tit.Next() { + var c chapter + c.Label = tit.Label() + c.Link = genLink(id, base, tit.Link()) + c.Depth = tit.Depth() + c.Active = cleanLink(tit.Link()) == file + chapters = append(chapters, c) + } + return chapters +} + func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { base, id, file, ext := parseUrl(r.URL.Path) @@ -102,6 +140,7 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) var data readData data.S = GetStatus(w, r) data.Book = book + data.Chapters = listChapters(e, file, id, base) data.Next, data.Prev = nextPrev(e, file, id, base) if base == "/readnew/" { data.Back = "/new/" diff --git a/templates/read.html b/templates/read.html index 03ecf68..3c59232 100644 --- a/templates/read.html +++ b/templates/read.html @@ -16,7 +16,19 @@ {{end}} +
+
+ +
+ +
{{.Txt}} +
+