diff --git a/libs/Daux.php b/libs/Daux.php index 01ca449..56f02c9 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -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; diff --git a/libs/Format/HTML/MarkdownPage.php b/libs/Format/HTML/MarkdownPage.php index 5d4150c..3a43d25 100644 --- a/libs/Format/HTML/MarkdownPage.php +++ b/libs/Format/HTML/MarkdownPage.php @@ -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']; diff --git a/libs/Format/HTML/Template.php b/libs/Format/HTML/Template.php index 62712c4..ef27ab0 100644 --- a/libs/Format/HTML/Template.php +++ b/libs/Format/HTML/Template.php @@ -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 = [ diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index 2e5e6e7..94713d5 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -1,5 +1,6 @@ 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; } } diff --git a/libs/Tree/Directory.php b/libs/Tree/Directory.php index 9a1694d..1ad0f1d 100644 --- a/libs/Tree/Directory.php +++ b/libs/Tree/Directory.php @@ -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; + } } diff --git a/libs/Tree/Entry.php b/libs/Tree/Entry.php index 15db34b..d922dc9 100644 --- a/libs/Tree/Entry.php +++ b/libs/Tree/Entry.php @@ -1,7 +1,5 @@ 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 */ - 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 + ]; } } diff --git a/libs/Tree/Root.php b/libs/Tree/Root.php new file mode 100644 index 0000000..6aae9d7 --- /dev/null +++ b/libs/Tree/Root.php @@ -0,0 +1,15 @@ +setUri($uri); + $this->path = $uri; + } +}