Skip to content

Commit

Permalink
Merge pull request #10 from 8fold/working
Browse files Browse the repository at this point in the history
Clean up and incorporate other page components
  • Loading branch information
joshbruce authored Oct 29, 2021
2 parents 523d0b8 + 2c96f1b commit da0efc2
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 233 deletions.
60 changes: 57 additions & 3 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');

Expand Down Expand Up @@ -85,22 +87,74 @@
* Process HTML response: local response time 75ms (90ms with table content)
*/

$markdownConverter = $markdownConverter->tables();
$markdownConverter = $markdownConverter->withConfig(['html_input' => 'allow'])
->tables()->externalLinks();

$headers['Content-Type'] = $content->mimeType();

$headElements = JoshBruce\Site\PageComponents\Favicons::create();
$headElements[] = Eightfold\HTMLBuilder\Element::link()
->props('rel stylesheet', 'href /css/main.css');

$markdown = $content->markdown();

$frontMatter = $markdownConverter->getFrontMatter($content->markdown());

$updated = '';
if (array_key_exists('updated', $frontMatter)) {
$updated = Eightfold\HTMLBuilder\Element::p(
"Updated on: {$frontMatter['updated']}"
);
}

$dateBlock = Eightfold\HTMLBuilder\Element::div(
Eightfold\HTMLBuilder\Element::p("Created on: {$frontMatter['created']}"),
$updated
)->props('is dateblock');

$body = $markdownConverter->getBody($markdown);

$body = $dateBlock . $body;

if (array_key_exists('header', $frontMatter)) {
$body = "# {$frontMatter['header']}\n\n" . $body;

} else {
$body = "# {$frontMatter['title']}\n\n" . $body;

}

if (array_key_exists('type', $frontMatter) and $frontMatter['type'] === 'log') {
$contents = $content->contentInSubfolders();
krsort($contents);
$logLinks = [];
foreach ($contents as $key => $c) {
if (! str_starts_with(strval($key), '_') and $c->exists()) {
$logLinks[] = Eightfold\HTMLBuilder\Element::li(
Eightfold\HTMLBuilder\Element::a(
$c->frontMatter()['title']
)->props('href ' . $c->pathWithoutFile())
);
}
}
$list = Eightfold\HTMLBuilder\Element::ul(...$logLinks)->build();
$body = $body . $list;
}

$body = Eightfold\HTMLBuilder\Document::create(
$markdownConverter->getFrontMatter($content->markdown())['title']
$frontMatter['title']
)->head(
...$headElements
)->body(
JoshBruce\Site\PageComponents\Navigation::create($content)
->build(),
$markdownConverter->convert($content->markdown())
$markdownConverter->convert($body),
Eightfold\HTMLBuilder\Element::footer(
Eightfold\HTMLBuilder\Element::p(
'Copyright © 2004–' . date('Y') . 'Joshua C. Bruce. ' .
'All rights reserved.'
)
)
)->build();

JoshBruce\Site\Emitter::emitWithResponse(200, $headers, $body);
Expand Down
50 changes: 46 additions & 4 deletions src/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace JoshBruce\Site;

use DirectoryIterator;

use Eightfold\Markdown\Markdown;

use JoshBruce\Site;
Expand Down Expand Up @@ -56,11 +58,24 @@ public function for(string $path): Content
return $this;
}

public function pathWithoutFile(): string
{
if (strpos($this->path, '.') === 0) {
return $this->path;
}
return implode('/', array_slice(explode('/', $this->path), 0, -1));
}

public function notFound(): bool
{
return ! $this->exists();
}

public function exists(): bool
{
return file_exists($this->filePath());
}

public function hasMoved(): bool
{
return strlen($this->redirectPath()) > 0;
Expand Down Expand Up @@ -136,14 +151,41 @@ public function redirectPath(): string
return '';
}

private function folderExists(): bool
/**
* @return array<string, Content>
*/
public function contentInSubfolders(): array
{
return file_exists($this->root()) and is_dir($this->root());
$parts = explode('/', $this->path);
$parts = array_slice($parts, 0, -1);
$dirPath = implode('/', $parts);

$folderPath = $this->root() . $dirPath;

if (! is_dir($folderPath)) {
return [];
}

$content = [];
foreach (new DirectoryIterator($folderPath) as $folder) {
if ($folder->isFile() or $folder->isDot()) {
// I feel continue should be named next or something.
continue;
}
$path = str_replace($this->root(), '', $folder->getPathname());
$date = array_slice(explode('/', $path), -1);
$date = array_shift($date);
if ($date !== null) {
$clone = clone $this;
$content[$date] = $clone->for($path . '/content.md');
}
}
return $content;
}

private function exists(): bool
private function folderExists(): bool
{
return file_exists($this->filePath());
return file_exists($this->root()) and is_dir($this->root());
}

private function root(): string
Expand Down
6 changes: 5 additions & 1 deletion src/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ public static function emitRedirectionResponse(string $location): void
self::emitWithResponse(
301,
[
'Location' => $location
'Location' => $location,
'Cache-Control' => [
'no-cache',
'must-revalidate'
]
]
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/PageComponents/Favicons.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JoshBruce\Site\PageComponents;

use Eightfold\HTMLBuilder\Element as HtmlElement;
Expand Down
4 changes: 3 additions & 1 deletion src/PageComponents/Navigation.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JoshBruce\Site\PageComponents;

use Stringable;
Expand Down Expand Up @@ -79,7 +81,7 @@ public function build(): string
}
return HtmlElement::nav(
HtmlElement::ul(...$li)
);
)->build();
}

public function __toString(): string
Expand Down
1 change: 0 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function isMissingRequiredValues(): bool
'REQUEST_URI'
];

$hasRequired = true;
foreach ($required as $key) {
if (! array_key_exists($key, $this->serverGlobals)) {
return true;
Expand Down
84 changes: 0 additions & 84 deletions tests/App_.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@
'text/html'
);

expect($this->baseContent->folderIsMissing())->toBeFalse();
})->group('content');
50 changes: 0 additions & 50 deletions tests/Environment_.php

This file was deleted.

Loading

0 comments on commit da0efc2

Please sign in to comment.