Cleaned up the remaining 2 PagedArticle classes. Only PagedArticles is in the build, the other one will need more changes.

This commit is contained in:
Relintai 2022-07-03 16:28:57 +02:00
parent eb2abf779d
commit 2eb333b63d
7 changed files with 148 additions and 81 deletions

View File

@ -43,6 +43,8 @@ sources = [
"nodes/list_page/list_page.cpp", "nodes/list_page/list_page.cpp",
"nodes/paged_article/paged_article.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': if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':

View File

@ -46,6 +46,8 @@ def get_doc_classes():
"ListPage", "ListPage",
"PagedArticle", "PagedArticle",
"PagedArticles",
"PagedArticlesMDIndex",
] ]

View File

@ -1,56 +1,86 @@
#include "paged_articles.h" #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 "paged_article.h"
#include "web/html/utils.h"
#include <iostream> 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<WebServerRequest> request) {
render_menu(request); render_menu(request);
render_index(request); render_index(request);
request->compile_and_send_body(); request->compile_and_send_body();
} }
void PagedArticles::render_index(Request *request) { void PagedArticles::_render_index(Ref<WebServerRequest> request) {
// summary page // summary page
request->body += index_page; request->body += _index_page;
} }
void PagedArticles::render_preview(Request *request) { void PagedArticles::_render_preview(Ref<WebServerRequest> request) {
} }
void PagedArticles::load() { 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] == '/') { String folder = _folder;
folder.pop_back();
}
Ref<Directory> dir; if (folder.begins_with("res://")) {
dir.instance(); if (ProjectSettings::get_singleton()) {
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
ERR_FAIL_COND_MSG(dir->open_dir(folder) != OK, "Error opening PagedArticles::folder! folder: " + folder); if (resource_path != "") {
folder = folder.replace_first("res:/", resource_path);
while (dir->next()) { } else {
if (dir->current_is_dir()) { folder = folder.replace_first("res://", "");
String np = dir->current_get_path(); }
String fn = dir->current_get_name(); }
} else if (folder.begins_with("user://")) {
String ff = folder; String data_dir = OS::get_singleton()->get_user_data_dir();
ff.append_path(fn); if (data_dir != "") {
folder = folder.replace_first("user:/", data_dir);
PagedArticle *p = new PagedArticle(); } else {
String seg = dir->current_get_name(); folder = folder.replace_first("user://", "");
p->articles_folder = ff;
p->set_uri_segment(seg);
add_child(p);
} }
} }
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(); generate_index_page();
} }
@ -71,10 +101,10 @@ void PagedArticles::generate_index_page() {
b.cdiv(); b.cdiv();
index_page = b.result; _index_page = b.result;
} }
void PagedArticles::_notification(const int what) { void PagedArticles::_notification(int what) {
switch (what) { switch (what) {
case NOTIFICATION_ENTER_TREE: case NOTIFICATION_ENTER_TREE:
load(); load();
@ -82,13 +112,16 @@ void PagedArticles::_notification(const int what) {
default: default:
break; break;
} }
WebNode::_notification(what);
} }
PagedArticles::PagedArticles() : PagedArticles::PagedArticles() {
WebNode() {
} }
PagedArticles::~PagedArticles() { PagedArticles::~PagedArticles() {
} }
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");
}

View File

@ -1,13 +1,10 @@
#ifndef PAGED_ARTICLES_H #ifndef PAGED_ARTICLES_H
#define PAGED_ARTICLES_H #define PAGED_ARTICLES_H
#include "core/containers/vector.h" #include "core/ustring.h"
#include "core/string.h" #include "core/vector.h"
#include "web/file_cache.h" #include "../../http/web_node.h"
#include "web/http/web_node.h"
#include "web/http/request.h"
// This class will load and process all md files from the folder set to it's folder property, // 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. // and generate one page from them. TThe files are processed in alphabetical order.
@ -24,27 +21,34 @@
// ... // ...
// </div> // </div>
class WebServerRequest;
class PagedArticles : public WebNode { class PagedArticles : public WebNode {
RCPP_OBJECT(PagedArticles, WebNode); GDCLASS(PagedArticles, WebNode);
public: public:
void _handle_request_main(Request *request); String get_folder();
void set_folder(const String &val);
void render_index(Request *request); void _handle_request(Ref<WebServerRequest> request);
void render_preview(Request *request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> request);
void load(); void load();
void generate_index_page(); void generate_index_page();
void _notification(const int what);
PagedArticles(); PagedArticles();
~PagedArticles(); ~PagedArticles();
String folder;
protected: protected:
String index_page; void _notification(int what);
static void _bind_methods();
String _folder;
String _index_page;
}; };
#endif #endif

View File

@ -1,10 +1,22 @@
#include "paged_articles_md_index.h" #include "paged_articles_md_index.h"
#include "web/html/html_builder.h" #include "core/os/dir_access.h"
#include "web/http/request.h"
#include "web/http/web_permission.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<WebServerRequest> request) {
if (_web_permission.is_valid()) { if (_web_permission.is_valid()) {
if (_web_permission->activate(request)) { if (_web_permission->activate(request)) {
return; return;
@ -21,31 +33,35 @@ void PagedArticlesMDIndex::handle_request_main(Request *request) {
articles->handle_request_main(request); articles->handle_request_main(request);
} }
void PagedArticlesMDIndex::render_index(Request *request) { void PagedArticlesMDIndex::_render_index(Ref<WebServerRequest> request) {
main_page->render_index(request); main_page->render_index(request);
} }
void PagedArticlesMDIndex::render_preview(Request *request) { void PagedArticlesMDIndex::_render_preview(Ref<WebServerRequest> request) {
main_page->render_preview(request); main_page->render_preview(request);
} }
void PagedArticlesMDIndex::load() { void PagedArticlesMDIndex::load() {
main_page->folder = folder; main_page->set_folder(folder);
main_page->load(); main_page->load();
articles->articles_folder = folder; articles->set_articles_folder(folder);
articles->serve_folder = folder + "/files"; //articles->set_serve_folder(folder + "/files");
articles->load(); articles->load();
} }
PagedArticlesMDIndex::PagedArticlesMDIndex() : PagedArticlesMDIndex::PagedArticlesMDIndex() {
WebNode() { main_page = memnew(ListPage);
articles = memnew(PagedArticle);
main_page = new ListPage();
articles = new PagedArticle();
} }
PagedArticlesMDIndex::~PagedArticlesMDIndex() { PagedArticlesMDIndex::~PagedArticlesMDIndex() {
delete main_page; memdelete(main_page);
delete articles; 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");
} }

View File

@ -1,32 +1,38 @@
#ifndef PAGED_ARTICLES_MD_INDEX_H #ifndef PAGED_ARTICLES_MD_INDEX_H
#define 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 "../../http/web_node.h"
#include "web_modules/list_page/list_page.h"
#include "web_modules/paged_article/paged_article.h"
// Inherit from PagedArticles and override generate_index_page -> load and process md files in the set folder // Inherit from PagedArticles and override generate_index_page -> load and process md files in the set folder
// SHould probably be called something else. PagedArticlesMDIndex ? // SHould probably be called something else. PagedArticlesMDIndex ?
class WebServerRequest;
class ListPage;
class PagedArticle;
class PagedArticlesMDIndex : public WebNode { class PagedArticlesMDIndex : public WebNode {
RCPP_OBJECT(PagedArticlesMDIndex, WebNode); GDCLASS(PagedArticlesMDIndex, WebNode);
public: public:
void handle_request_main(Request *request); String get_folder();
void set_folder(const String &val);
void render_index(Request *request); void _handle_request_main(Ref<WebServerRequest> request);
void render_preview(Request *request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> request);
void load(); void load();
PagedArticlesMDIndex(); PagedArticlesMDIndex();
~PagedArticlesMDIndex(); ~PagedArticlesMDIndex();
protected:
static void _bind_methods();
String folder; String folder;
String base_path;
ListPage *main_page; ListPage *main_page;
PagedArticle *articles; PagedArticle *articles;

View File

@ -53,6 +53,8 @@ SOFTWARE.
#include "nodes/list_page/list_page.h" #include "nodes/list_page/list_page.h"
#include "nodes/paged_article/paged_article.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() { void register_web_types() {
ClassDB::register_class<_HTMLBuilder>(); ClassDB::register_class<_HTMLBuilder>();
@ -88,6 +90,8 @@ void register_web_types() {
ClassDB::register_class<ListPage>(); ClassDB::register_class<ListPage>();
ClassDB::register_class<PagedArticle>(); ClassDB::register_class<PagedArticle>();
ClassDB::register_class<PagedArticles>();
//ClassDB::register_class<PagedArticlesMDIndex>();
} }
void unregister_web_types() { void unregister_web_types() {