Refactor templates to make take care of fmt
This commit is contained in:
parent
e81df155a2
commit
c132c0fdf6
11 changed files with 51 additions and 59 deletions
9
admin.go
9
admin.go
|
@ -3,11 +3,12 @@ package main
|
|||
import log "github.com/cihub/seelog"
|
||||
|
||||
import (
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func deleteHandler(h handler) {
|
||||
|
@ -59,7 +60,7 @@ func editHandler(h handler) {
|
|||
var data bookData
|
||||
data.Book = book
|
||||
data.S = GetStatus(h)
|
||||
loadTemplate(h.w, "edit", data)
|
||||
loadTemplate(h, "edit", data)
|
||||
}
|
||||
|
||||
func cleanEmptyStr(s []string) []string {
|
||||
|
@ -162,7 +163,7 @@ func newHandler(h handler) {
|
|||
if page > 0 {
|
||||
data.Prev = "/new/?p=" + strconv.Itoa(page-1)
|
||||
}
|
||||
loadTemplate(h.w, "new", data)
|
||||
loadTemplate(h, "new", data)
|
||||
}
|
||||
|
||||
func storeHandler(h handler) {
|
||||
|
|
12
news.go
12
news.go
|
@ -1,8 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"net/http"
|
||||
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
)
|
||||
|
||||
type newsData struct {
|
||||
|
@ -27,12 +28,7 @@ func newsHandler(h handler) {
|
|||
data.S.News = true
|
||||
data.News = getNews(NUM_NEWS, 0, h.db)
|
||||
|
||||
format := h.r.Form["fmt"]
|
||||
if (len(format) > 0) && (format[0] == "rss") {
|
||||
loadTxtTemplate(h.w, "news_rss.xml", data)
|
||||
} else {
|
||||
loadTemplate(h.w, "news", data)
|
||||
}
|
||||
loadTemplate(h, "news", data)
|
||||
}
|
||||
|
||||
func editNewsHandler(h handler) {
|
||||
|
@ -44,7 +40,7 @@ func editNewsHandler(h handler) {
|
|||
var data statusData
|
||||
data.S = GetStatus(h)
|
||||
data.S.News = true
|
||||
loadTemplate(h.w, "edit_news", data)
|
||||
loadTemplate(h, "edit_news", data)
|
||||
}
|
||||
|
||||
func postNewsHandler(h handler) {
|
||||
|
|
|
@ -170,7 +170,7 @@ func readHandler(h handler) {
|
|||
data.Next, data.Prev = getNextPrev(e, file, id, "/read/")
|
||||
data.Chapters = getChapters(e, file, id, "/read/")
|
||||
data.Content = genLink(id, "/content/", file)
|
||||
loadTemplate(h.w, "read", data)
|
||||
loadTemplate(h, "read", data)
|
||||
}
|
||||
|
||||
func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
|
||||
|
|
10
search.go
10
search.go
|
@ -1,10 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
)
|
||||
|
||||
type searchData struct {
|
||||
|
@ -48,12 +49,7 @@ func searchHandler(h handler) {
|
|||
data.Prev = "/search/?q=" + req + "&p=" + strconv.Itoa(page-1) + "&num=" + strconv.Itoa(items_page)
|
||||
}
|
||||
|
||||
format := h.r.Form["fmt"]
|
||||
if (len(format) > 0) && (format[0] == "rss") {
|
||||
loadTxtTemplate(h.w, "search_rss.xml", data)
|
||||
} else {
|
||||
loadTemplate(h.w, "search", data)
|
||||
}
|
||||
loadTemplate(h, "search", data)
|
||||
}
|
||||
|
||||
func itemsPage(r *http.Request) int {
|
||||
|
|
9
stats.go
9
stats.go
|
@ -3,13 +3,14 @@ package main
|
|||
import log "github.com/cihub/seelog"
|
||||
|
||||
import (
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"git.gitorious.org/trantor/trantor.git/storage"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"git.gitorious.org/trantor/trantor.git/storage"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -93,7 +94,7 @@ func statsHandler(h handler) {
|
|||
data.DDownloads = getVisits(dailyLabel, h.db, database.Daily_downloads)
|
||||
data.MDownloads = getVisits(monthlyLabel, h.db, database.Monthly_downloads)
|
||||
|
||||
loadTemplate(h.w, "stats", data)
|
||||
loadTemplate(h, "stats", data)
|
||||
}
|
||||
|
||||
type statsData struct {
|
||||
|
|
36
template.go
36
template.go
|
@ -1,12 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
import log "github.com/cihub/seelog"
|
||||
import txt_tmpl "text/template"
|
||||
|
||||
import "html/template"
|
||||
|
||||
type Status struct {
|
||||
BaseURL string
|
||||
FullURL string
|
||||
|
@ -34,7 +32,7 @@ func GetStatus(h handler) Status {
|
|||
return s
|
||||
}
|
||||
|
||||
var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
||||
var tmpl_html = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
||||
TEMPLATE_PATH+"footer.html",
|
||||
TEMPLATE_PATH+"404.html",
|
||||
TEMPLATE_PATH+"index.html",
|
||||
|
@ -54,22 +52,20 @@ var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
|||
TEMPLATE_PATH+"help.html",
|
||||
))
|
||||
|
||||
func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
err := templates.ExecuteTemplate(w, tmpl+".html", data)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var txt_templates = txt_tmpl.Must(txt_tmpl.ParseFiles(TEMPLATE_PATH+"search_rss.xml",
|
||||
TEMPLATE_PATH+"news_rss.xml",
|
||||
var tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles(TEMPLATE_PATH+"search.rss",
|
||||
TEMPLATE_PATH+"news.rss",
|
||||
))
|
||||
|
||||
func loadTxtTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
err := txt_templates.ExecuteTemplate(w, tmpl, data)
|
||||
func loadTemplate(h handler, tmpl string, data interface{}) {
|
||||
var err error
|
||||
fmt := h.r.FormValue("fmt")
|
||||
if fmt == "rss" {
|
||||
err = tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data)
|
||||
} else {
|
||||
err = tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
tmpl_html.ExecuteTemplate(h.w, "404.html", data)
|
||||
log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err)
|
||||
}
|
||||
}
|
||||
|
|
17
trantor.go
17
trantor.go
|
@ -3,13 +3,14 @@ package main
|
|||
import log "github.com/cihub/seelog"
|
||||
|
||||
import (
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"git.gitorious.org/trantor/trantor.git/storage"
|
||||
"github.com/gorilla/mux"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"git.gitorious.org/trantor/trantor.git/storage"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type statusData struct {
|
||||
|
@ -20,14 +21,14 @@ func aboutHandler(h handler) {
|
|||
var data statusData
|
||||
data.S = GetStatus(h)
|
||||
data.S.About = true
|
||||
loadTemplate(h.w, "about", data)
|
||||
loadTemplate(h, "about", data)
|
||||
}
|
||||
|
||||
func helpHandler(h handler) {
|
||||
var data statusData
|
||||
data.S = GetStatus(h)
|
||||
data.S.Help = true
|
||||
loadTemplate(h.w, "help", data)
|
||||
loadTemplate(h, "help", data)
|
||||
}
|
||||
|
||||
func logoutHandler(h handler) {
|
||||
|
@ -55,7 +56,7 @@ func bookHandler(h handler) {
|
|||
}
|
||||
data.Book = book
|
||||
data.Description = strings.Split(data.Book.Description, "\n")
|
||||
loadTemplate(h.w, "book", data)
|
||||
loadTemplate(h, "book", data)
|
||||
}
|
||||
|
||||
func downloadHandler(h handler) {
|
||||
|
@ -107,7 +108,7 @@ func indexHandler(h handler) {
|
|||
data.VisitedBooks, _ = h.db.GetVisitedBooks()
|
||||
data.DownloadedBooks, _ = h.db.GetDownloadedBooks()
|
||||
data.News = getNews(1, DAYS_NEWS_INDEXPAGE, h.db)
|
||||
loadTemplate(h.w, "index", data)
|
||||
loadTemplate(h, "index", data)
|
||||
}
|
||||
|
||||
func notFound(h handler) {
|
||||
|
@ -115,7 +116,7 @@ func notFound(h handler) {
|
|||
|
||||
data.S = GetStatus(h)
|
||||
h.w.WriteHeader(http.StatusNotFound)
|
||||
loadTemplate(h.w, "404", data)
|
||||
loadTemplate(h, "404", data)
|
||||
}
|
||||
|
||||
func updateLogger() error {
|
||||
|
|
|
@ -6,13 +6,14 @@ import (
|
|||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"git.gitorious.org/go-pkg/epubgo.git"
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"git.gitorious.org/trantor/trantor.git/storage"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.gitorious.org/go-pkg/epubgo.git"
|
||||
"git.gitorious.org/trantor/trantor.git/database"
|
||||
"git.gitorious.org/trantor/trantor.git/storage"
|
||||
)
|
||||
|
||||
func InitUpload(database *database.DB, store *storage.Store) {
|
||||
|
@ -93,7 +94,7 @@ func uploadHandler(h handler) {
|
|||
var data uploadData
|
||||
data.S = GetStatus(h)
|
||||
data.S.Upload = true
|
||||
loadTemplate(h.w, "upload", data)
|
||||
loadTemplate(h, "upload", data)
|
||||
}
|
||||
|
||||
type uploadData struct {
|
||||
|
|
6
user.go
6
user.go
|
@ -14,7 +14,7 @@ func loginHandler(h handler) {
|
|||
|
||||
var data statusData
|
||||
data.S = GetStatus(h)
|
||||
loadTemplate(h.w, "login", data)
|
||||
loadTemplate(h, "login", data)
|
||||
}
|
||||
|
||||
func loginPostHandler(h handler) {
|
||||
|
@ -59,7 +59,7 @@ func dashboardHandler(h handler) {
|
|||
var data statusData
|
||||
data.S = GetStatus(h)
|
||||
data.S.Dasboard = true
|
||||
loadTemplate(h.w, "dashboard", data)
|
||||
loadTemplate(h, "dashboard", data)
|
||||
}
|
||||
|
||||
func settingsHandler(h handler) {
|
||||
|
@ -85,5 +85,5 @@ func settingsHandler(h handler) {
|
|||
|
||||
var data statusData
|
||||
data.S = GetStatus(h)
|
||||
loadTemplate(h.w, "settings", data)
|
||||
loadTemplate(h, "settings", data)
|
||||
}
|
||||
|
|
Reference in a new issue