-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add strip_bbcode() method to String #65839
Conversation
This is a great idea. Haven't looked in awhile, but if the implementation is the same, would it make more sense to add this to the |
I'm still not a big expert on Godot's code. I wasn't able to find the function that does that to RTLs already (to convert them to the |
1d9e467
to
100a0c5
Compare
I don't see why you would call it in the RTL given that if you're using BBCode it has to process the text and if not then you want it to be a raw string including the BBCode tags. |
I think they meant make the RTL and String use the same function for striping it, because the RTL converts |
100a0c5
to
1038af5
Compare
Actually, there is a way to escape BBCode. Maybe we should add |
So it needs a check to make it convert [lb] and [rb] to brackets? I can start on that soon. |
@markdibarry any idea where the script that checks for valid bbcode is? |
Given that custom BBCodes can be added, I think it's correct to only consider the syntax (strip all tags, including non-existent and non-matching ones) and point this out in the documentation. |
The only simple independent way I can think of this being implemented is by having a list of valid tag names in an array and "skipping" past anything outside that array, honestly doesn't look like that be difficult, (perhaps that can be considered, if so however it should be under a different name so it'll handle the default tags by distinctly and by default like |
If custom BBCode tags can be added, and RichTextLabel can access this, then surely this function could just automatically access whatever that store is, right? That seems like it would be the most elegant solution. |
That's the clunkiest solution actually, as you would be required to pass the RTL every time you made a call and makes the function heavily dependent upon the RTL. (meaning as well if you don't use the RTL now you're polluting the dependency of the class just for that function, which iirc is actually prohibited as new additions anyway as core can't be dependent upon scene files) And that aside its unnecessary since all the information this function needs to remove known tags is the name of the tags, as in all you need is strings, you don't need anything else in the RTL and don't need to keep a reference to an RTL in order to use it. |
Considering the primary use case here is for RTLs, maybe this should be a function attached to RTLs? Like $RichTextLabel.strip_bbcode(string) which just returns the stripped text as the RTL would display it, and doesn't affect the RTL's actual bbcode_text or text? It not returning an identical string to what the RTL will end up displaying feels like it could make this function have limited use for many. |
I guess the main purpose of |
3865426
to
8f8b213
Compare
Funny enough, that's the exact use case I have. When working with dialog, knowing the indices of rendered characters can be crucial. With any custom text syntax, your two options are to manually keep a list of valid bbcode tags up to date, or:
Can be a little over-the-top. |
This is my exact use case as well, but I don't use custom bbcode. Wasn't sure if it was an odd edge case or not. I use it to make the text make sounds only for precise durations, and pause when the text reaches a pause. |
Sounds rad. Keep me posted with your project's progress after this! |
6748318
to
4d08028
Compare
I prefer not to rely on indexes, but to use invisible control characters. According to your description, it should not be We also need to remember that |
if (operator[](i) == '[') { | ||
int skip_char = 0; | ||
|
||
//Check for [rb] and [lb]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess strip_bbcode
should just remove all BBCodes without parsing them (even [code]
). That is:
"[lb]text[rb]".strip_bbcode() # "text", not "[text]"
Otherwise, after strip_bbcode
, we can get a string containing the BBCode, which even sounds wrong:
"[lb]b[rb]text[lb]/b[rb]".strip_bbcode() # "[b]text[/b]"
As I wrote above, if the functionality is needed, then it should not be the String.strip_bbcode
method, but RichTextLabel.get_parsed_text_from_string
or something similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But what if they want brackets in the text they're typing for some reason? I don't see why example 2 would ever come up outside of somebody trying to intentionally "break" it.
Replaces #65384.
Adds a function to strip bbcode tags out of strings.
Example Code:
Result:
Hello
Closes godotengine/godot-proposals#5056