Add computed raw pages, to create special content at any time

Cette révision appartient à :
Stéphane Goetz 2016-01-31 12:00:29 +01:00
Parent 8e065982c5
révision b1964a7c37
7 fichiers modifiés avec 178 ajouts et 14 suppressions

Voir le fichier

@ -62,6 +62,12 @@ Two helpers from the class `Todaymade\Daux\Tree\Builder` will greatly help you d
Both methods `getOrCreateDir` and `getOrCreatePage` take two parameters : `parent` and `title` Both methods `getOrCreateDir` and `getOrCreatePage` take two parameters : `parent` and `title`
The page will automatically be treated as markdown and converted like a normal page.
If you create a new ContentType, like let's say LaTeX, you would set the title `My Page.tex` it will keep the title `My Page` and use your renderer.
If the extension is not mapped to a Generator, it will simply create the file as-is without manipulation.
### Extend the Markdown Generator ### Extend the Markdown Generator
You can extend the Markdown Parser in any way wou want with this method. You can extend the Markdown Parser in any way wou want with this method.

Voir le fichier

@ -0,0 +1,18 @@
<?php namespace Todaymade\Daux\Format\Base;
use Todaymade\Daux\Tree\ComputedRaw;
abstract class ComputedRawPage implements Page
{
protected $raw;
public function __construct(ComputedRaw $content)
{
$this->raw = $content;
}
public function getContent()
{
return $this->raw->getContent();
}
}

Voir le fichier

@ -0,0 +1,6 @@
<?php namespace Todaymade\Daux\Format\HTML;
class ComputedRawPage extends \Todaymade\Daux\Format\Base\ComputedRawPage
{
}

Voir le fichier

@ -9,6 +9,7 @@ use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Base\LiveGenerator; use Todaymade\Daux\Format\Base\LiveGenerator;
use Todaymade\Daux\GeneratorHelper; use Todaymade\Daux\GeneratorHelper;
use Todaymade\Daux\Tree\ComputedRaw;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Entry; use Todaymade\Daux\Tree\Entry;
@ -94,7 +95,7 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
$output, $output,
$width, $width,
function() use ($node, $output_dir, $key, $params) { function() use ($node, $output_dir, $key, $params) {
if (!$node instanceof Content) { if ($node instanceof Raw) {
copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key); copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
return; return;
} }
@ -118,6 +119,10 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
return new RawPage($node->getPath()); return new RawPage($node->getPath());
} }
if ($node instanceof ComputedRaw) {
return new ComputedRawPage($node);
}
$params['request'] = $node->getUrl(); $params['request'] = $node->getUrl();
return ContentPage::fromFile($node, $params, $this->daux->getContentTypeHandler()->getType($node)); return ContentPage::fromFile($node, $params, $this->daux->getContentTypeHandler()->getType($node));
} }

Voir le fichier

@ -169,32 +169,35 @@ class Builder
*/ */
public static function getOrCreatePage(Directory $parent, $path) public static function getOrCreatePage(Directory $parent, $path)
{ {
$title = static::getName($path); $extension = pathinfo($path, PATHINFO_EXTENSION);
// If the file doesn't have an extension, set .md as a default // If the file doesn't have an extension, set .md as a default
if (pathinfo($path, PATHINFO_EXTENSION) == '') { if ($extension == '') {
$extension = 'md';
$path .= '.md'; $path .= '.md';
} }
$uri = $slug = DauxHelper::slug($title); $raw = !in_array($extension, $parent->getConfig()['valid_content_extensions']);
if ($parent->getConfig()['mode'] === Daux::STATIC_MODE) {
$uri = $slug . ".html"; $title = $uri = $path;
if (!$raw) {
$title = static::getName($path);
$uri = DauxHelper::slug($title);
if ($parent->getConfig()['mode'] === Daux::STATIC_MODE) {
$uri .= ".html";
}
} }
if (array_key_exists($uri, $parent->getEntries())) { if (array_key_exists($uri, $parent->getEntries())) {
return $parent->getEntries()[$uri]; return $parent->getEntries()[$uri];
} }
$page = new Content($parent, $uri); $page = $raw? new ComputedRaw($parent, $uri) : new Content($parent, $uri);
$page->setContent("-"); //set an almost empty content to avoid problems $page->setContent("-"); //set an almost empty content to avoid problems
$page->setName($path);
$page->setTitle($title);
if ($title == 'index') { if ($title == 'index' || $title == '_index') {
// TODO :: clarify the difference between 'index' and '_index'
$page->setName('_index.' . pathinfo($path, PATHINFO_EXTENSION));
$page->setTitle($parent->getTitle()); $page->setTitle($parent->getTitle());
} else {
$page->setName($path);
$page->setTitle($title);
} }
return $page; return $page;

23
libs/Tree/ComputedRaw.php Fichier normal
Voir le fichier

@ -0,0 +1,23 @@
<?php namespace Todaymade\Daux\Tree;
class ComputedRaw extends Entry
{
/** @var string */
protected $content;
/**
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}
}

Voir le fichier

@ -1,6 +1,9 @@
<?php namespace Todaymade\Daux\Tree; <?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
class BuilderTest extends \PHPUnit_Framework_TestCase class BuilderTest extends \PHPUnit_Framework_TestCase
{ {
public function providerRemoveSorting() public function providerRemoveSorting()
@ -34,4 +37,104 @@ class BuilderTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertEquals($expected, Builder::removeSortingInformations($value)); $this->assertEquals($expected, Builder::removeSortingInformations($value));
} }
public function testGetOrCreateDirNew() {
$root = new Root(new Config(), '');
$dir = Builder::getOrCreateDir($root, 'directory');
$this->assertSame($root, $dir->getParent());
$this->assertEquals('directory', $dir->getTitle());
$this->assertEquals('directory', $dir->getUri());
}
public function testGetOrCreateDirExisting() {
$root = new Root(new Config(), '');
$directory = new Directory($root, 'directory');
$directory->setTitle('directory');
$dir = Builder::getOrCreateDir($root, 'directory');
$this->assertSame($root, $dir->getParent());
$this->assertEquals('directory', $dir->getTitle());
$this->assertEquals('directory', $dir->getUri());
$this->assertSame($directory, $dir);
}
public function getStaticRoot() {
$config = new Config();
$config['mode'] = Daux::STATIC_MODE;
$config['index_key'] = 'index.html';
$config['valid_content_extensions'] = ['md'];
return new Root($config, '');
}
public function testGetOrCreatePage()
{
$directory = new Directory($this->getStaticRoot(), 'dir');
$entry = Builder::getOrCreatePage($directory, 'A Page.md');
$this->assertSame($directory, $entry->getParent());
$this->assertEquals('dir/A_Page.html', $entry->getUrl());
$this->assertEquals('A_Page.html', $entry->getUri());
$this->assertEquals('A Page', $entry->getTitle());
$this->assertInstanceOf('Todaymade\Daux\Tree\Content', $entry);
}
public function testGetOrCreatePageAutoMarkdown()
{
$directory = new Directory($this->getStaticRoot(), 'dir');
$entry = Builder::getOrCreatePage($directory, 'A Page');
$this->assertSame($directory, $entry->getParent());
$this->assertEquals('dir/A_Page.html', $entry->getUrl());
$this->assertEquals('A_Page.html', $entry->getUri());
$this->assertEquals('A Page', $entry->getTitle());
$this->assertInstanceOf('Todaymade\Daux\Tree\Content', $entry);
}
public function testGetOrCreateIndexPage()
{
$directory = new Directory($this->getStaticRoot(), 'dir');
$directory->setTitle('Tutorials');
$entry = Builder::getOrCreatePage($directory, 'index.md');
$this->assertSame($directory, $entry->getParent());
$this->assertEquals('dir/index.html', $entry->getUrl());
$this->assertEquals('Tutorials', $entry->getTitle());
$this->assertInstanceOf('Todaymade\Daux\Tree\Content', $entry);
}
public function testGetOrCreatePageExisting()
{
$directory = new Directory($this->getStaticRoot(), 'dir');
$existingEntry = new Content($directory, 'A_Page.html');
$existingEntry->setContent('-');
$entry = Builder::getOrCreatePage($directory, 'A Page.md');
$this->assertSame($directory, $entry->getParent());
$this->assertSame($existingEntry, $entry);
$this->assertEquals('dir/A_Page.html', $entry->getUrl());
$this->assertEquals('A_Page.html', $entry->getUri());
$this->assertInstanceOf('Todaymade\Daux\Tree\Content', $entry);
}
public function testGetOrCreateRawPage()
{
$directory = new Directory($this->getStaticRoot(), 'dir');
$entry = Builder::getOrCreatePage($directory, 'file.json');
$this->assertSame($directory, $entry->getParent());
$this->assertEquals('dir/file.json', $entry->getUrl());
$this->assertEquals('file.json', $entry->getUri());
$this->assertInstanceOf('Todaymade\Daux\Tree\ComputedRaw', $entry);
}
} }