Skip to content

Commit

Permalink
Support generating non-html files (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Jun 14, 2021
1 parent f723c48 commit e281815
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
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);
}
}

0 comments on commit e281815

Please sign in to comment.