@@ -3,6 +3,7 @@ package textutil
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"strings"
8
9
"text/template"
@@ -74,8 +75,46 @@ var TemplateFuncMap = template.FuncMap{
74
75
}
75
76
return "---\n " + strings .TrimSuffix (b .String (), "\n " )
76
77
},
77
- "indent" : IndentString ,
78
- "missing" : MissingString ,
78
+ "indent" : func (a ... interface {}) (string , error ) {
79
+ if len (a ) == 0 {
80
+ return "" , errors .New ("function takes at at least one string argument" )
81
+ }
82
+ if len (a ) > 2 {
83
+ return "" , errors .New ("function takes at most 2 arguments" )
84
+ }
85
+ var ok bool
86
+ size := 2
87
+ if len (a ) > 1 {
88
+ if size , ok = a [0 ].(int ); ! ok {
89
+ return "" , errors .New ("optional first argument must be an integer" )
90
+ }
91
+ }
92
+ text := ""
93
+ if text , ok = a [len (a )- 1 ].(string ); ! ok {
94
+ return "" , errors .New ("last argument must be a string" )
95
+ }
96
+ return IndentString (size , text ), nil
97
+ },
98
+ "missing" : func (a ... interface {}) (string , error ) {
99
+ if len (a ) == 0 {
100
+ return "" , errors .New ("function takes at at least one string argument" )
101
+ }
102
+ if len (a ) > 2 {
103
+ return "" , errors .New ("function takes at most 2 arguments" )
104
+ }
105
+ var ok bool
106
+ message := "<missing>"
107
+ if len (a ) > 1 {
108
+ if message , ok = a [0 ].(string ); ! ok {
109
+ return "" , errors .New ("optional first argument must be a string" )
110
+ }
111
+ }
112
+ text := ""
113
+ if text , ok = a [len (a )- 1 ].(string ); ! ok {
114
+ return "" , errors .New ("last argument must be a string" )
115
+ }
116
+ return MissingString (message , text ), nil
117
+ },
79
118
}
80
119
81
120
// TemplateFuncHelp is help for TemplateFuncMap.
0 commit comments