Extended Table Syntax TrueTable Plugin

truetable plugin by Kuanysh K
Parses extended tables (similar to simplified MediaWiki).

Last updated on 2007-10-13. Provides Syntax.
Compatible with DokuWiki 2006-11-06b.

Similar to exttab2.

Tagged with mediawiki, tables.

    About

    TrueTable Plugin parse ALL DokuWiki syntax correctly within table.

    Installation

    • create the folder lib/plugins/truetable
    • and copy the source code to lib/plugins/truetable/syntax.php

    Usage

    Syntax (only in leftmost(!) position):
       {|		<- <table class="inline">
       !!		<- <th>
       ||		<- <td>
       |-		<- <tr>
       |#		<- <td> # = [1-9] see below
       !#		<- <th> # = [1-9] see below
       |}		<- </table>
     
    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 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

     
    <?php
    /**
     * truetable-Plugin: Parses extended tables (similar to simplified MediaWiki).
     *
     * Syntax (only in leftmost position):
     * {|		<- <table class="inline">
     * !!		<- <th>
     * ||		<- <td>
     * |-		<- <tr>
     * |#		<- # = [1-9] see below
     * !#		<- # = [1-9] see below
     * |}		<- </table>
     *
     * 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 <kuanyshk@google.com>
     * @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 . '</th>' . DOKU_LF;
    			$insideTH = false;
    		}
    		else if ( $insideTD )
    		{
    			$res .= DOKU_TAB . DOKU_TAB . '</td>' . DOKU_LF;
    			$insideTD = false;
    		}
     
    		switch( $arg )
    		{
    			// {|
    			// ||
    			case '|':
    					if ( $insideTable )  // ||
    					{
    						$res .= DOKU_TAB . DOKU_TAB . '<td>' . DOKU_LF;
    						$insideTD = true;
    					}
    					else
    					{
    						$res .= '<table class="inline">' .DOKU_LF;
    						$res .= DOKU_TAB . '<tr>' .DOKU_LF;
    						$insideTable = true;
    						$insideTR = true;
    					}
    					break;
     
    			// |}
    			case '}':
    					$res .= DOKU_TAB . '</tr>' .DOKU_LF;
    					$res .= '</table>' .DOKU_LF;
    					$insideTable = false;
    					$insideTR = false;
    					break;
     
    			// |-
    			case '-':
    					$res .= DOKU_TAB . '</tr><tr>' . DOKU_LF;
    					break;
     
    			// !!
    			case '!':
    					$res .= DOKU_TAB . DOKU_TAB . '<th>' . 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 . '<th ' . $alignmentsList[$arg] . '>' . DOKU_LF;
    						$insideTH = true;
    					}
    					else if ( $willTD )
    					{
    						$res .= DOKU_TAB . DOKU_TAB . '<td ' . $alignmentsList[$arg] . '>' . 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:

    102030
    123765252
    444588548
    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

     
    plugin/truetable.txt · Last modified: 2009/05/06 19:47 by 195.35.72.54
     
    Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
    Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
    WikiForumIRCBugsGitXRefTranslate