Logo Search packages:      
Sourcecode: bamboo version File versions

symbols.php

<?php

require_once('filter.php');

class symbols extends filter {

function apply(&$content,&$page) {
      $content = preg_replace(
            array(
                  "/^#toc\s*$/me",
                  "/^#index list\s*$/me",
                  "/^#index outline\s*$/me",
                  "/^#index info\s*$/me",
                  "/^#attachments\s*$/me",
                  '/^#img\s+"?(.*?)"?\s+(.*?)(\s+.*)?$/me', 
            ),
            array(
                  "\$this->toc(\$page->toc)",
                  "\$this->filelist(\$page)",
                  "\$this->outline(\$page)",
                  "\$this->info(\$page)",
                  "\$this->attachments(\$page)",
                  '$this->insertimg("$1","$2","$3")',
            ),
            $content
      );
}

// generate table of contents       
function toc($outline) {
      $count = 0;
      for($i=0; $i<count($outline); $i++) {
            if ($i==0) $toc = "<ol class=toc>\n";
            $a = &$outline[$i];
            $toc .= "<li class=toc><a href=\"#$a[tag]\">$a[text]</a></li>\n";
            $count++;
            if ( isset($outline[$i+1]) ) {
                  if ($a['level'] < $outline[$i+1]['level'])
                        $toc .= "<ol class=toc>";
                  elseif ($a['level'] > $outline[$i+1]['level'])
                        $toc .= "</ol>";
            }
      }
      if ($count > 1) {
            $toc .= str_repeat("</ol>", (int)($outline[$i-1]['level'])); // close all remaining open <ol>   
            return $toc;
      }
      else
            return '';
}

// generate a list of files
function filelist(&$page) {
      global $nav;
      $tree = $nav->getOpenTree($page);
      $ret = "<div class=filelist><ol>\n";
      foreach($tree as $entry) {    
            if ($entry['level'] === 'push')
                  $ret .= "<ol>\n";
            elseif ($entry['level'] === 'pop')
                  $ret .= "</ol>\n";
            else {
                  $link = alink($entry['path'] . '/',$entry['title']);
                  if ($entry['selected']) $link = "<b>$link</b>";
                  $ret .= "<li>$link</li>\n";
            }
      }
      $ret .= "</ol></div>\n";
      return $ret;
}

function outline(&$page) {
      global $nav,$ps;
      $ret = '';
      #$tree = $nav->getOpenTree($page);
      $tree = $page->children();
      foreach($tree as $p) {
            #$page = &$entry['page'];
            if ($p != null && !$p->fnf)
                  $p->fetch();
            
            $links = array();
            if ($p->type == 'txt') {
                  preg_match_all("/^\w*(\*{2,4})\s(.*)\s(\*{2,4})\w*/m", $p->content, $matches);
                  for($i=0; $i<count($matches[2]); $i++) {
                        $links[$matches[2][$i]] = 5 - strlen($matches[1][$i]);
                        // ie $links[text] = # of stars
                  }
            }
            elseif ($p->type == 'html') {
                  preg_match_all("/^\w*<h([123])>(.*)</h[123]>/m", $p->content, $matches);
                  $links[$matches[2][$i]] = $matches[1][$i];
            }
            $ret .= "<h2 class=outline>$p->title</h2>";
            $ret .= "<ol>";
            $h1=0; $h2=0; $h3=0;
            foreach( $links as $text => $level ) {
                  if ($level == 1) $h1++;
                  if ($level == 2) $h2++;
                  if ($level == 3) $h3++;
                  $ret .= "<li>" . alink($p->path . "/#$h1.$h2.$h3", $text);
            }
            $ret .= "</ol>";
      }
      return $ret;
}

function insertimg($file, $align, $caption) {
      global $prop;
      if ($align == '' || $align == 'left')
            $style = "style='float:left'";
      elseif ($align == 'right')
            $style = "style='float:right'";
      elseif ($align == 'center')
            $style = "style='text-align:center'";
            
      $root = $prop->getGlobal('siteroot');
      #$file = preg_replace('"^/"','',$file);
      $ret = "<div $style class='photo'>";
      $ret .= "<img src=\"$root$file\" >";
      if ($caption) {
            $ret .= "<div class='caption'>$caption</div>";
      }
      $ret .= "</div>";
      
      return $ret;
}

function info(&$page) {
      $children = $page->children();
      $ret = '';
      foreach($children as $child) {
            if (!$child->get('visible')) continue;
            $ret .= "<dl class=index>\n";
            $ret .= "<dt>" . alink($child->path . '/',$child->get('title')) . "</dt>\n";
            $ret .= "<dd>" . $child->get('description') . "</dd>\n";
            $ret .= "</dl>\n";
      }
      return $ret;
}

function attachments(&$page) {
      $ainfos = $page->attachments();
      $ret = '';
      $ret .= "<table class=attachments>";
      foreach($ainfos as $info) {
            $ret .= "<tr><td>";
            $class = 'type-file';   
            $ret .= alink($page->path . "/$info[name]", $info['name'], "class=\"$class\" style=\"padding-left: 16px\"");
            $ret .= "</td><td>";
            $ret .= round($info['size'] / 1024,1) . 'K';
            $ret .= "</td><td>";
            $ret .= $info['type'];
            $ret .= "</td><td>";
            $ret .= date('M d Y g:i a',$info['mtime']);
            $ret .= "</td><td></tr>\n";
      }
      $ret .= "</table>";
      return $ret;
}


}

function get_file($file) {
    ob_start();
    readfile($file);
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}


/*

example usage:

ob_start();
readfile("data.html");
$file = ob_get_contents();
ob_end_clean();
$filter = new HtmlFilter();
$filter->insert_toc($file);
print $file;


*/

/*
not used, why is this here?
*/
class HtmlFilter {
      var $anchor_count;
      var $anchors;
      
      /*
     * inserts Table of Contents right in front of existing $html
     */
      function prepend_toc(&$html, $minheader=1, $maxheader=5) {
            $toc = $this->get_toc($html, $minheader, $maxheader);
            $html = $toc . $html;
      }

      /*
     * inserts Table of Contents right after
     * <body> tag.
     */
      function insert_toc(&$html, $minheader=1, $maxheader=5) {
            $toc = $this->get_toc($html, $minheader, $maxheader);
            $html = preg_replace('/<\\s*body.*?>/si',"\$0\n" . $toc, $html);  
      }

      function get_toc(&$html, $minheader, $maxheader) {
            $this->anchor_count = 0;
            $this->anchors = array();

            // add <a name=blah> and get list of anchors
            $find = '/<h(['."$minheader-$maxheader".'])>(.*?)<\/h\d>/si';
            #$find = '/<h([1-5])>(.*?)<\/h\d>/si';
            $html = preg_replace_callback($find, array(&$this,'toc_callback'), $html);
            
            if (!count($this->anchors)) return "";

            // generate table of contents 
            $count = 0;
            $toc = "<ol class=toc>\n";
            for($i=0; $i<count($this->anchors); $i++) {
                  $a = $this->anchors[$i];
                  $toc .= "<li class=toc><a href=\"#$a[anchor]\">$a[text]</a></li>\n";
                  $count++;
                  if ( isset($this->anchors[$i+1]) ) {
                        if ($a['level'] < $this->anchors[$i+1]['level'])
                              $toc .= "<ol class=toc>";
                        elseif ($a['level'] > $this->anchors[$i+1]['level'])
                              $toc .= "</ol>";
                  }
            }
            if ($count > 1) {
                  $toc .= str_repeat("</ol>", (int)($this->anchors[$i-1]['level'])); // close all remaining open <ol>   
                  return $toc;
            }
            else
                  return '';
      }           
      
      /*
     * replaces <h1>hello</h1> with <a name=hello><h1>hello</h1>
     * and adds entry to list of anchors
     */
      function toc_callback($matches) {
            if ($matches[2] == '') return;
            $a = array();
            $a['text'] = $matches[2];
            $a['anchor'] = $this->anchor_count++;
            $a['level'] = $matches[1];
            array_push($this->anchors,$a);
            return '<a name="' . $a['anchor'] . '">' . $matches[0]; 
      }
}

?>

Generated by  Doxygen 1.6.0   Back to index