Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add initial docs #12

Merged
merged 1 commit into from
Feb 28, 2024
Merged

docs: Add initial docs #12

merged 1 commit into from
Feb 28, 2024

Conversation

bradenhilton
Copy link
Member

Same order as the FuncMap for now. Splitting or re-ordering can be done later.

I feel like some of them aren't quite right...

Copy link
Member

@twpayne twpayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you!

@bradenhilton
Copy link
Member Author

I'll merge this, but I'm still not quite sure how to achieve what you outlined in #1 (comment), both how to set up embed, and how to consume it within chezmoi, having never really done anything like this before.

@bradenhilton bradenhilton merged commit a75cd7f into chezmoi:master Feb 28, 2024
2 checks passed
@twpayne
Copy link
Member

twpayne commented Feb 28, 2024

how to achieve what you outlined in #1 (comment)

chezmoi uses this for its documentation and examples. The rough outline is:

  1. The markdown documentation is included in chezmoi's binary using the Go standard library's embed package.
  2. chezmoi includes a markdown parser and renderer and uses this to extract relevant bits of documentation.

The relevant chezmoi code is:

  1. https://github.com/twpayne/chezmoi/blob/4504bc3b4fd221d4a20e29a2b95ee9cffebadb10/assets/chezmoi.io/docs/reference/commands/commands.go makes all the command *.md files available in a io/fs.FS. This is in the Go package github.com/twpayne/chezmoi/v2/assets/chezmoi.io/docs/references/commands.
  2. This package is imported and, on startup, the command *.md files are parsed and the help text extracted: https://github.com/twpayne/chezmoi/blob/4504bc3b4fd221d4a20e29a2b95ee9cffebadb10/internal/cmd/cmd.go#L80-L91. The actual parsing happens here: https://github.com/twpayne/chezmoi/blob/4504bc3b4fd221d4a20e29a2b95ee9cffebadb10/internal/cmd/cmd.go#L152. It's a crude regexp-based state machine that extracts sections of the markdown files based on section titles.
  3. The extracted markdown sections are rendered as plain text and stored in strings: https://github.com/twpayne/chezmoi/blob/4504bc3b4fd221d4a20e29a2b95ee9cffebadb10/internal/cmd/cmd.go#L199-L210.
  4. Later, the strings are used in chezmoi's help output.

For template funcs, what's needed it for the docs subdirectory to contain a .go file that:

  1. Embeds templatefuncs.md.
  2. Includes an init() function that parses templatefuncs.md and populates an exported map[string]Reference, something like:
package docs

import (
    _ "embed"
)

//go:embed templatefuncs.md
var templateFuncsStr string

type Reference struct {
    Title   string
    Body    string
    Example string
}

var References map[string]Reference

func init() {
    // Parse templateFuncStr and store the results in References.
}

References then contains something like:

References["contains"] = Reference{
    Title: "`contains` *substring* *string*",
    Body:  "`contains` returns whether *substring* is in *string*.".
    Example: "```text\n" +
             "{{ "abc" | contains "ab" }}\n" +
             "\n" +
             "true\n" +
             "```\n",
}

Then, finally, a tool in chezmoi's build system imports github.com/chezmoi/templatefuncs/docs and generates Markdown files for chezmoi's website from the contents of the References variable.

@bradenhilton bradenhilton mentioned this pull request Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants