Skip to content

Commit da30c45

Browse files
committed
Add extended indentation function
Added a strings function to support a much more broad variety of indentation needs. In addition to specifying a custom indentation string, it also inherently: - fixes Masterminds#357 - fixes Masterminds#314 (with some careful awareness and in-template programmating addition/subtraction) - obsoletes Masterminds#434
1 parent 989da45 commit da30c45

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

docs/strings.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ nindent 4 $lots_of_text
268268
The above will indent every line of text by 4 space characters and add a new
269269
line to the beginning.
270270

271+
## extindent
272+
273+
The `extindent` function offers a much more flexible extended indenter
274+
at the cost of complexity.
275+
276+
It allows for a customized indentation and indentation behavor but
277+
requires 5 arguments in addition to the string to be indented.
278+
279+
```
280+
$levels := 1
281+
$skipFirst := false
282+
$skipEmpty := true
283+
$skipWs := true
284+
$indentString := "\t"
285+
286+
extindent $levels $skipFirst $skipEmpty $skipWs $indentString $lots_of_text
287+
```
288+
289+
The above will indent every line of `$lots_of_text` with a single (`1`) `\t` (horizontal tab),
290+
*unless* that line is entirely empty (only a newline) or is comprised only entirely of whitespace
291+
(tabs, spaces, etc.).
292+
271293
## replace
272294

273295
Perform simple string replacement.

functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ var genericMap = map[string]interface{}{
157157
"cat": cat,
158158
"indent": indent,
159159
"nindent": nindent,
160+
"extindent": extIndent,
160161
"replace": replace,
161162
"plural": plural,
162163
"sha1sum": sha1sum,

strings.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ func nindent(spaces int, v string) string {
115115
return "\n" + indent(spaces, v)
116116
}
117117

118+
func extIndent(levels int, skipFirst, skipEmpty, skipWhitespace bool, indentStr, v string) string {
119+
pad := strings.Repeat(indentStr, int(levels))
120+
lines := strings.Split(v, "\n")
121+
for idx, line := range lines {
122+
if idx == 0 && skipFirst {
123+
continue
124+
}
125+
if skipWhitespace && strings.TrimSpace(line) == "" && line != "" {
126+
continue
127+
}
128+
if skipEmpty && (line == "" || line == "\r") {
129+
continue
130+
}
131+
lines[idx] = pad + line
132+
}
133+
return strings.Join(lines, "\n")
134+
}
135+
118136
func replace(old, new, src string) string {
119137
return strings.Replace(src, old, new, -1)
120138
}

strings_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,50 @@ func TestNindent(t *testing.T) {
280280
}
281281
}
282282

283+
func TestExtIndent(t *testing.T) {
284+
285+
cmpstr := "1\nmt\n\nws\n\t\n."
286+
287+
for _, tCase := range []struct{
288+
indt string
289+
lvls int
290+
skipFirst bool
291+
skipEmpty bool
292+
skipWs bool
293+
tgt string
294+
295+
}{
296+
{indt: "\t", lvls: 1, skipFirst: true, skipEmpty: true, skipWs: true, tgt: "1\n\tmt\n\n\tws\n\t\n\t."},
297+
{indt: "\t", lvls: 1, skipFirst: true, skipEmpty: true, skipWs: false, tgt: "1\n\tmt\n\n\tws\n\t\t\n\t."},
298+
{indt: "\t", lvls: 1, skipFirst: true, skipEmpty: false, skipWs: true, tgt: "1\n\tmt\n\t\n\tws\n\t\n\t."},
299+
{indt: "\t", lvls: 1, skipFirst: true, skipEmpty: false, skipWs: false, tgt: "1\n\tmt\n\t\n\tws\n\t\t\n\t."},
300+
{indt: "\t", lvls: 1, skipFirst: false, skipEmpty: true, skipWs: true, tgt: "\t1\n\tmt\n\n\tws\n\t\n\t."},
301+
{indt: "\t", lvls: 1, skipFirst: false, skipEmpty: true, skipWs: false, tgt: "\t1\n\tmt\n\n\tws\n\t\t\n\t."},
302+
{indt: "\t", lvls: 1, skipFirst: false, skipEmpty: false, skipWs: true, tgt: "\t1\n\tmt\n\t\n\tws\n\t\n\t."},
303+
{indt: "\t", lvls: 1, skipFirst: false, skipEmpty: false, skipWs: false, tgt: "\t1\n\tmt\n\t\n\tws\n\t\t\n\t."},
304+
} {
305+
tplStr := fmt.Sprintf(
306+
"{{- $lvls := %d -}}\n"+
307+
"{{- $no1st := %v -}}\n"+
308+
"{{- $noMT := %v -}}\n"+
309+
"{{- $noWs := %v -}}\n"+
310+
"{{- $idtstr := \"%s\" -}}\n"+
311+
"{{- $tstr := `%s` -}}\n"+
312+
"{{ $tstr | extindent $lvls $no1st $noMT $noWs $idtstr }}",
313+
tCase.lvls,
314+
tCase.skipFirst,
315+
tCase.skipEmpty,
316+
tCase.skipWs,
317+
tCase.indt,
318+
cmpstr,
319+
)
320+
t.Log(tplStr)
321+
if err := runt(tplStr, tCase.tgt); err != nil {
322+
t.Error(err)
323+
}
324+
}
325+
}
326+
283327
func TestReplace(t *testing.T) {
284328
tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
285329
if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {

0 commit comments

Comments
 (0)