Create a notion of Export format, prepare for multiple generation systems

This commit is contained in:
Stéphane Goetz 2015-05-18 14:26:29 +02:00 committed by Stéphane Goetz
parent cc4ba14dc2
commit 90dbdb4f0b
19 changed files with 246 additions and 229 deletions

View File

@ -1,6 +1,6 @@
<?php namespace Todaymade\Daux;
use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Builder;
class Daux
{
@ -9,8 +9,6 @@ class Daux
public static $VALID_MARKDOWN_EXTENSIONS;
public $local_base;
public $base_url = '';
public $host;
private $docs_path;
/**
@ -25,19 +23,6 @@ class Daux
$this->mode = $mode;
$this->local_base = dirname(__DIR__);
$this->base_url = '';
if ($this->mode == Daux::LIVE_MODE) {
$this->host = $_SERVER['HTTP_HOST'];
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$t = strrpos($this->base_url, '/index.php');
if ($t != false) {
$this->base_url = substr($this->base_url, 0, $t);
}
if (substr($this->base_url, -1) !== '/') {
$this->base_url .= '/';
}
}
}
public static function initConstants()
@ -45,10 +30,10 @@ class Daux
define("DS", DIRECTORY_SEPARATOR);
}
public function initialize($global_config_file = null, $config_file = 'config.json')
public function initialize($global_config_file = null, $override_file = 'config.json')
{
$this->loadConfig($global_config_file);
$this->loadConfigOverrides($config_file);
$this->loadConfigOverrides($override_file);
$this->generateTree();
}
@ -84,11 +69,13 @@ class Daux
private function loadConfigOverrides($config_file)
{
$config_file = $this->docs_path . DS . $config_file;
$this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true);
$config_file = $this->local_base . DS . $config_file;
if (!file_exists($config_file)) {
throw new Exception('The local config file is missing. Check path : ' . $config_file);
}
$this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true);
if (is_file($config_file)) {
$config = json_decode(file_get_contents($config_file), true);
if (!isset($config)) {
@ -96,6 +83,7 @@ class Daux
}
$this->options = array_merge($this->options, $config);
}
if (isset($this->options['timezone'])) {
date_default_timezone_set($this->options['timezone']);
} elseif (!ini_get('date.timezone')) {
@ -139,21 +127,8 @@ class Daux
}
}
if ($this->mode == self::LIVE_MODE) {
$params['index_key'] = 'index';
$params['host'] = $this->host;
$params['base_page'] = $params['base_url'] = '//' . $this->base_url;
if (!$this->options['clean_urls']) {
$params['base_page'] .= 'index.php/';
}
if ($params['image'] !== '') {
$params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
}
} else {
$params['index_key'] = 'index.html';
$params['base_page'] = $params['base_url'] = '';
}
$params['index_key'] = 'index.html';
$params['base_page'] = $params['base_url'] = '';
$params['theme'] = DauxHelper::getTheme(
$this->options['theme-name'],

View File

@ -1,5 +1,7 @@
<?php namespace Todaymade\Daux;
use Todaymade\Daux\Tree\Directory;
class DauxHelper
{
public static function getTheme($theme_name, $base_url, $local_base, $current_url)
@ -65,4 +67,44 @@ class DauxHelper
}
return $ret;
}
public static function getFile($tree, $request)
{
$request = explode('/', $request);
foreach ($request as $node) {
// If the element we're in currently is not a
// directory, we failed to find the requested file
if (!$tree instanceof Directory) {
return false;
}
// if the node exists in the current request tree,
// change the $tree variable to reference the new
// node and proceed to the next url part
if (isset($tree->value[$node])) {
$tree = $tree->value[$node];
continue;
}
// At this stage, we're in a directory, but no
// sub-item matches, so the current node must
// be an index page or we failed
if ($node !== 'index' && $node !== 'index.html') {
return false;
}
return $tree->getIndexPage();
}
// If the entry we found is not a directory, we're done
if (!$tree instanceof Directory) {
return $tree;
}
if ($tree->getIndexPage()) {
return $tree->getIndexPage();
}
return false;
}
}

View File

@ -0,0 +1,45 @@
<?php namespace Todaymade\Daux\Format\Base;
use Todaymade\Daux\Tree\Content;
abstract class MarkdownPage extends SimplePage
{
/**
* @var Content
*/
protected $file;
/**
* @var array
*/
protected $params;
public function __construct($title, $content)
{
$this->initializePage($title, $content);
}
public function setFile(Content $file)
{
$this->file = $file;
}
public function setParams(array $params)
{
$this->params = $params;
}
protected function generatePage()
{
return (new \Parsedown())->text($this->content);
}
public static function fromFile(Content $file, $params)
{
$page = new static($file->title, file_get_contents($file->getPath()));
$page->setFile($file);
$page->setParams($params);
return $page;
}
}

View File

@ -0,0 +1,6 @@
<?php namespace Todaymade\Daux\Format\Base;
interface Page
{
public function getContent();
}

View File

@ -0,0 +1,23 @@
<?php namespace Todaymade\Daux\Format\Base;
use Todaymade\Daux\Exception;
abstract class RawPage implements Page
{
protected $file;
public function __construct($filename)
{
$this->file = $filename;
}
public function getFile()
{
return $this->file;
}
public function getContent()
{
throw new Exception("you should not use this method to show a raw content");
}
}

View File

@ -0,0 +1,33 @@
<?php namespace Todaymade\Daux\Format\Base;
abstract class SimplePage implements Page
{
protected $title;
protected $content;
protected $generated = null;
public function __construct($title, $content)
{
$this->initializePage($title, $content);
}
public function getContent()
{
if (is_null($this->generated)) {
$this->generated = $this->generatePage();
}
return $this->generated;
}
protected function initializePage($title, $content)
{
$this->title = $title;
$this->content = $content;
}
protected function generatePage()
{
return $this->content;
}
}

View File

@ -1,48 +1,28 @@
<?php namespace Todaymade\Daux;
<?php namespace Todaymade\Daux\Format\HTML;
use Todaymade\Daux\Tree\Content;
class MarkdownPage extends SimplePage
class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage
{
private $file;
private $params;
private $language;
private $homepage;
public function __construct()
private function initialize()
{
}
// For Future Expansion
public static function fromFile($file, $params)
{
$instance = new self();
$instance->initialize($file, $params);
return $instance;
}
private function initialize(Content $file, $params)
{
$this->file = $file;
$this->params = $params;
$this->title = $file->title;
$this->homepage = false;
if ($this->title === 'index') {
$minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1;
if (count($file->getParents()) >= $minimum_parent_dir_size) {
$parents = $file->getParents();
$minimum_parent_dir_size = ($this->params['multilanguage']) ? 2 : 1;
if (count($this->file->getParents()) >= $minimum_parent_dir_size) {
$parents = $this->file->getParents();
$this->title = end($parents)->getTitle();
} else {
$this->homepage = ($this->file->getName() === '_index');
$this->title = $params['title'];
$this->title = $this->params['title'];
}
}
$this->language = '';
if ($params['multilanguage'] && count($file->getParents())) {
reset($file->getParents());
$language_dir = current($file->getParents());
if ($this->params['multilanguage'] && count($this->file->getParents())) {
reset($this->file->getParents());
$language_dir = current($this->file->getParents());
$this->language = $language_dir->name;
}
}
@ -61,18 +41,9 @@ class MarkdownPage extends SimplePage
return $breadcrumb_trail;
}
public function getContent()
{
if (is_null($this->html)) {
$this->content = file_get_contents($this->file->getPath());
$this->html = $this->generatePage();
}
return $this->html;
}
private function generatePage()
protected function generatePage()
{
$this->initialize();
$params = $this->params;
$entry_page = [];

View File

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

View File

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

View File

@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux;
<?php namespace Todaymade\Daux\Format\HTML;
use League\Plates\Engine;
use Todaymade\Daux\Daux;
class Template
{

View File

@ -2,7 +2,7 @@
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\Format\HTML\MarkdownPage;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Content;

View File

@ -2,7 +2,6 @@
class Helper
{
public static function copyAssets($path, $local_base)
{
@mkdir($path);

View File

@ -1,7 +0,0 @@
<?php namespace Todaymade\Daux;
interface Page
{
public function getContent();
public function display();
}

View File

@ -1,32 +0,0 @@
<?php namespace Todaymade\Daux;
use Todaymade\Daux\Server\MimeType;
class RawPage implements Page
{
private $file;
public function __construct($filename)
{
$this->file = $filename;
}
public function getContent()
{
throw new Exception("you should not use this method to show a raw content");
}
public function display()
{
header('Content-type: ' . MimeType::get($this->file));
// Transfer file in 1024 byte chunks to save memory usage.
if ($fd = fopen($this->file, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
}
}

View File

@ -1,7 +1,7 @@
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\SimplePage;
use Todaymade\Daux\Template;
use Todaymade\Daux\Format\HTML\SimplePage;
use Todaymade\Daux\Format\HTML\Template;
class ErrorPage extends SimplePage
{
@ -17,22 +17,7 @@ class ErrorPage extends SimplePage
$this->params = $params;
}
public function display()
{
http_response_code(404);
parent::display();
}
public function getContent()
{
if (is_null($this->html)) {
$this->html = $this->generatePage();
}
return $this->html;
}
private function generatePage()
protected function generatePage()
{
$params = $this->params;
$page['title'] = $this->title;

View File

@ -1,18 +1,20 @@
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception;
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\RawPage;
use Todaymade\Daux\SimplePage;
use Todaymade\Daux\Format\HTML\MarkdownPage;
use Todaymade\Daux\Format\HTML\RawPage;
use Todaymade\Daux\Format\HTML\SimplePage;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Raw;
class Server
{
private $daux;
private $params;
private $host;
private $base_url;
public static function serve()
{
@ -24,20 +26,64 @@ class Server
$page = $server->handle($_REQUEST);
} catch (NotFoundException $e) {
http_response_code(404);
$page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams());
}
$page->display();
if ($page instanceof RawPage) {
header('Content-type: ' . MimeType::get($page->getFile()));
// Transfer file in 1024 byte chunks to save memory usage.
if ($fd = fopen($page->getFile(), 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
return;
}
header('Content-type: text/html; charset=utf-8');
echo $page->getContent();
}
public function __construct(Daux $daux)
{
$this->daux = $daux;
$this->host = $_SERVER['HTTP_HOST'];
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$t = strrpos($this->base_url, '/index.php');
if ($t != false) {
$this->base_url = substr($this->base_url, 0, $t);
}
if (substr($this->base_url, -1) !== '/') {
$this->base_url .= '/';
}
}
public function getParams()
{
$params = $this->daux->getParams();
$params['index_key'] = 'index';
$params['host'] = $this->host;
$params['base_page'] = $params['base_url'] = '//' . $this->base_url;
if (!$this->daux->options['clean_urls']) {
$params['base_page'] .= 'index.php/';
}
if ($params['image'] !== '') {
$params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
}
return $params;
}
public function handle($query = [])
{
$this->params = $this->daux->getParams();
$this->params = $this->getParams();
$request = $this->getRequest();
$request = urldecode($request);
@ -75,50 +121,9 @@ class Server
return new SimplePage('Success', 'Successfully Edited');
}
private function getFile($request)
{
$tree = $this->daux->tree;
$request = explode('/', $request);
foreach ($request as $node) {
// If the element we're in currently is not a
// directory, we failed to find the requested file
if (!$tree instanceof Directory) {
return false;
}
// if the node exists in the current request tree,
// change the $tree variable to reference the new
// node and proceed to the next url part
if (isset($tree->value[$node])) {
$tree = $tree->value[$node];
continue;
}
// At this stage, we're in a directory, but no
// sub-item matches, so the current node must
// be an index page or we failed
if ($node !== 'index' && $node !== 'index.html') {
return false;
}
return $tree->getIndexPage();
}
// If the entry we found is not a directory, we're done
if (!$tree instanceof Directory) {
return $tree;
}
if ($tree->getIndexPage()) {
return $tree->getIndexPage();
}
return false;
}
private function getPage($request)
{
$file = $this->getFile($request);
$file = DauxHelper::getFile($this->daux->tree, $request);
if ($file === false) {
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}

View File

@ -1,39 +0,0 @@
<?php namespace Todaymade\Daux;
class SimplePage implements Page
{
protected $title;
protected $content;
protected $html = null;
public function __construct($title, $content)
{
$this->initializePage($title, $content);
}
public function display()
{
header('Content-type: text/html; charset=utf-8');
echo $this->getContent();
}
public function getContent()
{
if (is_null($this->html)) {
$this->html = $this->generatePage();
}
return $this->html;
}
private function initializePage($title, $content)
{
$this->title = $title;
$this->content = $content;
}
private function generatePage()
{
return $this->content;
}
}

View File

@ -1,7 +1,6 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
class Builder
{

View File

@ -73,11 +73,11 @@ abstract class Entry
if ($this instanceof Directory) {
foreach ($this->value as $node) {
if ($node instanceof Content) {
if (!count($node->getParents()) && $node->title == 'index') {
//the homepage should not count as first page
continue;
}
if (!count($node->getParents()) && $node->title == 'index') {
//the homepage should not count as first page
continue;
}
$this->first_page = $node;
return $node;
}