Repository navigation
Expand file tree
/
Copy pathhelpers.go
More file actions
155 lines (137 loc) · 3.79 KB
/
Copy pathhelpers.go
File metadata and controls
155 lines (137 loc) · 3.79 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package plush
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"reflect"
"strings"
"sync"
"github.com/gobuffalo/plush/ast"
"github.com/gobuffalo/envy"
"github.com/markbates/inflect"
"github.com/pkg/errors"
)
// Helpers contains all of the default helpers for
// These will be available to all templates. You should add
// any custom global helpers to this list.
var Helpers = HelperMap{
moot: &sync.Mutex{},
}
func init() {
Helpers.Add("json", toJSONHelper)
Helpers.Add("jsEscape", template.JSEscapeString)
Helpers.Add("htmlEscape", htmlEscape)
Helpers.Add("upcase", strings.ToUpper)
Helpers.Add("downcase", strings.ToLower)
Helpers.Add("contentFor", contentForHelper)
Helpers.Add("contentOf", contentOfHelper)
Helpers.Add("markdown", MarkdownHelper)
Helpers.Add("len", lenHelper)
Helpers.Add("debug", debugHelper)
Helpers.Add("inspect", inspectHelper)
Helpers.Add("range", rangeHelper)
Helpers.Add("between", betweenHelper)
Helpers.Add("until", untilHelper)
Helpers.Add("groupBy", groupByHelper)
Helpers.Add("form", BootstrapFormHelper)
Helpers.Add("form_for", BootstrapFormForHelper)
Helpers.Add("truncate", truncateHelper)
Helpers.Add("env", envy.MustGet)
Helpers.Add("envOr", envy.Get)
Helpers.Add("raw", func(s string) template.HTML {
return template.HTML(s)
})
Helpers.AddMany(inflect.Helpers)
}
// HelperContext is an optional last argument to helpers
// that provides the current context of the call, and access
// to an optional "block" of code that can be executed from
// within the helper.
type HelperContext struct {
*Context
compiler *compiler
block *ast.BlockStatement
}
const helperContextKind = "HelperContext"
// Render a string with the current context
func (h HelperContext) Render(s string) (string, error) {
return Render(s, h.Context)
}
// HasBlock returns true if a block is associated with the helper function
func (h HelperContext) HasBlock() bool {
return h.block != nil
}
// Block executes the block of template associated with
// the helper, think the block inside of an "if" or "each"
// statement.
func (h HelperContext) Block() (string, error) {
return h.BlockWith(h.Context)
}
// BlockWith executes the block of template associated with
// the helper, think the block inside of an "if" or "each"
// statement, but with it's own context.
func (h HelperContext) BlockWith(ctx *Context) (string, error) {
octx := h.compiler.ctx
defer func() { h.compiler.ctx = octx }()
h.compiler.ctx = ctx
if h.block == nil {
return "", errors.New("no block defined")
}
i, err := h.compiler.evalBlockStatement(h.block)
if err != nil {
return "", err
}
bb := &bytes.Buffer{}
h.compiler.write(bb, i)
return bb.String(), nil
}
// toJSONHelper converts an interface into a string.
func toJSONHelper(v interface{}) (template.HTML, error) {
b, err := json.Marshal(v)
if err != nil {
return "", errors.WithStack(err)
}
return template.HTML(b), nil
}
func lenHelper(v interface{}) int {
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
return rv.Len()
}
// Debug by verbosely printing out using 'pre' tags.
func debugHelper(v interface{}) template.HTML {
return template.HTML(fmt.Sprintf("<pre>%s</pre>", inspectHelper(v)))
}
func inspectHelper(v interface{}) string {
return fmt.Sprintf("%+v", v)
}
func htmlEscape(s string, help HelperContext) (string, error) {
var err error
if help.HasBlock() {
s, err = help.Block()
}
if err != nil {
return "", err
}
return template.HTMLEscapeString(s), nil
}
func truncateHelper(s string, opts map[string]interface{}) string {
if opts["size"] == nil {
opts["size"] = 50
}
if opts["trail"] == nil {
opts["trail"] = "..."
}
size := opts["size"].(int)
if len(s) <= size {
return s
}
trail := opts["trail"].(string)
if len(trail) >= size {
return trail
}
return s[:size-len(trail)] + trail
}