1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

Package reorganization part 2

End goal is to simplify build
This commit is contained in:
James Hillyerd
2012-10-22 15:48:55 -07:00
parent 4e5c0ce4d8
commit 7215c041dc
11 changed files with 60 additions and 65 deletions

View File

@@ -3,24 +3,29 @@ package web
import (
"fmt"
"github.com/jhillyerd/inbucket/log"
"html"
"html/template"
"strings"
"time"
)
var TemplateFuncs = template.FuncMap{
// Reversable routing function (shared with templates)
"friendlyTime": friendlyTime,
"reverse": reverse,
// Friendly date & time rendering
"friendlyTime": func(t time.Time) template.HTML {
ty, tm, td := t.Date()
ny, nm, nd := time.Now().Date()
if (ty == ny) && (tm == nm) && (td == nd) {
return template.HTML(t.Format("03:04:05 PM"))
}
return template.HTML(t.Format("Mon Jan 2, 2006"))
},
"textToHtml": textToHtml,
}
// Friendly date & time rendering
func friendlyTime(t time.Time) template.HTML {
ty, tm, td := t.Date()
ny, nm, nd := time.Now().Date()
if (ty == ny) && (tm == nm) && (td == nd) {
return template.HTML(t.Format("03:04:05 PM"))
}
return template.HTML(t.Format("Mon Jan 2, 2006"))
}
// Reversable routing function (shared with templates)
func reverse(name string, things ...interface{}) string {
// Convert the things to strings
strs := make([]string, len(things))
@@ -35,3 +40,11 @@ func reverse(name string, things ...interface{}) string {
}
return u.Path
}
// textToHtml takes plain text, escapes it and tries to pretty it up for
// HTML display
func textToHtml(text string) template.HTML {
text = html.EscapeString(text)
replacer := strings.NewReplacer("\r\n", "<br/>\n", "\r", "<br/>\n", "\n", "<br/>\n")
return template.HTML(replacer.Replace(text))
}

19
web/helpers_test.go Normal file
View File

@@ -0,0 +1,19 @@
package web
import (
"github.com/stretchrcom/testify/assert"
"testing"
)
func TestTextToHtml(t *testing.T) {
// Identity
assert.Equal(t, textToHtml("html"), "html")
// Check it escapes
assert.Equal(t, textToHtml("<html>"), "&lt;html&gt;")
// Check for linebreaks
assert.Equal(t, textToHtml("line\nbreak"), "line<br/>\nbreak")
assert.Equal(t, textToHtml("line\r\nbreak"), "line<br/>\nbreak")
assert.Equal(t, textToHtml("line\rbreak"), "line<br/>\nbreak")
}

View File

@@ -1,7 +1,6 @@
package web
import (
"github.com/jhillyerd/inbucket"
"github.com/jhillyerd/inbucket/log"
"html/template"
"io"
@@ -73,7 +72,7 @@ func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *Context) (err er
if err != nil {
return err
}
body := template.HTML(inbucket.TextToHtml(mime.Text))
body := template.HTML(textToHtml(mime.Text))
htmlAvailable := mime.Html != ""
return RenderPartial("mailbox/_show.html", w, map[string]interface{}{

View File

@@ -7,7 +7,7 @@ import (
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/jhillyerd/inbucket"
"github.com/jhillyerd/inbucket/config"
"github.com/jhillyerd/inbucket/log"
"net/http"
"thegoods.biz/httpbuf"
@@ -20,7 +20,7 @@ var Router *mux.Router
var sessionStore sessions.Store
func setupRoutes(cfg inbucket.WebConfig) {
func setupRoutes(cfg config.WebConfig) {
Router = mux.NewRouter()
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
@@ -42,7 +42,7 @@ func setupRoutes(cfg inbucket.WebConfig) {
// Start() the web server
func Start() {
cfg := inbucket.GetWebConfig()
cfg := config.GetWebConfig()
setupRoutes(cfg)
sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))

View File

@@ -1,7 +1,7 @@
package web
import (
"github.com/jhillyerd/inbucket"
"github.com/jhillyerd/inbucket/config"
"github.com/jhillyerd/inbucket/log"
"html/template"
"net/http"
@@ -49,7 +49,7 @@ func ParseTemplate(name string, partial bool) (*template.Template, error) {
return t, nil
}
cfg := inbucket.GetWebConfig()
cfg := config.GetWebConfig()
tempPath := strings.Replace(name, "/", string(filepath.Separator), -1)
tempFile := filepath.Join(cfg.TemplateDir, tempPath)
log.Trace("Parsing template %v", tempFile)