Skip to content

Commit

Permalink
Apply default docs edit rules to installation docs (#2211)
Browse files Browse the repository at this point in the history
Adds default doc edit rules plus a few more installation-doc specific
edit rules to the plain docs parser functionality.

Fixes #2208
  • Loading branch information
guineveresaenger authored Jul 19, 2024
1 parent 1c84840 commit 59ab72c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
39 changes: 36 additions & 3 deletions pkg/tfgen/installation_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {

//TODO: See https://github.com/pulumi/pulumi-terraform-bridge/issues/2078
// - translate code blocks with code choosers
// - apply default edit rules
// - reformat TF names
// - Translation for certain headers such as "Arguments Reference" or "Configuration block"
// - Ability to omit irrelevant sections
return []byte(contentStr), nil

// Apply edit rules to transform the doc for Pulumi-ready presentation
contentBytes, err := applyEditRules([]byte(contentStr), docFile)
if err != nil {
return nil, err
}
return contentBytes, nil
}

func writeFrontMatter(title string) string {
Expand Down Expand Up @@ -91,3 +95,32 @@ func writeInstallationInstructions(goImportBasePath, providerName string) string
goImportBasePath,
)
}

func applyEditRules(contentBytes []byte, docFile *DocFile) ([]byte, error) {
// Obtain default edit rules for documentation files
edits := defaultEditRules()

// Additional edit rules for installation files
edits = append(edits,
// Replace all "T/terraform" with "P/pulumi"
reReplace(`Terraform`, `Pulumi`),
reReplace(`terraform`, `pulumi`),
// Replace all "H/hashicorp" strings
reReplace(`Hashicorp`, `Pulumi`),
reReplace(`hashicorp`, `pulumi`),
// Reformat certain headers
reReplace(`The following arguments are supported`,
`The following configuration inputs are supported`),
reReplace(`Argument Reference`,
`Configuration Reference`),
reReplace(`block contains the following arguments`,
`input has the following nested fields`))
var err error
for _, rule := range edits {
contentBytes, err = rule.Edit(docFile.FileName, contentBytes)
if err != nil {
return nil, err
}
}
return contentBytes, nil
}
48 changes: 48 additions & 0 deletions pkg/tfgen/installation_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,51 @@ func TestWriteIndexFrontMatter(t *testing.T) {
require.Equal(t, tc.expected, actual)
})
}

func TestApplyEditRules(t *testing.T) {
t.Parallel()

type testCase struct {
// The name of the test case.
name string
docFile DocFile
expected []byte
}

tests := []testCase{
{
name: "Replaces h/Hashicorp With p/Pulumi",
docFile: DocFile{
Content: []byte("Any mention of Hashicorp or hashicorp will be Pulumi or pulumi"),
},
expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"),
},
{
name: "Replaces t/Terraform With p/Pulumi",
docFile: DocFile{
Content: []byte("Any mention of Terraform or terraform will be Pulumi or pulumi"),
},
expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"),
},
{
name: "Replaces argument headers with input headers",
docFile: DocFile{
Content: []byte("# Argument Reference\n" +
"The following arguments are supported:\n* `some_argument`\n" +
"block contains the following arguments"),
},
expected: []byte("# Configuration Reference\n" +
"The following configuration inputs are supported:\n* `some_argument`\n" +
"input has the following nested fields"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
actual, err := applyEditRules(tt.docFile.Content, &tt.docFile)
require.NoError(t, err)
require.Equal(t, string(tt.expected), string(actual))
})
}
}

0 comments on commit 59ab72c

Please sign in to comment.