File: options.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.4%2Bgit20160102.51.71afeb7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 468 kB
  • ctags: 249
  • sloc: makefile: 3; sh: 1
file content (93 lines) | stat: -rw-r--r-- 2,434 bytes parent folder | download
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
package ace

import "html/template"

// Defaults
const (
	defaultExtension          = "ace"
	defaultDelimLeft          = "{{"
	defaultDelimRight         = "}}"
	defaultAttributeNameClass = "class"
)

// Default NoCloseTagNames
var defaultNoCloseTagNames = []string{
	"br",
	"hr",
	"img",
	"input",
	"link",
	"meta",
}

// Options represents options for the template engine.
type Options struct {
	// Extension represents an extension of files.
	Extension string
	// DelimLeft represents a left delimiter for the html template.
	DelimLeft string
	// DelimRight represents a right delimiter for the html template.
	DelimRight string
	// AttributeNameClass is the attribute name for classes.
	AttributeNameClass string
	// NoCloseTagNames defines a set of tags which should not be closed.
	NoCloseTagNames []string
	// DynamicReload represents a flag which means whether Ace reloads
	// templates dynamically.
	// This option should only be true in development.
	DynamicReload bool
	// BaseDir represents a base directory of the Ace templates.
	BaseDir string
	// Asset loads and returns the asset for the given name.
	// If this function is set, Ace load the template data from
	// this function instead of the template files.
	Asset func(name string) ([]byte, error)
	// FuncMap represents a template.FuncMap which is set to
	// the result template.
	FuncMap template.FuncMap
}

// InitializeOptions initializes the options.
func InitializeOptions(opts *Options) *Options {
	if opts == nil {
		opts = &Options{}
	}

	if opts.Extension == "" {
		opts.Extension = defaultExtension
	}

	if opts.DelimLeft == "" {
		opts.DelimLeft = defaultDelimLeft
	}

	if opts.DelimRight == "" {
		opts.DelimRight = defaultDelimRight
	}

	if opts.AttributeNameClass == "" {
		opts.AttributeNameClass = defaultAttributeNameClass
	}
	if opts.NoCloseTagNames == nil {
		opts.NoCloseTagNames = make([]string, len(defaultNoCloseTagNames))
		copy(opts.NoCloseTagNames, defaultNoCloseTagNames)
	}

	return opts
}

// AddNoCloseTagName appends name to .NoCloseTagNames set.
func (opts *Options) AddNoCloseTagName(name string) {
	opts.NoCloseTagNames = append(opts.NoCloseTagNames, name)
}

// DeleteNoCloseTagName deletes name from .NoCloseTagNames set.
func (opts *Options) DeleteNoCloseTagName(name string) {
	var newset []string
	for _, n := range opts.NoCloseTagNames {
		if n != name {
			newset = append(newset, n)
		}
	}
	opts.NoCloseTagNames = newset
}