2015-04-21 17:11:43 +02:00
|
|
|
<?php namespace Todaymade\Daux;
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
use Todaymade\Daux\Tree\Content;
|
|
|
|
|
2015-04-21 17:11:43 +02:00
|
|
|
class MarkdownPage extends SimplePage
|
|
|
|
{
|
2015-04-23 00:32:30 +02:00
|
|
|
private $file;
|
|
|
|
private $params;
|
2015-04-21 17:11:43 +02:00
|
|
|
private $language;
|
|
|
|
private $homepage;
|
|
|
|
private $breadcrumb_trail;
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-04-21 17:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For Future Expansion
|
2015-04-23 00:32:30 +02:00
|
|
|
public static function fromFile($file, $params)
|
|
|
|
{
|
2015-04-21 17:11:43 +02:00
|
|
|
$instance = new self();
|
2015-04-23 00:32:30 +02:00
|
|
|
$instance->initialize($file, $params);
|
2015-04-21 17:11:43 +02:00
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
private function initialize(Content $file, $params)
|
|
|
|
{
|
|
|
|
$this->file = $file;
|
2015-04-21 17:11:43 +02:00
|
|
|
$this->params = $params;
|
2015-04-23 00:32:30 +02:00
|
|
|
$this->title = $file->title;
|
2015-04-21 17:11:43 +02:00
|
|
|
|
2015-04-23 15:47:05 +02:00
|
|
|
$this->homepage = false;
|
2015-04-21 17:11:43 +02:00
|
|
|
if ($this->title === 'index') {
|
|
|
|
$minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1;
|
2015-04-23 00:32:30 +02:00
|
|
|
if (count($file->getParents()) >= $minimum_parent_dir_size) {
|
|
|
|
$parents = $file->getParents();
|
|
|
|
$parent = end($parents);
|
|
|
|
$this->title = $parent->getTitle();
|
|
|
|
} else {
|
2015-04-23 15:47:05 +02:00
|
|
|
$this->homepage = ($this->file->getName() === '_index');
|
2015-04-23 00:32:30 +02:00
|
|
|
$this->title = $params['title'];
|
|
|
|
}
|
2015-04-21 17:11:43 +02:00
|
|
|
}
|
2015-04-26 18:24:22 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if ($params['breadcrumbs']) {
|
|
|
|
$this->breadcrumb_trail = $this->buildBreadcrumbTrail($file->getParents(), $params['multilanguage']);
|
|
|
|
}
|
2015-04-21 17:11:43 +02:00
|
|
|
$this->language = '';
|
2015-04-23 00:32:30 +02:00
|
|
|
if ($params['multilanguage'] && count($file->getParents())) {
|
|
|
|
reset($file->getParents());
|
|
|
|
$language_dir = current($file->getParents());
|
2015-04-21 17:11:43 +02:00
|
|
|
$this->language = $language_dir->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
private function buildBreadcrumbTrail($parents, $multilanguage)
|
|
|
|
{
|
|
|
|
if ($multilanguage && !empty($parents)) {
|
|
|
|
$parents = array_splice($parents, 1);
|
|
|
|
}
|
2015-04-21 17:11:43 +02:00
|
|
|
$breadcrumb_trail = array();
|
|
|
|
if (!empty($parents)) {
|
|
|
|
foreach ($parents as $node) {
|
2015-04-23 00:32:30 +02:00
|
|
|
$breadcrumb_trail[$node->getTitle()] = $node->getUrl();
|
2015-04-21 17:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $breadcrumb_trail;
|
|
|
|
}
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getContent()
|
|
|
|
{
|
2015-04-21 17:11:43 +02:00
|
|
|
if (is_null($this->html)) {
|
2015-04-23 00:32:30 +02:00
|
|
|
$this->content = file_get_contents($this->file->getPath());
|
|
|
|
$this->html = $this->generatePage();
|
2015-04-21 17:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->html;
|
|
|
|
}
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
private function generatePage()
|
|
|
|
{
|
2015-04-21 17:11:43 +02:00
|
|
|
$params = $this->params;
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$entry_page = [];
|
|
|
|
if ($params['request'] === $params['index_key']) {
|
|
|
|
if ($params['multilanguage']) {
|
|
|
|
foreach ($params['languages'] as $key => $name) {
|
2015-04-23 10:24:50 +02:00
|
|
|
$entry_page[$name] = $params['base_page'] . $params['entry_page'][$key]->getUrl();
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-26 18:24:22 +02:00
|
|
|
$entry_page['View Documentation'] = $params['base_page'] . $params['entry_page']->getUrl();
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
} elseif ($params['file_uri'] === 'index') {
|
2015-04-23 10:24:50 +02:00
|
|
|
$entry_page[$params['entry_page']->title] = $params['base_page'] . $params['entry_page']->getUrl();
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-04-21 17:11:43 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$page['entry_page'] = $entry_page;
|
2015-04-21 17:11:43 +02:00
|
|
|
$page['homepage'] = $this->homepage;
|
2015-04-23 00:32:30 +02:00
|
|
|
$page['title'] = $this->file->getTitle();
|
|
|
|
$page['filename'] = $this->file->getName();
|
2015-04-21 17:11:43 +02:00
|
|
|
if ($page['breadcrumbs'] = $params['breadcrumbs']) {
|
|
|
|
$page['breadcrumb_trail'] = $this->breadcrumb_trail;
|
|
|
|
$page['breadcrumb_separator'] = $params['breadcrumb_separator'];
|
|
|
|
}
|
|
|
|
$page['language'] = $this->language;
|
2015-04-23 00:32:30 +02:00
|
|
|
$page['path'] = $this->file->getPath();
|
|
|
|
$page['modified_time'] = filemtime($this->file->getPath());
|
2015-04-21 17:11:43 +02:00
|
|
|
$page['markdown'] = $this->content;
|
2015-04-23 15:47:05 +02:00
|
|
|
$page['request'] = $params['request'];
|
2015-04-21 17:11:43 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$Parsedown = new \Parsedown();
|
|
|
|
$page['content'] = $Parsedown->text($this->content);
|
|
|
|
|
2015-04-23 10:24:50 +02:00
|
|
|
$template = new Template($params['templates'], $params['theme']['templates']);
|
|
|
|
return $template->render($this->homepage? 'home' : 'content', ['page' => $page, 'params' => $params]);
|
2015-04-21 17:11:43 +02:00
|
|
|
}
|
|
|
|
}
|