Cleaned up StaticPages, and added them to the build.

This commit is contained in:
Relintai 2022-07-02 13:22:10 +02:00
parent a94ee653d1
commit 56d2ccbb26
9 changed files with 235 additions and 115 deletions

View File

@ -31,6 +31,10 @@ sources = [
"http_server_simple/http_parser.cpp", "http_server_simple/http_parser.cpp",
"http_server_simple/http_writer.cpp", "http_server_simple/http_writer.cpp",
"http_server_simple/http_parser/http_parser.c", "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': if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':

View File

@ -34,6 +34,10 @@ def get_doc_classes():
"HTMLTag", "HTMLTag",
"WebServerSimple", "WebServerSimple",
"StaticPage",
"StaticPageFile",
"StaticPageFolderFiles",
] ]

View File

@ -1,73 +1,94 @@
#include "static_page.h" #include "static_page.h"
#include "core/os/directory.h" #include "../../http/web_server_request.h"
#include "web/file_cache.h" #include "core/os/file_access.h"
#include "web/html/html_builder.h"
#include "web/html/utils.h"
#include "web/http/request.h"
void StaticPage::_handle_request_main(Request *request) { String StaticPage::get_data() {
if (should_render_menu) { return _data;
render_menu(request); }
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<WebServerRequest> request) {
if (_should_render_menu) {
render_main_menu(request);
} }
render_index(request); render_index(request);
request->compile_and_send_body(); request->compile_and_send_body();
} }
void StaticPage::render_index(Request *request) { void StaticPage::_render_index(Ref<WebServerRequest> request) {
request->body += data; request->body += _data;
} }
void StaticPage::render_preview(Request *request) { void StaticPage::_render_preview(Ref<WebServerRequest> request) {
request->body += preview_data; request->body += _preview_data;
} }
void StaticPage::load_file(const String &path) { void StaticPage::load_file(const String &path) {
Ref<Directory> d; FileAccess *f = FileAccess::open(path, FileAccess::READ);
d.instance();
d->read_file_into(path, &data); if (f) {
} _data = f->get_as_utf8_string();
f->close();
void StaticPage::load_and_process_file(const String &path) { memdelete(f);
Ref<Directory> d; } else {
d.instance(); _data = "";
d->read_file_into(path, &data);
if (path.file_get_extension() == "md") {
Utils::markdown_to_html(&data);
} }
} }
void StaticPage::load_md_file(const String &path) { void StaticPage::load_and_process_file(const String &path) {
Ref<Directory> d; FileAccess *f = FileAccess::open(path, FileAccess::READ);
d.instance();
d->read_file_into(path, &data); if (f) {
Utils::markdown_to_html(&data); _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) { StaticPage::StaticPage() {
data.clear(); _should_render_menu = true;
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() { 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);
}

View File

@ -1,33 +1,42 @@
#ifndef STATIC_PAGE_H #ifndef STATIC_PAGE_H
#define 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 { class StaticPage : public WebNode {
RCPP_OBJECT(StaticPage, WebNode); GDCLASS(StaticPage, WebNode);
public: public:
void _handle_request_main(Request *request); String get_data();
void set_data(const String &val);
void render_index(Request *request); String get_preview_data();
void render_preview(Request *request); void set_preview_data(const String &val);
bool get_should_render_menu();
void set_should_render_menu(const bool &val);
void _handle_request(Ref<WebServerRequest> request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> request);
void load_file(const String &path); void load_file(const String &path);
void load_and_process_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();
~StaticPage(); ~StaticPage();
protected:
static void _bind_methods();
String _data;
String _preview_data;
bool _should_render_menu;
}; };
#endif #endif

View File

@ -1,18 +1,28 @@
#include "static_page_file.h" #include "static_page_file.h"
#include "web/file_cache.h" String StaticPageFile::get_file_path() {
#include "web/html/html_builder.h" return _file_path;
#include "web/http/request.h" }
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() { void StaticPageFile::load() {
if (file_path == "") { if (_file_path == "") {
return; return;
} }
if (process_if_can) { if (_process_if_can) {
load_and_process_file(file_path); load_and_process_file(_file_path);
} else { } else {
load_file(file_path); load_file(_file_path);
} }
} }
@ -26,11 +36,21 @@ void StaticPageFile::_notification(const int what) {
} }
} }
StaticPageFile::StaticPageFile() : StaticPageFile::StaticPageFile() {
StaticPage() { _process_if_can = true;
process_if_can = true;
} }
StaticPageFile::~StaticPageFile() { 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);
}

View File

@ -1,23 +1,31 @@
#ifndef STATIC_PAGE_FILE_H #ifndef STATIC_PAGE_FILE_H
#define STATIC_PAGE_FILE_H #define STATIC_PAGE_FILE_H
#include "core/string.h" #include "core/ustring.h"
#include "static_page.h" #include "static_page.h"
class StaticPageFile : public StaticPage { class StaticPageFile : public StaticPage {
RCPP_OBJECT(StaticPageFile, StaticPage); GDCLASS(StaticPageFile, StaticPage);
public: 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 load();
void _notification(const int what);
String file_path;
bool process_if_can;
StaticPageFile(); StaticPageFile();
~StaticPageFile(); ~StaticPageFile();
protected:
void _notification(const int what);
static void _bind_methods();
String _file_path;
bool _process_if_can;
}; };
#endif #endif

View File

@ -1,40 +1,67 @@
#include "static_page_folder_files.h" #include "static_page_folder_files.h"
#include "core/os/directory.h" #include "core/os/dir_access.h"
#include "web/file_cache.h" #include "core/os/file_access.h"
#include "web/html/html_builder.h"
#include "web/html/utils.h" String StaticPageFolderFiles::get_dir_path() {
#include "web/http/request.h" 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() { void StaticPageFolderFiles::load() {
if (dir_path == "") { if (_dir_path == "") {
return; return;
} }
Ref<Directory> d; DirAccessRef da = DirAccess::open(_dir_path);
d.instance();
ERR_FAIL_COND_MSG(!d->open_dir(dir_path), "Dir Path = " + dir_path); ERR_FAIL_COND_MSG(!da, "Dir Path = " + _dir_path);
String str; if (da) {
while (d->has_next()) { da->list_dir_begin();
if (d->current_is_file()) {
String fn = dir_path;
fn.append_path(d->current_get_name_cstr());
d->read_file_into(fn, &str); while (true) {
String file = da->get_next();
if (process_if_can && d->current_get_extension() == "md") { if (file == "") {
Utils::markdown_to_html(&str); 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) { void StaticPageFolderFiles::append_data(const String &d) {
data += d; _data += d;
} }
void StaticPageFolderFiles::_notification(const int what) { void StaticPageFolderFiles::_notification(const int what) {
@ -47,11 +74,22 @@ void StaticPageFolderFiles::_notification(const int what) {
} }
} }
StaticPageFolderFiles::StaticPageFolderFiles() : StaticPageFolderFiles::StaticPageFolderFiles() {
StaticPage() { _process_if_can = true;
process_if_can = true;
} }
StaticPageFolderFiles::~StaticPageFolderFiles() { 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);
}

View File

@ -1,24 +1,32 @@
#ifndef STATIC_PAGE_FOLDER_FILES_H #ifndef STATIC_PAGE_FOLDER_FILES_H
#define STATIC_PAGE_FOLDER_FILES_H #define STATIC_PAGE_FOLDER_FILES_H
#include "core/string.h" #include "core/ustring.h"
#include "static_page.h" #include "static_page.h"
class StaticPageFolderFiles : public StaticPage { class StaticPageFolderFiles : public StaticPage {
RCPP_OBJECT(StaticPageFolderFiles, StaticPage); GDCLASS(StaticPageFolderFiles, StaticPage);
public: 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(); void load();
virtual void append_data(const String &d); virtual void append_data(const String &d);
void _notification(const int what);
String dir_path;
bool process_if_can;
StaticPageFolderFiles(); StaticPageFolderFiles();
~StaticPageFolderFiles(); ~StaticPageFolderFiles();
protected:
void _notification(const int what);
static void _bind_methods();
String _dir_path;
bool _process_if_can;
}; };
#endif #endif

View File

@ -42,6 +42,10 @@ SOFTWARE.
#include "http_server_simple/web_server_simple.h" #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() { void register_web_types() {
ClassDB::register_class<_HTMLBuilder>(); ClassDB::register_class<_HTMLBuilder>();
ClassDB::register_class<_HTMLTag>(); ClassDB::register_class<_HTMLTag>();
@ -63,6 +67,10 @@ void register_web_types() {
ClassDB::register_class<WebServerRequest>(); ClassDB::register_class<WebServerRequest>();
ClassDB::register_class<WebServerSimple>(); ClassDB::register_class<WebServerSimple>();
ClassDB::register_class<StaticPage>();
ClassDB::register_class<StaticPageFile>();
ClassDB::register_class<StaticPageFolderFiles>();
} }
void unregister_web_types() { void unregister_web_types() {