@@ -39,23 +39,19 @@ var _REGEX_REPLACE_MAP := {}
39
39
func _init () -> void :
40
40
_REGEXES ["code" ] = RegEx .new ()
41
41
_REGEXES ["func" ] = RegEx .new ()
42
- _REGEXES ["method_call" ] = RegEx .new ()
43
42
_REGEXES ["number" ] = RegEx .new ()
44
43
_REGEXES ["symbol" ] = RegEx .new ()
44
+ _REGEXES ["format" ] = RegEx .new ()
45
+
45
46
46
47
_REGEXES ["code" ].compile ("\\ [code\\ ](.+?)\\ [\\ /code\\ ]" )
47
48
_REGEXES ["func" ].compile ("(?<func>func)" )
48
- _REGEXES ["method_call" ].compile ("(?<method_name>\\ w[\\ w\\ d]*)(?<method_args>\\ (.*?\\ ))" )
49
49
_REGEXES ["number" ].compile ("(?<number>\\ d+(\\ .\\ d+)?)" )
50
- _REGEXES ["symbol" ].compile ("(?<symbol>[a-zA-Z][a-zA-Z0-9_]+)" )
50
+ _REGEXES ["symbol" ].compile ("(?<symbol>[a-zA-Z][a-zA-Z0-9_]+|[a-zA-Z])" )
51
+ _REGEXES ["format" ].compile ("\\ d+(\\ .\\ d+)?|[a-zA-Z0-9_]+" )
51
52
52
53
_REGEX_REPLACE_MAP = {
53
54
"func" : "[color=#%s ]$func[/color]" % CodeEditorEnhancer .COLOR_KEYWORD .to_html (false ),
54
- "method_call" :
55
- (
56
- "[color=#%s ]$method_name[/color]$method_args"
57
- % CodeEditorEnhancer .COLOR_MEMBER .to_html (false )
58
- ),
59
55
"number" : "[color=#%s ]$number[/color]" % CodeEditorEnhancer .COLOR_NUMBERS .to_html (false ),
60
56
"symbol" : "[color=#%s ]$symbol[/color]" % CodeEditorEnhancer .COLOR_MEMBER .to_html (false )
61
57
}
@@ -71,21 +67,30 @@ func bbcode_add_code_color(bbcode_text := "") -> String:
71
67
var match_string : String = regex_match .strings [1 ]
72
68
73
69
var colored_string := ""
74
- for regex_type in [
75
- "func" ,
76
- "method_call" ,
77
- "number" ,
78
- "symbol" ,
79
- ]:
80
- var replaced : String = _REGEXES [regex_type ].sub (
81
- match_string , _REGEX_REPLACE_MAP [regex_type ], false
82
- )
83
- if match_string != replaced :
84
- colored_string = replaced
85
- break
86
-
87
- if colored_string == "" :
88
- colored_string = match_string
70
+ # The algorithm consists of finding all regex matches of a-zA-Z0-9_ and \d.\d
71
+ # Then formatting these regex matches, and adding the parts in-between
72
+ # matches to the formatted string.
73
+ var to_format : Array = _REGEXES ["format" ].search_all (match_string )
74
+ var last_match_end := - 1
75
+ for match_to_format in to_format :
76
+ var match_start : int = match_to_format .get_start ()
77
+ if last_match_end != - 1 :
78
+ colored_string += match_string .substr (last_match_end , match_start - last_match_end )
79
+ var part : String = match_to_format .get_string ()
80
+ for regex_type in [
81
+ "func" ,
82
+ "symbol" ,
83
+ "number" ,
84
+ ]:
85
+ var replaced : String = _REGEXES [regex_type ].sub (
86
+ part , _REGEX_REPLACE_MAP [regex_type ], false
87
+ )
88
+ if part != replaced :
89
+ colored_string += replaced
90
+ last_match_end = match_to_format .get_end ()
91
+ break
92
+
93
+ colored_string += match_string .substr (last_match_end )
89
94
colored_string = "[code]" + colored_string + "[/code]"
90
95
bbcode_text .erase (index_offset , initial_length )
91
96
bbcode_text = bbcode_text .insert (index_offset , colored_string )
0 commit comments