/** * Display Table of Contents * * @access static * @param string $page * @param array $options * @return string html */ function display_toc($page, $options) { $toc = new PluginSonotsToc($page, $options['cache']); if ($options['include']) { $toc->expand_includes(); } $headlines = $toc->get_headlines(); if ($options['fromhere']) { $fromhere = $toc->get_fromhere(); $offset = 0; $headline = reset($headlines); while (!($headline->page === $page && $headline->linenum > $fromhere)) { ++$offset; if (($headline = next($headlines)) === false) { break; } } $headlines = sonots::array_slice($headlines, $offset, null, true); } if (isset($options['filter'])) { sonots::grep_by($headlines, 'string', 'preg', '/' . str_replace('/', '\\/', $options['filter']) . '/'); } if (isset($options['except'])) { sonots::grep_by($headlines, 'string', 'preg', '/' . str_replace('/', '\\/', $options['except']) . '/', true); // inverse } if (is_array($options['depth'])) { // Do not use negative offsets list($min, $max) = PluginSonotsOption::conv_interval($options['depth'], array(1, PHP_INT_MAX)); sonots::grep_by($headlines, 'depth', 'ge', $min); sonots::grep_by($headlines, 'depth', 'le', $max); } if (is_array($options['num'])) { list($offset, $length) = $options['num']; $headlines = sonots::array_slice($headlines, $offset, $length, true); } if ($options['hierarchy']) { if ($options['include'] && count($toc->get_includes()) >= 1) { // depth of included page is 0, shift up sonots::map_members($headlines, 'depth', create_function('$x', 'return $x+1;')); } if ($options['compact']) { PluginSonotsToc::compact_depth($headlines); } } else { sonots::init_members($headlines, 'depth', 1); // flatten (to all 1) } $html = PluginSonotsToc::display_toc($headlines, 'contentsx', $options['link']); return $html; }
/** * Expand headlines of included pages * * $visited is used only inside for recursive call, you do not need to use it. * * @access public * @param array $visited a flag to check whether the page is processed already. * @return array $visited * @see shrink_includes */ function expand_includes($visited = array()) { if (!in_array($this->page, $visited)) { $visited[] = $this->page; } // combine headlines and includes lines $hlines = array_map(create_function('', 'return "h";'), $this->headlines); $ilines = array_map(create_function('', 'return "i";'), $this->includes); $lines = $hlines + $ilines; ksort($lines); $headlines = array(); foreach ($lines as $linenum => $flag) { if ($flag === 'h') { $headline = $this->headlines[$linenum]; $headline->page = $this->page; $headlines[] = $headline; } else { // expand included page $include = $this->includes[$linenum]; if (in_array($include->page, $visited)) { continue; } $headlines[] = $include->headline($this->usecache); $toc = new PluginSonotsToc($include->page, $this->usecache); $visited = $toc->expand_includes($visited); $headlines = array_merge($headlines, $toc->headlines); } } $this->headlines = $headlines; return $visited; }