Skip to content
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 a function to erase BBCode from a String, without depending on RichTextLabel #5056

Open
DeeJayLSP opened this issue Aug 2, 2022 · 4 comments · May be fixed by godotengine/godot#78310
Open

Comments

@DeeJayLSP
Copy link

DeeJayLSP commented Aug 2, 2022

Describe the project you are working on

Dialogue boxes with custom tags

Describe the problem or limitation you are having in your project

In order to work properly with custom tags in a String, one needs to remove the BBCode from it first. The only ways to remove BBCode from a String are either giving it to a RichTextLabel and getting its text property (which may be slow if you're intending to reassign it right after), or writing a whole new function just to do the work, which is not an easy task.

Considering how removing BBCode is done in a certain way by Godot, there should be an easy way to do it without depending on those previous cases.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

String has many built-in functions that somehow modify it.

One function that should be there should be to remove BBCode.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Some function like String.erase_bbcode()

var bbcoded_string = "[b]Bold text[/b]"
print(bbcoded_string.erase_bbcode()) #Prints Bold text

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, but it's something that could just be exposed as a function.

I believe this would be used more often than half of the built-in String functions.

The workarounds have already been explained above.

Is there a reason why this should be core and not an add-on in the asset library?

It's intended to work directly on a String, therefore an addon would add some unnecessary code when it could be a simple function.

@YuriSizov
Copy link
Contributor

YuriSizov commented Aug 2, 2022

strip_bbcode probably would be a better name.

It should likely strip every [tag]-like substring in the source string (and the closing pair, if present), without relying on Godot's own implementation of BBCode and the tags that we have defined.

@mrcdk
Copy link

mrcdk commented Aug 2, 2022

If the purpose is to strip all bbcode tags then this should do the trick:

func strip_bbcode(source:String) -> String:
	var regex = RegEx.new()
	regex.compile("\\[.+?\\]")
	return regex.sub(source, "", true)

But I'm not sure I'm following, what do you mean with custom tags? You can use String.format() to preprocess the string swapping the custom tags with your values by changing the placeholder to [_]. You can also change the placeholder as you wish. I usually use {{_}}

For example:

extends Node

func _ready():
	var message = "[url={'meta':'json}]JSON[/url] [randomtag a=30 b='hello' c=true]something[/randomtag] My name is [name] and I'm [age] years old"
	var stripped_tags = strip_bbcode(message)
	print(stripped_tags) # JSON something My name is  and I'm  years old
	
	var formatted_string = message.format({"name": "John Doe", "age": "30"}, "[_]")
	print(formatted_string) # [url={'meta':'json}]JSON[/url] [randomtag a=30 b='hello' c=true]something[/randomtag] My name is John Doe and I'm 30 years old
	
	var formatted_and_stripped = strip_bbcode(formatted_string)
	print(formatted_and_stripped) # JSON something My name is John Doe and I'm 30 years old
	
func strip_bbcode(source:String) -> String:
	var regex = RegEx.new()
	regex.compile("\\[.+?\\]")
	return regex.sub(source, "", true)

@ztc0611
Copy link

ztc0611 commented Sep 5, 2022

Just ran into this issue myself, the code above does work but a clean built in function to handle it would be nice.

@dalexeev
Copy link
Member

Actually, there is a way to escape BBCode. Maybe we should add escape_bbcode instead of / together with strip_bbcode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants