From 0dcf36052e8ad60e2fc8e0037aeb16e5c06a6a3f Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 24 Aug 2024 13:09:28 +0200 Subject: [PATCH] ... unsure --- www/src/db_handler/post.php | 36 +++++++++++-------- www/src/db_handler/post_handler.php | 10 ++++-- www/src/dbtest.php | 8 +++-- www/src/router.php | 16 +++++++-- www/src/serve/ajax.php | 26 ++++++++++++++ www/src/{serve_post.php => serve/post.php} | 22 ++++++------ www/src/setup_db.php | 2 ++ www/static/dergstyle.css | 13 +++++-- www/templates/ajax/closed_folder_listing.html | 13 ------- .../ajax/compact_filelist/entry.html | 27 ++++++++++++++ .../ajax/compact_filelist/listing.html | 4 +++ www/templates/ajax/folder_listing.html | 4 --- www/templates/ajax/folder_listing_entry.html | 26 -------------- www/templates/ajax/open_folder_listing.html | 20 ----------- www/templates/fragments/filepath_bar.html | 20 +++++------ www/templates/pathed_content.html | 6 ++-- www/templates/post_types/markdown.html | 6 +++- www/templates/root.html | 5 ++- 18 files changed, 152 insertions(+), 112 deletions(-) create mode 100644 www/src/serve/ajax.php rename www/src/{serve_post.php => serve/post.php} (52%) delete mode 100644 www/templates/ajax/closed_folder_listing.html create mode 100644 www/templates/ajax/compact_filelist/entry.html create mode 100644 www/templates/ajax/compact_filelist/listing.html delete mode 100644 www/templates/ajax/folder_listing.html delete mode 100644 www/templates/ajax/folder_listing_entry.html delete mode 100644 www/templates/ajax/open_folder_listing.html diff --git a/www/src/db_handler/post.php b/www/src/db_handler/post.php index 95e150b..4e9494e 100644 --- a/www/src/db_handler/post.php +++ b/www/src/db_handler/post.php @@ -7,7 +7,7 @@ class Post implements ArrayAccess { private $content_html; private $content_markdown; - public $site_defaults; + private $site_defaults; public $data; @@ -15,6 +15,7 @@ class Post implements ArrayAccess { public $raw_data; private $_child_posts; + private $_parent_post; public static function _generate_404($post_data) { $post_data ??= [ @@ -61,13 +62,13 @@ class Post implements ArrayAccess { return $icon_mapping[$type] ?? 'unknown'; } - function __construct($post_handler, $post_data) { + function __construct($post_handler, $post_data, $site_defaults) { $this->handler = $post_handler; $this->content_html = null; $this->content_markdown = null; - $this->site_defaults = null; + $this->site_defaults = $site_defaults; if(!isset($post_data) or !isset($post_data['id'])) { $post_data = $this->_generate_404($post_data); @@ -75,9 +76,17 @@ class Post implements ArrayAccess { $data = $post_data; + if($data['path'] == '') { + $data['path'] = '/'; + $data['title'] ??= 'root'; + $data['basename'] ??= 'root'; + } + $post_data['host'] ??= 'localhost:8081'; - $data['url'] ??= 'https://' . $post_data['host'] . $post_data['path']; + $data['url'] ??= 'http://' . $post_data['host'] . $post_data['path']; + + $data['basename'] ??= basename($data['path']); $data['title'] ??= basename($data['path']); @@ -111,6 +120,9 @@ class Post implements ArrayAccess { if($name == 'child_posts') { return $this->get_child_posts(); } + if($name == 'parent') { + return $this->get_parent_post(); + } if(isset($this->data[$name])) { return $this->data[$name]; @@ -137,10 +149,10 @@ class Post implements ArrayAccess { return !is_null($this->offsetGet($offset)); } public function offsetSet($offset, $value) : void { - throw RuntimeError("Setting of post data is not allowed!"); + $this->data[$offset] = $value; } public function offsetUnset($offset) : void { - throw RuntimeError("Unsetting of post data is not allowed!"); + unset($this->data[$offset]); } public function get_html() { @@ -156,14 +168,11 @@ class Post implements ArrayAccess { } public function get_child_posts(...$search_args) { - var_dump($search_args); - - if(count($search_args) == 0) { - $this->child_posts ??= + $this->_child_posts ??= $this->handler->get_children_for($this); - return $this->child_posts; + return $this->_child_posts; } else { return $this->handler->get_children_for($this, ...$search_args); @@ -195,10 +204,9 @@ class Post implements ArrayAccess { if($parent_path == '') return null; - $this->parent_post ??= new PostData($this->handler, - $this->handler->get_post_by_path($parent_path)); + $this->_parent_post ??= $this->handler->get_post($parent_path); - return $this->parent_post; + return $this->_parent_post; } } diff --git a/www/src/db_handler/post_handler.php b/www/src/db_handler/post_handler.php index 33f249c..06fd3fc 100644 --- a/www/src/db_handler/post_handler.php +++ b/www/src/db_handler/post_handler.php @@ -9,10 +9,14 @@ class PostHandler { public $markdown_engine; + public $site_defaults; + function __construct($db_adapter) { $this->db = $db_adapter; $this->posts = []; + $this->site_defaults = null; + $this->markdown_engine = null; } @@ -26,7 +30,7 @@ class PostHandler { $post_data = $this->db->get_postdata($key); $post = null; if(isset($post_data)) { - $post = new Post($this, $post_data); + $post = new Post($this, $post_data, $this->site_defaults); } $this->posts[$key] = $post; @@ -39,7 +43,7 @@ class PostHandler { } public function render_post($post) { - return $this->markdown_engine($post); + return ($this->markdown_engine)($post); } public function get_children_for($post, ...$search_opts) { @@ -47,7 +51,7 @@ class PostHandler { $out_list = []; foreach($child_list as $child_data) { - array_push($out_list, new Post($this, $child_data)); + array_push($out_list, new Post($this, $child_data, $this->site_defaults)); } return $out_list; diff --git a/www/src/dbtest.php b/www/src/dbtest.php index 86983d6..ffbc378 100644 --- a/www/src/dbtest.php +++ b/www/src/dbtest.php @@ -126,13 +126,17 @@ echo "Done!\n\n"; test_accounce("Setting post markdown..."); $sql_adapter->set_postdata([ 'path' => '/testing/markdowntest', - 'markdown' => 'Inline markdown test should work...' + 'markdown' => 'Inline markdown test should work...', + 'title' => "A Markdown Test" ]); $post = $sql_adapter->get_postdata('/testing/markdowntest'); var_dump($sql_adapter->get_post_markdown($post['id'])); $sql_adapter->set_post_markdown($post['id'], - 'This is one hell of a cute test!' + ' + This is one hell of a cute test! +> Just checking in... + ' ); var_dump($sql_adapter->get_post_markdown($post['id'])); unset($post); diff --git a/www/src/router.php b/www/src/router.php index 6041798..c2f3dc7 100644 --- a/www/src/router.php +++ b/www/src/router.php @@ -12,8 +12,20 @@ require_once 'dergdown.php'; require_once 'setup_twig.php'; -$PARSED_URL = parse_url($_SERVER['REQUEST_URI']); +$REQUEST_URI = parse_url($_SERVER['REQUEST_URI']); +$REQUEST_PATH = $REQUEST_URI['path']; + +parse_str($REQUEST_URI['query'] ?? '', $REQUEST_QUERY); + +if(preg_match('/^\/api/', $REQUEST_PATH)) { + require_once 'serve/api.php'; +} +elseif(preg_match('/^\/ajax/', $REQUEST_PATH)) { + require_once 'serve/ajax.php'; +} +else { + require_once 'serve/post.php'; +} -require_once 'serve_post.php'; ?> \ No newline at end of file diff --git a/www/src/serve/ajax.php b/www/src/serve/ajax.php new file mode 100644 index 0000000..679afe0 --- /dev/null +++ b/www/src/serve/ajax.php @@ -0,0 +1,26 @@ +get_post($REQUEST_QUERY['page']); +} + + +$ajax_args['fa'] = $FONT_AWESOME_ARRAY; +$ajax_args['page'] ??= $SITE_CONFIG['site_defaults']; + +echo $twig->render('/ajax/' . $AJAX_REQUEST_TEMPLATE, $ajax_args); + +?> \ No newline at end of file diff --git a/www/src/serve_post.php b/www/src/serve/post.php similarity index 52% rename from www/src/serve_post.php rename to www/src/serve/post.php index 29fabbb..6430550 100644 --- a/www/src/serve_post.php +++ b/www/src/serve/post.php @@ -2,26 +2,24 @@ require_once 'fontawesome.php'; -$post = $adapter->get_post($PARSED_URL['path']); -if(isset($post)) { - $post->site_defaults = $SITE_CONFIG['site_defaults']; -} +$post = $adapter->get_post($REQUEST_PATH); function render_root_template($template, $args = []) { global $twig; global $FONT_AWESOME_ARRAY; global $SITE_CONFIG; - $args['fontawesome'] = $FONT_AWESOME_ARRAY; + $args['fa'] = $FONT_AWESOME_ARRAY; $args['page'] ??= $SITE_CONFIG['site_defaults']; $page = $args['page']; + $page['base'] ??= $page['url']; $args['opengraph'] = [ - "site_name" => $page['site_name'] ?? 'UNSET SITE NAME', - "title" => $page['title'] ?? 'UNSET TITLE', - "url" => $page['url'] ?? 'UNSET URL', - "description" => $page['description'] ?? 'UNSET DESCRIPTION' + "site_name" => $page['site_name'] ?? 'Nameless Site', + "title" => $page['title'] ?? 'Titleless', + "url" => $page['url'] ?? $page['path'] ?? 'No URL set', + "description" => $page['description'] ?? 'No description set' ]; $args['banners'] = json_encode($page['banners'] ?? []); @@ -32,7 +30,11 @@ function render_root_template($template, $args = []) { echo $twig->render($template, $args); } -render_root_template('root.html', [ +function render_pathed_content_template($template, $args = []) { + render_root_template($template, $args); +} + +render_pathed_content_template('post_types/markdown.html', [ 'page' => $post ]); die(); diff --git a/www/src/setup_db.php b/www/src/setup_db.php index 97195aa..70b3d88 100644 --- a/www/src/setup_db.php +++ b/www/src/setup_db.php @@ -40,4 +40,6 @@ function post_to_html($post) { } $adapter->markdown_engine = "post_to_html"; +$adapter->site_defaults = $SITE_CONFIG['site_defaults']; + ?> \ No newline at end of file diff --git a/www/static/dergstyle.css b/www/static/dergstyle.css index 2a4c1f9..6ccc0f6 100644 --- a/www/static/dergstyle.css +++ b/www/static/dergstyle.css @@ -311,12 +311,21 @@ body.htmx-request::before { .folder-listing input { display: none; } -.folder-listing input + ul { +.folder-listing input ~ ul { display: none; } -.folder-listing input:checked + ul { +.folder-listing input:checked ~ ul { display: block; } +.folder-listing label > :nth-child(2) { + display: none; +} +.folder-listing input:checked ~ label > :nth-child(1) { + display: none; +} +.folder-listing input:checked ~ label > :nth-child(2) { + display: inline-block; +} #navbar-expand-label { cursor: pointer; diff --git a/www/templates/ajax/closed_folder_listing.html b/www/templates/ajax/closed_folder_listing.html deleted file mode 100644 index 399894f..0000000 --- a/www/templates/ajax/closed_folder_listing.html +++ /dev/null @@ -1,13 +0,0 @@ - -
  • - - {{ fa[post.post_metadata.icon] | raw }} - - - - {{ post.post_metadata.title }} - -
  • \ No newline at end of file diff --git a/www/templates/ajax/compact_filelist/entry.html b/www/templates/ajax/compact_filelist/entry.html new file mode 100644 index 0000000..6c63345 --- /dev/null +++ b/www/templates/ajax/compact_filelist/entry.html @@ -0,0 +1,27 @@ + + +
  • + {% set folder_key = random() %} + + + + + + {{ post.basename }} - {{ post.title }} + + + +
  • \ No newline at end of file diff --git a/www/templates/ajax/compact_filelist/listing.html b/www/templates/ajax/compact_filelist/listing.html new file mode 100644 index 0000000..ccdc7c9 --- /dev/null +++ b/www/templates/ajax/compact_filelist/listing.html @@ -0,0 +1,4 @@ + +{% for post in page.child_posts %} +{{ include('ajax/compact_filelist/entry.html') }} +{% endfor %} \ No newline at end of file diff --git a/www/templates/ajax/folder_listing.html b/www/templates/ajax/folder_listing.html deleted file mode 100644 index b90fb59..0000000 --- a/www/templates/ajax/folder_listing.html +++ /dev/null @@ -1,4 +0,0 @@ - -{% for post in subposts %} -{{ include('ajax/folder_listing_entry.html') }} -{% endfor %} \ No newline at end of file diff --git a/www/templates/ajax/folder_listing_entry.html b/www/templates/ajax/folder_listing_entry.html deleted file mode 100644 index e47bf9b..0000000 --- a/www/templates/ajax/folder_listing_entry.html +++ /dev/null @@ -1,26 +0,0 @@ - - -
  • - {% set folder_key = random() %} - - - - - {{ post.post_metadata.title }} - - - - -
  • \ No newline at end of file diff --git a/www/templates/ajax/open_folder_listing.html b/www/templates/ajax/open_folder_listing.html deleted file mode 100644 index 9bd6414..0000000 --- a/www/templates/ajax/open_folder_listing.html +++ /dev/null @@ -1,20 +0,0 @@ - - -
  • - - {{ fa[post.post_metadata.icon] | raw }} - - - - {{ post.post_metadata.title }} - - - -
  • \ No newline at end of file diff --git a/www/templates/fragments/filepath_bar.html b/www/templates/fragments/filepath_bar.html index 314e019..f4a43a2 100644 --- a/www/templates/fragments/filepath_bar.html +++ b/www/templates/fragments/filepath_bar.html @@ -1,11 +1,11 @@
  • - {{ post.post_metadata.title }} + {{ page.basename }}
  • - {% set split_post = post.post_path |split('/') %} + {% set split_post = page.path |split('/') %} {% for i in range(0, split_post|length - 1) %}
  • {% if i != 0 %} @@ -22,7 +22,7 @@
  • + style="padding-left: 0.3rem;" href="/feed/rss{{page.path}}"> {{ fa['rss']|raw }}
  • @@ -32,7 +32,7 @@
    diff --git a/www/templates/pathed_content.html b/www/templates/pathed_content.html index 90a0350..c8a3ffa 100644 --- a/www/templates/pathed_content.html +++ b/www/templates/pathed_content.html @@ -6,11 +6,11 @@ {{ parent() }} - + {% endblock %} {% block second_title %} -

    {{ post.post_metadata.title }}

    +

    {{ page.title }}

    {% endblock %} {%block main_content%} @@ -22,7 +22,7 @@ {%endblock%} - This article was created on {{ post.post_create_time }}, last edited {{ post.post_update_time }}, and was viewed {{ post.post_access_count }} times~ + This page was created on {{ page.created_at }}, last edited {{ page.updated_at }}, and was viewed {{ page.view_count }} times~ diff --git a/www/templates/post_types/markdown.html b/www/templates/post_types/markdown.html index b9d147c..f00142c 100644 --- a/www/templates/post_types/markdown.html +++ b/www/templates/post_types/markdown.html @@ -9,5 +9,9 @@ {%endblock %} {%block content_article%} - {{ content_html|raw }} + {% if page.title %} +

    {{ page.title }}

    + {% endif %} + + {{ page.html|raw }} {% endblock %} \ No newline at end of file diff --git a/www/templates/root.html b/www/templates/root.html index c36e6ff..08c3de4 100644 --- a/www/templates/root.html +++ b/www/templates/root.html @@ -12,7 +12,6 @@ - @@ -20,6 +19,10 @@ + {% if page.base %} + + {% endif %} + {% block feed_links %} {% endblock %}