Add chapters to book reader
This commit is contained in:
parent
a888d1ac3a
commit
6b0c8caed3
2 changed files with 51 additions and 0 deletions
39
reader.go
39
reader.go
|
@ -8,12 +8,21 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type chapter struct {
|
||||||
|
Label string
|
||||||
|
Link string
|
||||||
|
Depth int
|
||||||
|
Active bool
|
||||||
|
}
|
||||||
|
|
||||||
type readData struct {
|
type readData struct {
|
||||||
S Status
|
S Status
|
||||||
Book Book
|
Book Book
|
||||||
Txt template.HTML
|
Txt template.HTML
|
||||||
|
Chapters []chapter
|
||||||
Next string
|
Next string
|
||||||
Prev string
|
Prev string
|
||||||
Back string
|
Back string
|
||||||
|
@ -46,6 +55,10 @@ func cleanHtml(html string) string {
|
||||||
return "<div " + str[0] + ">" + strings.Split(str[1], "</body>")[0] + "</div>"
|
return "<div " + str[0] + ">" + strings.Split(str[1], "</body>")[0] + "</div>"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genLink(id string, base string, link string) string {
|
||||||
|
return base + id + "/" + link
|
||||||
|
}
|
||||||
|
|
||||||
/* return next and prev urls from document */
|
/* return next and prev urls from document */
|
||||||
func nextPrev(e *epub.Epub, file string, id string, base string) (string, string) {
|
func nextPrev(e *epub.Epub, file string, id string, base string) (string, string) {
|
||||||
it := e.Iterator(epub.EITERATOR_LINEAR)
|
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
|
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) {
|
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, ext := parseUrl(r.URL.Path)
|
||||||
|
@ -102,6 +140,7 @@ 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.Chapters = listChapters(e, file, id, base)
|
||||||
data.Next, data.Prev = nextPrev(e, file, id, base)
|
data.Next, data.Prev = nextPrev(e, file, id, base)
|
||||||
if base == "/readnew/" {
|
if base == "/readnew/" {
|
||||||
data.Back = "/new/"
|
data.Back = "/new/"
|
||||||
|
|
|
@ -16,7 +16,19 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="span4">
|
||||||
|
<ul class="nav nav-pills nav-stacked">
|
||||||
|
{{range .Chapters}}
|
||||||
|
<li {{if .Active}}class="active"{{end}}><a href="{{.Link}}">{{.Label}}</a></li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="span8">
|
||||||
{{.Txt}}
|
{{.Txt}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<ul class="pager">
|
<ul class="pager">
|
||||||
{{if .Prev}}
|
{{if .Prev}}
|
||||||
|
|
Reference in a new issue