~bigbes/confluence-md-utilities

ref: 5fabfe018459f627441085dd7f87f1f9e4e97af8 confluence-md-utilities/confluence/elements.go -rw-r--r-- 2.7 KiB
5fabfe01 — Eugene Blikh feat: add verify command, improve round-trip fidelity 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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)
}