<?php
/**
 * DokuWiki Plugin floatdiv (Syntax Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Tinkerghost <geek@rentedgeek.com>
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

require_once DOKU_PLUGIN.'syntax.php';

class syntax_plugin_floatdiv_float extends DokuWiki_Syntax_Plugin {
    public function getType() {
        return 'container';
    }

    public function getPType() {
        return 'stack';
    }

    public function getSort() {
        return 500;
    }

    public function getAllowedTypes() {
//		return array('formatting','substitution','protected','disabled','paragraphs');
        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
    }

    public function connectTo($mode) {
//        $this->Lexer->addSpecialPattern('\[float*\]',$mode,'plugin_floatdiv_float');
        $this->Lexer->addEntryPattern('\[float.*?\]',$mode,'plugin_floatdiv_float');
    }

    public function postConnect() {
        $this->Lexer->addExitPattern('\[/float\]','plugin_floatdiv_float');
    }


    public function handle($match, $state, $pos, &$handler){
        $data = array();
    	switch ($state){
	    case DOKU_LEXER_ENTER :
	        $matches = preg_split('/\]/u',$match,2);
		if (trim($matches[0]) == ''){
		    $matches[0]=NULL;
		}
		else{
		    $matches[0] = trim($matches[0]);
		}
		//** Parsing Parameters **
		preg_match('/(right|left)/i', $matches[0],$side);
		preg_match('/width=([0-9]+)/i', $matches[0],$width);
		preg_match('/size=([0-9]+)/i', $matches[0], $size);
		preg_match('/background=#([0-9a-fA-F]{6})/i', $matches[0], $background);
		return array ($state,$side[1],$width[1],$size[1],$background[1]);
	    case DOKU_LEXER_UNMATCHED : return array($state, $match);
	    case DOKU_LEXER_EXIT : return array($state, '');
	}
	return array();

    }

    public function render($mode, &$renderer, $data) {
        if($mode != 'xhtml') return false;
		list($state,$match) = $data;
        
        switch($state) {
	    case DOKU_LEXER_ENTER :
		list($state, $side,$width,$size,$background) = $data;
		$style = '';
		if ($side){$style .= "float: $side;";}
		if ($width){
			if ($width < 50){$style .= "width: 50px;";}
			elseif ($width > 500){$style .= "width: 500px;";}
			else{$style .= "width: ".$width."px;";}
		}
		if ($size){
			if ($size<8){$style .= "font-size: 8px;";}
			elseif ($size >20){$style .= "font-size: 20px;";}
			else {$style .= "font-size: ".$size."px;";}
		}
		if ($background) {$style .= "background-color:#$background;";}
   		$renderer->doc .="<div class='floatpane' style='$style'>";
		break;
	    case DOKU_LEXER_UNMATCHED : 
			$renderer->doc .= $renderer->_xmlEntities($match); break;
	    case DOKU_LEXER_EXIT : $renderer->doc .= "</div>"; break;
        }

        return true;
    }
}

// vim:ts=4:sw=4:et:
