Skip to content

Commit 59ab72c

Browse files
Apply default docs edit rules to installation docs (#2211)
Adds default doc edit rules plus a few more installation-doc specific edit rules to the plain docs parser functionality. Fixes #2208
1 parent 1c84840 commit 59ab72c

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

pkg/tfgen/installation_docs.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
2323

2424
//TODO: See https://github.com/pulumi/pulumi-terraform-bridge/issues/2078
2525
// - translate code blocks with code choosers
26-
// - apply default edit rules
2726
// - reformat TF names
28-
// - Translation for certain headers such as "Arguments Reference" or "Configuration block"
2927
// - Ability to omit irrelevant sections
30-
return []byte(contentStr), nil
28+
29+
// Apply edit rules to transform the doc for Pulumi-ready presentation
30+
contentBytes, err := applyEditRules([]byte(contentStr), docFile)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return contentBytes, nil
3135
}
3236

3337
func writeFrontMatter(title string) string {
@@ -91,3 +95,32 @@ func writeInstallationInstructions(goImportBasePath, providerName string) string
9195
goImportBasePath,
9296
)
9397
}
98+
99+
func applyEditRules(contentBytes []byte, docFile *DocFile) ([]byte, error) {
100+
// Obtain default edit rules for documentation files
101+
edits := defaultEditRules()
102+
103+
// Additional edit rules for installation files
104+
edits = append(edits,
105+
// Replace all "T/terraform" with "P/pulumi"
106+
reReplace(`Terraform`, `Pulumi`),
107+
reReplace(`terraform`, `pulumi`),
108+
// Replace all "H/hashicorp" strings
109+
reReplace(`Hashicorp`, `Pulumi`),
110+
reReplace(`hashicorp`, `pulumi`),
111+
// Reformat certain headers
112+
reReplace(`The following arguments are supported`,
113+
`The following configuration inputs are supported`),
114+
reReplace(`Argument Reference`,
115+
`Configuration Reference`),
116+
reReplace(`block contains the following arguments`,
117+
`input has the following nested fields`))
118+
var err error
119+
for _, rule := range edits {
120+
contentBytes, err = rule.Edit(docFile.FileName, contentBytes)
121+
if err != nil {
122+
return nil, err
123+
}
124+
}
125+
return contentBytes, nil
126+
}

pkg/tfgen/installation_docs_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,51 @@ func TestWriteIndexFrontMatter(t *testing.T) {
136136
require.Equal(t, tc.expected, actual)
137137
})
138138
}
139+
140+
func TestApplyEditRules(t *testing.T) {
141+
t.Parallel()
142+
143+
type testCase struct {
144+
// The name of the test case.
145+
name string
146+
docFile DocFile
147+
expected []byte
148+
}
149+
150+
tests := []testCase{
151+
{
152+
name: "Replaces h/Hashicorp With p/Pulumi",
153+
docFile: DocFile{
154+
Content: []byte("Any mention of Hashicorp or hashicorp will be Pulumi or pulumi"),
155+
},
156+
expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"),
157+
},
158+
{
159+
name: "Replaces t/Terraform With p/Pulumi",
160+
docFile: DocFile{
161+
Content: []byte("Any mention of Terraform or terraform will be Pulumi or pulumi"),
162+
},
163+
expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"),
164+
},
165+
{
166+
name: "Replaces argument headers with input headers",
167+
docFile: DocFile{
168+
Content: []byte("# Argument Reference\n" +
169+
"The following arguments are supported:\n* `some_argument`\n" +
170+
"block contains the following arguments"),
171+
},
172+
expected: []byte("# Configuration Reference\n" +
173+
"The following configuration inputs are supported:\n* `some_argument`\n" +
174+
"input has the following nested fields"),
175+
},
176+
}
177+
for _, tt := range tests {
178+
tt := tt
179+
t.Run(tt.name, func(t *testing.T) {
180+
t.Parallel()
181+
actual, err := applyEditRules(tt.docFile.Content, &tt.docFile)
182+
require.NoError(t, err)
183+
require.Equal(t, string(tt.expected), string(actual))
184+
})
185+
}
186+
}

0 commit comments

Comments
 (0)