<?php
/**
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Dan Kreiser <dan.kreiser@gmail.com>
 */

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


/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_googlechart extends DokuWiki_Syntax_Plugin {

    function getInfo() {
        return array(
            'author' => 'Dan Kreiser',
            'email'  => 'dan.kreiser@gmail.com',
            'date'   => '2011-03-01',
            'name'   => 'googlecharts',
            'desc'   => 'Integrate charts with the google charts api',
            'url'    => 'http://www.tux-tips.de/tux-wiki/doku.php?id=start:dokuwiki:plugins',
        );
    }
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    function getPType(){
        return 'block';
    }

    /**
     * Where to sort in?
     */
    function getSort(){
        return 160;
    }

    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern('<googlechart>\n.*?\n</googlechart>',$mode,'plugin_googlechart');
    }

    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){

        // prepare default data
        
		$return=explode("\n",$match);
        return $return;
    }

    /**
     * Create output
     */
    function render($mode, &$R, $data) {
        if($mode != 'xhtml') return false;

        $url  = 'http://chart.apis.google.com/chart?';
	  $index=0;
	  foreach ( $data as $line ) {
	  if($index==0 OR $line=='</googlechart>' ){}
	  else {$url .= $line.'&amp;';}
	  $index++;
	  }
	  $R->doc .= '<img src="'.$url.'&.png" />';
        return true;
    }
}
