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

Support generating non-html files #64

Merged
merged 1 commit into from
Jun 14, 2021
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
16 changes: 13 additions & 3 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function write($request)

$html = $response->getContent();

if (! $this->is404() && ! $this->files->exists($this->directory())) {
if (! $this->files->exists($this->directory())) {
$this->files->makeDirectory($this->directory(), 0755, true);
}

Expand All @@ -56,7 +56,7 @@ protected function write($request)

public function directory()
{
return $this->config['destination'] . $this->url();
return dirname($this->path());
}

public function path()
Expand All @@ -65,7 +65,17 @@ public function path()
return $this->config['destination'] . '/404.html';
}

return $this->directory() . '/index.html';
$url = $this->url();

$ext = pathinfo($url, PATHINFO_EXTENSION) ?: 'html';

$url = $this->config['destination'] . $url;

if ($ext === 'html') {
$url .= '/index.html';
}

return $url;
}

public function url()
Expand Down
52 changes: 52 additions & 0 deletions tests/PageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests;

use Illuminate\Filesystem\Filesystem;
use Mockery;
use Statamic\Contracts\Entries\Entry;
use Statamic\StaticSite\Page;

class PageTest extends TestCase
{
/** @test */
public function it_gets_the_path()
{
$entry = $this->mock(Entry::class);
$entry->shouldReceive('urlWithoutRedirect')->andReturn('/foo/bar');

$page = $this->page($entry, ['destination' => '/path/to/static']);

$this->assertEquals('/path/to/static/foo/bar/index.html', $page->path());
$this->assertEquals('/path/to/static/foo/bar', $page->directory());
}

/** @test */
public function it_gets_the_path_of_a_url_with_a_file_extension()
{
$entry = $this->mock(Entry::class);
$entry->shouldReceive('urlWithoutRedirect')->andReturn('/foo/bar/sitemap.xml');

$page = $this->page($entry, ['destination' => '/path/to/static']);

$this->assertEquals('/path/to/static/foo/bar/sitemap.xml', $page->path());
$this->assertEquals('/path/to/static/foo/bar', $page->directory());
}

/** @test */
public function it_gets_the_path_of_the_404_url()
{
$entry = $this->mock(Entry::class);
$entry->shouldReceive('urlWithoutRedirect')->andReturn('/404');

$page = $this->page($entry, ['destination' => '/path/to/static']);

$this->assertEquals('/path/to/static/404.html', $page->path());
$this->assertEquals('/path/to/static', $page->directory());
}

private function page($entry, $config)
{
return new Page($this->mock(Filesystem::class), $config, $entry);
}
}