Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 25 - Help/Wiki Viewer #65

Open
wants to merge 35 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
cb1bf8d
added help button to navbar
ponbac Oct 26, 2021
0ca6d5c
added empty container for the help info
ponbac Oct 26, 2021
80cbd69
began working on fetching wiki
ponbac Oct 27, 2021
d58cb0b
now fetches wiki pages into /media/wiki
ponbac Oct 27, 2021
9987258
now displays a list of all wiki pages in help viewer
ponbac Oct 27, 2021
36351c3
Added animation to the HelpViewer window, Added text window with the …
Oct 30, 2021
893eb42
Resolved help menu and margins
Nov 1, 2021
4ec61bd
Page titles are now showing without the suffix and '-'
Nov 4, 2021
a209333
Home page is now showed first in the menu
Nov 4, 2021
b062887
Removed extra code
Nov 4, 2021
5be2e23
Now fetches with HTTPRequest instead of curl
Pontus53 Nov 8, 2021
71d96b3
RichTextLabel now allows BBCode
Pontus53 Nov 9, 2021
0b94a04
Added BBCode custom font files, Added bold and italics BBCode to Rich…
Nov 13, 2021
339e08b
Removed some extra code
Nov 13, 2021
901d2ef
Refactoring of the download_texture() and its usage
Nov 18, 2021
0faecfe
Changed the width of the images, todo: underlining urls
Nov 18, 2021
903af2d
Changed res:// to user:// directory
Nov 29, 2021
3a9572a
Changed res to user directory entry.gd
Nov 29, 2021
8ce4566
now properly fetches images to //user
Pontus53 Nov 29, 2021
e8af5f4
removed test file
Pontus53 Nov 29, 2021
80c5d5e
Revert "Changed res to user directory entry.gd"
Schajbi Nov 29, 2021
04b5dbb
Resolved issue with loading images, added code snippet and hyperlink …
Schajbi Dec 2, 2021
41861e3
Changed the size of the contents window
Schajbi Dec 4, 2021
bf71949
Refactoring: added _markdown_line() function
Schajbi Dec 4, 2021
800db6d
now uses mono font for code snippets
ponbac Dec 5, 2021
6cbd61c
Changed subtitles to bold italics, added notes italics
Schajbi Dec 5, 2021
1c2ee5a
Added clickable URLs
Schajbi Dec 5, 2021
1422566
Added dynamic fetch of github wiki pages
Schajbi Dec 5, 2021
e7912d6
Now waits for wiki.html http request to finish
Schajbi Dec 6, 2021
568b0f7
small changes to note formatting
ponbac Dec 6, 2021
c7c1e1a
Changed _markdown_to_bbcode() to handle file instead of line
Schajbi Dec 11, 2021
d0df102
_get_wiki_from_storage() now uses Util instead of Directory
Schajbi Dec 11, 2021
cfa7376
Changed how we handle hyperlinks
Schajbi Dec 11, 2021
8067014
Added queue_free() for http nodes, yield in _download_image()
Schajbi Dec 12, 2021
416a962
Relocated downloading wiki files from Entry.gd to HelpViewer.gd
Schajbi Dec 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions project/Entry.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ onready var _button: Button = $Button

var error: String = ""

const USER_DIR = "user://"


func _ready():
var custom_dir = OS.get_environment("SMCEGD_USER_DIR")
if custom_dir != "":
print("Custom user directory set")
if !Global.set_user_dir(custom_dir):
return _error("Failed to setup custom user directory")

_button.connect("pressed", self, "_on_clipboard_copy")
print("Reading version file..")
var file = File.new()
Expand All @@ -50,42 +52,43 @@ func _ready():
print("Mode: %s" % "Debug" if OS.is_debug_build() else "Release")
print("User dir: %s" % Global.user_dir)
print()

var dir = Directory.new()

if dir.open("res://share/RtResources") != OK:
return _error("Internal RtResources not found!")
if ! Util.copy_dir("res://share/RtResources", Global.usr_dir_plus("RtResources")):

if !Util.copy_dir("res://share/RtResources", Global.usr_dir_plus("RtResources")):
return _error("Failed to copy in RtResources")
if ! Util.copy_dir("res://share/library_patches", Global.usr_dir_plus("library_patches")):

if !Util.copy_dir("res://share/library_patches", Global.usr_dir_plus("library_patches")):
return _error("Failed to copy in library_patches")

Util.mkdir(Global.usr_dir_plus("mods"))
Util.mkdir(Global.usr_dir_plus("config/profiles"), true)

print("Copied RtResources")

var bar = Toolchain.new()
if ! is_instance_valid(bar):
if !is_instance_valid(bar):
return _error("Shared library not loaded")

var res = bar.init(Global.user_dir)
if ! res.ok():
if !res.ok():
return _error("Unsuitable environment: %s" % res.error())
print(bar.resource_dir())
bar.free()

Global.scan_named_classes("res://src")

# somehow destroys res://
ModManager.load_mods()

_continue()


func _continue():
if ! main_scene:
if !main_scene:
return _error("No Main Scene")
get_tree().change_scene_to(main_scene)

Expand Down
Binary file added project/media/fonts/Open_Sans/OpenSans-Bold.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
249 changes: 249 additions & 0 deletions project/src/ui/help_viewer/HelpViewer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
#
# HelpViewer.gd
# Copyright 2021 ItJustWorksTM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

extends Control

signal exited

onready var close_btn = $LogPopout/Panel/Control/VBoxContainer/MarginContainer/CloseButton
onready var center_label = $LogPopout/Panel/Control/EmptyLabel
onready var item_list = $LogPopout/Panel/Control/VBoxContainer/HBoxContainer/ItemList
onready var rich_text_label = $LogPopout/Panel/Control/VBoxContainer/HBoxContainer/RichTextLabel

const USER_DIR = "user://"
var wiki_pages = []
var wiki_content = []


class WikiPage:
var title: String
var content: String


func _ready():
var wiki_titles = yield(_fetch_wiki_titles(), "completed")
yield(_download_wiki_pages(wiki_titles), "completed")

close_btn.connect("pressed", self, "_close")
wiki_pages = _get_wiki_from_storage(USER_DIR)

if wiki_pages.size() > 0:
center_label.set_text("") # TODO: Should remove the entire node?
for page in wiki_pages:
item_list.add_item(page.title)

# Add on-click event
item_list.connect("item_selected", self, "_on_help_selected")
item_list.connect("nothing_selected", self, "_on_help_selected", [-1])

rich_text_label.connect("meta_clicked", self, "_richtextlabel_on_meta_clicked")

# Fetch smce-gd GitHub wiki html, return all the wiki page names to downloaded
# TODO: Temporary solution (GitHub can change the html tags, breaking this)
func _fetch_wiki_titles():
var wiki_file_name = "wiki.html"

# Download the html
var http_node = HTTPRequest.new()
http_node.set_use_threads(true)
add_child(http_node)
var output_name = USER_DIR + wiki_file_name
http_node.set_download_file(output_name)
var download_link = "https://github.com/ItJustWorksTM/smce-gd/wiki.html"
var error = http_node.request(download_link)
yield(http_node, "request_completed")
http_node.queue_free()
if error != OK:
push_error("An error occurred in the HTTP request.")


# Get the wiki pages names
var file = File.new()
var wiki_pages = []
file.open(USER_DIR + wiki_file_name, File.READ)
while not file.eof_reached():
var line = file.get_line()
if '<a class="flex-1 py-1 text-bold"' in line:
line = line.split(">")[1].split("<")[0]
line = line.replacen(" ", "-")
wiki_pages.append(line)
return wiki_pages

# Fetch smce-gd GitHub wiki into user directory
func _download_wiki_pages(wiki_pages_to_download) -> void:
var base_url = "https://raw.githubusercontent.com/wiki/ItJustWorksTM/smce-gd/"
for page in wiki_pages_to_download:
var http_node = HTTPRequest.new()
http_node.set_use_threads(true)
add_child(http_node)
var output_name = USER_DIR + page + ".md"
http_node.set_download_file(output_name)
var download_link = base_url + page + ".md"
var error = http_node.request(download_link)
yield(http_node, "request_completed")
http_node.queue_free()
if error != OK:
push_error("An error occurred in the HTTP request.")


func _get_wiki_from_storage(path: String) -> Array:
var pages = []
var array = Util.ls(path)
for item in array:
if item != "" && item.ends_with(".md"):
var page = WikiPage.new()
page.title = item.trim_suffix(".md").replace("-", " ")
page.content = _read_wiki_file(item)
if page.title == "Home":
pages.push_front(page)
else:
pages.append(page)
return pages


# Read line by line from the wiki file, format the text using BBCode, return the formatted content
func _read_wiki_file(file_name: String):
var file = File.new()

file.open(USER_DIR + file_name, File.READ)
var content = _markdown_to_bbcode(file)
file.close()

return content


func _markdown_to_bbcode(file: File):
var content : String
var code_snippet = false
while not file.eof_reached():
var line = file.get_line()
# Detects start of code snippet
if line.begins_with("```") && code_snippet == false:
code_snippet = true
line = "[code][color=aqua]" + line.replace("```", "") + "[/color][/code]"
# Content part of code snippet
elif !line.begins_with("```") && code_snippet == true:
line = "[code][color=aqua]" + line + "[/color][/code]"
# End of code snippet
elif line.begins_with("```") && code_snippet == true:
line = line.replace("```", "")
line = "[code][color=aqua]" + line + "[/color][/code]"
code_snippet = false

# Heading
elif line.begins_with("## "):
line = line.replacen("## ", "")
line = "[b]" + line + "[/b]"
elif line.begins_with("### "):
line = line.replacen("### ", "")
line = "[b][i]" + line + "[/i][/b]"

# Note
if "**note:**" in line:
line = line.replacen("_**note:**", "note:")
line = line.trim_suffix("_")
line = "[i]" + line + "[/i]"

# Image
var img_width = 464 # depends on the window width
if line.begins_with("![](https://i.imgur.com/"):
line = line.replacen("![](", "").replacen(")", "")
var image_file_name = line.split("/")[-1]
_download_image(line, USER_DIR + image_file_name)
line = "[img=<" + str(img_width) + ">]" + USER_DIR + image_file_name + "[/img]"

# Link
var hyperlink_position_start = 0
var hyperlink_position_end_space = null
var hyperlink_position_end_bracket = null
var hyperlink_position_end = null
if "https://" in line:
hyperlink_position_start = line.find("https://")
hyperlink_position_end_space = line.findn(" ", hyperlink_position_start)
hyperlink_position_end_bracket = line.findn(")", hyperlink_position_start)
if hyperlink_position_end_space == -1 && hyperlink_position_end_bracket == -1:
hyperlink_position_end = line.length()
elif hyperlink_position_end_space == -1 || hyperlink_position_end_bracket == -1:
hyperlink_position_end = max(hyperlink_position_end_space, hyperlink_position_end_bracket)
else:
hyperlink_position_end = min(hyperlink_position_end_space, hyperlink_position_end_bracket)
line = line.insert(hyperlink_position_start, "[url]")
line = line.insert(hyperlink_position_end + 5, "[/url]") # for some reason doesn't end at the actual end, need + 5
line += "\n"
content = content + line
return content



func _richtextlabel_on_meta_clicked(meta):
OS.shell_open(str(meta))


func _gui_input(event: InputEvent):
if event.is_action_pressed("mouse_left"):
_close()


# Added enter_tree() to initialize the screen
func _enter_tree() -> void:
_open()


func _close() -> void:
emit_signal("exited")
var tween = TempTween.new()

add_child(tween)
tween.interpolate_property(self, "rect_scale:y", 1, 0, 0.3, Tween.TRANS_CUBIC)
tween.interpolate_property(self, "modulate:a", 1, 0, 0.15)
tween.start()

yield(tween, "tween_all_completed")
queue_free()


func _open() -> void:
var tween = TempTween.new()
add_child(tween)
tween.interpolate_property(self, "rect_scale:y", 0, 1, 0.15, Tween.TRANS_CUBIC)
tween.interpolate_property(self, "modulate:a", 0, 1, 0.15)
tween.start()


# Find the selected help and update the text window
func _select_help() -> void:
for n in item_list.get_item_count():
if item_list.is_selected(n) == true:
rich_text_label.clear()
rich_text_label.append_bbcode(wiki_pages[n].content)


func _download_image(url: String, file_name: String):
RuthgerD marked this conversation as resolved.
Show resolved Hide resolved
var http_node = HTTPRequest.new()
http_node.set_use_threads(true)
add_child(http_node)
RuthgerD marked this conversation as resolved.
Show resolved Hide resolved
http_node.set_download_file(file_name)
var error = http_node.request(url)
yield(http_node, "request_completed")
http_node.queue_free()
if error != OK:
push_error("An error occurred in the HTTP request.")


# Catch the on-click event
func _on_help_selected(_index: int) -> void:
_select_help()
Loading