From e19bafa42a3a18df422662af73090e70b2b8294a Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Tue, 23 Jun 2020 18:14:47 +0200 Subject: [PATCH] Add support for replies --- functions.php | 57 ++++++++++++++++++++++++++++++------------------- index.php | 18 ++++++++++++++-- views/reply.pug | 23 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 views/reply.pug diff --git a/functions.php b/functions.php index fe6e9cb..3eebb95 100644 --- a/functions.php +++ b/functions.php @@ -37,6 +37,11 @@ function getProjects($params) { return $projects; } +function getReplies($params) { + $projects = scandirRec('content/replies/', 'replies', $params); + return $projects; +} + function getWebmentions($target) { $data = json_decode(file_get_contents("https://webm.yarmo.eu/get?target=$target"), true); @@ -142,14 +147,6 @@ function scandirRec($path, $category, $params) { if (!$item['published'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) { return; } - if (isset($params['slug']) && $item['slug'] != $params['slug']) { - return; - } - - if (isset($params['slug'])) { - $item["webmentions"] = getWebmentions($item["url"]); - $item["hasWebmentions"] = count($item["webmentions"]) > 0; - } break; case 'notes': @@ -172,14 +169,6 @@ function scandirRec($path, $category, $params) { if (!$item['published'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) { return; } - if (isset($params['slug']) && $item['slug'] != $params['slug']) { - return; - } - - if (isset($params['slug'])) { - $item["webmentions"] = getWebmentions($item["url"]); - $item["hasWebmentions"] = count($item["webmentions"]) > 0; - } break; case 'projects': @@ -196,16 +185,32 @@ function scandirRec($path, $category, $params) { $date = new DateTime($item['date']); $item['date_formatted'] = $date->format('Y-m-d'); $item['date_rss'] = $date->format('Y-m-d'); + if (!$item['listed'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) { return; } - if (isset($params['slug']) && $item['slug'] != $params['slug']) { - return; - } + break; - if (isset($params['slug'])) { - $item["webmentions"] = getWebmentions($item["url"]); - $item["hasWebmentions"] = count($item["webmentions"]) > 0; + case 'replies': + $item = array( + "type" => "note", + "title" => $meta["title"], + "reply-url" => $meta["reply-url"], + "reply-title" => $meta["reply-title"], + "author" => $meta["author"], + "urlrel" => "/reply/".$meta["slug"], + "url" => "https://yarmo.eu/reply/".$meta["slug"], + "slug" => $meta["slug"], + "date" => $meta["date"], + "published" => $meta["published"], + "content" => $mp->text($content)); + + $date = new DateTime($item['date']); + $item['date_formatted'] = $date->format('Y-m-d'); + $item['date_rss'] = $date->format('Y-m-d'); + + if (!$item['published'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) { + return; } break; @@ -214,5 +219,13 @@ function scandirRec($path, $category, $params) { break; } + if (isset($params['slug']) && $item['slug'] != $params['slug']) { + return; + } + if (isset($params['slug'])) { + $item["webmentions"] = getWebmentions($item["url"]); + $item["hasWebmentions"] = count($item["webmentions"]) > 0; + } + return $item; } diff --git a/index.php b/index.php index 67e7c9e..3534fca 100644 --- a/index.php +++ b/index.php @@ -23,6 +23,7 @@ $router->map('GET', '/blog/[*:slug]', function() {}, 'blog_post'); $router->map('GET', '/notes', function() {}, 'notes'); $router->map('GET', '/notes/[*:slug]', function() {}, 'notes_post'); $router->map('GET', '/post/[*:slug]', function() {}, 'post'); +$router->map('GET', '/reply/[*:slug]', function() {}, 'reply'); $router->map('GET', '/projects', function() {}, 'projects'); $router->map('GET', '/projects/[*:slug]', function() {}, 'projects_details'); $router->map('GET', '/foss', function() {}, 'foss'); @@ -127,7 +128,6 @@ if (($match['name'] == 'blog_post') || ($match['name'] == 'notes_post')) { // If we are dealing with a post if ($match['name'] == 'post') { - $match['params']['slug'] = $match['params']['slug']; $variables['post'] = getBlogPosts($match['params']); if (count($variables['post']) == 0) { $variables['post'] = getNotes($match['params']); @@ -140,6 +140,13 @@ if ($match['name'] == 'post') { } } +// If we are dealing with a reply +if ($match['name'] == 'reply') { + $variables['reply'] = getReplies($match['params']); + $variables['reply'] = $variables['reply'][0]; + $variables['title'] = htmlspecialchars_decode(str_replace('·','·',$variables['reply']['title'])).' — '.$basetitle; +} + // If we are dealing with the projects if ($match['name'] == 'projects') { $variables['projects'] = getProjects($match['params']); @@ -148,7 +155,6 @@ if ($match['name'] == 'projects') { // If we are dealing with a project's details if ($match['name'] == 'projects_details') { - $match['params']['slug'] = $match['params']['slug']; $variables['project'] = getProjects($match['params']); $variables['project'] = $variables['project'][0]; $variables['title'] = htmlspecialchars_decode(str_replace('·','·',$variables['project']['title'])).' — '.$basetitle; @@ -247,6 +253,10 @@ if ($environment === 'production') { \Phug\Optimizer::call('displayFile', ['post', $variables], $options); break; + case 'reply': + \Phug\Optimizer::call('displayFile', ['reply', $variables], $options); + break; + case 'projects': \Phug\Optimizer::call('displayFile', ['projects', $variables], $options); break; @@ -343,6 +353,10 @@ if(is_array($match) && is_callable($match['target'])) { Phug::displayFile('post', $variables, $options); break; + case 'reply': + Phug::displayFile('reply', $variables, $options); + break; + case 'projects': Phug::displayFile('projects', $variables, $options); break; diff --git a/views/reply.pug b/views/reply.pug new file mode 100644 index 0000000..fddabed --- /dev/null +++ b/views/reply.pug @@ -0,0 +1,23 @@ +extends layout + +block content + header + nav + a(href="/about") about me + | > !{$reply['slug']} + + main + article.longform.h-entry + h1.p-name !{$reply['title']} + p.longform__header + | Posted on + a(href="{$reply['url']}" datetime="{$reply['date']}").u-url.dt-published !{$reply['date_formatted']} + | by + a(href="/" rel="author").p-author.h-card !{$reply['author']} + + .longform__content.e-content + | Reply to + a(href="{$reply['reply-url']}").in-reply-to !{$reply['reply-title']} + | : + br + | !{$reply['content']}