<?php
/**
 * DokuWiki Plugin processing (Syntax Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Edwin Dertien <mail@edwindertien.nl>
 */

// 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';

require_once DOKU_INC.'inc/geshi.php';
require_once DOKU_INC.'inc/parser/code.php';


class syntax_plugin_processing extends DokuWiki_Syntax_Plugin {
	function getInfo() {
	        return array('author' => 'Edwin Dertien',
                     'email'  => 'mail@edwindertien.nl',
                     'date'   => '2010-08-22',
                     'name'   => 'Processing code Plugin',
                     'desc'   => 'Include processing code and embedded processing.js applet',
                     'url'    => 'http://www.dokuwiki.org/plugin:tutorial');
	}
    function getType() {
        return 'substition';
    }

    function getPType() {
        return 'normal';
    }

    function getSort() {
        return 100;
    }


    function connectTo($mode) {
        $this->Lexer->addEntryPattern('<processing>',$mode,'plugin_processing');
    }

 function postConnect() {
        $this->Lexer->addExitPattern('</processing>','plugin_processing');
    }
        
 function handle($match, $state, $pos, &$handler){
 $width = 300;
 $height = 300;
      switch ($state) {
          case DOKU_LEXER_ENTER : 
            break;
          case DOKU_LEXER_MATCHED :
            break;
          case DOKU_LEXER_UNMATCHED :
          // ugly piece of string decomposition to get width and hight from the sketch
                $numbers = substr($match,strpos($match,"size(")+5,20);
                $width = trim(substr($numbers,0,strpos($numbers,",")));
                $height = trim(substr($numbers,strpos($numbers,",")+1,strpos($numbers,");")-strpos($numbers,",")-1));
                return array($state,$match,$width,$height);
            	break;
          case DOKU_LEXER_EXIT :
            break;
          case DOKU_LEXER_SPECIAL :
            break;
        }

    }


    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
        list($state, $match,$width,$height) = $data;
        switch ($state) {
        case DOKU_LEXER_ENTER :
        	break;
        case DOKU_LEXER_UNMATCHED :	 
       $renderer->doc .=
  		"<script type=\"application/processing\">".
       $renderer->_xmlEntities($match).
        "</script><canvas width=\"".
        $width.
        "\" height=\"".
        $height.
        "\"></canvas>";

        $renderer->code($match,'java'); 
         
        $renderer->doc .= "<br>build with <a href=\"http://www.processing.org\">processing</a> and <a href=\"http://www.processingjs.org\">processing.js</a><br>";
        
        break;
        case DOKU_LEXER_EXIT :
        	break;
        default:
        break;
        }

            return true;
        }
        return false;
    }

}

?>
