From 6c22b0da1977c79e12cff23edfbabb2e4c68f6e1 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 2 Jan 2024 02:59:41 +0100 Subject: [PATCH] Added a new static serve standalone script demo. --- web_standalone_scripts/static_serve/README.md | 26 +++ .../static_serve/static_serve.gd | 151 ++++++++++++++++++ .../static_serve/test_site_1/index.html | 13 ++ .../test_site_1/www_root/icon.png | Bin 0 -> 3218 bytes .../test_site_2/a/b/c/an_another_html.html | 1 + .../static_serve/test_site_2/a/b/c/index.html | 12 ++ .../static_serve/test_site_2/a/b/index.html | 13 ++ .../static_serve/test_site_2/a/index.html | 12 ++ .../static_serve/test_site_2/a/test.md | 7 + .../static_serve/test_site_2/index.html | 15 ++ .../test_site_2/www_root/icon.png | Bin 0 -> 3218 bytes 11 files changed, 250 insertions(+) create mode 100644 web_standalone_scripts/static_serve/README.md create mode 100644 web_standalone_scripts/static_serve/static_serve.gd create mode 100644 web_standalone_scripts/static_serve/test_site_1/index.html create mode 100644 web_standalone_scripts/static_serve/test_site_1/www_root/icon.png create mode 100644 web_standalone_scripts/static_serve/test_site_2/a/b/c/an_another_html.html create mode 100644 web_standalone_scripts/static_serve/test_site_2/a/b/c/index.html create mode 100644 web_standalone_scripts/static_serve/test_site_2/a/b/index.html create mode 100644 web_standalone_scripts/static_serve/test_site_2/a/index.html create mode 100644 web_standalone_scripts/static_serve/test_site_2/a/test.md create mode 100644 web_standalone_scripts/static_serve/test_site_2/index.html create mode 100644 web_standalone_scripts/static_serve/test_site_2/www_root/icon.png diff --git a/web_standalone_scripts/static_serve/README.md b/web_standalone_scripts/static_serve/README.md new file mode 100644 index 0000000..2313213 --- /dev/null +++ b/web_standalone_scripts/static_serve/README.md @@ -0,0 +1,26 @@ +# Static Serve Demo + +This is a small script that parses a simple folder structure into a static site. + +It's derived from the SceneTree, so you don't need to run this as a project, +instead you need to get the engine, and run it with the `-s` (`--script`) option like so: + +`./pandemonium -s ./static_serve.gd test_site_1` + +`./pandemonium -s ./static_serve.gd test_site_2` + +## Folder structure that it supports: + +``` + + <- this directory will be served / set as the www_root. If you have more those are not. Put + +``` + +It parses the other folders and files into a WebNode hierarchy. + +It creates StaticWebPageFile from all files, also it strips .html from the file names when setting uri_segments. + +In every folder it parses index.html-s directly into that folder's root WebNode. + +It will render out .md files to HTML. diff --git a/web_standalone_scripts/static_serve/static_serve.gd b/web_standalone_scripts/static_serve/static_serve.gd new file mode 100644 index 0000000..07f1975 --- /dev/null +++ b/web_standalone_scripts/static_serve/static_serve.gd @@ -0,0 +1,151 @@ +# Run this script like: /pandemonium -s static_serve.gd test_site_1 + +#extends Node +extends SceneTree +class_name StaticServe + +var web_server_simple : WebServerSimple = null + +func process_data_folder(path : String, uri_segment : String, parent_web_node : WebNode) -> void: + var dir : Directory = Directory.new() + var f : File = File.new() + + var index_page_path : String = path + "index.html" + + var has_index_page : bool = dir.file_exists(index_page_path) + var current_node : WebNode = null + + if has_index_page: + var wr : StaticWebPageFile = StaticWebPageFile.new() + wr.file_path = index_page_path + current_node = wr + else: + var wr : StaticWebPage = StaticWebPage.new() + current_node = wr + + current_node.uri_segment = uri_segment + parent_web_node.add_child(current_node) + + if dir.open(path) == OK: + dir.list_dir_begin(true) + var file_name = dir.get_next() + while file_name != "": + if dir.current_is_dir(): + process_data_folder(path + file_name + "/", file_name, current_node) + else: + if file_name == "index.html": + file_name = dir.get_next() + continue + + var wp : StaticWebPageFile = StaticWebPageFile.new() + wp.file_path = path + file_name + wp.uri_segment = file_name.replace(".html", "") + current_node.add_child(wp) + + file_name = dir.get_next() + dir.list_dir_end() + +func setup(): + OS.low_processor_usage_mode = true + + var project_path : String = "./" + + var cmdline_args : PoolStringArray = OS.get_cmdline_args() + + if cmdline_args.size() > 0: + project_path = cmdline_args[cmdline_args.size() - 1].path_ensure_end_slash() + + var dir : Directory = Directory.new() + + var www_root_path : String = project_path + "www_root" + var index_page_path : String = project_path + "index.html" + + if !dir.dir_exists(project_path): + PLogger.log_error("Error project directory doens't seems to exist! " + project_path) + # If Node + #get_tree().quit(-1) + # If SceneTree + quit(-1) + return + + var has_www_root_folder : bool = dir.dir_exists(www_root_path) + var has_index_page : bool = dir.file_exists(index_page_path) + var web_root : WebNode = null + + if has_www_root_folder: + var wr : WebRoot = WebRoot.new() + wr.www_root_path = www_root_path + web_root = wr + + if has_index_page: + var index : StaticWebPageFile = StaticWebPageFile.new() + index.file_path = index_page_path + index.uri_segment = "/" + web_root.add_child(index) + else: + if has_index_page: + var wr : StaticWebPageFile = StaticWebPageFile.new() + wr.file_path = index_page_path + web_root = wr + else: + var wr : StaticWebPage = StaticWebPage.new() + web_root = wr + + if dir.open(project_path) == OK: + dir.list_dir_begin(true) + var file_name = dir.get_next() + while file_name != "": + if dir.current_is_dir(): + if file_name == "www_root": + file_name = dir.get_next() + continue + + process_data_folder(project_path + file_name + "/", file_name, web_root) + else: + if file_name == "index.html": + file_name = dir.get_next() + continue + + var wp : StaticWebPageFile = StaticWebPageFile.new() + wp.file_path = project_path + file_name + wp.uri_segment = file_name.replace(".html", "") + web_root.add_child(wp) + + file_name = dir.get_next() + dir.list_dir_end() + + web_server_simple = WebServerSimple.new() + web_server_simple.start_on_ready = true + web_server_simple.add_child(web_root) + + +# If it's a Node +#func _ready() -> void: +# setup() +# add_child(web_server_simple) + +# If it's a SceneTree +func _initialize(): + setup() + root.add_child(web_server_simple) + + var link0 : String = "http://" + web_server_simple.bind_host + ":" + str(web_server_simple.bind_port) + var link1 : String = "http://127.0.0.1:" + str(web_server_simple.bind_port) + + var run_text : String = "Running server on: " + link1 + " (" + link0 + ")" + + PLogger.log_message(run_text) + + #For the lolz + var pc : PanelContainer = PanelContainer.new() + root.add_child(pc) + pc.set_anchors_and_margins_preset(Control.PRESET_WIDE) + + var cc : CenterContainer = CenterContainer.new() + pc.add_child(cc) + + var lb : LinkButton = LinkButton.new() + cc.add_child(lb) + lb.uri = link1 + lb.text = run_text + diff --git a/web_standalone_scripts/static_serve/test_site_1/index.html b/web_standalone_scripts/static_serve/test_site_1/index.html new file mode 100644 index 0000000..6381df5 --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_1/index.html @@ -0,0 +1,13 @@ + + + + + + + + + This is a super simple static test site.
+ + + + \ No newline at end of file diff --git a/web_standalone_scripts/static_serve/test_site_1/www_root/icon.png b/web_standalone_scripts/static_serve/test_site_1/www_root/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..87f1f7549e0489d0758939152cd1f879f1e653b5 GIT binary patch literal 3218 zcmV;D3~lp?P)Px>Oi4sRRCt`-n|p9v)qTf5=iI$}?`o~pW3{W5wUUr5+ZZ>+Ha2kvrbE(n3@r>e z6xz@>Qyxj3Ng!!SGDBuM`Ku++LLq@nT^K`)n@5w-mPttloU|q-HU{G-mStPWl5IU# zZ>{!q?>+rvB_U}a+Lc#+^!wM&{r&Fm{Lb&5^EM0?_ip!zwaziOKi4c#X)IUXr zhJa}U`2Bta{8R|>nair-C4y4wCkpNv8X7`WRaGIS{Gt@%dkYzLUB_dZNKLyOdR{af z+a?x|lS-vBX-e&I9A~GfsHkYxU9O{ZmwkD8Id!2BE|=?)D!g2>F5+YG_y^U)9vBc;47$}R__lvMb9BoYY{$s_2=)Z9m#TtcZ$i1cA#8b(Q$y=7Sp4G&}6HZ^`fZnt|$as~zmxzN)? zGMQv`Yb(`NRTrg~%jLo}jM-Iy5Sv^G5tw0Es-knDl%oB_2{tJW7lQu2e#RRc(#LEO z=sbIt=8*|jYi^XG^KM5cm76LqdTYAf?vD%rb@QYNAu``660ul}H8yyphAt&xmr3wk zFK4^D3VPph9NJHvV$E2TN|%lh0x2YGHJxZQdeQnVoMXv7T`HBrBjz8QKF!72(Misn zIkV{d7kYa5SS*EC&X?iUG-C01Np*$gFo0!|Z6SH2M(ae3;W=UGLfE!V)xa=iGBfVb z5X54!QmP6pg8`IMIfYheE=D@Luq-Ppy|<@_m71QFCWXL`UIhk_QsU&G2_aao>2!5> zXV5ImqAD_il)3$oYZ*e5l81VlreP@u$R0kMQo}bq&XMS`3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7e?u65Ll~*+>5i_J9_C{=-NcFg@8*<0($^0_Wgs})4VEq!-pWe+4Pk;! zP1Lnq!$|)?P8l&}NgPz+;*?cJ|JHDv@M-c$lv|_iSDn9fxy=ugU2vFfSZE zm{-Qh1G$aZWhZIQ(+60Hq72~k`RE>u(5z=#SfKc`mbJL1Gd#CxvZ6YDudlYDk?Oj1 z0&{TBp4T}fQEyqDQmu71?N)fS=^jk?13t|%?1Pw|Se5tZ1CpbAahIfw5 z9HkhuZBC`)#61Sli3!%YJ=DA0xP@G_eO(M}$032D-%iqQr3ls5qG=kP(I{o(lU!?< zxTmk^*12>#$HoeZ2MRWTQi|l{B*HAmavaWENiLW=HK8CaRaNM^PAZimGBm{dBO^>s zPU5m0f-Vjz#ODNBNu?NbY=$ihTi00O_2T!}v&HY9*Y=9X<9yKH zj~N+emCL{-BxB>_ghHX>46xV&o^TxYB%=g_L283R)*Hq=x~}Wgg+kO#4;)VmA$ z-=)^;lA*u9e_pXhDD{r;`FyQzT|X>@n7wCK;qws+g$nAgUk$Kro8G=Y#>dBJpDCrH z$y92CsH&_)lkyQEWkwojxZRi@&$5=dmJ`Zxh{xk||06UDN2xE{w*CCf%!J?Xe@zN; z`=wR!@<2JxYmwpMy8ty4)hLv@Lpjb@l~RRW1Gx&Jl$uhG^A+GurceEOYrCeR0vu;M1& literal 0 HcmV?d00001 diff --git a/web_standalone_scripts/static_serve/test_site_2/a/b/c/an_another_html.html b/web_standalone_scripts/static_serve/test_site_2/a/b/c/an_another_html.html new file mode 100644 index 0000000..2298b6d --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_2/a/b/c/an_another_html.html @@ -0,0 +1 @@ +an_another_html.html! \ No newline at end of file diff --git a/web_standalone_scripts/static_serve/test_site_2/a/b/c/index.html b/web_standalone_scripts/static_serve/test_site_2/a/b/c/index.html new file mode 100644 index 0000000..6954d24 --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_2/a/b/c/index.html @@ -0,0 +1,12 @@ + + + + + + + + + Last Page. + + + \ No newline at end of file diff --git a/web_standalone_scripts/static_serve/test_site_2/a/b/index.html b/web_standalone_scripts/static_serve/test_site_2/a/b/index.html new file mode 100644 index 0000000..6f25b3d --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_2/a/b/index.html @@ -0,0 +1,13 @@ + + + + + + + + + Link 3
+ an_another_html! + + + \ No newline at end of file diff --git a/web_standalone_scripts/static_serve/test_site_2/a/index.html b/web_standalone_scripts/static_serve/test_site_2/a/index.html new file mode 100644 index 0000000..5693f69 --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_2/a/index.html @@ -0,0 +1,12 @@ + + + + + + + + + Link 2 + + + \ No newline at end of file diff --git a/web_standalone_scripts/static_serve/test_site_2/a/test.md b/web_standalone_scripts/static_serve/test_site_2/a/test.md new file mode 100644 index 0000000..28efaa8 --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_2/a/test.md @@ -0,0 +1,7 @@ + +# Header + +## Sub header + +This is an md file! + diff --git a/web_standalone_scripts/static_serve/test_site_2/index.html b/web_standalone_scripts/static_serve/test_site_2/index.html new file mode 100644 index 0000000..7532e66 --- /dev/null +++ b/web_standalone_scripts/static_serve/test_site_2/index.html @@ -0,0 +1,15 @@ + + + + + + + + + This is more complex static test site.
+ Link 1
+ Link To MD File
+ + + + \ No newline at end of file diff --git a/web_standalone_scripts/static_serve/test_site_2/www_root/icon.png b/web_standalone_scripts/static_serve/test_site_2/www_root/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..87f1f7549e0489d0758939152cd1f879f1e653b5 GIT binary patch literal 3218 zcmV;D3~lp?P)Px>Oi4sRRCt`-n|p9v)qTf5=iI$}?`o~pW3{W5wUUr5+ZZ>+Ha2kvrbE(n3@r>e z6xz@>Qyxj3Ng!!SGDBuM`Ku++LLq@nT^K`)n@5w-mPttloU|q-HU{G-mStPWl5IU# zZ>{!q?>+rvB_U}a+Lc#+^!wM&{r&Fm{Lb&5^EM0?_ip!zwaziOKi4c#X)IUXr z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