|
| 1 | +@tool |
| 2 | +extends RichTextLabel |
| 3 | + |
| 4 | +const HEADING_STRING_REPLACEMENT = "$1[font_size=%d]$2[/font_size]" |
| 5 | +const BOLD_HEADING_STRING_REPLACEMENT = "$1[b][font_size=%d]$2[/font_size][/b]" |
| 6 | + |
| 7 | +@export_group("Font Sizes") |
| 8 | +@export var h1_font_size: int |
| 9 | +@export var h2_font_size: int |
| 10 | +@export var h3_font_size: int |
| 11 | +@export var h4_font_size: int |
| 12 | +@export var h5_font_size: int |
| 13 | +@export var h6_font_size: int |
| 14 | +@export var bold_headings : bool |
| 15 | +@export_group("Extra Options") |
| 16 | +@export var disable_images : bool = false |
| 17 | +@export var disable_urls : bool = false |
| 18 | +@export var disable_bolds : bool = false |
| 19 | + |
| 20 | +func regex_replace_imgs(markdown_text:String) -> String: |
| 21 | + var regex = RegEx.new() |
| 22 | + var match_string := "<img .* src=\"(.*)\" \\/>" |
| 23 | + var replace_string := "" |
| 24 | + if not disable_images: |
| 25 | + replace_string = "$1" |
| 26 | + regex.compile(match_string) |
| 27 | + return regex.sub(markdown_text, replace_string, true) |
| 28 | + |
| 29 | +func regex_replace_urls(markdown_text:String) -> String: |
| 30 | + var regex = RegEx.new() |
| 31 | + var match_string := "(https:\\/\\/.*)($|\\s)" |
| 32 | + var replace_string := "$1" |
| 33 | + if not disable_urls: |
| 34 | + replace_string = "[url=$1]$1[/url]" |
| 35 | + regex.compile(match_string) |
| 36 | + return regex.sub(markdown_text, replace_string, true) |
| 37 | + |
| 38 | +func regex_replace_bolds(markdown_text:String) -> String: |
| 39 | + var regex = RegEx.new() |
| 40 | + var match_string := "\\*\\*(.*)\\*\\*" |
| 41 | + var replace_string := "$1" |
| 42 | + if not disable_bolds: |
| 43 | + replace_string = "[b]$1[/b]" |
| 44 | + regex.compile(match_string) |
| 45 | + return regex.sub(markdown_text, replace_string, true) |
| 46 | + |
| 47 | +func regex_replace_titles(markdown_text:String) -> String: |
| 48 | + var iter = 0 |
| 49 | + var heading_font_sizes : Array[int] = [ |
| 50 | + h1_font_size, |
| 51 | + h2_font_size, |
| 52 | + h3_font_size, |
| 53 | + h4_font_size, |
| 54 | + h5_font_size, |
| 55 | + h6_font_size] |
| 56 | + for heading_font_size in heading_font_sizes: |
| 57 | + iter += 1 |
| 58 | + var regex = RegEx.new() |
| 59 | + var match_string : String = "([^#]|^)#{%d}\\s([^\n]*)" % iter |
| 60 | + var replace_string := HEADING_STRING_REPLACEMENT % [heading_font_size] |
| 61 | + if bold_headings: |
| 62 | + replace_string = BOLD_HEADING_STRING_REPLACEMENT % [heading_font_size] |
| 63 | + regex.compile(match_string) |
| 64 | + markdown_text = regex.sub(markdown_text, replace_string, true) |
| 65 | + return markdown_text |
| 66 | + |
| 67 | +func from_release_notes(markdown_text : String) -> void: |
| 68 | + markdown_text = regex_replace_imgs(markdown_text) |
| 69 | + markdown_text = regex_replace_urls(markdown_text) |
| 70 | + markdown_text = regex_replace_bolds(markdown_text) |
| 71 | + markdown_text = regex_replace_titles(markdown_text) |
| 72 | + text = markdown_text |
| 73 | + |
| 74 | +func _on_meta_clicked(meta: String) -> void: |
| 75 | + if meta.begins_with("https://"): |
| 76 | + var _err = OS.shell_open(meta) |
| 77 | + |
| 78 | +func _ready() -> void: |
| 79 | + meta_clicked.connect(_on_meta_clicked) |
0 commit comments