path = $path; } /** * Retrieve the table of contents * * @return Node */ public function getToc() { $tocStack = array((object) array( 'level' => 0, 'node' => new Node() )); foreach (new DocIterator($this->path) as $fileObject) { $line = null; $currentChapterName = null; while (! $fileObject->eof()) { // Save last line for setext-style headers $lastLine = $line; $line = $fileObject->fgets(); $header = $this->extractHeader($line, $lastLine); if ($header !== null) { list($header, $level) = $header; $id = $this->extractHeaderId($header); $nofollow = false; $this->reduceToc($tocStack, $level); if ($id === null) { $path = array(); foreach (array_slice($tocStack, 1) as $entity) { $path[] = $entity->node->getValue()->title; } $path[] = $header; $id = implode('-', $path); $nofollow = true; } $id = urlencode(str_replace('.', '.', strip_tags($id))); if ($currentChapterName === null) { // The first header is the chapter's name $currentChapterName = $id; $id = null; } $node = end($tocStack)->node->appendChild( (object) array( 'id' => $id, 'title' => $header, 'nofollow' => $nofollow, 'chapterName' => $currentChapterName ) ); $tocStack[] = (object) array( 'level' => $level, 'node' => $node ); } } } return $tocStack[0]->node; } /** * Retrieve a chapter * * @param string $chapterName * * @return string */ public function getChapter($chapterName) { $cat = array(); $tocStack = array((object) array( 'level' => 0, 'node' => new Node() )); $chapterFound = false; foreach (new DocIterator($this->path) as $fileObject) { $line = null; $currentChapterName = null; $chapter = array(); while (! $fileObject->eof()) { // Save last line for setext-style headers $lastLine = $line; $line = $fileObject->fgets(); $header = $this->extractHeader($line, $lastLine); if ($header !== null) { list($header, $level) = $header; $id = $this->extractHeaderId($header); $this->reduceToc($tocStack, $level); if ($id === null) { $path = array(); foreach (array_slice($tocStack, 1) as $entity) { $path[] = $entity->node->getValue()->title; } $path[] = $header; $id = implode('-', $path); } $id = urlencode(str_replace('.', '.', strip_tags($id))); if ($currentChapterName === null) { $currentChapterName = $id; $id = null; } $node = end($tocStack)->node->appendChild( (object) array( 'title' => $header ) ); $tocStack[] = (object) array( 'level' => $level, 'node' => $node ); $line = '' . PHP_EOL . $line; } $chapter[] = $line; } if ($currentChapterName === $chapterName) { $chapterFound = true; $cat = $chapter; } if (! $chapterFound) { $cat = array_merge($cat, $chapter); } } $html = preg_replace_callback( '#
(.*?)\