pulumi/pkg/codegen/docs
Guinevere Saenger 26bdac7438
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
..
templates Add ability to process docs without dedicated Examples logic (#15475) 2024-02-23 18:06:44 +00:00
test_data Add ability to process docs without dedicated Examples logic (#15475) 2024-02-23 18:06:44 +00:00
README.md Replace Hugo shortcodes (#9491) 2022-04-29 15:04:15 -07:00
description.go Add ability to process docs without dedicated Examples logic (#15475) 2024-02-23 18:06:44 +00:00
description_test.go Add ability to process docs without dedicated Examples logic (#15475) 2024-02-23 18:06:44 +00:00
examples.go Add ability to process docs without dedicated Examples logic (#15475) 2024-02-23 18:06:44 +00:00
gen.go Fix docs generator parent module computation (#15035) 2024-02-20 15:44:37 +00:00
gen_function.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
gen_kubernetes.go sdk/go: Remove 'nolint' directives from package docs 2023-01-06 09:06:47 -08:00
gen_method.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
gen_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
package_tree.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
package_tree_test.go Fix docs generator parent module computation (#15035) 2024-02-20 15:44:37 +00:00
utils.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
utils_test.go sdk/go: Remove 'nolint' directives from package docs 2023-01-06 09:06:47 -08:00

README.md

Docs generator

This generator generates resource-level docs by utilizing the Pulumi schema.

Crash course on templates

The templates use Go's built-in html/template package to process templates with data. The driver for this doc generator (e.g. tfbridge for TF-based providers) then persists each file from memory onto the disk as .md files.

Although we are using the html/template package, it has the same exact interface as the text/template package, except for some HTML specific things. Therefore, all of the functions available in the text/template package are also available with the html/template package.

  • Data can be injected using {{.PropertyName}}.
  • Nested properties can be accessed using the dot notation, i.e. {{.Property1.Property2}}.
  • Templates can inject other templates using the {{template "template_name"}} directive.
    • For this to work, you will need to first define the named template using {{define "template_name"}}.
  • You can pass data to nested templates by simply passing an argument after the template's name.
  • To remove whitespace from injected values, use the - in the template tags.
    • For example, {{if .SomeBool}} some text {{- else}} some other text {{- end}}. Note the use of - to eliminate whitespace from the enclosing text.
    • Read more here.
  • To render un-encoded content use the custom global function htmlSafe.
    • Note: This should only be used if you know for sure you are not injecting any user-generated content, as it by-passes the HTML encoding.
  • To render strings to Markdown, use the custom global function markdownify.
  • To print regular strings, that share the same syntax as the Go templating engine, use the built-in global function print function.

Learn more from here: https://curtisvermeeren.github.io/2017/09/14/Golang-Templates-Cheatsheet

Modifying templates and updating tests

We run tests that validate our template-rendering output. If you need to make change that produces a set of Markdown files that differs from the set that we use in our tests (see codegen/testing/test/testdata/**/*.md), your pull-request checks will fail, and to get them to pass, you'll need to modify the test data to match the output produced by your change.

For minor diffs, you can just update the test files manually and include those updates with your PR. But for large diffs, you may want to regenerate the full set. To do that, from the root of the repo, run:

cd pkg/codegen/docs && PULUMI_ACCEPT=true go test . && cd -