Skip to content

Commit

Permalink
Better handling of
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebQ42 committed Dec 27, 2024
1 parent 88a19e0 commit ef1f4ab
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 88 deletions.
30 changes: 16 additions & 14 deletions bb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

// The Magic RegEx string that matches bbCode tags.
const (
codePlaceholder = `%CODEBLOCK%`
codeInner = `%CODEBLOCK%`
codePlaceholder = `<code>%CODEBLOCK%</code>`
)

type BBTag struct {
Expand Down Expand Up @@ -61,7 +62,11 @@ func (b BBConverter) bbActualConv(in []rune, comboConv bool) string {
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune(codePlaceholder), in[match.Index+match.Length:])
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:])
}
codeBlocks = append(codeBlocks, strings.TrimPrefix(match.GroupByNumber(1).String(), "\n"))
}
}
Expand All @@ -75,16 +80,13 @@ func (b BBConverter) bbActualConv(in []rune, comboConv bool) string {
out := string(in)
if !comboConv {
out = "<p>" + strings.ReplaceAll(out, "\n", "</p>\n<p>") + "</p>"
out = strings.ReplaceAll(out, "<p></p>", "")
out = strings.ReplaceAll(out, "<p>\n</p>", "\n")
for i := range codeBlocks {
if strings.Contains(codeBlocks[i], "\n") {
out = strings.Replace(out, codePlaceholder, "<pre><code>"+codeBlocks[i]+"</code></pre>", 1)
} else {
out = strings.Replace(out, codePlaceholder, "<code>"+codeBlocks[i]+"</code>", 1)
}
out = strings.Replace(out, codeInner, codeBlocks[i], 1)
}
}
return out

}

func matchToHTML(match *regexp2.Match) string {
Expand Down Expand Up @@ -210,9 +212,9 @@ func matchToHTML(match *regexp2.Match) string {
style += "float:right;"
}
if style == "" {
return "<iframe src='" + middle + "' allowfullscreen></iframe>"
return "</p><iframe src='" + middle + "' allowfullscreen></iframe><p>"
}
return "<iframe src='" + middle + "' style='" + style + "' allowfullscreen></iframe>"
return "</p><iframe src='" + middle + "' style='" + style + "' allowfullscreen></iframe><p>"
case "img":
fallthrough
case "image":
Expand Down Expand Up @@ -274,7 +276,7 @@ func matchToHTML(match *regexp2.Match) string {
case "t5":
fallthrough
case "t6":
return "<h" + string(tag[1]) + ">" + middle + "</h" + string(tag[1]) + ">"
return "</p><h" + string(tag[1]) + ">" + middle + "</h" + string(tag[1]) + "><p>"
case "align":
if leading == "" && len(params) == 0 {
return middle
Expand All @@ -286,7 +288,7 @@ func matchToHTML(match *regexp2.Match) string {
break
}
}
return "<div style='text-align:" + align + ";'>" + middle + "</div>"
return "</p><div style='text-align:" + align + ";'><p>" + middle + "</p></div><p>"
case "float":
if leading == "" && len(params) == 0 {
return middle
Expand All @@ -298,7 +300,7 @@ func matchToHTML(match *regexp2.Match) string {
break
}
}
return "<div style='float:" + float + ";'>" + middle + "</div>"
return "</p><div style='float:" + float + ";'><p>" + middle + "</p></div><p>"
case "bullet":
tag = "ul"
fallthrough
Expand All @@ -308,7 +310,7 @@ func matchToHTML(match *regexp2.Match) string {
case "ol":
fallthrough
case "ul":
return "<" + tag + ">" + processListItems(middle) + "</" + tag + ">"
return "</p><" + tag + ">" + processListItems(middle) + "</" + tag + "><p>"
}
return middle
}
Expand Down
14 changes: 7 additions & 7 deletions combo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func (c ComboConverter) HTMLConvert(combo string) string {
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune(codePlaceholder), in[match.Index+match.Length:])
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) })
Expand All @@ -55,7 +59,7 @@ func (c ComboConverter) HTMLConvert(combo string) string {
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune(codePlaceholder), in[match.Index+match.Length:])
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) })
Expand All @@ -80,11 +84,7 @@ func (c ComboConverter) HTMLConvert(combo string) string {
}
out := c.bb.bbActualConv([]rune(c.md.mkActualConv(in, true)), true)
for i := range codeBlocks {
if strings.Contains(codeBlocks[i].code, "\n") {
out = strings.Replace(out, codePlaceholder, "<pre><code>"+codeBlocks[i].code+"</code></pre>", 1)
} else {
out = strings.Replace(out, codePlaceholder, "<code>"+codeBlocks[i].code+"</code>", 1)
}
out = strings.Replace(out, codeInner, codeBlocks[i].code, 1)
}
return out
}
Expand Down
Loading

0 comments on commit ef1f4ab

Please sign in to comment.