Make reading blog, note and project files recursively
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Yarmo Mackenbach 2020-06-23 17:41:05 +02:00
parent 322b77940a
commit 255675bbf2

View File

@ -21,156 +21,19 @@ function getIcons() {
}
function getBlogPosts($params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$posts = array();
$files = scandir('content/blog/');
foreach($files as $file) {
// Skip . and ..
if (($file == '.') || ($file == '..')) {
continue;
}
$content = file_get_contents('content/blog/' . $file);
$meta = $mp->meta($content);
$id = intval(substr($file, 0, -3));
$post = array(
"type" => "blog",
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/post/".$meta["slug"],
"url" => "https://yarmo.eu/post/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"discussion" => $meta["discussion"],
"content" => $mp->text($content));
$date = new DateTime($post['date']);
$post['date_formatted'] = $date->format('Y-m-d');
$post['date_rss'] = $date->format('Y-m-d');
if (!$post['published'] && !(isset($params['slug']) && $post['slug'] == $params['slug'])) {
continue;
}
if (isset($params['slug']) && $post['slug'] != $params['slug']) {
continue;
}
if (isset($params['slug'])) {
$post["webmentions"] = getWebmentions($post["url"]);
$post["hasWebmentions"] = count($post["webmentions"]) > 0;
}
$posts[] = $post;
}
$posts = scandirRec('content/blog/', 'blog', $params);
$posts = array_reverse($posts);
return $posts;
}
function getNotes($params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$posts = array();
$files = scandir('content/notes/');
foreach($files as $file) {
// Skip . and ..
if (($file == '.') || ($file == '..')) {
continue;
}
$content = file_get_contents('content/notes/' . $file);
$meta = $mp->meta($content);
$id = intval(substr($file, 0, -3));
$post = array(
"type" => "note",
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/post/".$meta["slug"],
"url" => "https://yarmo.eu/post/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"content" => $mp->text($content));
$date = new DateTime($post['date']);
$post['date_formatted'] = $date->format('Y-m-d');
$post['date_rss'] = $date->format('Y-m-d');
if (!$post['published'] && !(isset($params['slug']) && $post['slug'] == $params['slug'])) {
continue;
}
if (isset($params['slug']) && $post['slug'] != $params['slug']) {
continue;
}
if (isset($params['slug'])) {
$post["webmentions"] = getWebmentions($post["url"]);
$post["hasWebmentions"] = count($post["webmentions"]) > 0;
}
$posts[] = $post;
}
$posts = scandirRec('content/notes/', 'notes', $params);
$posts = array_reverse($posts);
return $posts;
}
function getProjects($params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$posts = array();
$files = scandir('content/projects/');
foreach($files as $file) {
// Skip . and ..
if (($file == '.') || ($file == '..')) {
continue;
}
$content = file_get_contents('content/projects/' . $file);
$meta = $mp->meta($content);
$id = intval(substr($file, 0, -3));
$project = array(
"title" => $meta["title"],
"status" => $meta["status"],
"urlrel" => "/projects/".$meta["slug"],
"url" => "https://yarmo.eu/projects/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"listed" => $meta["listed"],
"content" => $mp->text($content));
$date = new DateTime($project['date']);
$project['date_formatted'] = $date->format('Y-m-d');
$project['date_rss'] = $date->format('Y-m-d');
if (!$project['listed'] && !(isset($params['slug']) && $project['slug'] == $params['slug'])) {
continue;
}
if (isset($params['slug']) && $project['slug'] != $params['slug']) {
continue;
}
if (isset($params['slug'])) {
$project["webmentions"] = getWebmentions($project["url"]);
$project["hasWebmentions"] = count($project["webmentions"]) > 0;
}
$projects[] = $project;
}
$projects = scandirRec('content/projects/', 'projects', $params);
return $projects;
}
@ -228,3 +91,128 @@ function getRSS($params) {
echo $feed;
}
function scandirRec($path, $category, $params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$items = array();
// If a directory
if(is_dir($path)) {
$files = glob($path . '*', GLOB_MARK);
foreach($files as $file) {
if(is_dir($file)) {
$items = array_merge($items, scandirRec($file, $category, $params));
} else {
$return = scandirRec($file, $category, $params);
if (count($return) > 0) {
$items[] = $return;
}
}
}
return $items;
}
// If a file
if (($path == '.') || ($path == '..')) {
return;
}
$content = file_get_contents($path);
$meta = $mp->meta($content);
switch ($category) {
case 'blog':
$item = array(
"type" => "blog",
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/post/".$meta["slug"],
"url" => "https://yarmo.eu/post/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"discussion" => $meta["discussion"],
"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;
}
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':
$item = array(
"type" => "note",
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/post/".$meta["slug"],
"url" => "https://yarmo.eu/post/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"discussion" => $meta["discussion"],
"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;
}
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':
$item = array(
"title" => $meta["title"],
"status" => $meta["status"],
"urlrel" => "/projects/".$meta["slug"],
"url" => "https://yarmo.eu/projects/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"listed" => $meta["listed"],
"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['listed'] && !(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;
default:
return;
break;
}
return $item;
}