package confluence
import "strings"
// Confluence storage format macro helpers.
func CodeMacro(language string, body string) string {
return CodeMacroWithID(language, body, "", "")
}
func CodeMacroWithID(language string, body string, macroID string, attrOrder string) string {
var lang string
if language != "" {
lang = `<ac:parameter ac:name="language">` + language + `</ac:parameter>`
}
tag := buildStructuredMacroTag("code", macroID, attrOrder)
return tag +
lang +
`<ac:plain-text-body><![CDATA[` + escapeCDATA(body) + `]]></ac:plain-text-body>` +
`</ac:structured-macro>`
}
// buildStructuredMacroTag builds an opening <ac:structured-macro> tag
// with attributes in the specified order. attrOrder is a comma-separated
// list of short attribute names (e.g. "name,schema-version,macro-id").
func buildStructuredMacroTag(name string, macroID string, attrOrder string) string {
attrValues := map[string]string{
"name": name,
"schema-version": "1",
}
if macroID != "" {
attrValues["macro-id"] = macroID
}
var order []string
if attrOrder != "" {
order = strings.Split(attrOrder, ",")
} else {
// Default order when no original order is known
order = []string{"name", "schema-version"}
if macroID != "" {
order = append(order, "macro-id")
}
}
var buf strings.Builder
buf.WriteString("<ac:structured-macro")
for _, attr := range order {
if val, ok := attrValues[attr]; ok {
buf.WriteString(` ac:`)
buf.WriteString(attr)
buf.WriteString(`="`)
buf.WriteString(val)
buf.WriteString(`"`)
}
}
buf.WriteString(">")
return buf.String()
}
func InfoPanel(body string) string {
return `<ac:structured-macro ac:name="info" ac:schema-version="1">` +
`<ac:rich-text-body>` + body + `</ac:rich-text-body>` +
`</ac:structured-macro>`
}
func NotePanel(body string) string {
return `<ac:structured-macro ac:name="note" ac:schema-version="1">` +
`<ac:rich-text-body>` + body + `</ac:rich-text-body>` +
`</ac:structured-macro>`
}
func WarningPanel(body string) string {
return `<ac:structured-macro ac:name="warning" ac:schema-version="1">` +
`<ac:rich-text-body>` + body + `</ac:rich-text-body>` +
`</ac:structured-macro>`
}
func ImageExternal(url string) string {
return `<ac:image><ri:url ri:value="` + url + `"/></ac:image>`
}
// escapeCDATA splits ]]> sequences so they don't break CDATA sections.
func escapeCDATA(s string) string {
result := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
if i+2 < len(s) && s[i] == ']' && s[i+1] == ']' && s[i+2] == '>' {
result = append(result, ']', ']', '>', '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')
i += 2
} else {
result = append(result, s[i])
}
}
return string(result)
}