1<?php
2/**
3 * Pirate speak filter output
4 *
5 * @author Andreas Gohr <andi@splitbrain.org>
6 * @author Dougal Campbell
7 * @link   http://dougal.gunters.org/blog/2004/08/30/text-filter-suite
8 * @license GPL
9 */
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13// we inherit from the XHTML renderer instead directly of the base renderer
14require_once DOKU_INC.'inc/parser/xhtml.php';
15
16/**
17 * The Renderer
18 */
19class renderer_plugin_mundart extends Doku_Renderer_xhtml {
20
21    /**
22     * return some info
23     */
24    function getInfo(){
25        return array(
26            'author' => 'Christoph Lang',
27            'email'  => 'c.lang@cc-tech.de',
28            'date'   => '2009-12-02',
29            'name'   => 'Mundart',
30            'desc'   => 'Apply german Mundarts to your (X)Html Output',
31            'url'    => 'http://wiki.splitbrain.org/plugin:mundart',
32        );
33    }
34
35    function canRender($format) {
36      return ($format=='xhtml');
37    }
38
39		private $header = false;
40    function cdata($text) {
41
42    		if($this->getConf('mundart_alwayson') == '0'){
43    			$aDays =  explode(";",$this->getConf('mundart_timer'));
44
45    			if(!in_array(date('m-d'),$aDays)){
46	    			$this->doc .= $this->_xmlEntities($text);
47	    			return;
48	    		}
49    		}
50
51    		switch($this->getConf('mundart_type')){
52    			case '0': $this->doc .= $this->_xmlEntities($text); return;
53    			case '1': $filter = 'schwabe_filter'; break;
54    			case '2': $filter = 'sachse_filter'; break;
55    			case '3': $filter = 'ghetto_filter'; break;
56    			case '4': $filter = 'leet_filter'; break;
57    			default: $this->doc .= $this->_xmlEntities($text); return;
58    		}
59
60
61        $this->doc .= $this->_xmlEntities($this->$filter($text));
62
63        if($this->getConf('mundart_headeron') == '1')
64	        if(!$this->header){
65	        	$this->header=true;
66	        	$info = $this->getInfo();
67	        	$text = str_replace('[url]',$info['url'],$this->getLang('header'));
68	        	$this->doc = '<div class="redbox">'.$text.'</div>'.$this->doc;
69	        }
70    }
71
72    /**
73     * This function takes an array of ('/pattern/' => 'replacement') pairs
74     * and applies them all to $content.
75     */
76    function _array_apply_regexp($patterns,$content) {
77        // Extract the values:
78        $keys = array_keys($patterns);
79        $values = array_values($patterns);
80
81        // Replace the words:
82        $content = preg_replace($keys,$values,$content);
83
84        return $content;
85    }
86
87    function sachse_filter($content) {
88        $patterns = array(
89                '%\bdas\b%' => 'des',
90                '%Ka%' => 'Ga',
91                '%rk%' => 'rg',
92                '%Pr%' => 'Br',
93                '%akt%' => 'agd',
94                '%st%' => 'schd',
95                '%ta%' => 'da',
96                '%tu%' => 'du',
97                '%len%' => 'ln',
98                '%sp%' => 'schb',
99                '%gen%' => 'gn',
100                '%ekt%' => 'egd',
101                '%Ku%' => 'Gu',
102                '%Te%' => 'De',
103                '%ik%' => 'ig',
104                '%ten%' => 'den',
105                '%ich%' => 'isch',
106                '%ati%' => 'adsi',
107                '%at%' => 'ad',
108                '%ech%' => 'esch',
109                '%äch%' => 'äsch',
110                '%ein%' => 'een',
111                '%enen\b%' => 'en\'',
112                '%nen\b%' => 'en\'',
113                '%en%' => 'n',
114                '%tz%' => 'dz',
115                '%ay%' => 'aü',
116                '%te%' => 'de',
117                '%TE%' => 'DE',
118                '%et%' => 'ed',
119                '%pr%' => 'br',
120                '%uk%' => 'ug',
121                '%ko%' => 'go',
122                '%ka%' => 'ga',
123                '%kö%' => 'gö',
124                '%tr%' => 'dr',
125                '%nk%' => 'ng',
126                '%ti%' => 'di',
127                '%lt%' => 'ld',
128                '%ft%' => 'fd',
129                '%wa%' => 'we',
130                '%tw%' => 'dw',
131                '%ck%' => 'gg',
132                '%it%' => 'id',
133                '%rt%' => 'rd',
134                '%pe%' => 'be',
135
136                '%und\b%' => 'unn',
137                '%\beine\b%' => 'eene',
138                '%\bdie\b%' => 'de',
139                '%\bder\b%' => 'dor',
140
141
142
143
144
145
146                '/ing\b/' => "in'",
147                '/ings\b/' => "in's",
148                // These next two do cool random substitutions
149                '/(\.\s)/e' => 'renderer_plugin_mundart::avast("$0",3)',
150                '/([!\?]\s)/e' => 'renderer_plugin_mundart::avast("$0",2)', // Greater chance after exclamation
151                );
152
153        // Replace the words:
154        $content = $this->_array_apply_regexp($patterns,$content);
155
156        return $content;
157    }
158
159    function schwabe_filter($content) {
160        $patterns = array(
161
162                '%Google%' => 'Datenkrake',
163                '%cken%' => 'gga',
164                '%tte%' => 'dde',
165                '%st%' => 'schd',
166                '%te%' => 'de',
167                '%on\b%' => 'o',
168                '%en\b%' => 'et',
169                '%er\b%' => 'r',
170                '%\bauf\b%' => 'uf',
171                '%che\b%' => 'ch\'',
172                '%ein%' => 'oi',
173                '%pe%' => 'be',
174                '%ta%' => 'da',
175                '%ge\b%' => 'g',
176                '%rt%' => 'rd',
177                '%\bund\b%' => 'un',
178                '%en\b%' => 'a',
179                '%at\b%' => 'edd',
180                '%ati%' => 'azi',
181                '%kt%' => 'kd',
182                '%tu%' => 'du',
183                '%ck%' => 'gg',
184                '%t\b%' => 'd',
185                '%ki%' => 'ggi',
186                '%k\b%' => 'g',
187
188                '%er\b%' => 'r',
189                '%lt%' => 'ld',
190                '%ft%' => 'fd',
191                '%\brichtig\b%' => 'rechte',
192                '%\bnicht\b%' => 'nedd',
193                '%\bdenn\b%' => 'noh',
194                '%\bsich\b%' => 'si',
195                '%\bdie\b%' => 'd',
196                '%\bdas\b%' => 'des',
197                '%\?%' => ', gell?',
198
199
200
201                );
202
203        // Replace the words:
204        $content = $this->_array_apply_regexp($patterns,$content);
205
206        return $content;
207    }
208
209    function leet_filter($content) {
210        $patterns = array(
211
212                '%a%' => '4',
213                '%b%' => 'B',
214                '%c%' => '(',
215                '%d%' => 'D',
216                '%e%' => '3',
217                '%f%' => 'Ph',
218                '%g%' => '9',
219                '%h%' => '|-|',
220                '%i%' => '1',
221                '%j%' => 'j',
222                '%k%' => '|<',
223                '%l%' => 'L',
224                '%m%' => '/\\/\\',
225                '%n%' => '|\\|',
226                '%o%' => '0',
227                '%p%' => 'P',
228                '%q%' => 'Q',
229                '%r%' => 'r',
230                '%s%' => '$',
231                '%t%' => '7',
232                '%u%' => 'U',
233                '%v%' => '\\/',
234                '%w%' => '\\/\\/',
235                '%x%' => '><',
236                '%y%' => '\'/',
237                '%z%' => 'Z',
238
239
240                );
241
242        // Replace the words:
243        $content = $this->_array_apply_regexp($patterns,$content);
244
245        return $content;
246    }
247
248    function ghetto_filter($content) {
249        $patterns = array(
250
251                '%\bein\b%' => '',
252                '%\bnur\b%' => '',
253                '%\Ihre\b%' => 'Dein',
254                '%\ihr\b%' => 'du',
255                '%\beuch\b%' => '',
256                '%\bSie\b%' => 'Du',
257                '%\bIch\b%' => 'Isch',
258                '%\bbin\b%' => 'war',
259                '%\bso\b%' => '',
260
261                '%ei%' => 'ai',
262                '%zw%' => 'sw',
263                '%er\b%' => 'a',
264                '%e\b%' => '',
265                '%em\b%' => 'es',
266                '%tt%' => 'dd',
267                '%ten\b%' => 'dn',
268                '%nen\b%' => 'n',
269                '%en\b%' => 'n',
270                '%ss\b%' => 's',
271
272
273
274                );
275
276        // Replace the words:
277        $content = $this->_array_apply_regexp($patterns,$content);
278
279        return $content;
280    }
281
282
283    /**
284     * support function for pirate()
285     * this could probably be refactored to make it more generic, allowing
286     * different filters to pass their own patterns in.
287     */
288    function avast($stub = '',$chance = 5) {
289        $shouts = array(
290                    ", des kennste abr glaubn$stub",
291                    );
292
293        shuffle($shouts);
294
295        return (((1 == rand(1,$chance))?array_shift($shouts):$stub) . ' ');
296    }
297}
298
299?>
300