<?php
/**
 * Pirate speak filter output
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 * @author Dougal Campbell
 * @link   http://dougal.gunters.org/blog/2004/08/30/text-filter-suite
 * @license GPL
 */
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

// we inherit from the XHTML renderer instead directly of the base renderer
require_once DOKU_INC.'inc/parser/xhtml.php';
                     
/**
 * The Renderer
 */
class renderer_plugin_mundart extends Doku_Renderer_xhtml {

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Christoph Lang',
            'email'  => 'c.lang@cc-tech.de',
            'date'   => '2009-12-02',
            'name'   => 'Mundart',
            'desc'   => 'Apply german Mundarts to your (X)Html Output',
            'url'    => 'http://wiki.splitbrain.org/plugin:mundart',
        );
    }
    
    function canRender($format) {
      return ($format=='xhtml');
    }
		
		private $header = false;
    function cdata($text) {
    		
    		if($this->getConf('mundart_alwayson') == '0'){
    			$aDays =  explode(";",$this->getConf('mundart_timer'));
    			
    			if(!in_array(date('m-d'),$aDays)){
	    			$this->doc .= $this->_xmlEntities($text);
	    			return;
	    		}
    		}
    			
    		switch($this->getConf('mundart_type')){
    			case '0': $this->doc .= $this->_xmlEntities($text); return;
    			case '1': $filter = 'schwabe_filter'; break;
    			case '2': $filter = 'sachse_filter'; break;
    			case '3': $filter = 'ghetto_filter'; break;
    			case '4': $filter = 'leet_filter'; break;
    			default: $this->doc .= $this->_xmlEntities($text); return;
    		}
    			
    		
        $this->doc .= $this->_xmlEntities($this->$filter($text));
        
        if($this->getConf('mundart_headeron') == '1')
	        if(!$this->header){
	        	$this->header=true;
	        	$info = $this->getInfo();
	        	$text = str_replace('[url]',$info['url'],$this->getLang('header'));
	        	$this->doc = '<div class="redbox">'.$text.'</div>'.$this->doc;
	        }
    }

    /**
     * This function takes an array of ('/pattern/' => 'replacement') pairs
     * and applies them all to $content.
     */
    function _array_apply_regexp($patterns,$content) {
        // Extract the values:
        $keys = array_keys($patterns);
        $values = array_values($patterns);

        // Replace the words:
        $content = preg_replace($keys,$values,$content);

        return $content;
    }

    function sachse_filter($content) {
        $patterns = array(
                '%\bdas\b%' => 'des',
                '%Ka%' => 'Ga',
                '%rk%' => 'rg',
                '%Pr%' => 'Br',
                '%akt%' => 'agd',
                '%st%' => 'schd',
                '%ta%' => 'da',
                '%tu%' => 'du',
                '%len%' => 'ln',
                '%sp%' => 'schb',
                '%gen%' => 'gn',
                '%ekt%' => 'egd',
                '%Ku%' => 'Gu',
                '%Te%' => 'De',
                '%ik%' => 'ig',
                '%ten%' => 'den',
                '%ich%' => 'isch',
                '%ati%' => 'adsi',
                '%at%' => 'ad',
                '%ech%' => 'esch',
                '%äch%' => 'äsch',
                '%ein%' => 'een',
                '%enen\b%' => 'en\'',
                '%nen\b%' => 'en\'',
                '%en%' => 'n',
                '%tz%' => 'dz',
                '%ay%' => 'aü',
                '%te%' => 'de',
                '%TE%' => 'DE',
                '%et%' => 'ed',
                '%pr%' => 'br',
                '%uk%' => 'ug',
                '%ko%' => 'go',
                '%ka%' => 'ga',
                '%kö%' => 'gö',
                '%tr%' => 'dr',
                '%nk%' => 'ng',
                '%ti%' => 'di',
                '%lt%' => 'ld',
                '%ft%' => 'fd',
                '%wa%' => 'we',
                '%tw%' => 'dw',
                '%ck%' => 'gg',
                '%it%' => 'id',
                '%rt%' => 'rd',
                '%pe%' => 'be',
                
                '%und\b%' => 'unn',
                '%\beine\b%' => 'eene',
                '%\bdie\b%' => 'de',
                '%\bder\b%' => 'dor',
                
                
                
                
                
                
                '/ing\b/' => "in'",
                '/ings\b/' => "in's",
                // These next two do cool random substitutions
                '/(\.\s)/e' => 'renderer_plugin_mundart::avast("$0",3)',
                '/([!\?]\s)/e' => 'renderer_plugin_mundart::avast("$0",2)', // Greater chance after exclamation
                );

        // Replace the words:
        $content = $this->_array_apply_regexp($patterns,$content);

        return $content;
    }
    
    function schwabe_filter($content) {
        $patterns = array(
        
                '%Google%' => 'Datenkrake',
                '%cken%' => 'gga',
                '%tte%' => 'dde',
                '%st%' => 'schd',
                '%te%' => 'de',
                '%on\b%' => 'o',
                '%en\b%' => 'et',
                '%er\b%' => 'r',
                '%\bauf\b%' => 'uf',
                '%che\b%' => 'ch\'',
                '%ein%' => 'oi',
                '%pe%' => 'be',
                '%ta%' => 'da',
                '%ge\b%' => 'g',
                '%rt%' => 'rd',
                '%\bund\b%' => 'un',
                '%en\b%' => 'a',                
                '%at\b%' => 'edd',
                '%ati%' => 'azi',
                '%kt%' => 'kd',
                '%tu%' => 'du',
                '%ck%' => 'gg',
                '%t\b%' => 'd',
                '%ki%' => 'ggi',
                '%k\b%' => 'g',
                
                '%er\b%' => 'r',
                '%lt%' => 'ld',
                '%ft%' => 'fd',
                '%\brichtig\b%' => 'rechte',
                '%\bnicht\b%' => 'nedd',
                '%\bdenn\b%' => 'noh',
                '%\bsich\b%' => 'si',
                '%\bdie\b%' => 'd',
                '%\bdas\b%' => 'des',
                '%\?%' => ', gell?',
                
                
                
                );

        // Replace the words:
        $content = $this->_array_apply_regexp($patterns,$content);

        return $content;
    }
    
    function leet_filter($content) {
        $patterns = array(
        
                '%a%' => '4',
                '%b%' => 'B',
                '%c%' => '(',
                '%d%' => 'D',
                '%e%' => '3',
                '%f%' => 'Ph',
                '%g%' => '9',
                '%h%' => '|-|',
                '%i%' => '1',
                '%j%' => 'j',
                '%k%' => '|<',
                '%l%' => 'L',
                '%m%' => '/\\/\\',
                '%n%' => '|\\|',
                '%o%' => '0',
                '%p%' => 'P',
                '%q%' => 'Q',
                '%r%' => 'r',
                '%s%' => '$',
                '%t%' => '7',
                '%u%' => 'U',
                '%v%' => '\\/',
                '%w%' => '\\/\\/',
                '%x%' => '><',
                '%y%' => '\'/',
                '%z%' => 'Z',                
                
                
                );

        // Replace the words:
        $content = $this->_array_apply_regexp($patterns,$content);

        return $content;
    }
    
    function ghetto_filter($content) {
        $patterns = array(
        
                '%\bein\b%' => '',
                '%\bnur\b%' => '',
                '%\Ihre\b%' => 'Dein',
                '%\ihr\b%' => 'du',
                '%\beuch\b%' => '',
                '%\bSie\b%' => 'Du',
                '%\bIch\b%' => 'Isch',
                '%\bbin\b%' => 'war',
                '%\bso\b%' => '',
                
                '%ei%' => 'ai',
                '%zw%' => 'sw',
                '%er\b%' => 'a',
                '%e\b%' => '',
                '%em\b%' => 'es',
                '%tt%' => 'dd',
                '%ten\b%' => 'dn',
                '%nen\b%' => 'n',
                '%en\b%' => 'n',
                '%ss\b%' => 's',
                
                
                
                );

        // Replace the words:
        $content = $this->_array_apply_regexp($patterns,$content);

        return $content;
    }


    /**
     * support function for pirate()
     * this could probably be refactored to make it more generic, allowing
     * different filters to pass their own patterns in.
     */
    function avast($stub = '',$chance = 5) {
        $shouts = array(
                    ", des kennste abr glaubn$stub",
                    );

        shuffle($shouts);

        return (((1 == rand(1,$chance))?array_shift($shouts):$stub) . ' ');
    }
}

?>
