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/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':

View File

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

View File

@ -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 <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_index(request);
request->compile_and_send_body();
}
void PagedArticles::render_index(Request *request) {
void PagedArticles::_render_index(Ref<WebServerRequest> request) {
// 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() {
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<Directory> 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() {
}
}
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
#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 @@
// ...
// </div>
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<WebServerRequest> request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> 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
#endif

View File

@ -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<WebServerRequest> 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<WebServerRequest> request) {
main_page->render_index(request);
}
void PagedArticlesMDIndex::render_preview(Request *request) {
void PagedArticlesMDIndex::_render_preview(Ref<WebServerRequest> 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");
}

View File

@ -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<WebServerRequest> request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> request);
void load();
PagedArticlesMDIndex();
~PagedArticlesMDIndex();
protected:
static void _bind_methods();
String folder;
String base_path;
ListPage *main_page;
PagedArticle *articles;
};
#endif
#endif

View File

@ -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<ListPage>();
ClassDB::register_class<PagedArticle>();
ClassDB::register_class<PagedArticles>();
//ClassDB::register_class<PagedArticlesMDIndex>();
}
void unregister_web_types() {