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

View File

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

View File

@ -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<WebServerRequest> 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<WebServerRequest> request) {
request->body += _data;
}
void StaticPage::render_preview(Request *request) {
request->body += preview_data;
void StaticPage::_render_preview(Ref<WebServerRequest> request) {
request->body += _preview_data;
}
void StaticPage::load_file(const String &path) {
Ref<Directory> 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<Directory> 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<Directory> 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);
}

View File

@ -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<WebServerRequest> request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> 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
#endif

View File

@ -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);
}

View File

@ -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
#endif

View File

@ -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<Directory> 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);
}

View File

@ -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
#endif

View File

@ -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<WebServerRequest>();
ClassDB::register_class<WebServerSimple>();
ClassDB::register_class<StaticPage>();
ClassDB::register_class<StaticPageFile>();
ClassDB::register_class<StaticPageFolderFiles>();
}
void unregister_web_types() {