Open RedMser opened 3 months ago
I had a similar idea. We could add an abstract SyntaxParser
and default implementations for BBCode and Godot Doc BBCode. Users could then implement their own parsers for RichTextLabel
, for example to add Markdown support.
This might be useful for proper BBCode-to-ANSI conversion as well (which is needed for print_rich()
to fully support terminal output correctly).
@dalexeev I like your proposal to make this more focused on inheritence, although the API would have to be well thought out to make it worth expanding to other domains on in the future. I'd personally remove the generic SyntaxParser base class. Too many domain specific demands, like parsing PackedByteArray or streaming data. Something like XML, JSON or GDScript is unlikely to be able to switch to a new base class like this one.
Also Resource makes sense, for re-using parsers and storing their configurations in tres files.
Not sure why you need to set the RTL instance, it should probably only be a one-directional relationship, and RTL updates properties on the parser instance to add custom richtexteffects.
I'll update my proposal soon accordingly. Then I'll ask for feedback from RTL maintainers, also since I saw a new PR for BBCode stringify appear.
@bruvzg Hi, I'd like your feedback on this proposal if you have the time, since you're very deep in the code of RichTextLabel and BBCode.
I'm mainly looking for answers of the open questions near the end of the issue. But a look over the code API might make sense as well, since it would be great to expose to scripting / GDExtensions.
Describe the project you are working on
Godot Editor
Describe the problem or limitation you are having in your project
While thinking about solutions for #10318, I've noticed that Godot has many ways a BBCode string is parsed or tokenized:
RichTextLabel::append_text
EditorHelp::_add_text_to_rt
BindingsGenerator::bbcode_to_text
make_rst.py
'sformat_text_block
Each implementation has its own flavor of BBCode and supported tags and syntaxes. It makes changing the BBCode syntax or parsing it for a new purpose (e.g. conversion to different formats like markdown or HTML) very difficult.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Create a configurable
BBCodeParser
class which will be used to replace the individual parser implementations. Try to unify the different BBCode syntaxes as much as possible, still allowing configuration while being type-safe. From what I can tell, the intended syntax flavors are:[Node2D]
or[method blah]
)I am not sure if XML documentation is supposed to be a superset of RichTextLabel. As it stands, many tags are not supported, and the new documentation syntax is only very loosely interpretable as BBCode (it should be
[method=blah]
or[method name=blah]
to be correct - but we could generally allow things like[color green]
in the new parser too).I would expose the
BBCodeParser
to scripting as well, unless there is opposition due to the number of classes it would introduce (see code snippets below).Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Class definitions:
Example usage:
RichTextLabel would for example instantiate a
RichTextLabelBBCodeParser
in its constructor and add any RichTextEffects to the tag definition list.Stringifying the parser results back to BBCode could be part of the base parser API as well. But other conversion targets like Markdown or RST should be implemented by the caller of the parser.
Open discussion points:
Item
structure, or can we completely move to the parser's result structure? Does it need any other API or information to make theItem
structure obsolete? For example maybe it needs to store the start and end location of every item...?push
andpop
methods like RichTextLabel has them? Not something likepush_bold
, but a genericpush_tag(tag: BBCodeTag)
.[Abc]
is a valid type directly when parsing.reset()
orflush()
method? It basically asserts "this is the end of the String, anything that's wrong now is an error".[lb]
and[rb]
this is how RichTextLabel does it[[
and]]
[[]
and[]]
\[
and\]
this is how BBob does itIf this enhancement will not be used often, can it be worked around with a few lines of script?
Yes but writing a new feature-complete BBCode parser is a lot of effort to maintain and prone to diverge from other implementations.
Is there a reason why this should be core and not an add-on in the asset library?
BBCode parsers are already in core. This is a refactor combined with exposing the new class to scripting for user's convenience.