From ee4ed51530371bee6c7ad076d151253f1add4154 Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 2 Apr 2018 16:46:22 +1200 Subject: [PATCH] bbcode: separate string/func transformations into an interface --- bbcode.go | 63 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/bbcode.go b/bbcode.go index aed4780..4495787 100644 --- a/bbcode.go +++ b/bbcode.go @@ -32,46 +32,63 @@ func (this *BBCodeRenderer) Reset() { this.CodePresent = false } +// + +type transformation interface { + MatchString(in string) bool + Apply(in string) string +} + type pregReplaceRule struct { - match *regexp.Regexp - replace string + *regexp.Regexp + replace string +} + +func (this pregReplaceRule) Apply(in string) string { + return this.ReplaceAllString(in, this.replace) +} + +type pregReplaceFunc struct { + *regexp.Regexp replaceFunc func([]string) string } +func (this pregReplaceFunc) Apply(in string) string { + return PregReplaceCallback(this.Regexp, this.replaceFunc, in) +} + // Internal part of BBCode rendering. // It handles most leaf-level tags. func (this *BBCodeRenderer) bbcode(data string) string { - s_to_r := []pregReplaceRule{ - pregReplaceRule{regexp.MustCompile(`(?si)\[h\](.*?)\[/h\]`), `

$1

`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[b\](.*?)\[/b\]`), `$1`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[i\](.*?)\[/i\]`), `$1`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[u\](.*?)\[/u\]`), `$1`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[s\](.*?)\[/s\]`), `$1`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[spoiler\](.*?)\[/spoiler\]`), `$1`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[img\](.*?)\[/img\]`), ``, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[list\](.*?)\[/list\]`), ``, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[\*\]`), `
  • `, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[url=(.*?)\](.*?)\[/url\]`), `$2`, nil}, - pregReplaceRule{regexp.MustCompile(`(?si)\[article=(.*?)\](.*?)\[/article\]`), "", func(m []string) string { + s_to_r := []transformation{ + pregReplaceRule{regexp.MustCompile(`(?si)\[h\](.*?)\[/h\]`), `

    $1

    `}, + pregReplaceRule{regexp.MustCompile(`(?si)\[b\](.*?)\[/b\]`), `$1`}, + pregReplaceRule{regexp.MustCompile(`(?si)\[i\](.*?)\[/i\]`), `$1`}, + pregReplaceRule{regexp.MustCompile(`(?si)\[u\](.*?)\[/u\]`), `$1`}, + pregReplaceRule{regexp.MustCompile(`(?si)\[s\](.*?)\[/s\]`), `$1`}, + pregReplaceRule{regexp.MustCompile(`(?si)\[spoiler\](.*?)\[/spoiler\]`), `$1`}, + pregReplaceRule{regexp.MustCompile(`(?si)\[img\](.*?)\[/img\]`), ``}, + pregReplaceRule{regexp.MustCompile(`(?si)\[list\](.*?)\[/list\]`), ``}, + pregReplaceRule{regexp.MustCompile(`(?si)\[\*\]`), `
  • `}, + pregReplaceRule{regexp.MustCompile(`(?si)\[url=(.*?)\](.*?)\[/url\]`), `$2`}, + pregReplaceFunc{regexp.MustCompile(`(?si)\[article=(.*?)\](.*?)\[/article\]`), func(m []string) string { return `` + m[2] + `` }}, - pregReplaceRule{regexp.MustCompile(`(?si)\[rev=(.*?)\](.*?)\[/rev\]`), "", func(m []string) string { + pregReplaceFunc{regexp.MustCompile(`(?si)\[rev=(.*?)\](.*?)\[/rev\]`), func(m []string) string { return `` + m[2] + `` }}, pregReplaceRule{regexp.MustCompile(`(?si)\[imgur\](.*?)\.(...)\[/imgur\]`), ``, - nil, }, pregReplaceRule{ regexp.MustCompile(`(?si)\[youtube](.*?)\[/youtube\]`), ``, - nil, }, - pregReplaceRule{regexp.MustCompile(`(?si)\[section=(.*?)](.*?)\[/section\]`), "", func(m []string) string { + pregReplaceFunc{regexp.MustCompile(`(?si)\[section=(.*?)](.*?)\[/section\]`), func(m []string) string { return `
    ` + m[1] + `` + strings.TrimSpace(m[2]) + `
    ` }}, } @@ -80,20 +97,14 @@ func (this *BBCodeRenderer) bbcode(data string) string { s_to_r = append(s_to_r, pregReplaceRule{regexp.MustCompile(`(?si)\[` + regexp.QuoteMeta(this.ContentedTag) + `\](.*?)\[/` + regexp.QuoteMeta(this.ContentedTag) + `\]`), ``, - nil, }, ) } for _, prr := range s_to_r { - for prr.match.MatchString(data) { // repeat until all recursive replacements are consumed - if len(prr.replace) > 0 { - data = prr.match.ReplaceAllString(data, prr.replace) - - } else { - data = PregReplaceCallback(prr.match, prr.replaceFunc, data) - } + for prr.MatchString(data) { // repeat until all recursive replacements are consumed + data = prr.Apply(data) } } return data