2020-05-08 23:25:28 +00:00
|
|
|
// Copyright 2016-2020, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Pulling out some of the repeated strings tokens into constants would harm readability, so we just ignore the
|
|
|
|
// goconst linter's warning.
|
|
|
|
//
|
2023-01-06 00:07:45 +00:00
|
|
|
//nolint:lll, goconst
|
2020-05-08 23:25:28 +00:00
|
|
|
package docs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
"github.com/pgavlin/goldmark/ast"
|
|
|
|
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
2020-05-08 23:25:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultMissingExampleSnippetPlaceholder = "Coming soon!"
|
|
|
|
|
2024-07-09 14:54:50 +00:00
|
|
|
type examplesSection struct {
|
|
|
|
// Examples is a list of exampleSections. Each exampleSection contains a title and code snippets
|
|
|
|
Examples []exampleSection
|
|
|
|
// LangChooserLanguages is a comma-separated list of languages to pass to the
|
|
|
|
// language chooser shortcode. Use this to customize the languages shown for a
|
|
|
|
// resource. By default, the language chooser will show all languages supported
|
|
|
|
// by Pulumi for all resources.
|
|
|
|
// Supported values are "typescript", "python", "go", "csharp", "java", "yaml"
|
|
|
|
LangChooserLanguages string
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:25:28 +00:00
|
|
|
type exampleSection struct {
|
|
|
|
Title string
|
|
|
|
// Snippets is a map of language to its code snippet, if any.
|
|
|
|
Snippets map[string]string
|
|
|
|
}
|
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
type docInfo struct {
|
2020-11-09 14:12:58 +00:00
|
|
|
description string
|
|
|
|
examples []exampleSection
|
|
|
|
importDetails string
|
2020-06-18 19:32:15 +00:00
|
|
|
}
|
2020-05-08 23:25:28 +00:00
|
|
|
|
2024-07-09 14:54:50 +00:00
|
|
|
func (dctx *docGenContext) decomposeDocstring(docstring, supportedSnippetLanguages string) docInfo {
|
2020-06-18 19:32:15 +00:00
|
|
|
if docstring == "" {
|
|
|
|
return docInfo{}
|
2020-05-08 23:25:28 +00:00
|
|
|
}
|
Add ability to process docs without dedicated Examples logic (#15475)
This pull request depends on corresponding changes in the
pulumi-terraform bridge.
The bridge will mark up each code section it finds (not just ones found
under an "Example Usage" header) with an HTML comment - currently this
will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but
the name is subject to change.
In this PR, we are adding logic to detect if an incoming Description
block has an `{{% examples }}` shortcode markup: if yes, we will process
it as before. If no, we will switch to new functionality that relies on
documentation with code sections having no short codes, no "Examples
Section" logic, and will generate every code section it finds in an
incoming schema that is in the Description/Content section of a registry
page with a chooser and language choosables.
This work is part of https://github.com/pulumi/pulumi-aws/issues/2624.
Edit: unit tests are added
This PR is a prerequisite for
https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to
support both legacy and new behavior.
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
Fixes # (issue)
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-02-23 18:06:44 +00:00
|
|
|
if strings.Contains(docstring, beginCodeBlock) {
|
2024-07-09 14:54:50 +00:00
|
|
|
return dctx.processDescription(docstring, supportedSnippetLanguages)
|
Add ability to process docs without dedicated Examples logic (#15475)
This pull request depends on corresponding changes in the
pulumi-terraform bridge.
The bridge will mark up each code section it finds (not just ones found
under an "Example Usage" header) with an HTML comment - currently this
will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but
the name is subject to change.
In this PR, we are adding logic to detect if an incoming Description
block has an `{{% examples }}` shortcode markup: if yes, we will process
it as before. If no, we will switch to new functionality that relies on
documentation with code sections having no short codes, no "Examples
Section" logic, and will generate every code section it finds in an
incoming schema that is in the Description/Content section of a registry
page with a chooser and language choosables.
This work is part of https://github.com/pulumi/pulumi-aws/issues/2624.
Edit: unit tests are added
This PR is a prerequisite for
https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to
support both legacy and new behavior.
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
Fixes # (issue)
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-02-23 18:06:44 +00:00
|
|
|
}
|
2020-05-08 23:25:28 +00:00
|
|
|
|
2021-10-06 15:03:21 +00:00
|
|
|
languages := codegen.NewStringSet(dctx.snippetLanguages...)
|
2020-06-18 19:32:15 +00:00
|
|
|
|
|
|
|
source := []byte(docstring)
|
|
|
|
parsed := schema.ParseDocs(source)
|
|
|
|
|
|
|
|
var examplesShortcode *schema.Shortcode
|
|
|
|
var exampleShortcode *schema.Shortcode
|
|
|
|
var examples []exampleSection
|
2022-10-04 01:06:38 +00:00
|
|
|
currentSection := exampleSection{
|
|
|
|
Snippets: map[string]string{},
|
|
|
|
}
|
|
|
|
var nextTitle string
|
2022-10-04 18:21:57 +00:00
|
|
|
var nextInferredTitle string
|
2022-10-04 01:06:38 +00:00
|
|
|
// Push any examples we have found. Since `pushExamples` is called between sections,
|
|
|
|
// it needs to behave correctly when no examples were found.
|
|
|
|
pushExamples := func() {
|
|
|
|
if len(currentSection.Snippets) > 0 {
|
2022-10-04 17:24:06 +00:00
|
|
|
for _, l := range dctx.snippetLanguages {
|
|
|
|
if _, ok := currentSection.Snippets[l]; !ok {
|
|
|
|
currentSection.Snippets[l] = defaultMissingExampleSnippetPlaceholder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 01:06:38 +00:00
|
|
|
examples = append(examples, currentSection)
|
|
|
|
}
|
2022-10-04 18:21:57 +00:00
|
|
|
if nextTitle == "" {
|
|
|
|
nextTitle = nextInferredTitle
|
|
|
|
}
|
2022-10-04 01:06:38 +00:00
|
|
|
currentSection = exampleSection{
|
|
|
|
Snippets: map[string]string{},
|
|
|
|
Title: nextTitle,
|
|
|
|
}
|
|
|
|
nextTitle = ""
|
2022-10-04 18:21:57 +00:00
|
|
|
nextInferredTitle = ""
|
2022-10-04 01:06:38 +00:00
|
|
|
}
|
2020-06-18 19:32:15 +00:00
|
|
|
err := ast.Walk(parsed, func(n ast.Node, enter bool) (ast.WalkStatus, error) {
|
2022-10-04 01:06:38 +00:00
|
|
|
// ast.Walk visits each node twice. The first time descending and the second time
|
|
|
|
// ascending. We only want to view the nodes while descending, so we skip when
|
|
|
|
// `enter` is false.
|
|
|
|
if !enter {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
2020-06-18 19:32:15 +00:00
|
|
|
if shortcode, ok := n.(*schema.Shortcode); ok {
|
|
|
|
name := string(shortcode.Name)
|
|
|
|
switch name {
|
|
|
|
case schema.ExamplesShortcode:
|
|
|
|
if examplesShortcode == nil {
|
|
|
|
examplesShortcode = shortcode
|
|
|
|
}
|
|
|
|
case schema.ExampleShortcode:
|
|
|
|
if exampleShortcode == nil {
|
2022-10-04 01:06:38 +00:00
|
|
|
exampleShortcode = shortcode
|
|
|
|
currentSection.Title, currentSection.Snippets = "", map[string]string{}
|
2020-06-18 19:32:15 +00:00
|
|
|
} else if !enter && shortcode == exampleShortcode {
|
2022-10-04 01:06:38 +00:00
|
|
|
pushExamples()
|
2020-06-18 19:32:15 +00:00
|
|
|
exampleShortcode = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
2022-10-04 01:06:38 +00:00
|
|
|
|
|
|
|
// We check to make sure we are in an examples section.
|
2020-06-18 19:32:15 +00:00
|
|
|
if exampleShortcode == nil {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n := n.(type) {
|
|
|
|
case *ast.Heading:
|
2022-10-04 01:06:38 +00:00
|
|
|
if n.Level == 3 {
|
|
|
|
title := strings.TrimSpace(schema.RenderDocsToString(source, n))
|
|
|
|
if currentSection.Title == "" && len(currentSection.Snippets) == 0 {
|
|
|
|
currentSection.Title = title
|
|
|
|
} else {
|
|
|
|
nextTitle = title
|
|
|
|
}
|
2020-06-18 19:32:15 +00:00
|
|
|
}
|
2022-10-04 18:21:57 +00:00
|
|
|
return ast.WalkSkipChildren, nil
|
2022-10-04 01:06:38 +00:00
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
case *ast.FencedCodeBlock:
|
|
|
|
language := string(n.Language(source))
|
2022-10-04 01:06:38 +00:00
|
|
|
snippet := schema.RenderDocsToString(source, n)
|
|
|
|
if !languages.Has(language) || len(snippet) == 0 {
|
2020-06-18 19:32:15 +00:00
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
2022-10-04 01:06:38 +00:00
|
|
|
if _, ok := currentSection.Snippets[language]; ok {
|
|
|
|
// We have the same language appearing multiple times in a {{% examples
|
|
|
|
// %}} without an {{% example %}} to break them up. We are going to just
|
|
|
|
// pretend there was an {{% example %}}
|
|
|
|
pushExamples()
|
|
|
|
}
|
|
|
|
currentSection.Snippets[language] = snippet
|
|
|
|
case *ast.Text:
|
|
|
|
// We only want to change the title before we collect any snippets
|
2022-10-04 01:07:48 +00:00
|
|
|
title := strings.TrimSuffix(string(n.Text(source)), ":")
|
2022-10-04 01:06:38 +00:00
|
|
|
if currentSection.Title == "" && len(currentSection.Snippets) == 0 {
|
|
|
|
currentSection.Title = title
|
|
|
|
} else {
|
|
|
|
// Since we might find out we are done with the previous section only
|
|
|
|
// after we have consumed the next title, we store the title.
|
2022-10-04 18:21:57 +00:00
|
|
|
nextInferredTitle = title
|
2020-05-08 23:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
return ast.WalkContinue, nil
|
|
|
|
})
|
2023-02-17 01:23:09 +00:00
|
|
|
contract.AssertNoErrorf(err, "error walking AST")
|
2022-10-04 01:06:38 +00:00
|
|
|
pushExamples()
|
2020-05-08 23:25:28 +00:00
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
if examplesShortcode != nil {
|
|
|
|
p := examplesShortcode.Parent()
|
|
|
|
p.RemoveChild(p, examplesShortcode)
|
2020-05-08 23:25:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
description := schema.RenderDocsToString(source, parsed)
|
2020-11-09 14:12:58 +00:00
|
|
|
importDetails := ""
|
|
|
|
parts := strings.Split(description, "\n\n## Import")
|
2020-11-09 14:45:55 +00:00
|
|
|
if len(parts) > 1 { // we only care about the Import section details here!!
|
2020-11-09 14:12:58 +00:00
|
|
|
importDetails = parts[1]
|
|
|
|
}
|
2020-06-18 19:32:15 +00:00
|
|
|
|
2020-11-14 00:20:33 +00:00
|
|
|
// When we split the description above, the main part of the description is always part[0]
|
|
|
|
// the description must have a blank line after it to render the examples correctly
|
2023-12-12 12:19:42 +00:00
|
|
|
description = parts[0] + "\n"
|
2020-11-14 00:20:33 +00:00
|
|
|
|
2020-06-18 19:32:15 +00:00
|
|
|
return docInfo{
|
2020-11-14 00:20:33 +00:00
|
|
|
description: description,
|
2020-11-09 14:12:58 +00:00
|
|
|
examples: examples,
|
|
|
|
importDetails: importDetails,
|
2020-06-18 19:32:15 +00:00
|
|
|
}
|
2020-05-08 23:25:28 +00:00
|
|
|
}
|