Skip to content

Preserve whitespaces of provided content and template #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/user-guide/configuration/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ content: |-

{{ .Header }}

and even in between sections
and even in between sections. also spaces will be preserved:

- item 1
- item 1-1
- item 1-2
- item 2
- item 3

{{ .Providers }}

Expand Down
11 changes: 8 additions & 3 deletions examples/.terraform-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# version: ">= 0.10, < 0.12"

# see: https://terraform-docs.io/user-guide/configuration/formatter
# formatter: markdown table
formatter: template
formatter: markdown table

# see: https://terraform-docs.io/user-guide/configuration/header-from
header-from: doc.txt
Expand Down Expand Up @@ -31,7 +30,13 @@ sections:
#
# {{ .Header }}
#
# and even in between sections
# and even in between sections. also spaces will be preserved:
#
# - item 1
# - item 1-1
# - item 1-2
# - item 2
# - item 3
#
# ## Resources
# {{ range .Module.Resources }}
Expand Down
5 changes: 3 additions & 2 deletions format/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type pretty struct {
// NewPretty returns new instance of Pretty.
func NewPretty(config *print.Config) Type {
tt := template.New(config, &template.Item{
Name: "pretty",
Text: string(prettyTpl),
Name: "pretty",
Text: string(prettyTpl),
TrimSpace: true,
})
tt.CustomFunc(gotemplate.FuncMap{
"colorize": func(c string, s string) string {
Expand Down
5 changes: 3 additions & 2 deletions format/tfvars_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ var padding []int
// NewTfvarsHCL returns new instance of TfvarsHCL.
func NewTfvarsHCL(config *print.Config) Type {
tt := template.New(config, &template.Item{
Name: "tfvars",
Text: string(tfvarsHCLTpl),
Name: "tfvars",
Text: string(tfvarsHCLTpl),
TrimSpace: true,
})
tt.CustomFunc(gotemplate.FuncMap{
"align": func(s string, i int) string {
Expand Down
5 changes: 3 additions & 2 deletions format/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ func readTemplateItems(efs embed.FS, prefix string) []*template.Item {
}

items = append(items, &template.Item{
Name: name,
Text: string(content),
Name: name,
Text: string(content),
TrimSpace: true,
})
}
return items
Expand Down
15 changes: 0 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,18 @@ go 1.16
require (
github.com/BurntSushi/toml v0.4.1
github.com/Masterminds/sprig/v3 v3.2.2
github.com/agext/levenshtein v1.2.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-hclog v1.0.0
github.com/hashicorp/go-plugin v1.4.3
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/iancoleman/orderedmap v0.2.0
github.com/imdario/mergo v0.3.12
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/afero v1.8.0 // indirect
github.com/spf13/cobra v1.3.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
github.com/terraform-docs/terraform-config-inspect v0.0.0-20210728164355-9c1f178932fa
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
google.golang.org/genproto v0.0.0-20220106181925-4b6d468c965f // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
honnef.co/go/tools v0.2.0
mvdan.cc/xurls/v2 v2.3.0
Expand Down
64 changes: 16 additions & 48 deletions go.sum

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions template/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ the root directory of this source tree.
//
// func render(config *print.Config, module *terraform.Module) (string, error) {
// tt := template.New(config, &template.Item{
// Name: "main",
// Text: mainTpl,
// Name: "main",
// Text: mainTpl,
// TrimSpace: true,
// })
//
// tt := template.New(config, items...)
Expand Down
24 changes: 13 additions & 11 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (

// Item represents a named templated which can reference other named templated too.
type Item struct {
Name string
Text string
Name string
Text string
TrimSpace bool
}

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

tmpl := gotemplate.New(item.Name)
tmpl.Funcs(t.funcMap)
gotemplate.Must(tmpl.Parse(normalize(item.Text)))
gotemplate.Must(tmpl.Parse(normalize(item.Text, item.TrimSpace)))

for _, ii := range t.items {
tt := tmpl.New(ii.Name)
tt.Funcs(t.funcMap)
gotemplate.Must(tt.Parse(normalize(ii.Text)))
gotemplate.Must(tt.Parse(normalize(ii.Text, ii.TrimSpace)))
}

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

// GenerateIndentation generates indentation of Markdown and AsciiDoc headers
Expand Down
48 changes: 48 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,51 @@ func TestGenerateIndentation(t *testing.T) {
})
}
}

func TestNormalize(t *testing.T) {
tests := []struct {
name string
text string
trim bool
expected string
}{
{
name: "normalize with trim space",
text: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
trim: true,
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
},
{
name: "normalize with trim space",
text: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit\n",
trim: true,
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit\n",
},
{
name: "normalize with trim space",
text: "Lorem ipsum\ndolor sit amet,\n consectetur\nadipiscing\nelit",
trim: true,
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
},
{
name: "normalize without trim space",
text: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
trim: false,
expected: "Lorem ipsum\ndolor sit amet,\nconsectetur\nadipiscing\nelit",
},
{
name: "normalize without trim space",
text: "Lorem ipsum\ndolor sit amet,\n consectetur\nadipiscing\nelit",
trim: false,
expected: "Lorem ipsum\ndolor sit amet,\n consectetur\nadipiscing\nelit",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
actual := normalize(tt.text, tt.trim)

assert.Equal(tt.expected, actual)
})
}
}