Inheritance: extends Entry, implements ArrayAccess, implements IteratorAggregate
Ejemplo n.º 1
0
 private function generateRecursive(Directory $tree, Config $params, $base_url = '')
 {
     $final = ['title' => $this->prefix . $tree->getTitle()];
     $params['base_url'] = $params['base_page'] = $base_url;
     $params['image'] = str_replace('<base_url>', $base_url, $params['image']);
     if ($base_url !== '') {
         $params['entry_page'] = $tree->getFirstPage();
     }
     foreach ($tree->getEntries() as $key => $node) {
         if ($node instanceof Directory) {
             $final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive($node, $params, '../' . $base_url);
         } elseif ($node instanceof Content) {
             $params['request'] = $node->getUrl();
             $contentType = $this->daux->getContentTypeHandler()->getType($node);
             $data = ['title' => $this->prefix . $node->getTitle(), 'file' => $node, 'page' => ContentPage::fromFile($node, $params, $contentType)];
             // As the page is lazily generated
             // We do it now to fail fast in case of problem
             $data['page']->getContent();
             if ($key == 'index.html') {
                 $final['title'] = $this->prefix . $tree->getTitle();
                 $final['file'] = $node;
                 $final['page'] = $data['page'];
             } else {
                 $final['children'][$data['title']] = $data;
             }
         }
     }
     return $final;
 }
Ejemplo n.º 2
0
 /**
  * Recursively generate the documentation
  *
  * @param Directory $tree
  * @param string $output_dir
  * @param \Todaymade\Daux\Config $params
  * @param OutputInterface $output
  * @param int $width
  * @param bool $index_pages
  * @param string $base_url
  * @throws \Exception
  */
 private function generateRecursive(Directory $tree, $output_dir, $params, $output, $width, $index_pages, $base_url = '')
 {
     DauxHelper::rebaseConfiguration($params, $base_url);
     if ($base_url !== '' && empty($params['entry_page'])) {
         $params['entry_page'] = $tree->getFirstPage();
     }
     foreach ($tree->getEntries() as $key => $node) {
         if ($node instanceof Directory) {
             $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
             mkdir($new_output_dir);
             $this->generateRecursive($node, $new_output_dir, $params, $output, $width, $index_pages, '../' . $base_url);
             // Rebase configuration again as $params is a shared object
             DauxHelper::rebaseConfiguration($params, $base_url);
         } else {
             $this->runAction('- ' . $node->getUrl(), $output, $width, function () use($node, $output_dir, $key, $params, $index_pages) {
                 if ($node instanceof Raw) {
                     copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
                     return;
                 }
                 $generated = $this->generateOne($node, $params);
                 file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
                 if ($index_pages) {
                     $this->indexed_pages[] = ['title' => $node->getTitle(), 'text' => utf8_encode($this->strip_html_tags($generated->getPureContent())), 'tags' => '', 'url' => $node->getUrl()];
                 }
             });
         }
     }
 }
Ejemplo n.º 3
0
 private function buildNavigation(Directory $tree, $path, $current_url, $base_page, $mode)
 {
     $nav = [];
     foreach ($tree->getEntries() as $node) {
         $url = $node->getUri();
         if ($node instanceof Content) {
             if ($node->isIndex()) {
                 continue;
             }
             $link = $path === '' ? $url : $path . '/' . $url;
             $nav[] = ['title' => $node->getTitle(), 'href' => $base_page . $link, 'class' => $current_url === $link ? 'active' : ''];
         } elseif ($node instanceof Directory) {
             if (!$node->hasContent()) {
                 continue;
             }
             $link = $path === '' ? $url : $path . '/' . $url;
             $folder = ['title' => $node->getTitle(), 'class' => strpos($current_url, $link) === 0 ? 'open' : ''];
             if ($mode === Daux::STATIC_MODE) {
                 $link .= "/index.html";
             }
             if ($node->getIndexPage()) {
                 $folder['href'] = $base_page . $link;
             }
             //Child pages
             $new_path = $path === '' ? $url : $path . '/' . $url;
             $folder['children'] = $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
             $nav[] = $folder;
         }
     }
     return $nav;
 }
Ejemplo n.º 4
0
 /**
  * Recursively generate the documentation
  *
  * @param Directory $tree
  * @param string $output_dir
  * @param \Todaymade\Daux\Config $params
  * @param OutputInterface $output
  * @param integer $width
  * @param string $base_url
  * @throws \Exception
  */
 private function generateRecursive(Directory $tree, $output_dir, $params, $output, $width, $base_url = '')
 {
     DauxHelper::rebaseConfiguration($params, $base_url);
     if ($base_url !== '' && empty($params['entry_page'])) {
         $params['entry_page'] = $tree->getFirstPage();
     }
     foreach ($tree->getEntries() as $key => $node) {
         if ($node instanceof Directory) {
             $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
             mkdir($new_output_dir);
             $this->generateRecursive($node, $new_output_dir, $params, $output, $width, '../' . $base_url);
             // Rebase configuration again as $params is a shared object
             DauxHelper::rebaseConfiguration($params, $base_url);
         } else {
             $this->runAction("- " . $node->getUrl(), $output, $width, function () use($node, $output_dir, $key, $params) {
                 if (!$node instanceof Content) {
                     copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
                     return;
                 }
                 $generated = $this->generateOne($node, $params);
                 file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
             });
         }
     }
 }
Ejemplo n.º 5
0
 public function testGetOrCreateIndexPage()
 {
     $directory = new Directory($this->getStaticRoot(), 'dir');
     $directory->setTitle('Tutorials');
     $entry = Builder::getOrCreatePage($directory, 'index.md');
     $this->assertSame($directory, $entry->getParent());
     $this->assertEquals('dir/index.html', $entry->getUrl());
     $this->assertEquals('Tutorials', $entry->getTitle());
     $this->assertInstanceOf('Todaymade\\Daux\\Tree\\Content', $entry);
 }
Ejemplo n.º 6
0
 /**
  * @dataProvider providerSort
  */
 public function testSort($list, $expected)
 {
     shuffle($list);
     $directory = new Directory(new Root(new Config(), ''), 'dir');
     foreach ($list as $value) {
         $entry = new Content($directory, $value);
         $entry->setName($value);
     }
     $directory->sort();
     $final = [];
     foreach ($directory->getEntries() as $obj) {
         $final[] = $obj->getName();
     }
     $this->assertEquals($expected, $final);
 }
Ejemplo n.º 7
0
 protected function buildNavigation(Directory $tree)
 {
     $nav = [];
     foreach ($tree->getEntries() as $node) {
         if ($node instanceof Content) {
             if ($node->isIndex()) {
                 continue;
             }
             $nav[] = ['title' => $node->getTitle(), 'href' => '#section_' . $this->getSectionId($node)];
         } elseif ($node instanceof Directory) {
             if (!$node->hasContent()) {
                 continue;
             }
             $page_index = ($index = $node->getIndexPage()) ? $index : $node->getFirstPage();
             $nav[] = ['title' => $node->getTitle(), 'href' => '#section_' . $this->getSectionId($page_index), 'children' => $this->buildNavigation($node)];
         }
     }
     return $nav;
 }
Ejemplo n.º 8
0
 /**
  * Locate a file in the tree. Returns the file if found or false
  *
  * @param Directory $tree
  * @param string $request
  * @return Tree\Content|Tree\Raw|false
  */
 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->getEntries()[$node])) {
             $tree = $tree->getEntries()[$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 ($index = $tree->getIndexPage()) {
         return $index;
     }
     return false;
 }
Ejemplo n.º 9
0
 public function finalizeTree(Directory $current, $prev = null)
 {
     foreach ($current->getEntries() as $entry) {
         if ($entry instanceof Directory) {
             $prev = $this->finalizeTree($entry, $prev);
         } elseif ($entry instanceof Content) {
             if ($prev) {
                 $prev->setNext($entry);
                 $entry->setPrevious($prev);
             }
             $prev = $entry;
         }
     }
     return $prev;
 }
Ejemplo n.º 10
0
 /**
  * @param Directory $parent
  * @param string $path
  * @return ContentAbstract
  */
 public static function getOrCreatePage(Directory $parent, $path)
 {
     $extension = pathinfo($path, PATHINFO_EXTENSION);
     // If the file doesn't have an extension, set .md as a default
     if ($extension == '') {
         $extension = 'md';
         $path .= '.md';
     }
     $raw = !in_array($extension, $parent->getConfig()['valid_content_extensions']);
     $title = $uri = $path;
     if (!$raw) {
         $title = static::getName($path);
         $uri = DauxHelper::slug($title);
         if ($parent->getConfig()->isStatic()) {
             $uri .= '.html';
         }
     }
     if (array_key_exists($uri, $parent->getEntries())) {
         return $parent->getEntries()[$uri];
     }
     $page = $raw ? new ComputedRaw($parent, $uri) : new Content($parent, $uri);
     $page->setContent('-');
     //set an almost empty content to avoid problems
     $page->setName($path);
     $page->setTitle($title);
     if ($title == 'index' || $title == '_index') {
         $page->setTitle($parent->getTitle());
     }
     return $page;
 }
Ejemplo n.º 11
0
 /**
  * @param Directory $parent
  */
 protected function setParent(Directory $parent)
 {
     if ($this->parent) {
         $this->parent->removeChild($this);
     }
     $parent->addChild($this);
     $this->parent = $parent;
 }
Ejemplo n.º 12
0
 /**
  * @param Directory $parent
  * @param string $path
  * @return Content
  */
 public static function getOrCreatePage(Directory $parent, $path)
 {
     $title = static::getName($path);
     // If the file doesn't have an extension, set .md as a default
     if (pathinfo($path, PATHINFO_EXTENSION) == '') {
         $path .= '.md';
     }
     $uri = $slug = DauxHelper::slug($title);
     if ($parent->getConfig()['mode'] === Daux::STATIC_MODE) {
         $uri = $slug . ".html";
     }
     if (array_key_exists($uri, $parent->getEntries())) {
         return $parent->getEntries()[$uri];
     }
     $page = new Content($parent, $uri);
     $page->setContent("-");
     //set an almost empty content to avoid problems
     if ($title == 'index') {
         // TODO :: clarify the difference between 'index' and '_index'
         $page->setName('_index' . pathinfo($path, PATHINFO_EXTENSION));
         $page->setTitle($parent->getTitle());
     } else {
         $page->setName($path);
         $page->setTitle($title);
     }
     return $page;
 }
Ejemplo n.º 13
0
 private function buildNavigation(Directory $tree, $path, $current_url, $base_page, $mode)
 {
     $nav = [];
     foreach ($tree->getEntries() as $node) {
         $url = $node->getUri();
         if ($node instanceof Content) {
             if ($node->isIndex()) {
                 continue;
             }
             $link = $path === '' ? $url : $path . '/' . $url;
             $nav[] = ['title' => $node->getTitle(), 'href' => $base_page . $link, 'class' => $current_url === $link ? 'Nav__item--active' : ''];
         } elseif ($node instanceof Directory) {
             if (!$node->hasContent()) {
                 continue;
             }
             $link = $path === '' ? $url : $path . '/' . $url;
             $folder = ['title' => $node->getTitle(), 'class' => strpos($current_url, $link) === 0 ? 'Nav__item--open' : ''];
             if ($index = $node->getIndexPage()) {
                 $folder['href'] = $base_page . $index->getUrl();
             }
             //Child pages
             $new_path = $path === '' ? $url : $path . '/' . $url;
             $folder['children'] = $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
             if (!empty($folder['children'])) {
                 $folder['class'] .= ' has-children';
             }
             $nav[] = $folder;
         }
     }
     return $nav;
 }