Compare commits

..

6 Commits
0.1 ... master

Author SHA1 Message Date
f6cae08b10 0.1.2 2024-11-15 18:24:42 -06:00
901b01ac61 newline fix 2024-11-15 18:19:04 -06:00
4de5c4f057 0.1.1 2024-11-15 16:31:58 -06:00
da04c6201f fix logging 2024-11-15 16:27:20 -06:00
1862b9be04 bracket fixes? 2024-11-15 15:57:10 -06:00
d135ee52d8 img support 2024-11-15 15:08:30 -06:00
3 changed files with 50 additions and 11 deletions

View File

@ -2,3 +2,12 @@
---- ----
initial release initial release
0.1.1
----
- fix \[lb], \[rb], and \[text between brackets]
- add img tag support & stripping
- fix message logging
0.1.2
----
- fix newline handling

View File

@ -5,7 +5,8 @@ var DEBUG: bool = false
enum TAG_TYPE { NULL=0, ROOT=1, enum TAG_TYPE { NULL=0, ROOT=1,
color, color,
u, s, i, b, center, right, 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] 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) elif n is BBCodeTag: r += n.get_full(allowed_types)
return r return r
func get_stripped() -> String: func get_stripped(preserve_escape:bool=false) -> String:
var r := "" var r := ""
for n in inner: 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() elif n is BBCodeTag: r += n.get_stripped()
return r return r
@ -73,6 +75,15 @@ class BBCodeUnsafeTag extends BBCodeTag:
return "[" + tag_str + stuff + "]" + .get_full(allowed_types) + "[/" + tag_str + "]" return "[" + tag_str + stuff + "]" + .get_full(allowed_types) + "[/" + tag_str + "]"
else: return .get_full(allowed_types) 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: class BBCodeSimpleTag extends BBCodeTag:
func get_full(allowed_types: Array) -> String: func get_full(allowed_types: Array) -> String:
var tag_str = TAG_TYPE.keys()[tag_type] 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 var n: BBCodeTag
match tag_type: match tag_type:
TAG_TYPE.color: n = BBCodeColorTag.new() 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.s, TAG_TYPE.u, TAG_TYPE.i, TAG_TYPE.b,\
TAG_TYPE.center, TAG_TYPE.right: n = BBCodeSimpleTag.new() TAG_TYPE.center, TAG_TYPE.right: n = BBCodeSimpleTag.new()
TAG_TYPE.rainbow, TAG_TYPE.shake, TAG_TYPE.tornado, TAG_TYPE.wave,\ 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: if not tag_matcher:
tag_matcher = RegEx.new() tag_matcher = RegEx.new()
tag_matcher.compile("(.*?)(\\[(\\w+)([^\\[\\]]*?)\\]|\\[/(\\w+)\\])") tag_matcher.compile("(?s)(.*?)(\\[(\\w+)([^\\[\\]]*?)\\]|\\[/(\\w+)\\])")
var linear_matches: Array = tag_matcher.search_all(text) var linear_matches: Array = tag_matcher.search_all(text)
# no tags - plaintext # no tags - plaintext
@ -152,6 +164,17 @@ func parse_bbcode_text(text: String) -> BBCodeTag:
# add leading text to current tag # add leading text to current tag
cur_tag.inner.push_back(before.replace('[','[lb]')) 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 # we got a closing tag, unroll the stack
# until we get a matching open or root # until we get a matching open or root
if is_close: if is_close:
@ -242,10 +265,13 @@ static func find_in_strings(bbcode: BBCodeTag, find: String) -> bool:
func test(): func test():
var tests := [ 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 [u]foobar[/u] bar",
"foo [color=red]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=#ffffff]foobar[/u] bar",
"foo [color=#1111111111111]foobar[/u] bar", "foo [color=#1111111111111]foobar[/u] bar",
"foo [color=transparent]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 FULL ALL] ", r.get_full(TAG_TYPE.values()))
print("[BB TEST U] ", r.get_full([TAG_TYPE.u])) print("[BB TEST U] ", r.get_full([TAG_TYPE.u]))
print("[BB TEST STRIPPED] ", r.get_stripped()) 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)) print("[BB TEST LEN 10] ", parsed_to_text(r, DEFAULT_ALLOWED_TYPES, 10))
clamp_alpha(r, 0.5) clamp_alpha(r, 0.5)
print("[BB TEST ALPHA] ", r.get_full([TAG_TYPE.color])) print("[BB TEST ALPHA] ", r.get_full([TAG_TYPE.color]))

View File

@ -19,13 +19,16 @@ func set_hlogmsg(val): pass
func _enter_tree(): func _enter_tree():
NetManager = NetManager_t.new() NetManager = NetManager_t.new()
BBCode = BBCode_t.new() BBCode = BBCode_t.new()
#NetManager.DEBUG = true
#BBCode.DEBUG = true
func _ready(): func _ready():
print("[LUCYSLIB] LucysLib 0.1.0 ready") print("[LUCYSLIB] LucysLib 0.1.2 ready")
#BBCode.DEBUG = true
#BBCode.test() #BBCode.test()
#func packet_dump(PACKET):
# print("[PACKET] ", PACKET)
# print("[PACKET DECOMPRESSED DATA] ", PACKET.data.decompress_dynamic( - 1, Network.COMPRESSION_TYPE))
func register_bb_msg_support(): func register_bb_msg_support():
if HAS_BB_MSG: return if HAS_BB_MSG: return
print("[LUCYSLIB] registering bbcode message receive support...") print("[LUCYSLIB] registering bbcode message receive support...")
@ -40,13 +43,13 @@ func register_log_msg_support():
# future use # future use
func process_packet_lucy_packet(DATA, PACKET_SENDER, from_host) -> bool: func process_packet_lucy_packet(DATA, PACKET_SENDER, from_host) -> bool:
print("[LUCY PACKET] [" + PACKET_SENDER + " " + Network._get_username_from_id(PACKET_SENDER) + "]") print("[LUCY PACKET] [" + str(PACKET_SENDER) + " " + str(Network._get_username_from_id(PACKET_SENDER)) + "]")
return true return true
# message logging # message logging
func process_packet_message_log(DATA, PACKET_SENDER, from_host) -> bool: func process_packet_message_log(DATA, PACKET_SENDER, from_host) -> bool:
if LOG_MESSAGES: if LOG_MESSAGES:
print("[MSG] [" + PACKET_SENDER + " " + Network._get_username_from_id(PACKET_SENDER) + "] " + DATA) print("[MSG] [" + str(PACKET_SENDER) + " " + str(Network._get_username_from_id(PACKET_SENDER)) + "] " + str(DATA))
return false return false
# bbcode support in messages # bbcode support in messages