From 56d2ccbb26be8a5e074ba72930c5cc0e07fa6005 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 2 Jul 2022 13:22:10 +0200 Subject: [PATCH] Cleaned up StaticPages, and added them to the build. --- modules/web/SCsub | 4 + modules/web/config.py | 4 + .../web/nodes/static_pages/static_page.cpp | 117 +++++++++++------- modules/web/nodes/static_pages/static_page.h | 41 +++--- .../nodes/static_pages/static_page_file.cpp | 42 +++++-- .../web/nodes/static_pages/static_page_file.h | 24 ++-- .../static_pages/static_page_folder_files.cpp | 86 +++++++++---- .../static_pages/static_page_folder_files.h | 24 ++-- modules/web/register_types.cpp | 8 ++ 9 files changed, 235 insertions(+), 115 deletions(-) diff --git a/modules/web/SCsub b/modules/web/SCsub index 683906bb6..0d9c0d891 100644 --- a/modules/web/SCsub +++ b/modules/web/SCsub @@ -31,6 +31,10 @@ sources = [ "http_server_simple/http_parser.cpp", "http_server_simple/http_writer.cpp", "http_server_simple/http_parser/http_parser.c", + + "nodes/static_pages/static_page.cpp", + "nodes/static_pages/static_page_file.cpp", + "nodes/static_pages/static_page_folder_files.cpp", ] if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes': diff --git a/modules/web/config.py b/modules/web/config.py index b949f14ad..7abe20c95 100644 --- a/modules/web/config.py +++ b/modules/web/config.py @@ -34,6 +34,10 @@ def get_doc_classes(): "HTMLTag", "WebServerSimple", + + "StaticPage", + "StaticPageFile", + "StaticPageFolderFiles", ] diff --git a/modules/web/nodes/static_pages/static_page.cpp b/modules/web/nodes/static_pages/static_page.cpp index 6600c5409..184dcfdc3 100644 --- a/modules/web/nodes/static_pages/static_page.cpp +++ b/modules/web/nodes/static_pages/static_page.cpp @@ -1,73 +1,94 @@ #include "static_page.h" -#include "core/os/directory.h" -#include "web/file_cache.h" -#include "web/html/html_builder.h" -#include "web/html/utils.h" -#include "web/http/request.h" +#include "../../http/web_server_request.h" +#include "core/os/file_access.h" -void StaticPage::_handle_request_main(Request *request) { - if (should_render_menu) { - render_menu(request); +String StaticPage::get_data() { + return _data; +} +void StaticPage::set_data(const String &val) { + _data = val; +} + +String StaticPage::get_preview_data() { + return _preview_data; +} +void StaticPage::set_preview_data(const String &val) { + _preview_data = val; +} + +bool StaticPage::get_should_render_menu() { + return _should_render_menu; +} +void StaticPage::set_should_render_menu(const bool &val) { + _should_render_menu = val; +} + +void StaticPage::_handle_request(Ref request) { + if (_should_render_menu) { + render_main_menu(request); } render_index(request); request->compile_and_send_body(); } -void StaticPage::render_index(Request *request) { - request->body += data; +void StaticPage::_render_index(Ref request) { + request->body += _data; } -void StaticPage::render_preview(Request *request) { - request->body += preview_data; +void StaticPage::_render_preview(Ref request) { + request->body += _preview_data; } void StaticPage::load_file(const String &path) { - Ref d; - d.instance(); + FileAccess *f = FileAccess::open(path, FileAccess::READ); - d->read_file_into(path, &data); -} - -void StaticPage::load_and_process_file(const String &path) { - Ref d; - d.instance(); - - d->read_file_into(path, &data); - - if (path.file_get_extension() == "md") { - Utils::markdown_to_html(&data); + if (f) { + _data = f->get_as_utf8_string(); + f->close(); + memdelete(f); + } else { + _data = ""; } } -void StaticPage::load_md_file(const String &path) { - Ref d; - d.instance(); +void StaticPage::load_and_process_file(const String &path) { + FileAccess *f = FileAccess::open(path, FileAccess::READ); - d->read_file_into(path, &data); - Utils::markdown_to_html(&data); + if (f) { + _data = f->get_as_utf8_string(); + f->close(); + memdelete(f); + } else { + _data = ""; + } + + //todo prcess + //could support bbcode easily + //should probably support md -> I should probably write a parser } -void StaticPage::set_data_md(const String &d) { - data.clear(); - - Utils::markdown_to_html(&data); -} - -void StaticPage::set_data(const String &d) { - data = d; -} - -void StaticPage::set_preview_data(const String &d) { - preview_data = d; -} - -StaticPage::StaticPage() : - WebNode() { - - should_render_menu = true; +StaticPage::StaticPage() { + _should_render_menu = true; } StaticPage::~StaticPage() { } + +void StaticPage::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_data"), &StaticPage::get_data); + ClassDB::bind_method(D_METHOD("set_data", "val"), &StaticPage::set_data); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "data", PROPERTY_HINT_MULTILINE_TEXT), "set_data", "get_data"); + + ClassDB::bind_method(D_METHOD("get_preview_data"), &StaticPage::get_preview_data); + ClassDB::bind_method(D_METHOD("set_preview_data", "val"), &StaticPage::set_preview_data); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "preview_data", PROPERTY_HINT_MULTILINE_TEXT), "set_preview_data", "get_preview_data"); + + ClassDB::bind_method(D_METHOD("get_should_render_menu"), &StaticPage::get_should_render_menu); + ClassDB::bind_method(D_METHOD("set_should_render_menu", "val"), &StaticPage::set_should_render_menu); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "should_render_menu"), "set_should_render_menu", "get_should_render_menu"); + + ClassDB::bind_method(D_METHOD("load_file", "path"), &StaticPage::load_file); + ClassDB::bind_method(D_METHOD("load_and_process_file", "path"), &StaticPage::load_and_process_file); +} diff --git a/modules/web/nodes/static_pages/static_page.h b/modules/web/nodes/static_pages/static_page.h index 3783a70e3..bd4af7976 100644 --- a/modules/web/nodes/static_pages/static_page.h +++ b/modules/web/nodes/static_pages/static_page.h @@ -1,33 +1,42 @@ #ifndef STATIC_PAGE_H #define STATIC_PAGE_H -#include "core/string.h" +#include "core/ustring.h" -#include "web/http/web_node.h" +#include "../../http/web_node.h" + +class WebServerRequest; class StaticPage : public WebNode { - RCPP_OBJECT(StaticPage, WebNode); + GDCLASS(StaticPage, WebNode); public: - void _handle_request_main(Request *request); + String get_data(); + void set_data(const String &val); - void render_index(Request *request); - void render_preview(Request *request); + String get_preview_data(); + void set_preview_data(const String &val); + + bool get_should_render_menu(); + void set_should_render_menu(const bool &val); + + void _handle_request(Ref request); + + void _render_index(Ref request); + void _render_preview(Ref request); void load_file(const String &path); void load_and_process_file(const String &path); - void load_md_file(const String &path); - - void set_data_md(const String &d); - void set_data(const String &d); - void set_preview_data(const String &d); - - String data; - String preview_data; - bool should_render_menu; StaticPage(); ~StaticPage(); + +protected: + static void _bind_methods(); + + String _data; + String _preview_data; + bool _should_render_menu; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/nodes/static_pages/static_page_file.cpp b/modules/web/nodes/static_pages/static_page_file.cpp index 63fc4b598..52fbde6e8 100644 --- a/modules/web/nodes/static_pages/static_page_file.cpp +++ b/modules/web/nodes/static_pages/static_page_file.cpp @@ -1,18 +1,28 @@ #include "static_page_file.h" -#include "web/file_cache.h" -#include "web/html/html_builder.h" -#include "web/http/request.h" +String StaticPageFile::get_file_path() { + return _file_path; +} +void StaticPageFile::set_file_path(const String &val) { + _file_path = val; +} + +bool StaticPageFile::get_process_if_can() { + return _process_if_can; +} +void StaticPageFile::set_process_if_can(const bool &val) { + _process_if_can = val; +} void StaticPageFile::load() { - if (file_path == "") { + if (_file_path == "") { return; } - if (process_if_can) { - load_and_process_file(file_path); + if (_process_if_can) { + load_and_process_file(_file_path); } else { - load_file(file_path); + load_file(_file_path); } } @@ -26,11 +36,21 @@ void StaticPageFile::_notification(const int what) { } } -StaticPageFile::StaticPageFile() : - StaticPage() { - - process_if_can = true; +StaticPageFile::StaticPageFile() { + _process_if_can = true; } StaticPageFile::~StaticPageFile() { } + +void StaticPageFile::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_file_path"), &StaticPageFile::get_file_path); + ClassDB::bind_method(D_METHOD("set_file_path", "val"), &StaticPageFile::set_file_path); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "file_path"), "set_file_path", "get_file_path"); + + ClassDB::bind_method(D_METHOD("get_process_if_can"), &StaticPageFile::get_process_if_can); + ClassDB::bind_method(D_METHOD("set_process_if_can", "val"), &StaticPageFile::set_process_if_can); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "process_if_can"), "set_process_if_can", "get_process_if_can"); + + ClassDB::bind_method(D_METHOD("load"), &StaticPageFile::load); +} diff --git a/modules/web/nodes/static_pages/static_page_file.h b/modules/web/nodes/static_pages/static_page_file.h index 18e5473e1..48e9b02a6 100644 --- a/modules/web/nodes/static_pages/static_page_file.h +++ b/modules/web/nodes/static_pages/static_page_file.h @@ -1,23 +1,31 @@ #ifndef STATIC_PAGE_FILE_H #define STATIC_PAGE_FILE_H -#include "core/string.h" +#include "core/ustring.h" #include "static_page.h" class StaticPageFile : public StaticPage { - RCPP_OBJECT(StaticPageFile, StaticPage); + GDCLASS(StaticPageFile, StaticPage); public: + String get_file_path(); + void set_file_path(const String &val); + + bool get_process_if_can(); + void set_process_if_can(const bool &val); + void load(); - void _notification(const int what); - - String file_path; - bool process_if_can; - StaticPageFile(); ~StaticPageFile(); + +protected: + void _notification(const int what); + static void _bind_methods(); + + String _file_path; + bool _process_if_can; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/nodes/static_pages/static_page_folder_files.cpp b/modules/web/nodes/static_pages/static_page_folder_files.cpp index 40a421432..591e8608e 100644 --- a/modules/web/nodes/static_pages/static_page_folder_files.cpp +++ b/modules/web/nodes/static_pages/static_page_folder_files.cpp @@ -1,40 +1,67 @@ #include "static_page_folder_files.h" -#include "core/os/directory.h" -#include "web/file_cache.h" -#include "web/html/html_builder.h" -#include "web/html/utils.h" -#include "web/http/request.h" +#include "core/os/dir_access.h" +#include "core/os/file_access.h" + +String StaticPageFolderFiles::get_dir_path() { + return _dir_path; +} +void StaticPageFolderFiles::set_dir_path(const String &val) { + _dir_path = val; +} + +bool StaticPageFolderFiles::get_process_if_can() { + return _process_if_can; +} +void StaticPageFolderFiles::set_process_if_can(const bool &val) { + _process_if_can = val; +} void StaticPageFolderFiles::load() { - if (dir_path == "") { + if (_dir_path == "") { return; } - Ref d; - d.instance(); + DirAccessRef da = DirAccess::open(_dir_path); - ERR_FAIL_COND_MSG(!d->open_dir(dir_path), "Dir Path = " + dir_path); + ERR_FAIL_COND_MSG(!da, "Dir Path = " + _dir_path); - String str; - while (d->has_next()) { - if (d->current_is_file()) { - String fn = dir_path; - fn.append_path(d->current_get_name_cstr()); + if (da) { + da->list_dir_begin(); - d->read_file_into(fn, &str); - - if (process_if_can && d->current_get_extension() == "md") { - Utils::markdown_to_html(&str); + while (true) { + String file = da->get_next(); + if (file == "") { + break; } - append_data(str); + if (!da->current_is_dir() && !da->current_is_hidden()) { + String fn = _dir_path; + fn = fn.append_path(file); + + FileAccess *f = FileAccess::open(fn, FileAccess::READ); + + if (f) { + String str; + str = f->get_as_utf8_string(); + f->close(); + memdelete(f); + + //if (process_if_can && d->current_get_extension() == "md") { + // Utils::markdown_to_html(&str); + //} + + append_data(str); + } + } } + + da->list_dir_end(); } } void StaticPageFolderFiles::append_data(const String &d) { - data += d; + _data += d; } void StaticPageFolderFiles::_notification(const int what) { @@ -47,11 +74,22 @@ void StaticPageFolderFiles::_notification(const int what) { } } -StaticPageFolderFiles::StaticPageFolderFiles() : - StaticPage() { - - process_if_can = true; +StaticPageFolderFiles::StaticPageFolderFiles() { + _process_if_can = true; } StaticPageFolderFiles::~StaticPageFolderFiles() { } + +void StaticPageFolderFiles::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_dir_path"), &StaticPageFolderFiles::get_dir_path); + ClassDB::bind_method(D_METHOD("set_dir_path", "val"), &StaticPageFolderFiles::set_dir_path); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "dir_path"), "set_dir_path", "get_dir_path"); + + ClassDB::bind_method(D_METHOD("get_process_if_can"), &StaticPageFolderFiles::get_process_if_can); + ClassDB::bind_method(D_METHOD("set_process_if_can", "val"), &StaticPageFolderFiles::set_process_if_can); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "process_if_can"), "set_process_if_can", "get_process_if_can"); + + ClassDB::bind_method(D_METHOD("load"), &StaticPageFolderFiles::load); + ClassDB::bind_method(D_METHOD("append_data", "val"), &StaticPageFolderFiles::append_data); +} diff --git a/modules/web/nodes/static_pages/static_page_folder_files.h b/modules/web/nodes/static_pages/static_page_folder_files.h index 21f6b8ded..2e95494a7 100644 --- a/modules/web/nodes/static_pages/static_page_folder_files.h +++ b/modules/web/nodes/static_pages/static_page_folder_files.h @@ -1,24 +1,32 @@ #ifndef STATIC_PAGE_FOLDER_FILES_H #define STATIC_PAGE_FOLDER_FILES_H -#include "core/string.h" +#include "core/ustring.h" #include "static_page.h" class StaticPageFolderFiles : public StaticPage { - RCPP_OBJECT(StaticPageFolderFiles, StaticPage); + GDCLASS(StaticPageFolderFiles, StaticPage); public: + String get_dir_path(); + void set_dir_path(const String &val); + + bool get_process_if_can(); + void set_process_if_can(const bool &val); + void load(); virtual void append_data(const String &d); - void _notification(const int what); - - String dir_path; - bool process_if_can; - StaticPageFolderFiles(); ~StaticPageFolderFiles(); + +protected: + void _notification(const int what); + static void _bind_methods(); + + String _dir_path; + bool _process_if_can; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/register_types.cpp b/modules/web/register_types.cpp index e08511a41..b84bdc37a 100644 --- a/modules/web/register_types.cpp +++ b/modules/web/register_types.cpp @@ -42,6 +42,10 @@ SOFTWARE. #include "http_server_simple/web_server_simple.h" +#include "nodes/static_pages/static_page.h" +#include "nodes/static_pages/static_page_file.h" +#include "nodes/static_pages/static_page_folder_files.h" + void register_web_types() { ClassDB::register_class<_HTMLBuilder>(); ClassDB::register_class<_HTMLTag>(); @@ -63,6 +67,10 @@ void register_web_types() { ClassDB::register_class(); ClassDB::register_class(); + + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); } void unregister_web_types() {