/** * 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; }
function get_sections($lines, $page, $options) { $toc = new PluginSonotsToc($page, $options['cache']); $headlines = $toc->get_headlines(); 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); } $outlines = array(); if (is_array($options['num'])) { array_unshift($headlines, new PluginSonotsHeadline($page, 0, 0, '', '')); list($offset, $length) = $options['num']; $headlines = sonots::array_slice($headlines, $offset, $length, true); } $linenums = sonots::get_members($headlines, 'linenum'); // extract from current head till next head - 1 $allheadlines = $toc->get_headlines(); $alllinenums = sonots::get_members($allheadlines, 'linenum'); if (!isset($alllinenums[0])) { array_unshift($alllinenums, 0); } // virtual head at the file head array_push($alllinenums, end(array_keys($lines)) + 1); // virtual head at the file tail $outlines = array(); $current = 0; foreach ($alllinenums as $next) { if (in_array($current, $linenums)) { if ($next == $current) { continue; } $outlines += sonots::array_slice($lines, $current, $next - $current, true); } $current = $next; } return $outlines; }