8
0

Refactor the tree system

Each element of the tree now only has one parent, not a parent array.

- Creating an elements doesn't necessarily rely on an existing file
- Moving elements unregisters elements from the previous parent
- Moved some helpers to the builder instead of Entry
Dieser Commit ist enthalten in:
Stéphane Goetz 2015-07-18 20:52:14 +02:00
Ursprung 3235c49acd
Commit aa5602a0fb
7 geänderte Dateien mit 210 neuen und 134 gelöschten Zeilen

Datei anzeigen

@ -2,6 +2,7 @@
use Symfony\Component\Console\Output\NullOutput;
use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Root;
class Daux
{
@ -124,7 +125,9 @@ class Daux
private function generateTree()
{
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->getParams());
$this->tree = new Root($this->docs_path);
Builder::build($this->tree, $this->options['ignore'], $this->getParams());
if (!empty($this->options['languages'])) {
foreach ($this->options['languages'] as $key => $node) {
$this->tree->getEntries()[$key]->title = $node;

Datei anzeigen

@ -11,8 +11,7 @@ class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage
if ($this->title === 'index') {
$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();
$this->title = $this->file->getParent()->getTitle();
} else {
$this->homepage = ($this->file->getName() === '_index');
$this->title = $this->params['title'];

Datei anzeigen

@ -107,7 +107,7 @@ class Template
'href' => $base_page . $link,
'class' => ($current_url === $link) ? 'active' : ''
];
} else if ($node instanceof Directory) {
} elseif ($node instanceof Directory) {
$link = ($path === '') ? $url : $path . '/' . $url;
$folder = [

Datei anzeigen

@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
@ -8,25 +9,19 @@ class Builder
/**
* Build the initial tree
*
* @param string $dir
* @param Directory $node
* @param array $ignore
* @param \Todaymade\Daux\Config $params
* @param array $parents
* @return Directory|void
* @param Config $params
*/
public static function build($dir, $ignore, $params, $parents = null)
public static function build($node, $ignore, Config $params)
{
if (!$dh = opendir($dir)) {
if (!$dh = opendir($node->getPath())) {
return;
}
$node = new Directory($dir, $parents);
$new_parents = $parents;
if (is_null($new_parents)) {
$new_parents = array();
} else {
$new_parents[] = $node;
if ($node instanceof Root) {
// Ignore config.json in the root directory
$ignore['files'][] = 'config.json';
}
while (($file = readdir($dh)) !== false) {
@ -34,7 +29,7 @@ class Builder
continue;
}
$path = $dir . DS . $file;
$path = $node->getPath() . DS . $file;
if (is_dir($path) && in_array($file, $ignore['folders'])) {
continue;
@ -45,19 +40,12 @@ class Builder
$entry = null;
if (is_dir($path)) {
$entry = static::build($path, $ignore, $params, $new_parents);
} elseif (in_array(pathinfo($path, PATHINFO_EXTENSION), Daux::$VALID_MARKDOWN_EXTENSIONS)) {
$entry = new Content($path, $new_parents);
if ($params['mode'] === Daux::STATIC_MODE) {
$entry->setUri($entry->getUri() . '.html');
}
$new = new Directory($node, static::getUriFromFilename(static::getFilename($path)), $path);
$new->setName(DauxHelper::pathinfo($path)['filename']);
$new->setTitle(static::getTitleFromFilename($new->getName()));
static::build($new, $ignore, $params);
} else {
$entry = new Raw($path, $new_parents);
}
if ($entry instanceof Entry) {
$node->addChild($entry);
static::createContent($node, $path, $params);
}
}
@ -68,7 +56,91 @@ class Builder
} else {
$node->setIndexPage(false);
}
return $node;
}
/**
* @param Directory $parent
* @param string $path
* @param Config $params
* @return Content|Raw
*/
public static function createContent(Directory $parent, $path, Config $params)
{
$name = DauxHelper::pathinfo($path)['filename'];
if (in_array(pathinfo($path, PATHINFO_EXTENSION), Daux::$VALID_MARKDOWN_EXTENSIONS)) {
$uri = static::getUriFromFilename($name);
if ($params['mode'] === Daux::STATIC_MODE) {
$uri .= '.html';
}
$entry = new Content($parent, $uri, $path, filemtime($path));
} else {
$entry = new Raw($parent, static::getUriFromFilename(static::getFilename($path)), $path, filemtime($path));
}
if ($entry->getUri() == $params['index_key']) {
if ($parent instanceof Root) {
$entry->setTitle($params['title']);
} else {
$entry->setTitle($parent->getTitle());
}
} else {
$entry->setTitle(static::getTitleFromFilename($name));
}
$entry->setIndexPage(false);
$entry->setName($name);
return $entry;
}
/**
* @param string $file
* @return string
*/
protected static function getFilename($file)
{
$parts = explode('/', $file);
return end($parts);
}
/**
* @param string $filename
* @return string
*/
protected static function getTitleFromFilename($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode(' ', $filename);
return $filename;
}
/**
* @param string $filename
* @return string
*/
protected static function getUriFromFilename($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode('_', $filename);
return $filename;
}
/**
@ -84,48 +156,38 @@ class Builder
return $parent->getEntries()[$slug];
}
$dir = new Directory();
$dir = new Directory($parent, $slug);
$dir->setTitle($title);
$dir->setUri($slug);
$parent->addChild($dir);
return $dir;
}
/**
* @param array $parents
* @param Directory $parent
* @param string $title
* @return Content
*/
public static function getOrCreatePage($parents, $title)
public static function getOrCreatePage(Directory $parent, $title)
{
$slug = DauxHelper::slug($title);
$uri = $slug . ".html";
/**
* @var Directory $nearestParent
*/
$nearestParent = end($parents);
if (array_key_exists($uri, $nearestParent->getEntries())) {
return $nearestParent->getEntries()[$uri];
if (array_key_exists($uri, $parent->getEntries())) {
return $parent->getEntries()[$uri];
}
$page = new Content('', $parents);
$page->setUri($uri);
$page = new Content($parent, $uri);
$page->setContent("-"); //set an almost empty content to avoid problems
if ($title == 'index') {
$page->setName('_index');
$page->setTitle($nearestParent->getTitle());
$nearestParent->setIndexPage($page);
$page->setTitle($parent->getTitle());
$parent->setIndexPage($page);
} else {
$page->setName($slug);
$page->setTitle($title);
}
$nearestParent->addChild($page);
return $page;
}
}

Datei anzeigen

@ -16,9 +16,6 @@ class Directory extends Entry
public function addChild(Entry $entry)
{
//TODO :: set parent in the entry
//TODO :: remove child from previous parent
$this->children[$entry->getUri()] = $entry;
}
@ -71,4 +68,15 @@ class Directory extends Entry
}
}
}
public function dump()
{
$dump = parent::dump();
foreach ($this->getEntries() as $entry) {
$dump['children'][] = $entry->dump();
}
return $dump;
}
}

Datei anzeigen

@ -1,7 +1,5 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\DauxHelper;
abstract class Entry
{
/** @var string */
@ -19,23 +17,33 @@ abstract class Entry
/** @var string */
protected $uri;
/** @var Directory */
protected $parent;
/** @var string */
protected $local_path;
protected $path;
/** @var integer */
protected $last_modified;
/** @var array */
protected $parents;
/**
* @param Directory $parent
* @param string $uri
* @param string $path
* @param array $parents
* @param integer $last_modified
*/
public function __construct($path = '', $parents = array())
public function __construct(Directory $parent, $uri, $path = null, $last_modified = null)
{
$this->setPath($path);
$this->setParents($parents);
$this->setUri($uri);
$this->setParent($parent);
if ($path) {
$this->path = $path;
}
if ($last_modified) {
$this->last_modified = $last_modified;
}
}
/**
@ -67,7 +75,15 @@ abstract class Entry
*/
public function setUri($uri)
{
if ($this->parent) {
$this->parent->removeChild($this);
}
$this->uri = $uri;
if ($this->parent) {
$this->parent->addChild($this);
}
}
/**
@ -102,7 +118,7 @@ abstract class Entry
// First we try to find a real page
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
if (!count($node->getParents()) && $node->title == 'index') {
if (!$node->getParent() && $node->title == 'index') {
//the homepage should not count as first page
continue;
}
@ -148,19 +164,40 @@ abstract class Entry
}
/**
* @return array
* @return Directory
*/
public function getParents()
public function getParent()
{
return $this->parents;
return $this->parent;
}
/**
* @param array $parents
* Return all parents starting with the root
*
* @return array<Directory>
*/
public function setParents($parents)
public function getParents()
{
$this->parents = $parents;
$parents = [];
if ($this->parent && !$this->parent instanceof Root) {
$parents = $this->parent->getParents();
$parents[] = $this->parent;
}
return $parents;
}
/**
* @param Directory $parent
*/
protected function setParent(Directory $parent)
{
if ($this->parent) {
$this->parent->removeChild($this);
}
$parent->addChild($this);
$this->parent = $parent;
}
/**
@ -168,23 +205,7 @@ abstract class Entry
*/
public function getPath()
{
return $this->local_path;
}
/**
* @param string $path
*/
public function setPath($path)
{
if (!isset($path) || $path == '' || !file_exists($path)) {
return;
}
$this->local_path = $path;
$this->last_modified = filemtime($path);
$this->name = DauxHelper::pathinfo($path)['filename'];
$this->title = $this->getTitleInternal($this->name);
$this->uri = $this->getUrlInternal($this->getFilename($path));
$this->index_page = false;
return $this->path;
}
/**
@ -193,58 +214,26 @@ abstract class Entry
public function getUrl()
{
$url = '';
foreach ($this->parents as $node) {
$url .= $node->uri . '/';
if ($this->getParent() && !$this->getParent() instanceof Root) {
$url = $this->getParent()->getUrl() . '/' . $url;
}
$url .= $this->uri;
$url .= $this->getUri();
return $url;
}
/**
* @param string $file
* @return string
*/
protected function getFilename($file)
public function dump()
{
$parts = explode('/', $file);
return end($parts);
}
/**
* @param string $filename
* @return string
*/
protected function getTitleInternal($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode(' ', $filename);
return $filename;
}
/**
* @param string $filename
* @return string
*/
protected function getUrlInternal($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode('_', $filename);
return $filename;
return [
'title' => $this->getTitle(),
'type' => get_class($this),
'name' => $this->getName(),
'uri' => $this->getUri(),
'url' => $this->getUrl(),
'index' => $this->getIndexPage()? $this->getIndexPage()->getUrl() : '',
'first' => $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '',
'path' => $this->path
];
}
}

15
libs/Tree/Root.php Normale Datei
Datei anzeigen

@ -0,0 +1,15 @@
<?php namespace Todaymade\Daux\Tree;
class Root extends Directory
{
/**
* The root doesn't have a parent
*
* @param Directory $uri
*/
public function __construct($uri)
{
$this->setUri($uri);
$this->path = $uri;
}
}