-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpug.go
90 lines (71 loc) · 3.12 KB
/
pug.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
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
package pug
import (
"html/template"
"github.com/eknkc/pug/compiler"
"github.com/eknkc/pug/runtime"
)
type Options struct {
// Setting if pretty printing is enabled.
// Pretty printing ensures that the output html is properly indented and in human readable form.
// If disabled, produced HTML is compact. This might be more suitable in production environments.
// Default: false
PrettyPrint bool
// A Dir implements FileSystem using the native file system restricted to a specific directory tree.
//
// While the FileSystem.Open method takes '/'-separated paths, a Dir's string value is a filename on the native file system, not a URL, so it is separated by filepath.Separator, which isn't necessarily '/'.
// By default, a os package is used but you can supply a different filesystem using this option
Dir compiler.Dir
Funcs template.FuncMap
}
func newContext(dir compiler.Dir, options ...Options) compiler.Context {
opt := getOptions(options)
indentString := ""
if opt.PrettyPrint {
indentString = " "
}
if opt.Dir != nil {
dir = opt.Dir
}
context := compiler.NewContext(dir, indentString)
return context
}
func compileTemplate(options Options, name string, tplstring string) (*template.Template, error) {
return template.New(name).Funcs(options.Funcs).Funcs(runtime.FuncMap).Parse(tplstring)
}
// Parses and compiles the contents of supplied filename. Returns corresponding Go Template (html/templates) instance.
// Necessary runtime functions will be injected and the template will be ready to be executed
func CompileFile(filename string, options ...Options) (*template.Template, error) {
ctx := newContext(compiler.FsDir("."), options...)
if tplstring, err := ctx.CompileFile(filename); err != nil {
return nil, err
} else {
return compileTemplate(getOptions(options), filename, tplstring)
}
}
// Parses and compiles the supplied template string. Returns corresponding Go Template (html/templates) instance.
// Necessary runtime functions will be injected and the template will be ready to be executed
func CompileString(input string, options ...Options) (*template.Template, error) {
ctx := newContext(compiler.StringInputDir(input), options...)
if tplstring, err := ctx.CompileFile(""); err != nil {
return nil, err
} else {
return compileTemplate(getOptions(options), "", tplstring)
}
}
// Parses the contents of supplied filename template and return the Go Template source You would not be using this unless debugging / checking the output.
// Please use Compile method to obtain a template instance directly
func ParseFile(filename string, options ...Options) (string, error) {
return newContext(compiler.FsDir("."), options...).CompileFile(filename)
}
// Parses the supplied template string and return the Go Template source You would not be using this unless debugging / checking the output.
// Please use Compile method to obtain a template instance directly
func ParseString(input string, options ...Options) (string, error) {
return newContext(compiler.StringInputDir(input), options...).CompileFile("")
}
func getOptions(o []Options) Options {
opt := Options{}
if len(o) > 0 {
opt = o[0]
}
return opt
}