/**
  * 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;
 }
Example #2
0
 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;
 }
 /**
  * Get first headline string of the page
  * 
  * @access public
  * @static
  * @param string $page
  * @param boolean $usecache use toc cache or not
  * @return string|null first heading string or null
  * @uses PluginSonotsToc
  */
 function firsthead($page, $usecache = true)
 {
     $toc = new PluginSonotsToc($page, $usecache);
     return $toc->get_firsthead();
 }
 /**
  * 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;
 }