<?php
/**
* Plugin Google Adsense2: include ADS into wiki content.
*
* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author     Dirk Moeller <dirk@systemengineers.de>
*/

//--- Exported code
include_once(DOKU_PLUGIN.'googleads/code.php');
//--- Exported code
 
// 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_googleads extends DokuWiki_Syntax_Plugin {
	/**
	* return some info
	*/
	function getInfo(){
		return array(
                        'author' => 'Dirk Moeller',
                        'email'  => 'dirk@systemengineers.de',
                        'date'   => '2007-08-14',
                        'name'   => 'Google Adsense Plugin',
                        'desc'   => 'Plugin to embed your Google Adsense code in your site. Based on code from Bernd Zeimetz and Terence J.',
                        'url'    => 'http://www.systemengineers.de/plugins/googleads',
                );
	}
	function getType(){ return 'substition'; }
	function getPType(){ return 'block'; }
	function getSort(){ return 160; }
	function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{googleads>[^}]*\}\}',$mode,'plugin_googleads'); }
	
	/**
	* Handle the match
	*/
	function handle($match, $state, $pos, &$handler){
		$match = substr($match,12,-2); // Strip markup
		return array($state,explode('|',$match));

//////////////////
/*
		switch ($state) {
			case DOKU_LEXER_ENTER:
			$data = $this->_getstyle(trim(substr($match, 4, -1)));
			if (substr($match, -1) == '|') {
				$this->title_mode = true;
				return array('title_open',$data);
			} else {
				return array('box_open',$data);
			}

			case DOKU_LEXER_MATCHED:
				if ($this->title_mode) {
				$this->title_mode = false;
				return array('box_open','');
			} else {
				return array('data', $match);
			}

			case DOKU_LEXER_UNMATCHED:
				return array('data', $match);

			case DOKU_LEXER_EXIT:
				$data = trim(substr($match, 5, -1));
				$title =  ($data && $data{0} == "|") ? substr($data,1) : '';

				return array('box_close', $title);
		}
		return false;
*/
//////////////////

	}

	/**
	* Create output
	*/
	function render($mode, &$renderer, $data) {
		if($mode == 'xhtml'){

                        // prevent caching (to ensure ACL conformity)
                        // TODO sometimes: make more intelligent
                        $renderer->info['cache'] = false;

			list($state, $match) = $data;
			list($id,$param1) = $match;

			//$style = $this->_getstyle(trim($param1));
			$style = gads_getstyle(trim($param1));

			//$renderer->doc .= 'debug(id=' . $id . ' param1=' . $param1 . ')<br />';
			//$renderer->doc .= '<br />';

			$renderer->doc .= gads_showADS($id, $style);
			return true;
		}
		return false;
	}
/*
	function _getstyle($str) {
		if (!strlen($str)) return array();

		$styles = array();

		$tokens = preg_split('/\s+/', $str, 9);                 // limit is defensive
		foreach ($tokens as $token) {
			if (preg_match('/^\d*\.?\d+(%|px|em|ex|pt|cm|mm|pi|in)$/', $token)) {
				$styles['width'] = $token;
				continue;
			}

		// restrict token (class names) characters to prevent any malicious data
		if (preg_match('/[^A-Za-z0-9_-]/',$token)) continue;
		$styles['class'] = (isset($styles['class']) ? $styles['class'].' ' : '').$token;
		}

		return $styles;
	}
*/
}
?>
