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

@@ -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)