tree = $tree; $this->cover = $cover; } protected function getStyles() { // TODO :: un-hardcode that return ''; } protected function getPageUrl($page) { return "file_" . str_replace('/', '_', $page->getUrl()); } protected function buildNavigation(Directory $tree) { $nav = []; foreach ($tree->getEntries() as $node) { if ($node instanceof Content) { if ($node->isIndex()) { continue; } $nav[] = [ 'title' => $node->getTitle(), 'href' => "#" . $this->getPageUrl($node), ]; } elseif ($node instanceof Directory) { if (!$node->hasContent()) { continue; } $page_index = ($index = $node->getIndexPage()) ? $index : $node->getFirstPage(); $nav[] = [ 'title' => $node->getTitle(), 'href' => "#" . $this->getPageUrl($page_index), 'children' => $this->buildNavigation($node), ]; } } return $nav; } private function renderNavigation($entries) { $nav = ''; foreach ($entries as $entry) { if (array_key_exists('children', $entry)) { if (array_key_exists('href', $entry)) { $link = '' . $entry['title'] . ''; } else { $link = '' . $entry['title'] . ''; } $link .= $this->renderNavigation($entry['children']); } else { $link = '' . $entry['title'] . ''; } $nav .= "
  • $link
  • "; } return ""; } protected function generateTOC() { return '

    Table of Contents

    ' . $this->renderNavigation($this->buildNavigation($this->tree)) . '
     
    '; } protected function generateCover() { return "
    " . "

    {$this->cover['title']}

    " . "

    {$this->cover['subject']} by {$this->cover['author']}

    " . '
     
    '; } protected function generatePages() { $content = ''; foreach ($this->pages as $page) { $content .= ''; $content .= '

    ' . $page['page']->getTitle() . '

    '; $content .= '
    ' . $page['content'] . '
    '; $content .= '
     
    '; } return $content; } public function addPage($page, $content) { $this->pages[] = ['page' => $page, 'content' => $content]; } public function generateHead() { $head = [ "{$this->cover['title']}", "", "", "", $this->getStyles(), ]; return '' . implode('', $head) . ''; } public function generateBody() { return '' . $this->generateCover() . $this->generateTOC() . $this->generatePages() . ''; } public function generate() { return '' . $this->generateHead() . $this->generateBody() . ''; } }