<?php
/**
 * EasyVar Plugin: allows to insert your own variables
 *
 * Syntax : @variable@ will be replaced with valeurVariable
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Joel Dare <joel@joeldare.com>
 *
 * Based on KixoVar by Kixo SARL <infos@kixo.fr>.  Adds two improvements. First,
 * variables can be any length (‭KixoVar is limited to 2 to 5 characters). 
 * Second, you can use wiki syntax and HTML inside the variables.
 */

if(!defined('DOKU_INC')) die('Error');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
class syntax_plugin_easyVar extends DokuWiki_Syntax_Plugin {

  function getInfo(){
    return array(
      'author' => 'Joel Dare',
      'email'  => 'joel@joeldare.com',
      'date'   => '2009-12-28',
      'name'   => 'easyVar',
      'desc'   => 'Insert your own variables',
      'url'    => 'http://www.joeldare.com'
    );
  }

  function getType(){ return 'formatting'; }
  function getSort(){ return 999; } 
  function connectTo($mode) { $this->Lexer->addSpecialPattern('@\w*@', $mode, 'plugin_easyVar'); }

  function handle($match, $state, $pos, &$handler){
    $match = substr($match, 1, -1); // strip markup
    return array($match);
  }

  function render($mode, &$renderer, $data) {
    
    $var=$this->getConf('var');
    
    $meta    = $data[0];
    $nocache = false;
    $xhtml   = $meta;
    
    if($mode=='xhtml' and sizeof($var)>0){
		foreach($var as $key=>$value){
			if($key==$meta){
				$xhtml=$value;
			}
		}
      
		//$renderer->doc .= hsc($xhtml);
		$renderer->doc .= $xhtml;
    	return true;
	}
	return false;
	}
}
