Skip to content

Commit

Permalink
Merge pull request #118 from 8fold/file-metadata-in-file-class
Browse files Browse the repository at this point in the history
File metadata in file class
  • Loading branch information
joshbruce authored Nov 15, 2021
2 parents 4b9053b + ed72225 commit c3b09a3
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 136 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"symfony/console": "^5.3",
"symfony/finder": "^5.3",
"league/flysystem": "^2.3",
"nyholm/psr7-server": "^1.0"
"nyholm/psr7-server": "^1.0",
"symfony/yaml": "^5.3"
},
"scripts": {
"prod": "@production",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions src/Content/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,23 @@ public function body(): string
return $this->body;
}

public function pageTitle(): string
{
$titles = [];
$titles[] = $this->frontMatter()->title();

$file = clone $this->file();
while ($file->canGoUp()) {
$file = $file->up();

$m = Markdown::for($file, $this->fileSystem());

$titles[] = $m->frontMatter()->title();
}

$titles = array_filter($titles);
return implode(' | ', $titles);
}
// public function pageTitle(): string
// {
// $titles = [];
// $titles[] = $this->frontMatter()->title();
//
// $file = clone $this->file();
// while ($file->canGoUp()) {
// $file = $file->up();
//
// $m = Markdown::for($file, $this->fileSystem());
//
// $titles[] = $m->frontMatter()->title();
// }
//
// $titles = array_filter($titles);
// return implode(' | ', $titles);
// }

public function description(): string
{
Expand Down
50 changes: 50 additions & 0 deletions src/Documents/FullNav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace JoshBruce\Site\Documents;

use Eightfold\HTMLBuilder\Document;
use Eightfold\HTMLBuilder\Element;

use JoshBruce\Site\File;

use JoshBruce\Site\Documents\HtmlDefault;

use JoshBruce\Site\PageComponents\Navigation;

use JoshBruce\Site\Content\Markdown;

class FullNav
{
public static function create(File $file): string
{
$pageTitle = $file->pageTitle();
$html = '';
$description = '';
if ($file->isMarkdown()) {
$markdown = Markdown::for(
file: $file,
in: $file->fileSystem()
);
$html = $markdown->html();
$description = $markdown->description();
}

$html = Document::create(
$pageTitle
)->head(
...HtmlDefault::baseHead($description)
)->body(
Element::main($html),
Element::footer(
Element::p(
'Copyright © 2004–' . date('Y') . ' Joshua C. Bruce. ' .
'All rights reserved.'
)
)
)->build();

return $html;
}
}
66 changes: 32 additions & 34 deletions src/Documents/HtmlDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,50 @@ class HtmlDefault
{
public static function create(File $file): string
{
$template = '';
$pageTitle = '';
$pageTitle = $file->pageTitle();
$html = '';
$description = '';
if ($file->isMarkdown()) {
$markdown = Markdown::for(
file: $file,
in: $file->fileSystem()
);
$template = $markdown->frontMatter()->template();
$pageTitle = $markdown->pageTitle();
$html = $markdown->html();
$description = $markdown->description();
}

$html = Document::create(
$pageTitle
)->head(
...self::baseHead($description)
)->body(
Element::a('menu')->props('href #main-nav', 'id content-top'),
(str_replace('content.md', '', $file->path(false)) !== '/')
? Element::article(
$html
)->props('typeof BlogPosting', 'vocab https://schema.org/')
: Element::main(
$html
),
Element::a('top')->props('href #content-top', 'id go-to-top'),
Navigation::create('main.md', $file->fileSystem()),
Element::footer(
Element::p(
'Copyright © 2004–' . date('Y') . ' Joshua C. Bruce. ' .
'All rights reserved.'
)
)
)->build();

return $html;
}

/**
* @return Element[]
*/
public static function baseHead(string $description): array
{
return [
Element::meta()->omitEndTag()->props(
'name viewport',
'content width=device-width,initial-scale=1'
Expand Down Expand Up @@ -64,38 +90,10 @@ public static function create(File $file): string
'sizes 16x16'
),
self::cssElement()
)->body(
(strlen($template) === 0)
? Element::a('menu')->props('href #main-nav', 'id content-top')
: '',
(
strlen($template) === 0 and
str_replace('content.md', '', $file->path(false)) !== '/'
)
? Element::article(
$html
)->props('typeof BlogPosting', 'vocab https://schema.org/')
: Element::main(
$html
),
(strlen($template) === 0)
? Element::a('top')->props('href #content-top', 'id go-to-top')
: '',
(strlen($template) === 0)
? Navigation::create('main.md', $file->fileSystem())
: '',
Element::footer(
Element::p(
'Copyright © 2004–' . date('Y') . ' Joshua C. Bruce. ' .
'All rights reserved.'
)
)
)->build();

return $html;
];
}

private static function cssElement(): Element
public static function cssElement(): Element
{
$cssPath = '/assets/css/main.min.css';
// $filePath = $contentRoot . $cssPath;
Expand Down
131 changes: 96 additions & 35 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,17 @@ private function frontMatter(): array
return [];
}

$parts = explode('---', $contents);
$metadata = Yaml::parse($parts[1]);
$this->frontMatter = $metadata;
}
return $this->frontMatter;
}

public function isNotMarkdown(): bool
{
return ! $this->isMarkdown();
}

public function isMarkdown(): bool
{
$parts = explode('/', $this->localPath);
$possibleFileName = array_pop($parts);
return str_ends_with($possibleFileName, '.md');
}
$parts = explode('---', $contents);
if (count($parts) === 1) {
$this->frontMatter = [];

public function isHtml(): bool
{
$parts = explode('/', $this->localPath);
$possibleFileName = array_pop($parts);
return str_ends_with($possibleFileName, '.html');
}

public function found(): bool
{
return file_exists($this->path()) and is_file($this->path());
}
} else {
$metadata = Yaml::parse($parts[1]);
$this->frontMatter = $metadata;

public function isNotFound(): bool
{
return ! $this->found();
}
}
return $this->frontMatter;
}

/**
Expand All @@ -123,12 +100,33 @@ public function path(bool $full = true): string
);
}

public function canGoUp(): bool
public function title(): string
{
return $this->path(false) !== $this->contentFileName;
if (array_key_exists('title', $this->frontMatter())) {
$frontMatter = $this->frontMatter();
return $frontMatter['title'];
}
return '';
}

public function pageTitle(): string
{
$titles = [];
$titles[] = $this->title();

if (! str_contains($this->path(false), '/error-')) {
$file = clone $this;
while ($file->canGoUp()) {
$file = $file->up();
$titles[] = $file->title();
}
}

$titles = array_filter($titles);
return implode(' | ', $titles);
}

public function up(): File
private function up(): File
{
$parts = explode('/', $this->localPath);
$parts = array_slice($parts, 0, -2); // remove file name and one folder.
Expand All @@ -139,6 +137,69 @@ public function up(): File
);
}

private function canGoUp(): bool
{
return $this->path(false) !== $this->contentFileName;
}






























public function isNotMarkdown(): bool
{
return ! $this->isMarkdown();
}

public function isMarkdown(): bool
{
$parts = explode('/', $this->localPath);
$possibleFileName = array_pop($parts);
return str_ends_with($possibleFileName, '.md');
}

public function isHtml(): bool
{
$parts = explode('/', $this->localPath);
$possibleFileName = array_pop($parts);
return str_ends_with($possibleFileName, '.html');
}

public function found(): bool
{
return file_exists($this->path()) and is_file($this->path());
}

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

public function contents(): string
{
if (strlen($this->contents) === 0) {
Expand Down
4 changes: 0 additions & 4 deletions src/FileSystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

use Symfony\Component\Finder\Finder;

// use SplFileInfo;

// use Symfony\Component\Finder\Finder;

interface FileSystemInterface
{
public static function projectRoot(): string;
Expand Down
Loading

0 comments on commit c3b09a3

Please sign in to comment.