Skip to content

Commit 05d432e

Browse files
authored
Merge pull request #586 from khos2ow/preserve-content-spaces
Preserve whitespaces of provided content and template
2 parents 31344cf + 5727f8b commit 05d432e

File tree

10 files changed

+104
-86
lines changed

10 files changed

+104
-86
lines changed

docs/user-guide/configuration/content.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ content: |-
6969
7070
{{ .Header }}
7171
72-
and even in between sections
72+
and even in between sections. also spaces will be preserved:
73+
74+
- item 1
75+
- item 1-1
76+
- item 1-2
77+
- item 2
78+
- item 3
7379
7480
{{ .Providers }}
7581

examples/.terraform-docs.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# version: ">= 0.10, < 0.12"
33

44
# see: https://terraform-docs.io/user-guide/configuration/formatter
5-
# formatter: markdown table
6-
formatter: template
5+
formatter: markdown table
76

87
# see: https://terraform-docs.io/user-guide/configuration/header-from
98
header-from: doc.txt
@@ -31,7 +30,13 @@ sections:
3130
#
3231
# {{ .Header }}
3332
#
34-
# and even in between sections
33+
# and even in between sections. also spaces will be preserved:
34+
#
35+
# - item 1
36+
# - item 1-1
37+
# - item 1-2
38+
# - item 2
39+
# - item 3
3540
#
3641
# ## Resources
3742
# {{ range .Module.Resources }}

format/pretty.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ type pretty struct {
3535
// NewPretty returns new instance of Pretty.
3636
func NewPretty(config *print.Config) Type {
3737
tt := template.New(config, &template.Item{
38-
Name: "pretty",
39-
Text: string(prettyTpl),
38+
Name: "pretty",
39+
Text: string(prettyTpl),
40+
TrimSpace: true,
4041
})
4142
tt.CustomFunc(gotemplate.FuncMap{
4243
"colorize": func(c string, s string) string {

format/tfvars_hcl.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var padding []int
3838
// NewTfvarsHCL returns new instance of TfvarsHCL.
3939
func NewTfvarsHCL(config *print.Config) Type {
4040
tt := template.New(config, &template.Item{
41-
Name: "tfvars",
42-
Text: string(tfvarsHCLTpl),
41+
Name: "tfvars",
42+
Text: string(tfvarsHCLTpl),
43+
TrimSpace: true,
4344
})
4445
tt.CustomFunc(gotemplate.FuncMap{
4546
"align": func(s string, i int) string {

format/util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ func readTemplateItems(efs embed.FS, prefix string) []*template.Item {
9595
}
9696

9797
items = append(items, &template.Item{
98-
Name: name,
99-
Text: string(content),
98+
Name: name,
99+
Text: string(content),
100+
TrimSpace: true,
100101
})
101102
}
102103
return items

go.mod

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,18 @@ go 1.16
55
require (
66
github.com/BurntSushi/toml v0.4.1
77
github.com/Masterminds/sprig/v3 v3.2.2
8-
github.com/agext/levenshtein v1.2.3 // indirect
9-
github.com/google/uuid v1.3.0 // indirect
108
github.com/hashicorp/go-hclog v1.0.0
119
github.com/hashicorp/go-plugin v1.4.3
1210
github.com/hashicorp/go-version v1.4.0
1311
github.com/hashicorp/hcl/v2 v2.11.1
14-
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
15-
github.com/huandu/xstrings v1.3.2 // indirect
1612
github.com/iancoleman/orderedmap v0.2.0
1713
github.com/imdario/mergo v0.3.12
18-
github.com/mitchellh/copystructure v1.2.0 // indirect
1914
github.com/mitchellh/go-homedir v1.1.0
20-
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
21-
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
22-
github.com/oklog/run v1.1.0 // indirect
23-
github.com/shopspring/decimal v1.3.1 // indirect
24-
github.com/spf13/afero v1.8.0 // indirect
2515
github.com/spf13/cobra v1.3.0
2616
github.com/spf13/pflag v1.0.5
2717
github.com/spf13/viper v1.10.1
2818
github.com/stretchr/testify v1.7.0
2919
github.com/terraform-docs/terraform-config-inspect v0.0.0-20210728164355-9c1f178932fa
30-
github.com/zclconf/go-cty v1.10.0 // indirect
31-
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
32-
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 // indirect
33-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
34-
google.golang.org/genproto v0.0.0-20220106181925-4b6d468c965f // indirect
3520
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
3621
honnef.co/go/tools v0.2.0
3722
mvdan.cc/xurls/v2 v2.3.0

go.sum

Lines changed: 16 additions & 48 deletions
Large diffs are not rendered by default.

template/doc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ the root directory of this source tree.
3131
//
3232
// func render(config *print.Config, module *terraform.Module) (string, error) {
3333
// tt := template.New(config, &template.Item{
34-
// Name: "main",
35-
// Text: mainTpl,
34+
// Name: "main",
35+
// Text: mainTpl,
36+
// TrimSpace: true,
3637
// })
3738
//
3839
// tt := template.New(config, items...)

template/template.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525

2626
// Item represents a named templated which can reference other named templated too.
2727
type Item struct {
28-
Name string
29-
Text string
28+
Name string
29+
Text string
30+
TrimSpace bool
3031
}
3132

3233
// Template represents a new Template with given name and content to be rendered
@@ -102,12 +103,12 @@ func (t *Template) RenderContent(name string, data interface{}) (string, error)
102103

103104
tmpl := gotemplate.New(item.Name)
104105
tmpl.Funcs(t.funcMap)
105-
gotemplate.Must(tmpl.Parse(normalize(item.Text)))
106+
gotemplate.Must(tmpl.Parse(normalize(item.Text, item.TrimSpace)))
106107

107108
for _, ii := range t.items {
108109
tt := tmpl.New(ii.Name)
109110
tt.Funcs(t.funcMap)
110-
gotemplate.Must(tt.Parse(normalize(ii.Text)))
111+
gotemplate.Must(tt.Parse(normalize(ii.Text, ii.TrimSpace)))
111112
}
112113

113114
if err := tmpl.ExecuteTemplate(&buffer, item.Name, data); err != nil {
@@ -232,14 +233,15 @@ func builtinFuncs(config *print.Config) gotemplate.FuncMap { // nolint:gocyclo
232233
// normalize the template and remove any space from all the lines. This makes
233234
// it possible to have a indented, human-readable template which doesn't affect
234235
// the rendering of them.
235-
func normalize(s string) string {
236-
segments := strings.Split(s, "\n")
237-
buffer := bytes.NewBufferString("")
238-
for _, segment := range segments {
239-
buffer.WriteString(strings.TrimSpace(segment)) // nolint:gosec
240-
buffer.WriteString("\n") // nolint:gosec
236+
func normalize(s string, trimSpace bool) string {
237+
if !trimSpace {
238+
return s
241239
}
242-
return buffer.String()
240+
splitted := strings.Split(s, "\n")
241+
for i, v := range splitted {
242+
splitted[i] = strings.TrimSpace(v)
243+
}
244+
return strings.Join(splitted, "\n")
243245
}
244246

245247
// GenerateIndentation generates indentation of Markdown and AsciiDoc headers

template/template_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,51 @@ func TestGenerateIndentation(t *testing.T) {
508508
})
509509
}
510510
}
511+
512+
func TestNormalize(t *testing.T) {
513+
tests := []struct {
514+
name string
515+
text string
516+
trim bool
517+
expected string
518+
}{
519+
{
520+
name: "normalize with trim space",
521+
text: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
522+
trim: true,
523+
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
524+
},
525+
{
526+
name: "normalize with trim space",
527+
text: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit\n",
528+
trim: true,
529+
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit\n",
530+
},
531+
{
532+
name: "normalize with trim space",
533+
text: "Lorem ipsum\ndolor sit amet,\n consectetur\nadipiscing\nelit",
534+
trim: true,
535+
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
536+
},
537+
{
538+
name: "normalize without trim space",
539+
text: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
540+
trim: false,
541+
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
542+
},
543+
{
544+
name: "normalize without trim space",
545+
text: "Lorem ipsum\ndolor sit amet,\n consectetur\nadipiscing\nelit",
546+
trim: false,
547+
expected: "Lorem ipsum\ndolor sit amet,\n consectetur\nadipiscing\nelit",
548+
},
549+
}
550+
for _, tt := range tests {
551+
t.Run(tt.name, func(t *testing.T) {
552+
assert := assert.New(t)
553+
actual := normalize(tt.text, tt.trim)
554+
555+
assert.Equal(tt.expected, actual)
556+
})
557+
}
558+
}

0 commit comments

Comments
 (0)