@@ -25,8 +25,9 @@ import (
2525
2626// Item represents a named templated which can reference other named templated too.
2727type 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
0 commit comments