Skip to content

Commit

Permalink
Move format package from internal to public
Browse files Browse the repository at this point in the history
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
  • Loading branch information
khos2ow committed Sep 28, 2021
1 parent ca8f833 commit 90942f7
Show file tree
Hide file tree
Showing 248 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewAsciidocDocument(settings *print.Settings) print.Engine {
tt := template.New(settings, items...)
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {
result, extraline := printFencedAsciidocCodeBlock(t, "hcl")
result, extraline := PrintFencedAsciidocCodeBlock(t, "hcl")
if !extraline {
result += "\n"
}
Expand All @@ -47,7 +47,7 @@ func NewAsciidocDocument(settings *print.Settings) print.Engine {
if v == "n/a" {
return v
}
result, extraline := printFencedAsciidocCodeBlock(v, "json")
result, extraline := PrintFencedAsciidocCodeBlock(v, "json")
if !extraline {
result += "\n"
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func NewAsciidocTable(settings *print.Settings) print.Engine {
tt := template.New(settings, items...)
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {
inputType, _ := printFencedCodeBlock(t, "")
inputType, _ := PrintFencedCodeBlock(t, "")
return inputType
},
"value": func(v string) string {
var result = "n/a"
if v != "" {
result, _ = printFencedCodeBlock(v, "")
result, _ = PrintFencedCodeBlock(v, "")
}
return result
},
Expand Down
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions format/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 provides different, out of the box supported, output formats.
//
// Usage
//
// A specific format can be instantiated either for `format.Factory()` function or
// directly calling its function (e.g. `NewMarkdownTable`, etc)
//
// formatter, err := format.Factory("markdown table", settings)
// if err != nil {
// return err
// }
//
// generator, err := formatter.Generate(tfmodule)
// if err != nil {
// return err
// }
//
// output, err := generator.ExecuteTemplate("")
// if err != nil {
// return err
// }
//
// Supported formats are:
//
// • `NewAsciidocDocument`
// • `NewAsciidocTable`
// • `NewJSON`
// • `NewMarkdownDocument`
// • `NewMarkdownTable`
// • `NewPretty`
// • `NewTfvarsHCL`
// • `NewTfvarsJSON`
// • `NewTOML`
// • `NewXML`
// • `NewYAML`
//
package format
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewMarkdownDocument(settings *print.Settings) print.Engine {
tt := template.New(settings, items...)
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {
result, extraline := printFencedCodeBlock(t, "hcl")
result, extraline := PrintFencedCodeBlock(t, "hcl")
if !extraline {
result += "\n"
}
Expand All @@ -45,7 +45,7 @@ func NewMarkdownDocument(settings *print.Settings) print.Engine {
if v == "n/a" {
return v
}
result, extraline := printFencedCodeBlock(v, "json")
result, extraline := PrintFencedCodeBlock(v, "json")
if !extraline {
result += "\n"
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func NewMarkdownTable(settings *print.Settings) print.Engine {
tt := template.New(settings, items...)
tt.CustomFunc(gotemplate.FuncMap{
"type": func(t string) string {
inputType, _ := printFencedCodeBlock(t, "")
inputType, _ := PrintFencedCodeBlock(t, "")
return inputType
},
"value": func(v string) string {
var result = "n/a"
if v != "" {
result, _ = printFencedCodeBlock(v, "")
result, _ = PrintFencedCodeBlock(v, "")
}
return result
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/*
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.
*/
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions internal/format/util.go → format/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ func sanitize(markdown string) string {
return result
}

// printFencedCodeBlock prints codes in fences, it automatically detects if
// PrintFencedCodeBlock prints codes in fences, it automatically detects if
// the input 'code' contains '\n' it will use multi line fence, otherwise it
// wraps the 'code' inside single-tick block.
// If the fenced is multi-line it also appens an extra '\n` at the end and
// returns true accordingly, otherwise returns false for non-carriage return.
func printFencedCodeBlock(code string, language string) (string, bool) {
func PrintFencedCodeBlock(code string, language string) (string, bool) {
if strings.Contains(code, "\n") {
return fmt.Sprintf("\n\n```%s\n%s\n```\n", language, code), true
}
return fmt.Sprintf("`%s`", code), false
}

// printFencedAsciidocCodeBlock prints codes in fences, it automatically detects if
// PrintFencedAsciidocCodeBlock prints codes in fences, it automatically detects if
// the input 'code' contains '\n' it will use multi line fence, otherwise it
// wraps the 'code' inside single-tick block.
// If the fenced is multi-line it also appens an extra '\n` at the end and
// returns true accordingly, otherwise returns false for non-carriage return.
func printFencedAsciidocCodeBlock(code string, language string) (string, bool) {
func PrintFencedAsciidocCodeBlock(code string, language string) (string, bool) {
if strings.Contains(code, "\n") {
return fmt.Sprintf("\n[source,%s]\n----\n%s\n----\n", language, code), true
}
Expand Down
2 changes: 1 addition & 1 deletion internal/format/util_test.go → format/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestFenceCodeBlock(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
actual, extraline := printFencedCodeBlock(tt.code, tt.language)
actual, extraline := PrintFencedCodeBlock(tt.code, tt.language)

assert.Equal(tt.expected, actual)
assert.Equal(tt.extraline, extraline)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/spf13/viper"

pluginsdk "github.com/terraform-docs/plugin-sdk/plugin"
"github.com/terraform-docs/terraform-docs/internal/format"
"github.com/terraform-docs/terraform-docs/format"
"github.com/terraform-docs/terraform-docs/internal/plugin"
"github.com/terraform-docs/terraform-docs/internal/version"
"github.com/terraform-docs/terraform-docs/terraform"
Expand Down
2 changes: 1 addition & 1 deletion scripts/docs/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"

"github.com/terraform-docs/terraform-docs/cmd"
"github.com/terraform-docs/terraform-docs/internal/format"
"github.com/terraform-docs/terraform-docs/format"
"github.com/terraform-docs/terraform-docs/internal/print"
"github.com/terraform-docs/terraform-docs/terraform"
)
Expand Down

0 comments on commit 90942f7

Please sign in to comment.