Add summary element, useful for e.g. blog post lists.

This commit is contained in:
Jacob 2024-05-24 22:35:25 +02:00
parent 16fec9b8a3
commit 26ee35f795
3 changed files with 150 additions and 1 deletions

133
core.php
View file

@ -13,6 +13,7 @@ class parsedMarkdown {
public $date = '';
public $data = '';
public $authors = array();
public $tags = array();
public $allowComments = false;
public $displayTitle = false;
public $displayDate = false;
@ -223,7 +224,10 @@ function convertMarkdownToHTML($contents) {
'/.*@csgen\.favicon.*=.*"(.*)(");/',
'/.*@csgen\.license.*=.*"(.*)(");/',
'/.*@csgen\.date.*=.*"(.*)(");/',
'/.*@csgen\.author.*=.*"(.*)(");/',
'/.*@csgen\.tags.*=.*"(.*)(");/',
'/.*@csgen\.addAuthor.*=.*"(.*)(");/',
'/.*@csgen\.addSummary.*=.*"(.*)(");/',
'/.*@csgen\.allowComments.*=.*"(.*)(");/',
'/.*@csgen\.displayTitle.*=.*"(.*)(");/',
'/.*@csgen\.displayDate.*=.*"(.*)(");/',
@ -248,8 +252,10 @@ function convertMarkdownToHTML($contents) {
);
$out = $parser->transform($contents);
$maxit = 1000;
while (preg_match('/.*@csgen.*;/', $out)) {
while ((preg_match('/.*@csgen.*;/', $out)) && $maxit > 0) {
$maxit--;
foreach ($specialSyntax as $pattern) {
$matches = array();
@ -319,6 +325,131 @@ function convertMarkdownToHTML($contents) {
$ret->authors[] = $matches[1];
$out = str_replace($matches[0], '', $out);
break;
case '/.*@csgen\.addSummary.*=.*"(.*)(");/':
$page = $matches[1];
$Database = createTables($sqlDB);
$DatabaseQuery = $Database->query('SELECT * FROM pages');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['endpoint'] != $page) {
continue;
}
$converted = convertMarkdownToHTML(file_get_contents($line['file']));
$title = $converted->title;
$description = $converted->description;
$author_a = $converted->authors;
$authors = "";
foreach ($author_a as $i => $it) {
$authors .= $it;
if (count($author_a) != $i + 1) {
$authors .= ", ";
}
}
$tags_a = $converted->tags;
$tags = "";
foreach ($tags_a as $i => $it) {
$tags .= $it;
if (count($tags_a) != $i + 1) {
$tags .= ", ";
}
}
$date = $converted->date;
$license = $converted->license;
$text = "<div class=\"summary\" id=\"summary-" . $line['id'] . " style=\"cursor: pointer;\" onclick=\"location.href='" . $line['endpoint'] . "';\">\n";
if ($title != "") {
$text .= "\t<h2 id=\"summary-title-" . $line['id'] . "\">$title</h2>\n";
} else {
$text .= "\t<h2 id=\"summary-title-" . $line['id'] . "\">" . $line['endpoint'] . "</h2>\n";
}
$author_sep = "";
$date_sep = "";
$tags_sep = "";
$license_sep = "";
if ($date == "") {
$author_sep = "";
}
if ($tags == "") {
$date_sep = "";
}
if ($license == "") {
$tags_sep = "";
}
if ($date != "" && $authors == "") {
$date_sep = "";
}
if ($tags == "" && $license != "") {
$license_sep = "";
}
if ($authors == "") {
$license_sep = "";
}
if ($authors != "") {
$text .= "\t<small id=\"summary-author-" . $line['id'] . "\">by $authors$author_sep</small>\n";
}
if ($date != "") {
$text .= "\t<small id=\"summary-date-" . $line['id'] . "\">$date$date_sep</small>\n";
}
if ($tags != "") {
$text .= "\t<small id=\"summary-tags-" . $line['id'] . "\">$tags$tags_sep</small>\n";
}
if ($license != "") {
$text .= "\t<small id=\"summary-license-" . $line['id'] . "\">$license_sep$license</small>\n";
}
if ($description != "") {
$text .= "\t<p id=\"summary-description-" . $line['id'] . "\">$description</p>\n";
} else {
$text .= "\t<p id=\"summary-description-" . $line['id'] . "\">No description available.</p>\n";
}
$text .= "</div>\n";
$out = str_replace($matches[0], $text, $out);
break;
}
break;
case '/.*@csgen\.author.*=.*&quot;(.*)(&quot;);/':
$ret->authors[] = explode(',', $matches[1]);
if (count($ret->authors) == 1) {
$ret->authors = $ret->authors[0];
}
$out = str_replace($matches[0], '', $out);
break;
case '/.*@csgen\.tags.*=.*&quot;(.*)(&quot;);/':
$ret->tags[] = explode(',', $matches[1]);
if (count($ret->tags) == 1) {
$ret->tags = $ret->tags[0];
}
$out = str_replace($matches[0], '', $out);
break;
case '/.*@csgen\.includePage.*=.*&quot;(.*)(&quot;);/':
$ret->pages[] = $matches[1];

View file

@ -71,6 +71,11 @@ input[type=file]::file-selector-button {
color: #ffc0cb;
}
.summary {
background-color: #262626;
color: #f0eee4;
}
.dropdown-content a:hover {
background-color: #262626;
}
@ -223,6 +228,11 @@ footer {
color: #ff99aa;
}
.summary {
background-color: #f2f2f2;
color: #222222;
}
.dropdown-content a:hover {
background-color: #f2f2f2;
}

View file

@ -151,6 +151,14 @@ input[type=file]::file-selector-button {
border-radius: 10px;
}
.summary {
margin: 5px;
padding: 10px;
padding-top: 1px;
padding-bottom: 1px;
border-radius: 10px;
}
.content table {
width: 100%;
}