group inline text tags, new tag - span

This commit is contained in:
Face
2025-07-22 21:15:57 +03:00
parent 1f73a7070f
commit 441c921029
26 changed files with 86 additions and 193 deletions

View File

@@ -14,7 +14,8 @@ font_names = PackedStringArray("Serif")
font_italic = true
[sub_resource type="SystemFont" id="SystemFont_u0shw"]
font_names = PackedStringArray("Monospace")
font_names = PackedStringArray("Consolas")
oversampling = 2.0
[sub_resource type="SystemFont" id="SystemFont_g4dwp"]
font_names = PackedStringArray("Serif")

View File

@@ -1,23 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://ijrbim6j8e36"]
[ext_resource type="Script" uid="uid://chdftsciuecfk" path="res://Scripts/Tags/bold.gd" id="1_04sgx"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="1_mtsji"]
[node name="BOLD" type="VBoxContainer"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
theme = ExtResource("1_mtsji")
script = ExtResource("1_04sgx")
[node name="RichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_horizontal = 3
focus_mode = 2
mouse_default_cursor_shape = 1
theme = ExtResource("1_mtsji")
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

View File

@@ -1,22 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://bdw0ahhesgnvr"]
[ext_resource type="Script" uid="uid://bamty87whxwxi" path="res://Scripts/Tags/italic.gd" id="1_ch1cm"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="1_vh1cg"]
[node name="ITALIC" type="VBoxContainer"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
theme = ExtResource("1_vh1cg")
script = ExtResource("1_ch1cm")
[node name="RichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_horizontal = 3
focus_mode = 2
mouse_default_cursor_shape = 1
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

@@ -1,22 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://dlj64k35igexw"]
[ext_resource type="Script" uid="uid://c2i8dwyyuufff" path="res://Scripts/Tags/mark.gd" id="1_j8rvb"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="1_jr508"]
[node name="MARK" type="VBoxContainer"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
theme = ExtResource("1_jr508")
script = ExtResource("1_j8rvb")
[node name="RichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_horizontal = 3
focus_mode = 2
mouse_default_cursor_shape = 1
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

@@ -17,6 +17,5 @@ mouse_default_cursor_shape = 1
theme = ExtResource("2_1glvj")
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

@@ -1,22 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://btiiex1eu1krs"]
[ext_resource type="Script" uid="uid://xr7n1lat501x" path="res://Scripts/Tags/small.gd" id="1_5vt6h"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="1_yi3vu"]
[node name="SMALL" type="VBoxContainer"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
theme = ExtResource("1_yi3vu")
script = ExtResource("1_5vt6h")
[node name="RichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_horizontal = 3
focus_mode = 2
mouse_default_cursor_shape = 1
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

@@ -1,22 +1,21 @@
[gd_scene load_steps=3 format=3 uid="uid://i5otagppegeo"]
[gd_scene load_steps=3 format=3 uid="uid://bkj3x5y2m8qrl"]
[ext_resource type="Script" uid="uid://c3dnmqsnj0akr" path="res://Scripts/Tags/code.gd" id="1_g4dwp"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="2_u0shw"]
[ext_resource type="Script" uid="uid://cjk4x6y8m9wts" path="res://Scripts/Tags/span.gd" id="1_span"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="2_theme"]
[node name="CODE" type="VBoxContainer"]
[node name="SPAN" type="VBoxContainer"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
script = ExtResource("1_g4dwp")
script = ExtResource("1_span")
[node name="RichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_horizontal = 3
focus_mode = 2
mouse_default_cursor_shape = 1
theme = ExtResource("2_u0shw")
theme = ExtResource("2_theme")
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

@@ -1,22 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://br7ys1wgsp5om"]
[ext_resource type="Script" uid="uid://x0hphtm8kf12" path="res://Scripts/Tags/underline.gd" id="1_7hb8m"]
[ext_resource type="Theme" uid="uid://bn6rbmdy60lhr" path="res://Scenes/Styles/BrowserText.tres" id="1_xp1ft"]
[node name="UNDERLINE" type="VBoxContainer"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
theme = ExtResource("1_xp1ft")
script = ExtResource("1_7hb8m")
[node name="RichTextLabel" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_horizontal = 3
focus_mode = 2
mouse_default_cursor_shape = 1
theme_override_colors/default_color = Color(0, 0, 0, 1)
bbcode_enabled = true
text = "What the sigma"
fit_content = true
selection_enabled = true

View File

@@ -25,7 +25,6 @@ class HTMLElement:
return get_attribute("id")
func get_collapsed_text() -> String:
# Collapse whitespace: replace multiple spaces, tabs, newlines with single space
var collapsed = text_content.strip_edges()
# Replace multiple whitespace characters with single space
var regex = RegEx.new()
@@ -33,8 +32,48 @@ class HTMLElement:
return regex.sub(collapsed, " ", true)
func get_preserved_text() -> String:
# For pre tags - preserve all whitespace
return text_content
func get_bbcode_formatted_text() -> String:
var result = ""
var has_previous_content = false
if text_content.length() > 0:
result += get_collapsed_text()
has_previous_content = true
for child in children:
var child_content = ""
match child.tag_name:
"b":
child_content = "[b]" + child.get_bbcode_formatted_text() + "[/b]"
"i":
child_content = "[i]" + child.get_bbcode_formatted_text() + "[/i]"
"u":
child_content = "[u]" + child.get_bbcode_formatted_text() + "[/u]"
"small":
child_content = "[font_size=20]" + child.get_bbcode_formatted_text() + "[/font_size]"
"mark":
child_content = "[bgcolor=#FFFF00]" + child.get_bbcode_formatted_text() + "[/bgcolor]"
"code":
child_content = "[font_size=20][code]" + child.get_bbcode_formatted_text() + "[/code][/font_size]"
"span":
child_content = child.get_bbcode_formatted_text()
_:
child_content = child.get_bbcode_formatted_text()
if has_previous_content and child_content.length() > 0:
result += " "
result += child_content
if child_content.length() > 0:
has_previous_content = true
return result
func is_inline_element() -> bool:
return tag_name in ["b", "i", "u", "small", "mark", "code", "span"]
class ParseResult:
var root: HTMLElement

View File

@@ -1,5 +0,0 @@
extends VBoxContainer
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=24][b]%s[/b][/font_size]" % element.get_collapsed_text()

View File

@@ -1 +0,0 @@
uid://chdftsciuecfk

View File

@@ -1,5 +0,0 @@
extends VBoxContainer
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=20][code]%s[/code][/font_size]" % element.get_collapsed_text()

View File

@@ -1 +0,0 @@
uid://c3dnmqsnj0akr

View File

@@ -1,5 +0,0 @@
extends VBoxContainer
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=24][i]%s[/i][/font_size]" % element.get_collapsed_text()

View File

@@ -1 +0,0 @@
uid://bamty87whxwxi

View File

@@ -1,5 +0,0 @@
extends VBoxContainer
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=24][bgcolor=#FFFF00]%s[/bgcolor][/font_size]" % element.get_collapsed_text()

View File

@@ -1 +0,0 @@
uid://c2i8dwyyuufff

View File

@@ -2,4 +2,4 @@ extends Control
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=24]%s[/font_size]" % element.get_collapsed_text()
label.text = "[font_size=24]%s[/font_size]" % element.get_bbcode_formatted_text()

View File

@@ -1,5 +0,0 @@
extends VBoxContainer
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=18]%s[/font_size]" % element.get_collapsed_text()

View File

@@ -1 +0,0 @@
uid://xr7n1lat501x

5
Scripts/Tags/span.gd Normal file
View File

@@ -0,0 +1,5 @@
extends Control
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=24]%s[/font_size]" % element.get_bbcode_formatted_text()

1
Scripts/Tags/span.gd.uid Normal file
View File

@@ -0,0 +1 @@
uid://4pbphta3r67k

View File

@@ -1,5 +0,0 @@
extends VBoxContainer
func init(element: HTMLParser.HTMLElement) -> void:
var label: RichTextLabel = $RichTextLabel
label.text = "[font_size=24][u]%s[/u][/font_size]" % element.get_collapsed_text()

View File

@@ -1 +0,0 @@
uid://x0hphtm8kf12

View File

@@ -10,14 +10,9 @@ var loading_tween: Tween
const P = preload("res://Scenes/Tags/p.tscn")
const IMG = preload("res://Scenes/Tags/img.tscn")
const SEPARATOR = preload("res://Scenes/Tags/separator.tscn")
const BOLD = preload("res://Scenes/Tags/bold.tscn")
const ITALIC = preload("res://Scenes/Tags/italic.tscn")
const UNDERLINE = preload("res://Scenes/Tags/underline.tscn")
const SMALL = preload("res://Scenes/Tags/small.tscn")
const MARK = preload("res://Scenes/Tags/mark.tscn")
const CODE = preload("res://Scenes/Tags/code.tscn")
const PRE = preload("res://Scenes/Tags/pre.tscn")
const BR = preload("res://Scenes/Tags/br.tscn")
const SPAN = preload("res://Scenes/Tags/span.tscn")
func render():
# Clear existing content
@@ -26,7 +21,7 @@ func render():
var html_bytes = "<head>
<title>My cool web</title>
<icon src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Google_%22G%22_logo.svg/768px-Google_%22G%22_logo.svg.png\"> <!--This image will be the page's icon-->
<icon src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Google_%22G%22_logo.svg/768px-Google_%22G%22_logo.svg.png\">
<meta name=\"theme-color\" content=\"#000000\">
<meta name=\"description\" content=\"My cool web\">
@@ -38,11 +33,11 @@ func render():
<body>
<p>Hey there! this is a test</p>
<b>This is bold</b>
<i>This is italic</i>
<i>This is italic <mark>actually, and it's pretty <u>cool</u></mark></i>
<u>This is underline</u>
<small>this is small</small>
<mark>this is marked</mark>
<code>this is code</code>
<code>this is code<span> THIS IS A SPAN AND SHOULDNT BE ANY DIFFERENT</span></code>
<pre>
Text in a pre element
@@ -78,7 +73,26 @@ line breaks
stop_loading_icon()
var body = parser.find_first("body")
for element: HTMLParser.HTMLElement in body.children:
var i = 0
while i < body.children.size():
var element: HTMLParser.HTMLElement = body.children[i]
if element.is_inline_element():
# Collect consecutive inline elements and flatten nested ones
var inline_elements: Array[HTMLParser.HTMLElement] = []
while i < body.children.size() and body.children[i].is_inline_element():
inline_elements.append(body.children[i])
i += 1
var inline_container = P.instantiate()
var temp_parent = HTMLParser.HTMLElement.new()
temp_parent.tag_name = "p"
temp_parent.children = inline_elements
inline_container.init(temp_parent)
website_container.add_child(inline_container)
continue
match element.tag_name:
"p":
var p = P.instantiate()
@@ -92,10 +106,6 @@ line breaks
var br = BR.instantiate()
br.init(element)
website_container.add_child(br)
"b":
var bold = BOLD.instantiate()
bold.init(element)
website_container.add_child(bold)
"img":
var img = IMG.instantiate()
img.init(element)
@@ -104,28 +114,14 @@ line breaks
var separator = SEPARATOR.instantiate()
separator.init(element)
website_container.add_child(separator)
"i":
var italic = ITALIC.instantiate()
italic.init(element)
website_container.add_child(italic)
"u":
var underline = UNDERLINE.instantiate()
underline.init(element)
website_container.add_child(underline)
"small":
var small = SMALL.instantiate()
small.init(element)
website_container.add_child(small)
"mark":
var mark = MARK.instantiate()
mark.init(element)
website_container.add_child(mark)
"code":
var code = CODE.instantiate()
code.init(element)
website_container.add_child(code)
"span":
var span = SPAN.instantiate()
span.init(element)
website_container.add_child(span)
_:
print("Couldn't parse unsupported HTML tag \"%s\"" % element.tag_name)
i += 1
func set_loading_icon(tab: Tab) -> void:
tab.set_icon(LOADER_CIRCLE)