Syntax (only in leftmost(!) position):
{| <-
!! <-
|| <-
|- <-
|# <- # = [1-9] see below
!# <- # = [1-9] see below
|} <-
Horizontal & vertical alignment:
# = [1-9]
7 8 9 top-left top-center top-right
4 5 6 = middle-left middle-center middle-right
1 2 3 bottom-left bottom-center bottom-right
See also [[http://www.mediawiki.org/wiki/Help:Tables|mediawiki]].
===== Limits =====
Not supported elements:
* HTML-attributes and CSS styles;
* table's caption;
* first row token ({| ... |- ... ||)
* compact syntax (| ... || ... ||)
* rowspan and colspan attributes
==== Nested tables ====
To be continued...
===== Sources =====
* !! <-
* || <-
* |- <-
* |# <- # = [1-9] see below
* !# <- # = [1-9] see below
* |} <-
*
* Horizontal & vertical alignment:
* # = [1-9]
* 7 8 9 top-left top-center top-right
* 4 5 6 = middle-left middle-center middle-right
* 1 2 3 bottom-left bottom-center bottom-right
*
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Kuanysh K
* @date 2007-10-11
*/
// must be run within Dokuwiki
if( !defined('DOKU_INC') ) die();
if( !defined('DOKU_PLUGIN') ) define( 'DOKU_PLUGIN', DOKU_INC . 'lib/plugins/' );
require_once( DOKU_PLUGIN.'syntax.php' );
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_truetable extends DokuWiki_Syntax_Plugin
{
/**
* return some info
*/
function getInfo()
{
return array
(
'author' => 'Kuanysh K',
'email' => 'kuanyshk@google.com',
'date' => '2007-10-11',
'name' => 'truetable Plugin',
'desc' => 'Parses extended tables (similar to simplified MediaWiki).',
'url' => ''
);
}
/**
* What kind of syntax are we?
*/
function getType()
{
return 'protected';
}
/**
* What kind of plugin are we?
*/
function getPType()
{
return 'block';
}
function getAllowedTypes()
{
return array
(
'container',
'formatting',
'substition',
'disabled',
'protected',
'paragraphs'
);
}
function getSort()
{
return 55;
}
function accepts( $mode )
{
if ( $mode == substr( get_class( $this ), 7 ) ) return true;
return parent::accepts( $mode );
}
/**
* Connect pattern to lexer
*/
function connectTo( $mode )
{
// {|
$this->Lexer->addEntryPattern( "\{(?=\|)", $mode, 'plugin_truetable' );
// |}
$this->Lexer->addEntryPattern( "\n\|(?=\})", $mode, 'plugin_truetable' );
$this->Lexer->addEntryPattern( "\|(?=\})", $mode, 'plugin_truetable' );
// |-
$this->Lexer->addEntryPattern( "\n\|(?=\-)", $mode, 'plugin_truetable' );
// !! & !#
$this->Lexer->addEntryPattern( "\n\!(?=[\!0-9])", $mode, 'plugin_truetable' );
// || & |#
$this->Lexer->addEntryPattern( "\n\|(?=[\|0-9])", $mode, 'plugin_truetable' );
}
function postConnect()
{
// {|
$this->Lexer->addExitPattern( "\|", 'plugin_truetable' );
// |}
$this->Lexer->addExitPattern( "\}", 'plugin_truetable' );
// !
$this->Lexer->addExitPattern( "\-", 'plugin_truetable' );
// #
$this->Lexer->addExitPattern( "[1-9]", 'plugin_truetable' );
// |}
$this->Lexer->addExitPattern( "\!", 'plugin_truetable' );
}
/**
* Handle the match
*/
function handle( $match, $state, $pos, &$handler )
{
//error_log( $match );
switch( $state )
{
case DOKU_LEXER_ENTER:
return array( 'truetableOpen', $match );
break;
case DOKU_LEXER_UNMATCHED:
return array( 'truetableText', $match );
break;
case DOKU_LEXER_MATCHED:
return array();
break;
case DOKU_LEXER_EXIT:
return array( 'truetableClose', $match );
}
return array();
}
/**
* Create output
*/
function render( $mode, &$renderer, $data )
{
if ( $mode == 'xhtml' )
{
list( $tag, $tagData ) = $data;
switch ( $tag )
{
case 'truetableOpen':
$renderer->doc .= $this->_tagPrint( $tagData, true );
break;
case 'truetableClose':
$renderer->doc .= $this->_tagPrint( $tagData );
break;
case 'truetableText':
$renderer->doc .= $renderer->_xmlEntities( $tagData );
break;
}
return true;
}
return false;
}
// private methods
function _tagPrint( $arg = '', $onlyPrepare = false )
{
static $insideTable = false,
$insideTR = false,
$insideTH = false,
$insideTD = false,
// tell the difference between !# and |# when prepare
$willTD = false,
$willTH = false,
$alignmentsList = array(
'1' => 'class="leftalign" valign="bottom"',
'2' => 'class="centeralign" valign="bottom"',
'3' => 'class="rightalign" valign="bottom"',
'4' => 'class="leftalign" valign="middle"',
'5' => 'class="centeralign" valign="middle"',
'6' => 'class="rightalign" valign="middle"',
'7' => 'class="leftalign" valign="top"',
'8' => 'class="centeralign" valign="top"',
'9' => 'class="rightalign" valign="top"'
);
$arg = trim( $arg );
$res = '';
// prepare
if ( $onlyPrepare )
{
switch( $arg )
{
case '!':
$willTH = true;
break;
case '|':
$willTD = true;
break;
}
return $res;
}
// print tags
if ( $insideTH )
{
$res .= DOKU_TAB . DOKU_TAB . '' . DOKU_LF;
$insideTH = false;
}
else if ( $insideTD )
{
$res .= DOKU_TAB . DOKU_TAB . '' . DOKU_LF;
$insideTD = false;
}
switch( $arg )
{
// {|
// ||
case '|':
if ( $insideTable ) // ||
{
$res .= DOKU_TAB . DOKU_TAB . '' . DOKU_LF;
$insideTD = true;
}
else
{
$res .= '' .DOKU_LF;
$res .= DOKU_TAB . '' .DOKU_LF;
$insideTable = true;
$insideTR = true;
}
break;
// |}
case '}':
$res .= DOKU_TAB . ' ' .DOKU_LF;
$res .= '
' .DOKU_LF;
$insideTable = false;
$insideTR = false;
break;
// |-
case '-':
$res .= DOKU_TAB . ' ' . DOKU_LF;
break;
// !!
case '!':
$res .= DOKU_TAB . DOKU_TAB . '' . DOKU_LF;
$insideTH = true;
break;
// !# & |#
case '7': case '8': case '9':
case '4': case '5': case '6':
case '1': case '2': case '3':
if ( $willTH )
{
$res .= DOKU_TAB . DOKU_TAB . ' ' . DOKU_LF;
$insideTH = true;
}
else if ( $willTD )
{
$res .= DOKU_TAB . DOKU_TAB . ' ' . DOKU_LF;
$insideTD = true;
}
break;
}
$willTD = false;
$willTH = false;
return $res;
}
}
?>
===== Discussion =====
As great as this plugin is, it causes problems when trying to tabulate numerical data using DokuWiki's standard table notation, eg:
^10^20^30^
|123|765|252|
|44|458|8548|
|58| | |
Putting alphabetical characters in front of the numbers displays the table correctly. Would it be possible to only enable it for content between the {| and |} tags.
Thanks, Bob 10-Apr-08
Solved it myself by including a space before the numeric data :-D