| 1 | package confluence |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // Confluence storage format macro helpers. |
| 6 | |
| 7 | func CodeMacro(language string, body string) string { |
| 8 | return CodeMacroWithID(language, body, "", "") |
| 9 | } |
| 10 | |
| 11 | func CodeMacroWithID(language string, body string, macroID string, attrOrder string) string { |
| 12 | var lang string |
| 13 | if language != "" { |
| 14 | lang = `<ac:parameter ac:name="language">` + language + `</ac:parameter>` |
| 15 | } |
| 16 | tag := buildStructuredMacroTag("code", macroID, attrOrder) |
| 17 | return tag + |
| 18 | lang + |
| 19 | `<ac:plain-text-body><![CDATA[` + escapeCDATA(body) + `]]></ac:plain-text-body>` + |
| 20 | `</ac:structured-macro>` |
| 21 | } |
| 22 | |
| 23 | // buildStructuredMacroTag builds an opening <ac:structured-macro> tag |
| 24 | // with attributes in the specified order. attrOrder is a comma-separated |
| 25 | // list of short attribute names (e.g. "name,schema-version,macro-id"). |
| 26 | func buildStructuredMacroTag(name string, macroID string, attrOrder string) string { |
| 27 | attrValues := map[string]string{ |
| 28 | "name": name, |
| 29 | "schema-version": "1", |
| 30 | } |
| 31 | if macroID != "" { |
| 32 | attrValues["macro-id"] = macroID |
| 33 | } |
| 34 | |
| 35 | var order []string |
| 36 | if attrOrder != "" { |
| 37 | order = strings.Split(attrOrder, ",") |
| 38 | } else { |
| 39 | // Default order when no original order is known |
| 40 | order = []string{"name", "schema-version"} |
| 41 | if macroID != "" { |
| 42 | order = append(order, "macro-id") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | var buf strings.Builder |
| 47 | buf.WriteString("<ac:structured-macro") |
| 48 | for _, attr := range order { |
| 49 | if val, ok := attrValues[attr]; ok { |
| 50 | buf.WriteString(` ac:`) |
| 51 | buf.WriteString(attr) |
| 52 | buf.WriteString(`="`) |
| 53 | buf.WriteString(val) |
| 54 | buf.WriteString(`"`) |
| 55 | } |
| 56 | } |
| 57 | buf.WriteString(">") |
| 58 | return buf.String() |
| 59 | } |
| 60 | |
| 61 | func InfoPanel(body string) string { |
| 62 | return `<ac:structured-macro ac:name="info" ac:schema-version="1">` + |
| 63 | `<ac:rich-text-body>` + body + `</ac:rich-text-body>` + |
| 64 | `</ac:structured-macro>` |
| 65 | } |
| 66 | |
| 67 | func NotePanel(body string) string { |
| 68 | return `<ac:structured-macro ac:name="note" ac:schema-version="1">` + |
| 69 | `<ac:rich-text-body>` + body + `</ac:rich-text-body>` + |
| 70 | `</ac:structured-macro>` |
| 71 | } |
| 72 | |
| 73 | func WarningPanel(body string) string { |
| 74 | return `<ac:structured-macro ac:name="warning" ac:schema-version="1">` + |
| 75 | `<ac:rich-text-body>` + body + `</ac:rich-text-body>` + |
| 76 | `</ac:structured-macro>` |
| 77 | } |
| 78 | |
| 79 | func ImageExternal(url string) string { |
| 80 | return `<ac:image><ri:url ri:value="` + url + `"/></ac:image>` |
| 81 | } |
| 82 | |
| 83 | // escapeCDATA splits ]]> sequences so they don't break CDATA sections. |
| 84 | func escapeCDATA(s string) string { |
| 85 | result := make([]byte, 0, len(s)) |
| 86 | for i := 0; i < len(s); i++ { |
| 87 | if i+2 < len(s) && s[i] == ']' && s[i+1] == ']' && s[i+2] == '>' { |
| 88 | result = append(result, ']', ']', '>', '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[') |
| 89 | i += 2 |
| 90 | } else { |
| 91 | result = append(result, s[i]) |
| 92 | } |
| 93 | } |
| 94 | return string(result) |
| 95 | } |
| 96 | |