From 2eb333b63d32f35e1ea583785893565da987cafb Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 3 Jul 2022 16:28:57 +0200 Subject: [PATCH] Cleaned up the remaining 2 PagedArticle classes. Only PagedArticles is in the build, the other one will need more changes. --- modules/web/SCsub | 2 + modules/web/config.py | 2 + .../nodes/paged_article/paged_articles.cpp | 109 ++++++++++++------ .../web/nodes/paged_article/paged_articles.h | 36 +++--- .../paged_article/paged_articles_md_index.cpp | 48 +++++--- .../paged_article/paged_articles_md_index.h | 28 +++-- modules/web/register_types.cpp | 4 + 7 files changed, 148 insertions(+), 81 deletions(-) diff --git a/modules/web/SCsub b/modules/web/SCsub index 6bc208e36..f932bd842 100644 --- a/modules/web/SCsub +++ b/modules/web/SCsub @@ -43,6 +43,8 @@ sources = [ "nodes/list_page/list_page.cpp", "nodes/paged_article/paged_article.cpp", + "nodes/paged_article/paged_articles.cpp", + #"nodes/paged_article/paged_articles_md_index.cpp", ] if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes': diff --git a/modules/web/config.py b/modules/web/config.py index b9c06d13e..9dd5ecbd9 100644 --- a/modules/web/config.py +++ b/modules/web/config.py @@ -46,6 +46,8 @@ def get_doc_classes(): "ListPage", "PagedArticle", + "PagedArticles", + "PagedArticlesMDIndex", ] diff --git a/modules/web/nodes/paged_article/paged_articles.cpp b/modules/web/nodes/paged_article/paged_articles.cpp index d0648e04d..bcc6097f5 100644 --- a/modules/web/nodes/paged_article/paged_articles.cpp +++ b/modules/web/nodes/paged_article/paged_articles.cpp @@ -1,56 +1,86 @@ #include "paged_articles.h" -#include "web/html/html_builder.h" +#include "core/os/dir_access.h" +#include "core/os/os.h" +#include "core/project_settings.h" + +#include "../../html/html_builder.h" +#include "../../http/web_server_request.h" -#include "core/os/directory.h" #include "paged_article.h" -#include "web/html/utils.h" -#include +String PagedArticles::get_folder() { + return _folder; +} +void PagedArticles::set_folder(const String &val) { + _folder = val; +} -void PagedArticles::_handle_request_main(Request *request) { +void PagedArticles::_handle_request(Ref request) { render_menu(request); - render_index(request); request->compile_and_send_body(); } -void PagedArticles::render_index(Request *request) { +void PagedArticles::_render_index(Ref request) { // summary page - request->body += index_page; + request->body += _index_page; } -void PagedArticles::render_preview(Request *request) { +void PagedArticles::_render_preview(Ref request) { } void PagedArticles::load() { - ERR_FAIL_COND_MSG(folder == "", "Error: PagedArticles::load called, but a folder is not set!"); + ERR_FAIL_COND_MSG(_folder == "", "Error: PagedArticles::load called, but a folder is not set!"); - if (folder.size() > 0 && folder[folder.size() - 1] == '/') { - folder.pop_back(); - } + String folder = _folder; - Ref dir; - dir.instance(); - - ERR_FAIL_COND_MSG(dir->open_dir(folder) != OK, "Error opening PagedArticles::folder! folder: " + folder); - - while (dir->next()) { - if (dir->current_is_dir()) { - String np = dir->current_get_path(); - String fn = dir->current_get_name(); - - String ff = folder; - ff.append_path(fn); - - PagedArticle *p = new PagedArticle(); - String seg = dir->current_get_name(); - p->articles_folder = ff; - p->set_uri_segment(seg); - add_child(p); + if (folder.begins_with("res://")) { + if (ProjectSettings::get_singleton()) { + String resource_path = ProjectSettings::get_singleton()->get_resource_path(); + if (resource_path != "") { + folder = folder.replace_first("res:/", resource_path); + } else { + folder = folder.replace_first("res://", ""); + } + } + } else if (folder.begins_with("user://")) { + String data_dir = OS::get_singleton()->get_user_data_dir(); + if (data_dir != "") { + folder = folder.replace_first("user:/", data_dir); + } else { + folder = folder.replace_first("user://", ""); } } + folder = DirAccess::get_full_path(folder, DirAccess::ACCESS_FILESYSTEM); + folder = folder.path_clean_end_slash(); + + DirAccess *dir = DirAccess::open(folder); + + ERR_FAIL_COND_MSG(!dir, "Error opening PagedArticles::folder! folder: " + folder); + + dir->list_dir_begin(); + + String file_name = dir->get_next(); + while (file_name != "") { + if (dir->current_is_dir() && file_name != "." && file_name != "..") { + String ff = folder + "/" + file_name; + + PagedArticle *p = new PagedArticle(); + + p->set_articles_folder(ff); + p->set_uri_segment(file_name); + add_child(p); + } + + file_name = dir->get_next(); + } + + dir->list_dir_end(); + + memdelete(dir); + generate_index_page(); } @@ -71,10 +101,10 @@ void PagedArticles::generate_index_page() { b.cdiv(); - index_page = b.result; + _index_page = b.result; } -void PagedArticles::_notification(const int what) { +void PagedArticles::_notification(int what) { switch (what) { case NOTIFICATION_ENTER_TREE: load(); @@ -82,13 +112,16 @@ void PagedArticles::_notification(const int what) { default: break; } - - WebNode::_notification(what); } -PagedArticles::PagedArticles() : - WebNode() { +PagedArticles::PagedArticles() { } PagedArticles::~PagedArticles() { -} \ No newline at end of file +} + +void PagedArticles::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_folder"), &PagedArticles::get_folder); + ClassDB::bind_method(D_METHOD("set_folder", "val"), &PagedArticles::set_folder); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "folder"), "set_folder", "get_folder"); +} diff --git a/modules/web/nodes/paged_article/paged_articles.h b/modules/web/nodes/paged_article/paged_articles.h index 32d41c8a3..d95617c4c 100644 --- a/modules/web/nodes/paged_article/paged_articles.h +++ b/modules/web/nodes/paged_article/paged_articles.h @@ -1,13 +1,10 @@ #ifndef PAGED_ARTICLES_H #define PAGED_ARTICLES_H -#include "core/containers/vector.h" -#include "core/string.h" +#include "core/ustring.h" +#include "core/vector.h" -#include "web/file_cache.h" -#include "web/http/web_node.h" - -#include "web/http/request.h" +#include "../../http/web_node.h" // This class will load and process all md files from the folder set to it's folder property, // and generate one page from them. TThe files are processed in alphabetical order. @@ -24,27 +21,34 @@ // ... // +class WebServerRequest; + class PagedArticles : public WebNode { - RCPP_OBJECT(PagedArticles, WebNode); + GDCLASS(PagedArticles, WebNode); public: - void _handle_request_main(Request *request); + String get_folder(); + void set_folder(const String &val); - void render_index(Request *request); - void render_preview(Request *request); + void _handle_request(Ref request); + + void _render_index(Ref request); + void _render_preview(Ref request); void load(); void generate_index_page(); - void _notification(const int what); - PagedArticles(); ~PagedArticles(); - String folder; - protected: - String index_page; + void _notification(int what); + + static void _bind_methods(); + + String _folder; + + String _index_page; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/nodes/paged_article/paged_articles_md_index.cpp b/modules/web/nodes/paged_article/paged_articles_md_index.cpp index 65c564a7a..19ec2d022 100644 --- a/modules/web/nodes/paged_article/paged_articles_md_index.cpp +++ b/modules/web/nodes/paged_article/paged_articles_md_index.cpp @@ -1,10 +1,22 @@ #include "paged_articles_md_index.h" -#include "web/html/html_builder.h" -#include "web/http/request.h" -#include "web/http/web_permission.h" +#include "core/os/dir_access.h" -void PagedArticlesMDIndex::handle_request_main(Request *request) { +#include "../../html/html_builder.h" +#include "../../http/web_permission.h" +#include "../../http/web_server_request.h" + +#include "../list_page/list_page.h" +#include "paged_article.h" + +String PagedArticlesMDIndex::get_folder() { + return folder; +} +void PagedArticlesMDIndex::set_folder(const String &val) { + folder = val; +} + +void PagedArticlesMDIndex::_handle_request_main(Ref request) { if (_web_permission.is_valid()) { if (_web_permission->activate(request)) { return; @@ -21,31 +33,35 @@ void PagedArticlesMDIndex::handle_request_main(Request *request) { articles->handle_request_main(request); } -void PagedArticlesMDIndex::render_index(Request *request) { +void PagedArticlesMDIndex::_render_index(Ref request) { main_page->render_index(request); } -void PagedArticlesMDIndex::render_preview(Request *request) { +void PagedArticlesMDIndex::_render_preview(Ref request) { main_page->render_preview(request); } void PagedArticlesMDIndex::load() { - main_page->folder = folder; + main_page->set_folder(folder); main_page->load(); - articles->articles_folder = folder; - articles->serve_folder = folder + "/files"; + articles->set_articles_folder(folder); + //articles->set_serve_folder(folder + "/files"); articles->load(); } -PagedArticlesMDIndex::PagedArticlesMDIndex() : - WebNode() { - - main_page = new ListPage(); - articles = new PagedArticle(); +PagedArticlesMDIndex::PagedArticlesMDIndex() { + main_page = memnew(ListPage); + articles = memnew(PagedArticle); } PagedArticlesMDIndex::~PagedArticlesMDIndex() { - delete main_page; - delete articles; + memdelete(main_page); + memdelete(articles); +} + +void PagedArticlesMDIndex::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_folder"), &PagedArticlesMDIndex::get_folder); + ClassDB::bind_method(D_METHOD("set_folder", "val"), &PagedArticlesMDIndex::set_folder); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "folder"), "set_folder", "get_folder"); } diff --git a/modules/web/nodes/paged_article/paged_articles_md_index.h b/modules/web/nodes/paged_article/paged_articles_md_index.h index b95f50df1..b1d939a06 100644 --- a/modules/web/nodes/paged_article/paged_articles_md_index.h +++ b/modules/web/nodes/paged_article/paged_articles_md_index.h @@ -1,35 +1,41 @@ #ifndef PAGED_ARTICLES_MD_INDEX_H #define PAGED_ARTICLES_MD_INDEX_H -#include "core/string.h" +#include "core/ustring.h" -#include "web/http/web_node.h" - -#include "web_modules/list_page/list_page.h" -#include "web_modules/paged_article/paged_article.h" +#include "../../http/web_node.h" // Inherit from PagedArticles and override generate_index_page -> load and process md files in the set folder // SHould probably be called something else. PagedArticlesMDIndex ? +class WebServerRequest; +class ListPage; +class PagedArticle; + class PagedArticlesMDIndex : public WebNode { - RCPP_OBJECT(PagedArticlesMDIndex, WebNode); + GDCLASS(PagedArticlesMDIndex, WebNode); public: - void handle_request_main(Request *request); + String get_folder(); + void set_folder(const String &val); - void render_index(Request *request); - void render_preview(Request *request); + void _handle_request_main(Ref request); + + void _render_index(Ref request); + void _render_preview(Ref request); void load(); PagedArticlesMDIndex(); ~PagedArticlesMDIndex(); +protected: + static void _bind_methods(); + String folder; - String base_path; ListPage *main_page; PagedArticle *articles; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/register_types.cpp b/modules/web/register_types.cpp index 641274ed5..1db0c28a3 100644 --- a/modules/web/register_types.cpp +++ b/modules/web/register_types.cpp @@ -53,6 +53,8 @@ SOFTWARE. #include "nodes/list_page/list_page.h" #include "nodes/paged_article/paged_article.h" +#include "nodes/paged_article/paged_articles.h" +//#include "nodes/paged_article/paged_articles_md_index.h" void register_web_types() { ClassDB::register_class<_HTMLBuilder>(); @@ -88,6 +90,8 @@ void register_web_types() { ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + //ClassDB::register_class(); } void unregister_web_types() {