|
|
|
@ -5,7 +5,8 @@ var DEBUG: bool = false
|
|
|
|
|
enum TAG_TYPE { NULL=0, ROOT=1,
|
|
|
|
|
color,
|
|
|
|
|
u, s, i, b, center, right,
|
|
|
|
|
rainbow, tornado, shake, wave, font
|
|
|
|
|
rainbow, tornado, shake, wave, font,
|
|
|
|
|
img
|
|
|
|
|
}
|
|
|
|
|
const DEFAULT_ALLOWED_TYPES := [TAG_TYPE.color, TAG_TYPE.u, TAG_TYPE.s, TAG_TYPE.b, TAG_TYPE.i]
|
|
|
|
|
|
|
|
|
@ -26,10 +27,11 @@ class BBCodeTag extends Reference:
|
|
|
|
|
elif n is BBCodeTag: r += n.get_full(allowed_types)
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
func get_stripped() -> String:
|
|
|
|
|
func get_stripped(preserve_escape:bool=false) -> String:
|
|
|
|
|
var r := ""
|
|
|
|
|
for n in inner:
|
|
|
|
|
if n is String: r += n
|
|
|
|
|
if n is String and preserve_escape: r += n
|
|
|
|
|
elif n is String and not preserve_escape: r += n.replace("[lb]","[").replace("[rb]","]")
|
|
|
|
|
elif n is BBCodeTag: r += n.get_stripped()
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
@ -73,6 +75,15 @@ class BBCodeUnsafeTag extends BBCodeTag:
|
|
|
|
|
return "[" + tag_str + stuff + "]" + .get_full(allowed_types) + "[/" + tag_str + "]"
|
|
|
|
|
else: return .get_full(allowed_types)
|
|
|
|
|
|
|
|
|
|
class BBCodeImgTag extends BBCodeUnsafeTag:
|
|
|
|
|
func get_full(allowed_types: Array) -> String:
|
|
|
|
|
if TAG_TYPE.img in allowed_types:
|
|
|
|
|
return .get_full(allowed_types)
|
|
|
|
|
else: return ""
|
|
|
|
|
# get stripped for image adds nothing!
|
|
|
|
|
func get_stripped(preserve_escape:bool=false) -> String:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
class BBCodeSimpleTag extends BBCodeTag:
|
|
|
|
|
func get_full(allowed_types: Array) -> String:
|
|
|
|
|
var tag_str = TAG_TYPE.keys()[tag_type]
|
|
|
|
@ -89,6 +100,7 @@ static func tag_creator(tag_type: int, junk: String) -> BBCodeTag:
|
|
|
|
|
var n: BBCodeTag
|
|
|
|
|
match tag_type:
|
|
|
|
|
TAG_TYPE.color: n = BBCodeColorTag.new()
|
|
|
|
|
TAG_TYPE.img: n = BBCodeImgTag.new()
|
|
|
|
|
TAG_TYPE.s, TAG_TYPE.u, TAG_TYPE.i, TAG_TYPE.b,\
|
|
|
|
|
TAG_TYPE.center, TAG_TYPE.right: n = BBCodeSimpleTag.new()
|
|
|
|
|
TAG_TYPE.rainbow, TAG_TYPE.shake, TAG_TYPE.tornado, TAG_TYPE.wave,\
|
|
|
|
@ -109,7 +121,7 @@ func parse_bbcode_text(text: String) -> BBCodeTag:
|
|
|
|
|
|
|
|
|
|
if not tag_matcher:
|
|
|
|
|
tag_matcher = RegEx.new()
|
|
|
|
|
tag_matcher.compile("(.*?)(\\[(\\w+)([^\\[\\]]*?)\\]|\\[/(\\w+)\\])")
|
|
|
|
|
tag_matcher.compile("(?s)(.*?)(\\[(\\w+)([^\\[\\]]*?)\\]|\\[/(\\w+)\\])")
|
|
|
|
|
|
|
|
|
|
var linear_matches: Array = tag_matcher.search_all(text)
|
|
|
|
|
# no tags - plaintext
|
|
|
|
@ -152,6 +164,17 @@ func parse_bbcode_text(text: String) -> BBCodeTag:
|
|
|
|
|
# add leading text to current tag
|
|
|
|
|
cur_tag.inner.push_back(before.replace('[','[lb]'))
|
|
|
|
|
|
|
|
|
|
# special case for [lb] [rb] escapes
|
|
|
|
|
if not is_close and tag == "lb" or tag == "rb":
|
|
|
|
|
cur_tag.inner.push_back("["+tag+"]")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# unsupported bbcode - treat as text
|
|
|
|
|
if tag_type == TAG_TYPE.NULL:
|
|
|
|
|
var opener = "[lb]" if not is_close else "[lb]/"
|
|
|
|
|
cur_tag.inner.push_back(opener+tag+junk+"[rb]")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# we got a closing tag, unroll the stack
|
|
|
|
|
# until we get a matching open or root
|
|
|
|
|
if is_close:
|
|
|
|
@ -242,10 +265,13 @@ static func find_in_strings(bbcode: BBCodeTag, find: String) -> bool:
|
|
|
|
|
|
|
|
|
|
func test():
|
|
|
|
|
var tests := [
|
|
|
|
|
"foo bar",
|
|
|
|
|
"Choose how the lobby operates.\n[color=#6a4420]PUBLIC[/color]: Public and visible in the Game Browser for all to join.\n[color=#6a4420]CODE-ONLY[/color]: Public, but not visible in the Game Browser, while still allowing players to join via.\n[color=#6a4420]FRIENDS-ONLY[/color]: Codeless Lobby that is hidden and inaccessible to all players except Steam friends.\n[color=#6a4420]OFFLINE/SOLO[/color]: Start a game alone, with players unable to join you.",
|
|
|
|
|
"[haha i am very cool]",
|
|
|
|
|
"[b]foo[img=500]test1[/img]bar[img]test2[i]a[/i][/img][/b]",
|
|
|
|
|
"foo [lb]bar[rb]",
|
|
|
|
|
"foo [u]foobar[/u] bar",
|
|
|
|
|
"foo [color=red]foobar[/u] bar",
|
|
|
|
|
"foo [color=#10ffffff]foobar[/u] bar",
|
|
|
|
|
"foo [color=#10ffffff]fo[lb]obar[/u] bar",
|
|
|
|
|
"foo [color=#ffffff]foobar[/u] bar",
|
|
|
|
|
"foo [color=#1111111111111]foobar[/u] bar",
|
|
|
|
|
"foo [color=transparent]foobar[/u] bar",
|
|
|
|
@ -267,6 +293,7 @@ func test():
|
|
|
|
|
print("[BB TEST FULL ALL] ", r.get_full(TAG_TYPE.values()))
|
|
|
|
|
print("[BB TEST U] ", r.get_full([TAG_TYPE.u]))
|
|
|
|
|
print("[BB TEST STRIPPED] ", r.get_stripped())
|
|
|
|
|
print("[BB TEST STRIPPED(true)] ", r.get_stripped(true))
|
|
|
|
|
print("[BB TEST LEN 10] ", parsed_to_text(r, DEFAULT_ALLOWED_TYPES, 10))
|
|
|
|
|
clamp_alpha(r, 0.5)
|
|
|
|
|
print("[BB TEST ALPHA] ", r.get_full([TAG_TYPE.color]))
|
|
|
|
|