Skip to content

Commit

Permalink
Merge pull request #53 from Jeremi360/main
Browse files Browse the repository at this point in the history
parser optimization, fix loading defalut markup option
  • Loading branch information
Jeremi360 authored Jan 3, 2022
2 parents 0d42b5a + c17008e commit cb56fd0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
24 changes: 17 additions & 7 deletions addons/advanced-text/nodes/AdvancedTextLabel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var variables := {
"test_color" : Color("#1acfa0"),
}

var _parser : EBBCodeParser
var _parser
var hf_paths : Array

func _ready() -> void:
Expand All @@ -41,25 +41,35 @@ func get_hf_paths() -> Array:

return hf_paths

func get_text_parser(_markup:String) -> EBBCodeParser:
func get_text_parser(_markup:String):
if _parser == null:
_parser = _get_text_parser(_markup)
elif markup != _markup:
_parser = _get_text_parser(_markup)

return _parser

func _get_text_parser(_markup_str:String) -> EBBCodeParser:
func _get_text_parser(_markup_str:String):
match _markup_str:
"default":
var default = ProjectSettings.get("advanced_text/markup")

var default := ""
# for some reason loading default parser setting works only in editor
# so I save it as metadata and load it during runtime
if Engine.editor_hint:
default = ProjectSettings.get("advanced_text/markup")
set_meta("_default_markup", default)
else:
default = get_meta("_default_markup")

return _get_text_parser(default)

"bbcode":
_parser = EBBCodeParser.new()
_parser = EBBCodeParser
"renpy":
_parser = RenPyMarkupParser.new()
_parser = RenpyParser
"markdown":
_parser = MarkdownParser.new()
_parser = MarkdownParser

return _parser

Expand Down
4 changes: 2 additions & 2 deletions addons/advanced-text/parsers/EBBCodeParser.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tool
extends Object
class_name EBBCodeParser
extends Node
# now is added as autoload singleton : "EBBCodeParser"

# Extended BBCode Parser
# Adds support for <values> and :emojis:
Expand Down
4 changes: 2 additions & 2 deletions addons/advanced-text/parsers/MarkdownParser.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tool
extends EBBCodeParser
class_name MarkdownParser
extends "EBBCodeParser.gd"
# now is added as autoload singleton : "MarkdownParser"

# Markdown Parser
# With support for <values> and :emojis:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tool
extends EBBCodeParser
class_name RenPyMarkupParser
extends "EBBCodeParser.gd"
# now is added as autoload singleton : "RenpyParser"

# RenPy Markup Parser
# it used '<' and '>' for values instead of '[' and ']'
Expand Down
20 changes: 17 additions & 3 deletions addons/advanced-text/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ var default_property_list:Dictionary = {
}

func _enter_tree():
# loads all parser onces
var parsers_dir := "res://addons/advanced-text/parsers/"
add_autoload_singleton("EBBCodeParser", parsers_dir + "EBBCodeParser.gd")
add_autoload_singleton("MarkdownParser", parsers_dir + "MarkdownParser.gd")
add_autoload_singleton("RenpyParser", parsers_dir + "RenpyParser.gd")

# load and add MarkupTextEditor to EditorUI
markup_text_editor = preload("MarkupTextEditor/MarkupTextEditor.tscn")
markup_text_editor = markup_text_editor.instance()
editor_parent = get_editor_interface().get_editor_viewport()
Expand All @@ -35,6 +42,7 @@ func _enter_tree():
markup_text_editor.editor = get_editor_interface()
editor_parent.add_child(markup_text_editor)

# add button for MarkupTextEditor to toolbar
markup_text_editor_button.text = "Markup Text Editor"
markup_text_editor_button.icon = preload("icons/MarkupTextEditor.svg")
markup_text_editor_button.toggle_mode = true
Expand Down Expand Up @@ -62,8 +70,16 @@ func _enter_tree():
ProjectTools.set_setting(property_key, property_value[0], property_value[1])

func _exit_tree():
# remove MarkupTextEditor from EditorUI
markup_text_editor.queue_free()

# remove button from toolbar
markup_text_editor_button.queue_free()

# unloaded all parsers
remove_autoload_singleton("EBBCodeParser")
remove_autoload_singleton("MarkdownParser")
remove_autoload_singleton("RenpyParser")

func hide_current_editor():
for editor in editor_parent.get_children():
Expand Down Expand Up @@ -116,6 +132,4 @@ func _on_toggle(toggled:bool, button:ToolButton):
show_editor(button)

markup_text_editor_button.pressed = toggled
markup_text_editor.visible = toggled


markup_text_editor.visible = toggled
26 changes: 7 additions & 19 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://addons/advanced-text/nodes/CodeEdit.gd"
}, {
"base": "Object",
"class": "EBBCodeParser",
"language": "GDScript",
"path": "res://addons/advanced-text/parsers/EBBCodeParser.gd"
}, {
"base": "EBBCodeParser",
"class": "MarkdownParser",
"language": "GDScript",
"path": "res://addons/advanced-text/parsers/MarkdownParser.gd"
}, {
"base": "Reference",
"class": "ProjectTools",
"language": "GDScript",
Expand All @@ -43,21 +33,13 @@ _global_script_classes=[ {
"class": "PropertyInfo",
"language": "GDScript",
"path": "res://addons/project-settings-helpers/PropertyInfo.gd"
}, {
"base": "EBBCodeParser",
"class": "RenPyMarkupParser",
"language": "GDScript",
"path": "res://addons/advanced-text/parsers/RenPyMarkupParser.gd"
} ]
_global_script_class_icons={
"AdvancedTextButton": "",
"AdvancedTextLabel": "res://addons/advanced-text/icons/AdvancedTextLabel.svg",
"CodeEdit": "res://addons/advanced-text/icons/CodeEdit.svg",
"EBBCodeParser": "",
"MarkdownParser": "",
"ProjectTools": "",
"PropertyInfo": "",
"RenPyMarkupParser": ""
"PropertyInfo": ""
}

[advanced_text]
Expand All @@ -69,6 +51,12 @@ default_vars="{\"test_setting\" : \"variable from project settings\"}"
config/name="AdvancedTextLabel"
config/icon="res://icon.png"

[autoload]

EBBCodeParser="*res://addons/advanced-text/parsers/EBBCodeParser.gd"
MarkdownParser="*res://addons/advanced-text/parsers/MarkdownParser.gd"
RenpyParser="*res://addons/advanced-text/parsers/RenpyParser.gd"

[debug]

gdscript/completion/autocomplete_setters_and_getters=true
Expand Down
12 changes: 7 additions & 5 deletions tests/AdvancedTextLabelTest.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,14 @@ size_flags_horizontal = 3
size_flags_vertical = 3
theme = ExtResource( 3 )
bbcode_enabled = true
bbcode_text = "**This is normally Markdown**
@color=#c39f00 {variable from settings:} @color=lime{variable from project settings}"
text = "**This is normally Markdown**
@color=#c39f00 {variable from settings:} @color=lime{variable from project settings}"
bbcode_text = "[b]This is normally Markdown[/b]
[color=#c39f00]variable from settings:[/color] [color=lime]variable from project settings[/color]"
text = "This is normally Markdown
variable from settings: variable from project settings"
script = ExtResource( 1 )
__meta__ = {
"_default_markup": "markdown"
}
markup_text = "**This is normally Markdown**
@color=#c39f00 {variable from settings:} @color=lime{<test_setting>}"
markup = "bbcode"
headers_fonts = [ ExtResource( 4 ), ExtResource( 2 ), ExtResource( 5 ) ]

0 comments on commit cb56fd0

Please sign in to comment.