<?php
/**
 * QRCode Plugin
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Daniel Pätzold <mailto://obel1x@web.de>
 */


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');
require_once(DOKU_INC.'inc/search.php');
require_once(DOKU_INC.'conf/dokuwiki.php');
    
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_qrcode2 extends DokuWiki_Syntax_Plugin {

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    /**
     * What about paragraphs?
     */
    function getPType(){
        return 'block';
    }

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


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('\{\{QRCODE>[^}]*\}\}',$mode,'plugin_qrcode2');
    }


    /**
     * Handle the match
     */
    function handle($match, $state, $pos, Doku_Handler $handler){
        $ret = urlencode(substr($match,9,-2)); //strip {{QRCODE> from start and }} from end
        return $ret;
    }

    /**
     * Create output
     */
    public function render($mode, Doku_Renderer $renderer, $data) {
		global $conf;
		
        // $data is what the function handle return'ed.
        if($mode == 'xhtml'){
            /** @var Doku_Renderer_xhtml $renderer */
			
			if(isset($conf['userewrite']) && $conf['userewrite'] == 2) {
				$renderer->doc .= '<img src="../lib/plugins/qrcode2/png.php?id='.$data.'" />';
			}
			else {
				$renderer->doc .= '<img src="lib/plugins/qrcode2/png.php?id='.$data.'" />';
			}
			
            return true;
        }
        return false;
    }
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
