Skip to content

Commit

Permalink
Merge pull request #48 from 8fold/working
Browse files Browse the repository at this point in the history
Update FileSystem.php
  • Loading branch information
joshbruce authored Nov 5, 2021
2 parents a2df4cf + 440a7a2 commit 242770c
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 183 deletions.
7 changes: 5 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
);

if ($fileSystem->notFound()) {
$fileSystem = $fileSystem->with('/content', '404.md');
$fileSystem = $fileSystem->with('/', '404.md');
JoshBruce\Site\Emitter::emitNotFoundResponse(
JoshBruce\Site\Content\Markdown::markdownConverter(),
$fileSystem
Expand All @@ -67,7 +67,10 @@
}

if ($server->isRequestingFile()) {
JoshBruce\Site\Emitter::emitFile($fileSystem->mimeType(), $fileSystem->filePath());
JoshBruce\Site\Emitter::emitFile(
$fileSystem->mimeType(),
$fileSystem->path()
);
exit;
}

Expand Down
13 changes: 10 additions & 3 deletions src/Content/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function convert(): string
$this->frontMatter()->hasMember('original') and
$copy->found()
) {
$copyContent = file_get_contents($copy->filePath());
$copyContent = file_get_contents($copy->path());
if (is_string($copyContent)) {
$originalLink = OriginalContentNotice::create(
copyContent: $copyContent,
messagePath: $copy->filePath(),
messagePath: $copy->path(),
originalLink: $this->frontMatter()->original()
);
}
Expand Down Expand Up @@ -101,7 +101,14 @@ public function convert(): string
public function markdown(): string
{
if (strlen($this->markdown) === 0 and $this->file->found()) {
$markdown = file_get_contents($this->file->filePath());
$fileName = 'content.md';
if (strlen($this->file->fileName()) > 0) {
$fileName = $this->file->fileName();
}

$markdown = file_get_contents(
$this->file->fileNamed($fileName)->path()
);

if (is_bool($markdown)) {
$markdown = '';
Expand Down
194 changes: 103 additions & 91 deletions src/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

use JoshBruce\Site\Content\FrontMatter;

/**
* @todo: Change contentRoot to be the folder in which the text-based content is.
*/
class FileSystem
{
public static function init(
Expand All @@ -29,41 +32,82 @@ public function __construct(
) {
}

/**
* @return string Path to where the text-based content for the site lives.
*/
public function contentRoot(): string
{
return $this->contentRoot;
}

public function with(string $folderPath, string $fileName = ''): FileSystem
{
return new self($this->contentRoot, $folderPath, $fileName);
}

public function fileNamed(string $fileName): FileSystem
{
return $this->with($this->folderPath, $fileName);
}

public function fileName(): string
{
return $this->fileName;
}

public function folderPath(bool $full = true): string
public function path(bool $full = true): string
{
if ($full) {
if (
str_contains($this->folderPath(false), 'navigation') or
str_contains($this->folderPath(false), 'messages') or
str_contains($this->folderPath(false), 'media')
) {
return $this->contentRoot . $this->folderPath(false);
}
return $this->contentRoot .
'/content' .
$this->folderPath(false);
$path = $this->contentRoot . $this->folderPath . '/' . $this->fileName();
if (! $full) {
$path = str_replace($this->contentRoot, '', $path);
}
return $this->folderPath;

if (str_ends_with($path, '/')) {
return substr($path, 0, -1);
}
return $path;
}

public function notFound(): bool
public function mimetype(): string
{
return ! $this->found();
$type = mime_content_type($this->path());
if (is_bool($type) and $type === false) {
return '';
}

if ($type === 'text/plain') {
$extensionMap = [
'md' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript'
];

$parts = explode('.', $this->path());
$extension = array_pop($parts);

$type = $extensionMap[$extension];
}
return $type;
}

public function found(): bool
public function navigation(string $file = ''): FileSystem
{
return $this->up()->with('/navigation', $file);
}

public function messages(string $file = ''): FileSystem
{
return file_exists($this->filePath());
return $this->up()->with('/messages', $file);
}

public function media(string $file = ''): FileSystem
{
return $this->up()->with('/media', $file);
}

public function assets(string $file = ''): FileSystem
{
return $this->up()->with('/assets', $file);
}

public function rootFolderIsMissing(): bool
Expand All @@ -74,79 +118,65 @@ public function rootFolderIsMissing(): bool
return ! is_dir($this->contentRoot());
}

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

public function isFile(): bool
public function found(): bool
{
return strlen($this->fileName()) > 0;
return file_exists($this->path());
}

public function isNotRoot(): bool
{
return ! $this->isRoot();
}

private function isRoot(): bool
public function isRoot(): bool
{
$subtract = str_replace(
$this->contentRoot() . '/content',
'',
$this->folderPath()
);
return strlen($subtract) === 0;
$subtract = str_replace($this->contentRoot(), '', $this->path());
return strlen($subtract) === 0 or $subtract === '/';
}

private function isNotFile(): bool
public function isFile(): bool
{
return ! $this->isFile();
return file_exists($this->path()) and ! is_dir($this->path());
}

public function filePath(): string
/**
* @return FileSystem[]
*/
public function folderStack(string $fileName = ''): array
{
$folderPath = $this->folderPath();
if (str_ends_with($folderPath, '/') or $folderPath === '/') {
$folderPath = substr($folderPath, 0, -1);
}

if ($this->isNotFile() or $this->fileName() === 'content.md') {
return $this->folderPath() . '/content.md';
$folderPath = $this->path();
if (! is_dir($folderPath)) {
return [];
}
$folderPath = str_replace($this->contentRoot(), '', $folderPath);

$path = $folderPath . '/' . $this->fileName();
return $path;
}
$folderPathParts = explode('/', $folderPath);

public function mimetype(): string
{
$type = mime_content_type($this->filePath());
if (is_bool($type) and $type === false) {
return '';
}
$folders = [];
while (count($folderPathParts) > 0) {
$path = implode('/', $folderPathParts);

if ($type === 'text/plain') {
$extensionMap = [
'md' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript'
];
$clone = clone $this;
$clone = $clone->with(folderPath: $path, fileName: $fileName);

$parts = explode('.', $this->filePath());
$extension = array_pop($parts);
$folders[] = $clone;

$type = $extensionMap[$extension];
array_pop($folderPathParts);
}
return $type;
return $folders;
}

/**
* @return array<string, FileSystem>
*/
public function subfolders(string $fileName = ''): array
{
$folderPath = $this->folderPath();
$folderPath = $this->path();
if (! is_dir($folderPath)) {
return [];
}
Expand All @@ -157,46 +187,28 @@ public function subfolders(string $fileName = ''): array
continue;
}

$path = str_replace(
$this->contentRoot() . '/content',
$fullPathToFolder = $folder->getPathname();
$partialPath = str_replace(
$this->contentRoot(),
'',
$folder->getPathname()
$fullPathToFolder
);

$folderName = array_slice(explode('/', $path), -1); // up 1
$folderName = array_shift($folderName);
if ($folderName !== null) {
$clone = clone $this;
$content[$folderName] = $clone->with($path, $fileName);
}
$parts = explode('/', $partialPath);

$folderName = array_pop($parts);

$clone = clone $this;
$content[$folderName] = $clone->with($partialPath, $fileName);
}
return $content;
}

/**
* @return FileSystem[]
*/
public function folderStack(string $fileName = ''): array
private function up(): FileSystem
{
$folderPath = $this->folderPath();
if (! is_dir($folderPath)) {
return [];
}
$folderPath = str_replace($this->contentRoot() . '/content', '', $folderPath);

$folderPathParts = explode('/', $folderPath);

$folders = [];
while (count($folderPathParts) > 0) {
$path = implode('/', $folderPathParts);

$clone = clone $this;
$clone = $clone->with(folderPath: $path, fileName: $fileName);

$folders[] = $clone;

array_pop($folderPathParts);
}
return $folders;
$parts = explode('/', $this->contentRoot);
array_pop($parts);
$newRoot = implode('/', $parts);
return FileSystem::init($newRoot);
}
}
14 changes: 10 additions & 4 deletions src/PageComponents/LogList.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ public static function create(

krsort($fileSubfolders);
$logLinks = [];
foreach ($fileSubfolders as $key => $f) {
if (! str_starts_with(strval($key), '_') and $f->found()) {
$markdown = Markdown::init($f);
foreach ($fileSubfolders as $key => $file) {
if (! str_starts_with(strval($key), '_') and $file->found()) {
$markdown = Markdown::init($file);

$linkPath = str_replace(
'/content.md',
'',
$file->path(full: false)
);

$logLinks[] = HtmlElement::li(
HtmlElement::a(
$markdown->frontMatter()->title() // ['title']
)->props('href ' . $f->folderPath(full: false))
)->props('href ' . $linkPath)
);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/PageComponents/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ private function anchor(string $for): HtmlElement
*/
private function navigation(): array
{
$file = FileSystem::init($this->contentRoot)
->with(folderPath: '/navigation', fileName: 'main.md');
$file = FileSystem::init($this->contentRoot)->navigation('main.md');
// $file = $this->file->with(folderPath: '/navigation', fileName: 'main.md');
return Markdown::init(file: $file)->frontMatter()->navigation();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pages/DefaultTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function pageTitle(): string
$titles = [];
foreach ($this->folderStack as $file) {
$fileContent = $file->with(
$file->folderPath(full: false),
$file->path(full: false),
'content.md'
);
$titles[] = Markdown::init($fileContent)
Expand Down
Loading

0 comments on commit 242770c

Please sign in to comment.