Table of Contents

ColumnList

Last update: 2007-02-03

A simple type of list that displays the entries in multiple columns.

Example

First column. Second column.
New row. (If using two columns.)
~~COL:2~~
  , First column.
  , Second column.
  , New row. (If using two columns.)

Source code

Copyright (C) 2007 Tom N Harris This plugin is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This plugin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

syntax.php
<?php
  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
  if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  require_once(DOKU_PLUGIN.'syntax.php');
 
  if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
 
/** Column List
 
    Display a list of items in columns.
 
    Syntax:
      , First column.
      , Second column.
      , New row.
        (If using two columns.)
 
    License: GPL
    */
class syntax_plugin_columnlist extends DokuWiki_Syntax_Plugin {
 
  var $columns = 4;
  var $cur_col = 0;
 
  function getInfo() {
    return array('author' => 'Tom N Harris',
                 'email'  => 'tnharris [at] whoopdedo.org',
                 'date'   => 'Sat, Feb 03, 2007',
                 'name'   => 'Column List Plugin',
                 'desc'   => 'Display a list of items formatted as columns. Syntax: <tab>, ...
                              Default formatting is 4 columns. Add ~~COL:<num>~~ before the first item to change this.',
                 'url'    => 'http://whoopdedo.org/doku/wiki/columnlist');
  }
 
  function getType() {
    return 'container';
  }
 
  function getPType() {
    return 'block';
  }
 
  function getSort() {
    return 19;
  }
 
  function accepts($mode) {
    if (!count($this->allowedModes)) {
      global $PARSER_MODES;
      $this->allowedModes = array_merge($PARSER_MODES['formatting'],
                                        $PARSER_MODES['substition'],
                                        $PARSER_MODES['protected'],
                                        $PARSER_MODES['disabled']);
    }
    return parent::accepts($mode);
  }
 
  function connectTo($mode) {
    $this->Lexer->addEntryPattern('\n {2,},', $mode, 'plugin_columnlist');
    $this->Lexer->addEntryPattern('\n\t{1,},', $mode, 'plugin_columnlist');
    $this->Lexer->addEntryPattern('~~COL:\d+~~', $mode, 'plugin_columnlist');
    $this->Lexer->addPattern('\n {2,},', 'plugin_columnlist');
    $this->Lexer->addPattern('\n\t{1,},', 'plugin_columnlist');
  }
 
  function postConnect() {
    $this->Lexer->addExitPattern('\n', 'plugin_columnlist');
  }
 
  function handle($match, $state, $pos, &$handler){
    if (substr($match,0,6) == '~~COL:') {
      $n = (int)substr($match, 6, -2);
      if ($n != 0) {
        $this->columns = $n;
      }
    }
    $tags = array();
    switch ($state) {
      case DOKU_LEXER_EXIT:
        $tags[] = 'end_col';
        $this->cur_col = 0;
        $this->columns = 4;
        break;
      case DOKU_LEXER_ENTER:
        $tags[] = 'open_col';
        $tags[] = $this->columns;
        $this->cur_col = 0;
        if (substr($match,0,6) == '~~COL:') break;
      case DOKU_LEXER_MATCHED:
        $this->cur_col++;
        if ($this->cur_col > $this->columns) {
          $tags[] = 'break';
          $this->cur_col = 1;
        } else {
          $tags[] = 'col';
        }
        break;
      case DOKU_LEXER_UNMATCHED:
        return array(false, $match);
    }
    return $tags;
  }
 
  function render($format, &$renderer, $data) {
    static $column = 4;
    static $cur_col = 0;
    if (substr($format,0,5) == 'xhtml'){
      if ($data[0] !== false) {
        while ($tag = array_shift($data)) {
          switch ($tag) {
          case 'open_col':
            $columns = array_shift($data);
            if (!$columns) $columns = 4;
            $renderer->doc .= "<table class=\"columnlist\">";
            $p = (int)(100 / $columns);
            for ($n=0;$n<$columns;$n++)
              $renderer->doc .= "<col width=\"$p%\"/>";
            $renderer->doc .= "<tr>";
            $cur_col = 0;
            break;
          case 'col':
            if ($cur_col > 0)
              $renderer->doc .= "</td>";
            $renderer->doc .= "<td>";
            $cur_col++;
            break;
          case 'break':
            if ($cur_col > 0)
              $renderer->doc .= "</td>";
            $renderer->doc .= "</tr><tr><td>";
            $cur_col=1;
            break;
          case 'end_col':
            if ($cur_col > 0) {
              $renderer->doc .= "</td>";
              for (;$cur_col < $columns;$cur_col++)
                $renderer->doc .= "<td></td>";
            }
            $renderer->doc .= "</tr></table>";
            $cur_col = 0;
            $clumns = 4;
            break;
          }
        }
      } else {
        $renderer->doc .= $renderer->_xmlEntities($data[1]);
      }
    } elseif ($format == 'metadata') {
      if ($renderer->capture) {
        if ($data[0] !== false) {
          if (in_array('open_col',$data) || in_array('col',$data) || in_array('break',$data)) {
            $renderer->doc .= DOKU_LF;
          } elseif (in_array('end_col',$data)) {
            if (strlen($renderer->doc) > 250) $renderer->capture = false;
          } else return false;
        } else {
          $renderer->doc .= $data[1];
        }
      }
      return true;
    }
    return false;
  }
}
style.css
/* plugin: columnlist */
table.columnlist {
  width: 100%;
  border:0;
}
 
/*end plugin: columnlist */