diff --git a/public/parsing.wasm b/public/parsing.wasm index 3467437..ca70a8e 100644 Binary files a/public/parsing.wasm and b/public/parsing.wasm differ diff --git a/src/parsing/bbcode.go b/src/parsing/bbcode.go index cbbae64..3d3437a 100644 --- a/src/parsing/bbcode.go +++ b/src/parsing/bbcode.go @@ -1,6 +1,7 @@ package parsing import ( + "fmt" "regexp" "github.com/frustra/bbcode" @@ -15,7 +16,7 @@ import ( var BBCodePriority = 1 // TODO: This is maybe too high a priority? -var reTag = regexp.MustCompile(`(?P\[\s*(?P[a-zA-Z]+))|(?P\[\s*\/\s*(?P[a-zA-Z]+)\s*\])`) +var reTag = regexp.MustCompile(`(?P\[\s*(?P[a-zA-Z0-9]+))|(?P\[\s*\/\s*(?P[a-zA-Z0-9]+)\s*\])`) var previewBBCodeCompiler = 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}$`) 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)) 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 { @@ -164,6 +206,7 @@ func (s bbcodeParser) Parse(parent gast.Node, block text.Reader, pc parser.Conte unparsedBBCode := restOfSource[:endIndex] block.Advance(len(unparsedBBCode)) + fmt.Println("parse this", string(unparsedBBCode)) compiler := realBBCodeCompiler if s.Preview { diff --git a/src/parsing/parsing_test.go b/src/parsing/parsing_test.go index 4f4573c..6abb8d0 100644 --- a/src/parsing/parsing_test.go +++ b/src/parsing/parsing_test.go @@ -51,3 +51,56 @@ func TestBBCodeParsing(t *testing.T) { fmt.Println(res) 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] +`