Add simple bbcode tags

This commit is contained in:
Ben Visness 2021-06-20 17:21:58 -05:00
parent efdc2216f9
commit 6e7a398dee
3 changed files with 97 additions and 1 deletions

Binary file not shown.

View File

@ -1,6 +1,7 @@
package parsing package parsing
import ( import (
"fmt"
"regexp" "regexp"
"github.com/frustra/bbcode" "github.com/frustra/bbcode"
@ -15,7 +16,7 @@ import (
var BBCodePriority = 1 // TODO: This is maybe too high a priority? var BBCodePriority = 1 // TODO: This is maybe too high a priority?
var reTag = regexp.MustCompile(`(?P<open>\[\s*(?P<opentagname>[a-zA-Z]+))|(?P<close>\[\s*\/\s*(?P<closetagname>[a-zA-Z]+)\s*\])`) var reTag = regexp.MustCompile(`(?P<open>\[\s*(?P<opentagname>[a-zA-Z0-9]+))|(?P<close>\[\s*\/\s*(?P<closetagname>[a-zA-Z0-9]+)\s*\])`)
var previewBBCodeCompiler = bbcode.NewCompiler(false, false) var previewBBCodeCompiler = bbcode.NewCompiler(false, false)
var realBBCodeCompiler = bbcode.NewCompiler(false, false) var realBBCodeCompiler = bbcode.NewCompiler(false, false)
@ -23,8 +24,49 @@ var realBBCodeCompiler = bbcode.NewCompiler(false, false)
var REYoutubeVidOnly = regexp.MustCompile(`^[a-zA-Z0-9_-]{11}$`) var REYoutubeVidOnly = regexp.MustCompile(`^[a-zA-Z0-9_-]{11}$`)
func init() { func init() {
type attr struct {
Name, Value string
}
addSimpleTag := func(name, tag string, notext bool, attrs ...attr) {
var tagFunc bbcode.TagCompilerFunc = func(bn *bbcode.BBCodeNode) (*bbcode.HTMLTag, bool) {
if notext {
var newChildren []*bbcode.BBCodeNode
for _, child := range bn.Children {
if child.ID != bbcode.TEXT {
newChildren = append(newChildren, child)
} else {
fmt.Printf("%+v", child)
}
}
bn.Children = newChildren
}
out := bbcode.NewHTMLTag("")
out.Name = tag
for _, a := range attrs {
out.Attrs[a.Name] = a.Value
}
return out, true
}
previewBBCodeCompiler.SetTag(name, tagFunc)
realBBCodeCompiler.SetTag(name, tagFunc)
}
previewBBCodeCompiler.SetTag("youtube", makeYoutubeBBCodeFunc(true)) previewBBCodeCompiler.SetTag("youtube", makeYoutubeBBCodeFunc(true))
realBBCodeCompiler.SetTag("youtube", makeYoutubeBBCodeFunc(false)) realBBCodeCompiler.SetTag("youtube", makeYoutubeBBCodeFunc(false))
addSimpleTag("h1", "h1", false)
addSimpleTag("h2", "h3", false)
addSimpleTag("h3", "h3", false)
addSimpleTag("m", "span", false, attr{"class", "monospace"})
addSimpleTag("ol", "ol", true)
addSimpleTag("ul", "ul", true)
addSimpleTag("li", "li", false)
addSimpleTag("spoiler", "span", false, attr{"class", "spoiler"})
addSimpleTag("table", "table", true)
addSimpleTag("tr", "tr", true)
addSimpleTag("th", "th", false)
addSimpleTag("td", "td", false)
} }
func makeYoutubeBBCodeFunc(preview bool) bbcode.TagCompilerFunc { func makeYoutubeBBCodeFunc(preview bool) bbcode.TagCompilerFunc {
@ -164,6 +206,7 @@ func (s bbcodeParser) Parse(parent gast.Node, block text.Reader, pc parser.Conte
unparsedBBCode := restOfSource[:endIndex] unparsedBBCode := restOfSource[:endIndex]
block.Advance(len(unparsedBBCode)) block.Advance(len(unparsedBBCode))
fmt.Println("parse this", string(unparsedBBCode))
compiler := realBBCodeCompiler compiler := realBBCodeCompiler
if s.Preview { if s.Preview {

View File

@ -51,3 +51,56 @@ func TestBBCodeParsing(t *testing.T) {
fmt.Println(res) fmt.Println(res)
t.Fail() t.Fail()
} }
const allBBCode = `
[b]bold[/b]
[i]italic[/i]
[u]underline[/u]
[h1]heading 1[/h1]
[h2]heading 2[/h2]
[h3]heading 3[/h3]
[m]monospace[/m]
[ol]
[li]ordered lists[/li]
[/ol]
[ul]
[li]unordered list[/li]
[/ul]
[url]https://handmade.network/[/url]
[url=https://handmade.network/]Handmade Network[/url]
[img=https://handmade.network/static/media/members/avatars/delix.jpeg]Ryan[/img]
[quote]quotes[/quote]
[quote=delix]Some quote[/quote]
[code]
Code
[/code]
[code language=go]
func main() {
fmt.Println("Hello, world!")
}
[/code]
[spoiler]spoilers[/spoiler]
[table]
[tr]
[th]Heading 1[/th] [th]Heading 2[/th]
[/tr]
[tr]
[td]Body 1[/td] [td]Body 2[/td]
[/tr]
[/table]
`