2015-04-22 18:24:10 +02:00
|
|
|
<?php namespace Todaymade\Daux\Tree;
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
class Content extends Entry
|
|
|
|
{
|
2015-11-11 00:09:47 +01:00
|
|
|
/** @var string */
|
2015-07-17 23:38:06 +02:00
|
|
|
protected $content;
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-11-11 00:09:47 +01:00
|
|
|
/** @var Content */
|
2015-10-28 00:18:29 +01:00
|
|
|
protected $previous;
|
|
|
|
|
2015-11-11 00:09:47 +01:00
|
|
|
/** @var Content */
|
2015-10-28 00:18:29 +01:00
|
|
|
protected $next;
|
|
|
|
|
2015-11-11 00:09:47 +01:00
|
|
|
/** @var array */
|
|
|
|
protected $attributes;
|
|
|
|
|
2016-04-28 23:33:26 +02:00
|
|
|
/** @var bool */
|
|
|
|
protected $manuallySetContent = false;
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent()
|
|
|
|
{
|
2016-04-28 23:33:26 +02:00
|
|
|
if (!$this->content && !$this->manuallySetContent) {
|
2015-07-17 23:38:06 +02:00
|
|
|
$this->content = file_get_contents($this->getPath());
|
|
|
|
}
|
|
|
|
|
2015-11-11 00:09:47 +01:00
|
|
|
if ($this->attributes === null) {
|
|
|
|
$this->parseAttributes();
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $content
|
|
|
|
*/
|
|
|
|
public function setContent($content)
|
|
|
|
{
|
2016-04-28 23:33:26 +02:00
|
|
|
$this->manuallySetContent = true;
|
2015-07-17 23:38:06 +02:00
|
|
|
$this->content = $content;
|
|
|
|
}
|
2015-10-28 00:18:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Content
|
|
|
|
*/
|
|
|
|
public function getPrevious()
|
|
|
|
{
|
|
|
|
return $this->previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Content $previous
|
|
|
|
*/
|
|
|
|
public function setPrevious($previous)
|
|
|
|
{
|
|
|
|
$this->previous = $previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Content
|
|
|
|
*/
|
|
|
|
public function getNext()
|
|
|
|
{
|
|
|
|
return $this->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Content $next
|
|
|
|
*/
|
|
|
|
public function setNext($next)
|
|
|
|
{
|
|
|
|
$this->next = $next;
|
|
|
|
}
|
|
|
|
|
2015-11-06 22:44:34 +01:00
|
|
|
public function isIndex()
|
|
|
|
{
|
2016-01-31 11:59:35 +01:00
|
|
|
// At some point, it was recommended that
|
|
|
|
// an index page starts with an underscore.
|
|
|
|
// This is not mandatory anymore, both with
|
|
|
|
// and without underscore are supported.
|
2015-11-06 22:44:34 +01:00
|
|
|
return $this->name == 'index' || $this->name == '_index';
|
|
|
|
}
|
|
|
|
|
2015-11-11 00:09:47 +01:00
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
if ($title = $this->getAttribute('title')) {
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseAttributes()
|
|
|
|
{
|
|
|
|
// We set an empty array first to
|
|
|
|
// avoid a loop when "parseAttributes"
|
|
|
|
// is called in "getContent"
|
|
|
|
$this->attributes = [];
|
|
|
|
|
|
|
|
$content = $this->getContent();
|
|
|
|
$sections = preg_split('/\s+-{3,}\s+/', $content, 2);
|
|
|
|
|
|
|
|
// Only do it if we have two sections
|
|
|
|
if (count($sections) != 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the different attributes
|
|
|
|
$lines = preg_split('/\n/', $sections[0]);
|
|
|
|
foreach ($lines as $line) {
|
2016-04-28 23:33:26 +02:00
|
|
|
$trimmed = trim($line);
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($trimmed == '') {
|
|
|
|
continue;
|
|
|
|
} // skip empty lines
|
|
|
|
if ($trimmed[0] == '#') {
|
|
|
|
continue;
|
|
|
|
} // can be taken as comments
|
|
|
|
|
|
|
|
$re = '/^([-\\w]*)\\s*?:(.*)/';
|
|
|
|
if (!preg_match($re, $trimmed, $parts)) {
|
|
|
|
break;
|
|
|
|
} //Break as soon as we have a line that doesn't match
|
2016-04-28 23:33:26 +02:00
|
|
|
|
|
|
|
$key = strtolower(trim($parts[1]));
|
|
|
|
$value = trim($parts[2]);
|
2015-11-11 00:09:47 +01:00
|
|
|
$this->attributes[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only remove the content if we have at least one attribute
|
|
|
|
if (count($this->attributes) > 0) {
|
|
|
|
$this->setContent($sections[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAttributes(array $attributes)
|
|
|
|
{
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAttribute($key = null)
|
|
|
|
{
|
|
|
|
if ($this->attributes === null) {
|
|
|
|
$this->parseAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($key)) {
|
|
|
|
return $this->attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists($key, $this->attributes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->attributes[$key];
|
|
|
|
}
|
|
|
|
|
2015-10-28 00:18:29 +01:00
|
|
|
public function dump()
|
|
|
|
{
|
|
|
|
$dump = parent::dump();
|
|
|
|
|
|
|
|
$dump['prev'] = $this->getPrevious() ? $this->getPrevious()->getUrl() : '';
|
|
|
|
$dump['next'] = $this->getNext() ? $this->getNext()->getUrl() : '';
|
|
|
|
|
|
|
|
return $dump;
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|