-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfuncmap.go
48 lines (40 loc) · 1.22 KB
/
funcmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (C) 2012 Numerotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
package spitz
import (
"fmt"
"html/template"
"strings"
)
// some functions to use inside templates
var funcMap = map[string]interface{}{"plural": pluralize, "mailto": mailto, "simpleformat": simpleFormat}
func pluralize(singular string, count int32) string {
if count == 1 {
return singular
}
return fmt.Sprintf("%ss", singular)
}
func mailto(email string) template.HTML {
pre := `<script type="text/javascript">eval(decodeURIComponent('`
post := `'))</script>`
inner := fmt.Sprintf("document.write('<a href=\"mailto:%s\">%s</a>');", email, email)
pieces := []string(nil)
for _, v := range inner {
pieces = append(pieces, fmt.Sprintf("%%%x", v))
}
return template.HTML(pre + strings.Join(pieces, "") + post)
}
func simpleFormat(in string) template.HTML {
lines := strings.Split(in, "\n")
paragraphs := []string(nil)
for _, v := range lines {
line := strings.TrimSpace(v)
if len(line) == 0 {
continue
}
line = template.HTMLEscapeString(line)
paragraphs = append(paragraphs, fmt.Sprintf("<p>%s</p>", line))
}
return template.HTML(strings.Join(paragraphs, ""))
}