﻿<?php
/**
 * DokuWiki Syntax Plugin Medialist
 * 
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author  Michael Klier <chi@chimeric.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');

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

    /**
     * General Info
     */
    function getInfo(){
        return array(
            'author' => 'Patrick Lerner',
            'email'  => 'PaddyLerner@gmail.com',
            'date'   => '2008-08-02',
            'name'   => 'bashorg',
            'desc'   => 'Shows funny quotes from bash.org',
            'url'    => 'https://www.dokuwiki.org/plugin:bashorg'
        );
    }

    /**
     * Syntax Type
     *
     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
     */
    function getType()  { return 'substition'; }
    function getPType() { return 'block'; }
    function getSort()  { return 299; }
    
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('{{bashorg}}',$mode,'plugin_bashorg');
    }

    /**
     * Handler to prepare matched data for the rendering process
     */
    function handle($match, $state, $pos, &$handler){
        global $ID;

        // catch the match
        $match = substr($match,12,-2);

        // process the match
        if(empty($match) || $match == '@PAGE@') {
            return array($ID);
        } elseif(@file_exists(wikiFN($match))) {
            return array(cleanID($match));
        } else {
            return array();
        }

    }

    /**
     * Handles the actual output creation.
     */
    function render($mode, &$renderer, $data) {
        
        if($mode == 'xhtml'){
            // disable caching
            if(!empty($data[0])) {
                include("bash.org.php");
                $renderer->info['cache'] = false;
                $renderer->doc .= $output;
            }
            return true;
        }
        return false;
    }
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
