This repository has been archived by the owner on Dec 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
108 lines (90 loc) · 2.38 KB
/
template.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"strings"
"time"
)
// Additional functions available in Jekyll templates
var funcMap = map[string]interface{}{
"capitalize": capitalize,
"date_to_string": dateToString,
"date_to_xmlschema": dateToXmlSchema,
"downcase": lower,
"eq": eq,
"newline_to_br": newlineToBreak,
"replace": replace,
"replace_first": replaceFirst,
"remove": remove,
"remove_first": removeFirst,
"split": split,
"strip_newlines": stripNewlines,
"truncate": truncate,
"truncatewords": truncateWords,
"upcase": upper,
}
// Capitalize words in the input sentence
func capitalize(s string) string {
return strings.Title(s)
}
// Checks if two values are equal
func eq(v1 interface{}, v2 interface{}) bool {
return v1 == v2
}
// Converts a date to a string
func dateToString(date time.Time) string {
return date.Format("2006-01-02")
}
// Converts a date to a string
func dateToXmlSchema(date time.Time) string {
return date.Format(time.RFC3339)
}
// Convert an input string to lowercase
func lower(s string) string {
return strings.ToLower(s)
}
// Replace each newline (\n) with html break
func newlineToBreak(s string) string {
return strings.Replace(s, "\n", "<br/>", -1)
}
// Remove each occurrence
func remove(s, pattern string) string {
return strings.Replace(s, pattern, "", -1)
}
// Remove the first occurrence
func removeFirst(s, pattern string) string {
return strings.Replace(s, pattern, "", 1)
}
// Replace each occurrence
func replace(s, old, new string) string {
return strings.Replace(s, old, new, -1)
}
// Replace the first occurrence
func replaceFirst(s, old, new string) string {
return strings.Replace(s, old, new, 1)
}
// Split a string on a matching pattern
func split(s, pattern string) []string {
return strings.Split(s, pattern)
}
// Strip all newlines (\n) from string
func stripNewlines(s string) string {
return strings.Replace(s, "\n", "", -1)
}
// Truncate a string down to x characters
func truncate(s string, x int) string {
if len(s) > x {
return s[0:x]
}
return s
}
// Truncate a string down to x words
func truncateWords(s string, x int) string {
words := strings.Split(s, " ")
if len(words) <= x {
return s
}
return strings.Join(words[0:x], " ")
}
// Convert an input string to uppercase
func upper(s string) string {
return strings.ToUpper(s)
}