fix: HTML tables → Markdown tables in docs-generator (mkdocs теперь рендерит таблицы корректно)
This commit is contained in:
@@ -552,28 +552,26 @@ func renderParamTable(params []types.ParamSpec, requiredTable, noDefault bool) s
|
||||
return "None.\n"
|
||||
}
|
||||
var b strings.Builder
|
||||
tableClass := "resource-table resource-table-compact"
|
||||
if requiredTable {
|
||||
tableClass += " resource-table-required"
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("<table class=\"%s\">\n", tableClass))
|
||||
if noDefault {
|
||||
b.WriteString("<thead><tr><th>Code</th><th>Type</th><th>Description</th><th>Constraints</th></tr></thead>\n<tbody>\n")
|
||||
b.WriteString("| Code | Type | Description | Constraints |\n")
|
||||
b.WriteString("|------|------|-------------|-------------|\n")
|
||||
} else {
|
||||
b.WriteString("<thead><tr><th>Code</th><th>Type</th><th>Default</th><th>Description</th><th>Constraints</th></tr></thead>\n<tbody>\n")
|
||||
b.WriteString("| Code | Type | Default | Description | Constraints |\n")
|
||||
b.WriteString("|------|------|---------|-------------|-------------|\n")
|
||||
}
|
||||
for _, p := range params {
|
||||
b.WriteString("<tr>")
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", formatParamCode(p.Code)))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", formatTypeCell(p)))
|
||||
if !noDefault {
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", defaultCell(p.Default)))
|
||||
code := formatParamCode(p.Code)
|
||||
dtype := formatTypeCell(p)
|
||||
desc := escapeText(pickTextTable(p))
|
||||
constr := escapeText(collectConstraints(p))
|
||||
if noDefault {
|
||||
b.WriteString(fmt.Sprintf("| %s | %s | %s | %s |\n", code, dtype, desc, constr))
|
||||
} else {
|
||||
def := defaultCell(p.Default)
|
||||
b.WriteString(fmt.Sprintf("| %s | %s | %s | %s | %s |\n", code, dtype, def, desc, constr))
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", escapeText(pickTextTable(p))))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", escapeText(collectConstraints(p))))
|
||||
b.WriteString("</tr>\n")
|
||||
}
|
||||
b.WriteString("</tbody></table>\n")
|
||||
b.WriteString("\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
@@ -582,17 +580,16 @@ func renderModifyTable(params []types.ParamSpec) string {
|
||||
return "None.\n"
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("<table class=\"resource-table resource-table-compact\">\n")
|
||||
b.WriteString("<thead><tr><th>Code</th><th>Type</th><th>Description</th><th>Constraints</th></tr></thead>\n<tbody>\n")
|
||||
b.WriteString("| Code | Type | Description | Constraints |\n")
|
||||
b.WriteString("|------|------|-------------|-------------|\n")
|
||||
for _, p := range params {
|
||||
b.WriteString("<tr>")
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", formatParamCode(p.Code)))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", formatTypeCell(p)))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", escapeText(pickTextTable(p))))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", escapeText(collectConstraints(p))))
|
||||
b.WriteString("</tr>\n")
|
||||
code := formatParamCode(p.Code)
|
||||
dtype := formatTypeCell(p)
|
||||
desc := escapeText(pickTextTable(p))
|
||||
constr := escapeText(collectConstraints(p))
|
||||
b.WriteString(fmt.Sprintf("| %s | %s | %s | %s |\n", code, dtype, desc, constr))
|
||||
}
|
||||
b.WriteString("</tbody></table>\n")
|
||||
b.WriteString("\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
@@ -750,7 +747,7 @@ func formatTypeCell(p types.ParamSpec) string {
|
||||
if dtype == "" {
|
||||
return ""
|
||||
}
|
||||
return "<code>" + escapeText(dtype) + "</code>"
|
||||
return "`" + dtype + "`"
|
||||
}
|
||||
|
||||
func firstType(p types.ParamSpec) string {
|
||||
@@ -777,9 +774,9 @@ func defaultCell(value interface{}) string {
|
||||
return ""
|
||||
}
|
||||
if s, ok := value.(string); ok {
|
||||
return "<code>" + escapeText(s) + "</code>"
|
||||
return "`" + s + "`"
|
||||
}
|
||||
return "<code>" + escapeText(formatLiteral(value)) + "</code>"
|
||||
return "`" + formatLiteral(value) + "`"
|
||||
}
|
||||
|
||||
func pickTextTable(p types.ParamSpec) string {
|
||||
@@ -869,35 +866,19 @@ func escapeText(value string) string {
|
||||
text = strings.ReplaceAll(text, "<br/>", "<br/>")
|
||||
text = strings.ReplaceAll(text, "<br />", "<br/>")
|
||||
text = strings.ReplaceAll(text, "<br>", "<br/>")
|
||||
// Escape pipe for markdown tables
|
||||
text = strings.ReplaceAll(text, "|", "\\|")
|
||||
// Replace literal newlines with <br/> for markdown table cells
|
||||
text = strings.ReplaceAll(text, "\n", "<br/>")
|
||||
return text
|
||||
}
|
||||
|
||||
func formatParamCode(code string) string {
|
||||
return formatCode(ToSnake(code))
|
||||
}
|
||||
|
||||
func formatCode(code string) string {
|
||||
if code == "" {
|
||||
snake := ToSnake(code)
|
||||
if snake == "" {
|
||||
return ""
|
||||
}
|
||||
if len(code) <= 40 {
|
||||
return "<strong><code class=\"code-no-wrap\">" + code + "</code></strong>"
|
||||
}
|
||||
out := []string{}
|
||||
prev := rune(0)
|
||||
for _, ch := range code {
|
||||
if ch == '_' {
|
||||
out = append(out, "_<wbr>")
|
||||
prev = ch
|
||||
continue
|
||||
}
|
||||
if prev != 0 && (unicodeIsLower(prev) || unicodeIsDigit(prev)) && unicodeIsUpper(ch) {
|
||||
out = append(out, "<wbr>")
|
||||
}
|
||||
out = append(out, string(ch))
|
||||
prev = ch
|
||||
}
|
||||
return "<strong><code class=\"code-wrap\">" + strings.Join(out, "") + "</code></strong>"
|
||||
return "**`" + snake + "`**"
|
||||
}
|
||||
|
||||
// ToSnake конвертирует CamelCase → snake_case.
|
||||
@@ -933,10 +914,6 @@ func ToSnake(value string) string {
|
||||
return strings.Trim(string(out), "_")
|
||||
}
|
||||
|
||||
func unicodeIsLower(r rune) bool { return r >= 'a' && r <= 'z' }
|
||||
func unicodeIsUpper(r rune) bool { return r >= 'A' && r <= 'Z' }
|
||||
func unicodeIsDigit(r rune) bool { return r >= '0' && r <= '9' }
|
||||
|
||||
func stripHTML(value string) string {
|
||||
if value == "" {
|
||||
return ""
|
||||
@@ -1026,23 +1003,21 @@ func renderNestedParams(p types.ParamSpec) string {
|
||||
label += " (map-fixed)"
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("\n### %s\n\n", label))
|
||||
b.WriteString("<table class=\"resource-table resource-table-compact resource-table-nested\">\n")
|
||||
b.WriteString("<thead><tr><th>Code</th><th>Type</th><th>Required</th><th>Default</th><th>Description</th><th>Constraints</th></tr></thead>\n<tbody>\n")
|
||||
b.WriteString("| Code | Type | Required | Default | Description | Constraints |\n")
|
||||
b.WriteString("|------|------|----------|---------|-------------|-------------|\n")
|
||||
for _, sp := range p.SubParams {
|
||||
req := "no"
|
||||
if sp.Required {
|
||||
req = "**yes**"
|
||||
}
|
||||
b.WriteString("<tr>")
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", formatParamCode(sp.Code)))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", formatTypeCell(sp)))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", req))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", defaultCell(sp.Default)))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", escapeText(pickTextTable(sp))))
|
||||
b.WriteString(fmt.Sprintf("<td>%s</td>", escapeText(collectConstraints(sp))))
|
||||
b.WriteString("</tr>\n")
|
||||
code := formatParamCode(sp.Code)
|
||||
dtype := formatTypeCell(sp)
|
||||
def := defaultCell(sp.Default)
|
||||
desc := escapeText(pickTextTable(sp))
|
||||
constr := escapeText(collectConstraints(sp))
|
||||
b.WriteString(fmt.Sprintf("| %s | %s | %s | %s | %s | %s |\n", code, dtype, req, def, desc, constr))
|
||||
}
|
||||
b.WriteString("</tbody></table>\n")
|
||||
b.WriteString("\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user