1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-18 01:57:02 +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))
}