databricks-cli/bundle/internal/docs/markdown.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.2 KiB
Go
Raw Normal View History

2025-01-10 17:33:58 +00:00
package main
import (
"fmt"
"log"
"os"
"strings"
)
func buildMarkdown(nodes []rootNode, outputFile, header string) error {
2025-01-15 17:59:07 +00:00
m := newMardownRenderer()
2025-01-10 17:33:58 +00:00
m = m.PlainText(header)
for _, node := range nodes {
m = m.LF()
if node.TopLevel {
m = m.H2(node.Title)
} else {
m = m.H3(node.Title)
}
m = m.LF()
if node.Type != "" {
m = m.PlainText(fmt.Sprintf("**`Type: %s`**", node.Type))
m = m.LF()
}
m = m.PlainText(node.Description)
m = m.LF()
if len(node.ObjectKeyAttributes) > 0 {
2025-01-13 14:16:47 +00:00
n := pickLastWord(node.Title)
n = removePluralForm(n)
2025-01-10 17:33:58 +00:00
m = m.CodeBlocks("yaml", fmt.Sprintf("%ss:\n <%s-name>:\n <%s-field-name>: <%s-field-value>", n, n, n, n))
m = m.LF()
m = buildAttributeTable(m, node.ObjectKeyAttributes)
} else if len(node.ArrayItemAttributes) > 0 {
m = m.LF()
m = buildAttributeTable(m, node.ArrayItemAttributes)
} else if len(node.Attributes) > 0 {
m = m.LF()
m = buildAttributeTable(m, node.Attributes)
}
if node.Example != "" {
m = m.LF()
m = m.PlainText("**Example**")
m = m.LF()
m = m.PlainText(node.Example)
}
}
2025-01-15 17:59:07 +00:00
f, err := os.Create(outputFile)
2025-01-10 17:33:58 +00:00
if err != nil {
log.Fatal(err)
}
2025-01-15 17:59:07 +00:00
_, err = f.WriteString(m.String())
if err != nil {
log.Fatal(err)
}
return f.Close()
2025-01-10 17:33:58 +00:00
}
2025-01-13 14:16:47 +00:00
func pickLastWord(s string) string {
words := strings.Split(s, ".")
return words[len(words)-1]
}
2025-01-10 17:33:58 +00:00
// Build a custom table which we use in Databricks website
2025-01-15 17:59:07 +00:00
func buildAttributeTable(m *markdownRenderer, attributes []attributeNode) *markdownRenderer {
2025-01-10 17:33:58 +00:00
m = m.LF()
m = m.PlainText(".. list-table::")
m = m.PlainText(" :header-rows: 1")
m = m.LF()
m = m.PlainText(" * - Key")
m = m.PlainText(" - Type")
m = m.PlainText(" - Description")
m = m.LF()
for _, a := range attributes {
m = m.PlainText(" * - " + fmt.Sprintf("`%s`", a.Title))
m = m.PlainText(" - " + a.Type)
m = m.PlainText(" - " + formatDescription(a))
m = m.LF()
}
return m
}
func formatDescription(a attributeNode) string {
s := strings.ReplaceAll(a.Description, "\n", " ")
2025-01-15 21:10:39 +00:00
if a.Link != "" {
2025-01-10 17:33:58 +00:00
if strings.HasSuffix(s, ".") {
s += " "
} else if s != "" {
s += ". "
}
2025-01-15 21:10:39 +00:00
s += fmt.Sprintf("See [_](#%s).", a.Link)
2025-01-10 17:33:58 +00:00
}
return s
}