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

This commit is contained in:
Stéphane Goetz
2016-01-31 12:00:29 +01:00
parent 8e065982c5
commit b1964a7c37
7 changed files with 178 additions and 14 deletions

View File

@ -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();
}
}

View File

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

View File

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

View File

@ -169,32 +169,35 @@ class Builder
*/
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 (pathinfo($path, PATHINFO_EXTENSION) == '') {
if ($extension == '') {
$extension = 'md';
$path .= '.md';
}
$uri = $slug = DauxHelper::slug($title);
if ($parent->getConfig()['mode'] === Daux::STATIC_MODE) {
$uri = $slug . ".html";
$raw = !in_array($extension, $parent->getConfig()['valid_content_extensions']);
$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())) {
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->setName($path);
$page->setTitle($title);
if ($title == 'index') {
// TODO :: clarify the difference between 'index' and '_index'
$page->setName('_index.' . pathinfo($path, PATHINFO_EXTENSION));
if ($title == 'index' || $title == '_index') {
$page->setTitle($parent->getTitle());
} else {
$page->setName($path);
$page->setTitle($title);
}
return $page;

23
libs/Tree/ComputedRaw.php Normal file
View File

@ -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;
}
}