-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcombo.go
100 lines (92 loc) · 3.08 KB
/
combo.go
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
96
97
98
99
100
package bbConvert
import (
"cmp"
"slices"
"strings"
"github.com/dlclark/regexp2"
)
// A BBCode and Markdown converter all in one.
// Attemps to prevent some potential errors when doing them separately.
type ComboConverter struct {
bb BBConverter
md MarkdownConverter
code *regexp2.Regexp
}
func NewComboConverter() ComboConverter {
return ComboConverter{
bb: NewBBConverter(),
md: NewMarkdownConverter(),
code: regexp2.MustCompile(`<code>[\s\S]*?<\/code>`, regexp2.Multiline),
}
}
type codeMatch struct {
index int
code string
}
// Convert BBCode and Markdown to HTML
func (c ComboConverter) HTMLConvert(combo string) string {
in := []rune(combo)
var codeBlocks []codeMatch
var match *regexp2.Match
var err error
var ind, dif int
for {
match, err = c.bb.codeConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
if strings.Contains(match.GroupByNumber(1).String(), "\n") {
in = slices.Concat(in[:match.Index], []rune("</p><pre>"+codePlaceholder+"</pre><p>"), in[match.Index+match.Length:])
} else {
in = slices.Concat(in[:match.Index], []rune(codePlaceholder), in[match.Index+match.Length:])
}
n := codeMatch{match.Index, strings.TrimPrefix(match.GroupByNumber(1).String(), "\n")}
dif = len(n.code) - len(codePlaceholder)
ind, _ = slices.BinarySearchFunc(codeBlocks, n, func(a, b codeMatch) int { return cmp.Compare(a.index, b.index) })
for i := ind; i < len(codeBlocks); i++ {
codeBlocks[i].index -= dif
}
codeBlocks = slices.Insert(codeBlocks, ind, n)
}
for {
match, err = c.md.largeCodeConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune("</p><pre>"+codePlaceholder+"</pre><p>"), in[match.Index+match.Length:])
n := codeMatch{match.Index, strings.TrimPrefix(match.GroupByNumber(1).String(), "\n")}
dif = len(n.code) - len(codePlaceholder)
ind, _ = slices.BinarySearchFunc(codeBlocks, n, func(a, b codeMatch) int { return cmp.Compare(a.index, b.index) })
for i := ind; i < len(codeBlocks); i++ {
codeBlocks[i].index -= dif
}
codeBlocks = slices.Insert(codeBlocks, ind, n)
}
for {
match, err = c.md.inlineCodeConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune(codePlaceholder), in[match.Index+match.Length:])
n := codeMatch{match.Index, strings.TrimPrefix(match.GroupByNumber(1).String(), "\n")}
dif = len(n.code) - len(codePlaceholder)
ind, _ = slices.BinarySearchFunc(codeBlocks, n, func(a, b codeMatch) int { return cmp.Compare(a.index, b.index) })
for i := ind; i < len(codeBlocks); i++ {
codeBlocks[i].index -= dif
}
codeBlocks = slices.Insert(codeBlocks, ind, n)
}
out := c.bb.bbActualConv([]rune(c.md.mkActualConv(in, true)), true)
for i := range codeBlocks {
out = strings.Replace(out, codeInner, codeBlocks[i].code, 1)
}
return out
}
// Converts just BBCode to HTML
func (c ComboConverter) BBHTMLConvert(in string) string {
return c.bb.HTMLConvert(in)
}
// Converts just Markdown to HTML
func (c ComboConverter) MarkdownHTMLConvert(in string) string {
return c.md.HTMLConvert(in)
}