-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from hydephp/simplify-page-parsers
Merge page parsers into single action
- Loading branch information
Showing
29 changed files
with
307 additions
and
552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Actions; | ||
|
||
use Hyde\Framework\Hyde; | ||
|
||
/** | ||
* Replaces @see \Hyde\Framework\Concerns\HasDynamicTitle. | ||
*/ | ||
class FindsTitleForDocument | ||
{ | ||
public static function get(string $slug = '', array $matter = [], string $markdown = ''): string | ||
{ | ||
if (isset($matter['title'])) { | ||
return $matter['title']; | ||
} | ||
|
||
return static::findTitleTagInMarkdown($markdown) | ||
?: Hyde::makeTitle($slug); | ||
} | ||
|
||
/** Attempt to find the title based on the first H1 tag. */ | ||
protected static function findTitleTagInMarkdown(string $markdown): string|false | ||
{ | ||
$lines = explode("\n", $markdown); | ||
|
||
foreach ($lines as $line) { | ||
if (str_starts_with($line, '# ')) { | ||
return trim(substr($line, 2), ' '); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Actions; | ||
|
||
use Hyde\Framework\Concerns\ValidatesExistence; | ||
use Hyde\Framework\Contracts\PageContract; | ||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Models\Pages\BladePage; | ||
use Hyde\Framework\Models\Pages\DocumentationPage; | ||
use Hyde\Framework\Models\Pages\MarkdownPage; | ||
use Hyde\Framework\Models\Pages\MarkdownPost; | ||
use Hyde\Framework\Modules\Markdown\MarkdownFileParser; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* Parses a source file and returns a new page model instance for it. | ||
* | ||
* Page Parsers are responsible for parsing a source file into a Page object, | ||
* and may also conduct pre-processing and/or data validation/assembly. | ||
* | ||
* Note that the Page Parsers do not compile any HTML or Markdown. | ||
* | ||
* @see \Hyde\Framework\Testing\Feature\SourceFileParserTest | ||
*/ | ||
class SourceFileParser | ||
{ | ||
use ValidatesExistence; | ||
|
||
protected string $slug; | ||
protected PageContract $page; | ||
|
||
public function __construct(string $pageClass, string $slug) | ||
{ | ||
$this->validateExistence($pageClass, $slug); | ||
|
||
$this->slug = $slug; | ||
|
||
$this->page = match ($pageClass) { | ||
BladePage::class => $this->parseBladePage(), | ||
MarkdownPage::class => $this->parseMarkdownPage(), | ||
MarkdownPost::class => $this->parseMarkdownPost(), | ||
DocumentationPage::class => $this->parseDocumentationPage(), | ||
}; | ||
} | ||
|
||
protected function parseBladePage(): BladePage | ||
{ | ||
return new BladePage($this->slug); | ||
} | ||
|
||
protected function parseMarkdownPage(): MarkdownPage | ||
{ | ||
$document = (new MarkdownFileParser( | ||
Hyde::getMarkdownPagePath($this->slug.MarkdownPage::$fileExtension) | ||
))->get(); | ||
|
||
$matter = $document->matter; | ||
$body = $document->body; | ||
|
||
return new MarkdownPage( | ||
matter: $matter, | ||
body: $body, | ||
title: FindsTitleForDocument::get($this->slug, $matter, $body), | ||
slug: $this->slug | ||
); | ||
} | ||
|
||
protected function parseMarkdownPost(): MarkdownPost | ||
{ | ||
$document = (new MarkdownFileParser( | ||
Hyde::getMarkdownPostPath($this->slug.MarkdownPost::$fileExtension) | ||
))->get(); | ||
|
||
$matter = $document->matter; | ||
$body = $document->body; | ||
|
||
return new MarkdownPost( | ||
matter: $matter, | ||
body: $body, | ||
title: FindsTitleForDocument::get($this->slug, $matter, $body), | ||
slug: $this->slug | ||
); | ||
} | ||
|
||
protected function parseDocumentationPage(): DocumentationPage | ||
{ | ||
$document = (new MarkdownFileParser( | ||
Hyde::getDocumentationPagePath($this->slug.DocumentationPage::$fileExtension) | ||
))->get(); | ||
|
||
$matter = array_merge($document->matter, [ | ||
'slug' => $this->slug, | ||
]); | ||
|
||
$body = $document->body; | ||
|
||
return new DocumentationPage( | ||
matter: $matter, | ||
body: $body, | ||
title: FindsTitleForDocument::get($this->slug, $matter, $body), | ||
slug: basename($this->slug), | ||
category: $this->getDocumentationPageCategory($matter), | ||
localPath: $this->slug | ||
); | ||
} | ||
|
||
protected function getDocumentationPageCategory(array $matter): ?string | ||
{ | ||
if (str_contains($this->slug, '/')) { | ||
return Str::before($this->slug, '/'); | ||
} | ||
|
||
return $matter['category'] ?? null; | ||
} | ||
|
||
public function get(): PageContract | ||
{ | ||
return $this->page; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.