Files
terraform-docs/format/markdown_table.go
Khosrow Moossavi 465dd14cff Make terraform.Module available in content
Add one extra special variable the `content`:

- `{{ .Module }}`

As opposed to the other variables, which are generated sections based on
a selected formatter, the `{{ .Module }}` variable is just a `struct`
representing a Terraform module.

It can be used to build highly complex and highly customized content:

```yaml
content: |-
  ## Resources

  {{ range .Module.Resources }}
  - {{ .GetMode }}.{{ .Spec }} ({{ .Position.Filename }}#{{ .Position.Line }})
  {{- end }}
```

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-10-04 20:01:44 -04:00

84 lines
1.9 KiB
Go

/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package format
import (
"embed"
gotemplate "text/template"
"github.com/terraform-docs/terraform-docs/print"
"github.com/terraform-docs/terraform-docs/template"
"github.com/terraform-docs/terraform-docs/terraform"
)
//go:embed templates/markdown_table*.tmpl
var markdownTableFS embed.FS
// markdownTable represents Markdown Table format.
type markdownTable struct {
*generator
config *print.Config
template *template.Template
}
// NewMarkdownTable returns new instance of Markdown Table.
func NewMarkdownTable(config *print.Config) Type {
items := readTemplateItems(markdownTableFS, "markdown_table")
tt := template.New(config, items...)
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {
inputType, _ := PrintFencedCodeBlock(t, "")
return inputType
},
"value": func(v string) string {
var result = "n/a"
if v != "" {
result, _ = PrintFencedCodeBlock(v, "")
}
return result
},
})
return &markdownTable{
generator: newGenerator(config, true),
config: config,
template: tt,
}
}
// Generate a Terraform module as Markdown tables.
func (t *markdownTable) Generate(module *terraform.Module) error {
err := t.generator.forEach(func(name string) (string, error) {
rendered, err := t.template.Render(name, module)
if err != nil {
return "", err
}
return sanitize(rendered), nil
})
t.generator.funcs(withModule(module))
return err
}
func init() {
register(map[string]initializerFn{
"markdown": NewMarkdownTable,
"markdown table": NewMarkdownTable,
"markdown tbl": NewMarkdownTable,
"md": NewMarkdownTable,
"md table": NewMarkdownTable,
"md tbl": NewMarkdownTable,
})
}