/** * Renders a sections and its children * * @param $section Sensei_Doc_Section section to be rendered * @return string rendered sections */ protected function _renderSection(Sensei_Doc_Section $section) { $output = ''; $title = $section->getIndex() . ' ' . $section->getName(); $level = $section->getLevel(); if ($level === 1) { $class = ' class="chapter"'; $title = 'Chapter ' . $title; } else { $class = ' class="section"'; } $output .= '<div' . $class .'>' . "\n"; $output .= "<h$level>"; if ( ! ($this->_options['section'] instanceof Sensei_Doc_Section) || ($level > $this->_options['section']->getLevel())) { $anchor = $this->makeAnchor($section); $output .= '<a href="#' . $anchor . '" id="' . $anchor . '">'; $output .= $title . '</a>'; } else { $output .= $title; } $output .= "</h$level>"; // Transform section contents from wiki syntax to XHTML $output .= $this->_wiki->transform($section->getText()); // Render children of this section recursively for ($i = 0; $i < count($section); $i++) { $output .= $this->_renderSection($section->getChild($i)); } $output .= '</div>' . "\n"; return $output; }
public function parse($path, $filename) { $file = file($path . DIRECTORY_SEPARATOR . $filename); $current = $this; if ($this->isRoot()) { $path .= DIRECTORY_SEPARATOR . basename($filename, '.txt'); } foreach ($file as $lineNum => $line) { // Checks if the line is a heading if (preg_match('/^(\+{1,6}) (.*)/', trim($line), $matches)) { $level = strlen($matches[1]); $heading = $matches[2]; if (($level > $this->getLevel()) && ($level <= $current->getLevel() + 1)) { // The current section did not have any text in this file. // Let's assume that the text is defined in another file. if ( ! $current->isRoot() && $current->_text === '') { $otherFilename = $current->getPath(false, DIRECTORY_SEPARATOR) . '.txt'; if (($filename !== $otherFilename) && (file_exists($path . DIRECTORY_SEPARATOR . $otherFilename))) { $current->parse($path, $otherFilename); } } $parent = $current; while ($parent->getLevel() >= $level) { $parent = $parent->_parent; } $current = new Sensei_Doc_Section($heading, $parent); } else { $format = 'Section has no direct parent, or level of the ' . 'heading is not greater than the level of the ' . 'file in "%s" on line %d'; $message = sprintf($format, $filename, $lineNum); throw new Sensei_Exception($message); } } else { if ($current->_text === '') { if (trim($line) !== '') { $current->_text = $line; } } else { $current->_text .= $line; } } } // The last section did not have any text in this file. // Let's assume that the text is defined in another file. if ( ! $current->isRoot() && $current->_text === '') { $otherFilename = $current->getPath(false, DIRECTORY_SEPARATOR) . '.txt'; if (($filename !== $otherFilename) && (file_exists($path . DIRECTORY_SEPARATOR . $otherFilename))) { $current->parse($path, $otherFilename); } } }
/** * Returns the number of sections (excluding their subsections). * * @return int */ public function count() { return $this->_toc->count(); }