Skip to content

Commit

Permalink
Merge pull request #322 from hydephp/simplify-page-parsers
Browse files Browse the repository at this point in the history
Merge page parsers into single action
  • Loading branch information
caendesilva authored Aug 3, 2022
2 parents a0ff31c + 97f4077 commit 47a9f84
Show file tree
Hide file tree
Showing 29 changed files with 307 additions and 552 deletions.
35 changes: 35 additions & 0 deletions packages/framework/src/Actions/FindsTitleForDocument.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Hyde\Framework\Contracts\ActionContract;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Parsers\DocumentationPageParser;
use Hyde\Framework\Services\DiscoveryService;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -60,7 +59,7 @@ public function generate(): static

public function generatePageObject(string $slug): object
{
$page = (new DocumentationPageParser($slug))->get();
$page = DocumentationPage::parse($slug);

return (object) [
'slug' => $page->slug,
Expand Down
120 changes: 120 additions & 0 deletions packages/framework/src/Actions/SourceFileParser.php
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;
}
}
2 changes: 2 additions & 0 deletions packages/framework/src/Concerns/HasDynamicTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* First check the front matter for a title. If one is not found,
* it searches the Markdown for a level one heading. Falls back to
* generating a title from the slug if no other title could be found.
*
* @deprecated Move to action to generate when constructing a page.
*/
trait HasDynamicTitle
{
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Contracts/AbstractMarkdownPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @test \Hyde\Framework\Testing\Feature\AbstractPageTest
*/
abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownPageContract
abstract class AbstractMarkdownPage extends AbstractPage
{
use HasDynamicTitle;

Expand Down
19 changes: 4 additions & 15 deletions packages/framework/src/Contracts/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Contracts;

use Hyde\Framework\Actions\SourceFileParser;
use Hyde\Framework\Concerns\CanBeInNavigation;
use Hyde\Framework\Concerns\HasPageMetadata;
use Hyde\Framework\Models\Route;
Expand All @@ -27,7 +28,7 @@ abstract class AbstractPage implements PageContract, CompilableContract
public static string $sourceDirectory;
public static string $outputDirectory;
public static string $fileExtension;
public static string $parserClass;

public static string $template;

/** @inheritDoc */
Expand All @@ -49,21 +50,9 @@ final public static function getFileExtension(): string
}

/** @inheritDoc */
final public static function getParserClass(): string
{
return static::$parserClass;
}

/** @inheritDoc */
public static function getParser(string $slug): PageParserContract
{
return new static::$parserClass($slug);
}

/** @inheritDoc */
public static function parse(string $slug): static
public static function parse(string $slug): PageContract
{
return (new static::$parserClass($slug))->get();
return (new SourceFileParser(static::class, $slug))->get();
}

/** @inheritDoc */
Expand Down
43 changes: 0 additions & 43 deletions packages/framework/src/Contracts/AbstractPageParser.php

This file was deleted.

15 changes: 0 additions & 15 deletions packages/framework/src/Contracts/MarkdownPageContract.php

This file was deleted.

15 changes: 1 addition & 14 deletions packages/framework/src/Contracts/PageContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ public static function getOutputDirectory(): string;
*/
public static function getFileExtension(): string;

/**
* Get the class that parses source files into page models.
*
* @return string<\Hyde\Framework\Contracts\PageParserContract>
*/
public static function getParserClass(): string;

/**
* Create and return a new PageParser instance for this model,
* with the given slug passed to the constructor.
*/
public static function getParser(string $slug): PageParserContract;

/**
* Parse a source file slug into a page model.
*
Expand All @@ -48,7 +35,7 @@ public static function getParser(string $slug): PageParserContract;
*
* @see \Hyde\Framework\Testing\Unit\PageModelParseHelperTest
*/
public static function parse(string $slug): static;
public static function parse(string $slug): PageContract;

/**
* Get an array of all the source file slugs for the model.
Expand Down
20 changes: 0 additions & 20 deletions packages/framework/src/Contracts/PageParserContract.php

This file was deleted.

22 changes: 1 addition & 21 deletions packages/framework/src/Models/Pages/BladePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Hyde\Framework\Models\Pages;

use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Contracts\PageParserContract;

/**
* A basic wrapper for the custom Blade View compiler.
*/
class BladePage extends AbstractPage implements PageParserContract
class BladePage extends AbstractPage
{
/**
* The name of the Blade View to compile.
Expand Down Expand Up @@ -41,25 +40,6 @@ public function __construct(string $view)
public static string $outputDirectory = '';

public static string $fileExtension = '.blade.php';
public static string $parserClass = self::class;

/**
* Since this model also acts as a Page parser,
* we implement the get method for compatability.
*/
public function get(): BladePage
{
return $this;
}

/**
* Since this model also acts as a Page parser,
* we implement the execute method for compatability.
*/
public function execute(): void
{
// There's nothing to do here.
}

/** @inheritDoc */
public function getBladeView(): string
Expand Down
2 changes: 0 additions & 2 deletions packages/framework/src/Models/Pages/DocumentationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Hyde\Framework\Concerns\HasTableOfContents;
use Hyde\Framework\Contracts\AbstractMarkdownPage;
use Hyde\Framework\Contracts\RouteContract;
use Hyde\Framework\Models\Parsers\DocumentationPageParser;
use Hyde\Framework\Models\Route;

class DocumentationPage extends AbstractMarkdownPage
Expand All @@ -14,7 +13,6 @@ class DocumentationPage extends AbstractMarkdownPage

public static string $sourceDirectory = '_docs';
public static string $outputDirectory = 'docs';
public static string $parserClass = DocumentationPageParser::class;
public static string $template = 'hyde::layouts/docs';

/**
Expand Down
Loading

0 comments on commit 47a9f84

Please sign in to comment.