Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for plain HTML pages #519

Merged
merged 20 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

'features' => [
// Page Modules
Features::htmlPages(),
Features::blogPosts(),
Features::bladePages(),
Features::markdownPages(),
Expand Down
1 change: 1 addition & 0 deletions packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

'features' => [
// Page Modules
Features::htmlPages(),
Features::blogPosts(),
Features::bladePages(),
Features::markdownPages(),
Expand Down
22 changes: 13 additions & 9 deletions packages/framework/src/Actions/SourceFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function __construct(string $pageClass, string $identifier)
$this->page = $this->constructPage($pageClass);
}

protected function constructPage(string $pageClass): HydePage|BladePage|BaseMarkdownPage
{
if ($pageClass === BladePage::class) {
return $this->parseBladePage();
}

if (is_subclass_of($pageClass, BaseMarkdownPage::class)) {
return $this->parseMarkdownPage($pageClass);
}

return new $pageClass($this->identifier);
}

protected function parseBladePage(): BladePage
{
return new BladePage(
Expand All @@ -59,13 +72,4 @@ public function get(): HydePage
{
return $this->page;
}

protected function constructPage(string $pageClass): BladePage|BaseMarkdownPage
{
if ($pageClass === BladePage::class) {
return $this->parseBladePage();
}

return $this->parseMarkdownPage($pageClass);
}
}
5 changes: 5 additions & 0 deletions packages/framework/src/Foundation/FileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Framework\Models\File;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\HtmlPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Services\DiscoveryService;
Expand Down Expand Up @@ -45,6 +46,10 @@ public function getMediaFiles(): self

protected function runDiscovery(): self
{
if (Features::hasHtmlPages()) {
$this->discoverFilesFor(HtmlPage::class);
}

if (Features::hasBladePages()) {
$this->discoverFilesFor(BladePage::class);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/framework/src/Foundation/PageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\HtmlPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Illuminate\Support\Collection;
Expand All @@ -32,6 +33,10 @@ public function getPages(?string $pageClass = null): self

protected function runDiscovery(): self
{
if (Features::hasHtmlPages()) {
$this->discoverPagesFor(HtmlPage::class);
}

if (Features::hasBladePages()) {
$this->discoverPagesFor(BladePage::class);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/framework/src/Helpers/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function enabled(string $feature): bool
{
return in_array($feature, config('hyde.features', [
// Page Modules
static::htmlPages(),
static::blogPosts(),
static::bladePages(),
static::markdownPages(),
Expand All @@ -54,6 +55,11 @@ public static function hasBlogPosts(): bool
return static::enabled(static::blogPosts());
}

public static function hasHtmlPages(): bool
{
return static::enabled(static::htmlPages());
}

public static function hasBladePages(): bool
{
return static::enabled(static::bladePages());
Expand Down Expand Up @@ -106,6 +112,11 @@ public static function blogPosts(): string
return 'blog-posts';
}

public static function htmlPages(): string
{
return 'html-pages';
}

public static function bladePages(): string
{
return 'blade-pages';
Expand Down
22 changes: 22 additions & 0 deletions packages/framework/src/Models/Pages/HtmlPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Hyde\Framework\Models\Pages;

use Hyde\Framework\Concerns\HydePage;

class HtmlPage extends HydePage
{
public static string $sourceDirectory = '_pages';
public static string $outputDirectory = '';
public static string $fileExtension = '.html';

public function contents(): string
{
return file_get_contents($this->getSourcePath());
}

public function compile(): string
{
return $this->contents();
}
}
30 changes: 30 additions & 0 deletions packages/framework/tests/Feature/HtmlPageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Models\Pages\HtmlPage;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Models\Pages\HtmlPage
*/
class HtmlPageTest extends TestCase
{
public function testHtmlPageCanBeCompiled()
{
$this->file(HtmlPage::$sourceDirectory.'/foo.html', 'bar');

$page = new HtmlPage('foo');

$this->assertEquals('bar', $page->compile());
}

public function testCompileMethodUsesContents()
{
$this->file(HtmlPage::$sourceDirectory.'/foo.html', 'bar');

$page = new HtmlPage('foo');

$this->assertSame($page->contents(), $page->compile());
}
}
12 changes: 12 additions & 0 deletions packages/framework/tests/Feature/SourceFileParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Hyde\Framework\Actions\SourceFileParser;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\HtmlPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
Expand Down Expand Up @@ -60,6 +61,17 @@ public function test_documentation_page_parser()
$this->assertEquals('Foo Bar Baz', $page->title);
}

public function test_html_page_parser()
{
$this->file('_pages/foo.html', '<h1>Foo Bar</h1>');

$parser = new SourceFileParser(HtmlPage::class, 'foo');
$page = $parser->get();
$this->assertInstanceOf(HtmlPage::class, $page);
$this->assertEquals('foo', $page->identifier);
$this->assertEquals('<h1>Foo Bar</h1>', $page->contents());
}

public function test_parsed_page_is_run_through_dynamic_constructor()
{
$this->markdown('_pages/foo.md', '# Foo Bar', ['title' => 'Foo Bar Baz']);
Expand Down
29 changes: 29 additions & 0 deletions packages/framework/tests/Feature/StaticPageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Framework\HydeServiceProvider;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\HtmlPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
Expand Down Expand Up @@ -95,6 +96,34 @@ public function test_can_build_documentation_page()
$this->validateBasicHtml(file_get_contents(Hyde::path('_site/'.'docs/foo.html')));
}

public function test_can_build_html_page()
{
$this->file('_pages/foo.html', 'bar');
$page = new HtmlPage('foo');

new StaticPageBuilder($page, true);

$this->assertFileExists(Hyde::path('_site/foo.html'));
$this->assertStringEqualsFile(Hyde::path('_site/foo.html'), 'bar');
unlink(Hyde::path('_site/foo.html'));
}

public function test_can_build_nested_html_page()
{
mkdir(Hyde::path('_pages/foo'));
file_put_contents(Hyde::path('_pages/foo/bar.html'), 'baz');
$page = new HtmlPage('foo/bar');

new StaticPageBuilder($page, true);

$this->assertFileExists(Hyde::path('_site/foo/bar.html'));
$this->assertStringEqualsFile(Hyde::path('_site/foo/bar.html'), 'baz');

unlink(Hyde::path('_site/foo/bar.html'));
unlink(Hyde::path('_pages/foo/bar.html'));
rmdir(Hyde::path('_pages/foo'));
}

public function test_creates_custom_documentation_directory()
{
$page = DocumentationPage::make('foo');
Expand Down
24 changes: 24 additions & 0 deletions packages/framework/tests/Feature/StaticSiteServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ public function test_build_command_transfers_media_asset_files()
unlink(Hyde::path('_site/media/test-image.png'));
}

public function test_all_page_types_can_be_compiled()
{
$this->file('_pages/html.html');
$this->file('_pages/blade.blade.php');
$this->file('_pages/markdown.md');
$this->file('_posts/post.md');
$this->file('_docs/docs.md');

$this->artisan('build')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('_site/html.html'));
$this->assertFileExists(Hyde::path('_site/blade.html'));
$this->assertFileExists(Hyde::path('_site/markdown.html'));
$this->assertFileExists(Hyde::path('_site/posts/post.html'));
$this->assertFileExists(Hyde::path('_site/docs/docs.html'));

unlink(Hyde::path('_site/html.html'));
unlink(Hyde::path('_site/blade.html'));
unlink(Hyde::path('_site/markdown.html'));
unlink(Hyde::path('_site/posts/post.html'));
unlink(Hyde::path('_site/docs/docs.html'));
}

public function test_print_initial_information_allows_api_to_be_disabled()
{
$this->artisan('build --no-api')
Expand Down