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_pirate extends Doku_Renderer_xhtml {
20
21    function canRender($format) {
22      return ($format=='xhtml');
23    }
24
25    function cdata($text) {
26        $this->doc .= $this->_xmlEntities($this->pirate_filter($text));
27    }
28
29    /**
30     * This function takes an array of ('/pattern/' => 'replacement') pairs
31     * and applies them all to $content.
32     */
33    function _array_apply_regexp($patterns,$content) {
34        // Extract the values:
35        $keys = array_keys($patterns);
36        $values = array_values($patterns);
37
38        // Replace the words:
39        $content = preg_replace($keys,$values,$content);
40
41        return $content;
42    }
43
44    function pirate_filter($content) {
45        $patterns = array(
46                '%\bmy\b%' => 'me',
47                '%\bboss\b%' => 'admiral',
48                '%\bmanager\b%' => 'admiral',
49                '%\b[Cc]aptain\b%' => "Cap'n",
50                '%\bmyself\b%' => 'meself',
51                '%\byour\b%' => 'yer',
52                '%\byou\b%' => 'ye',
53                '%\bfriend\b%' => 'matey',
54                '%\bfriends\b%' => 'maties',
55                '%\bco[-]?worker\b%' => 'shipmate',
56                '%\bco[-]?workers\b%' => 'shipmates',
57                '%\bpeople\b%' => 'scallywags',
58                '%\bearlier\b%' => 'afore',
59                '%\bold\b%' => 'auld',
60                '%\bthe\b%' => "th'",
61                '%\bof\b%' =>  "o'",
62                "%\bdon't\b%" => "dern't",
63                '%\bdo not\b%' => "dern't",
64                '%\bnever\b%' => "ne'er",
65                '%\bever\b%' => "e'er",
66                '%\bover\b%' => "o'er",
67                '%\bYes\b%' => 'Aye',
68                '%\bNo\b%' => 'Nay',
69                '%\bYeah\b%' => 'Aye',
70                '%\byeah\b%' => 'aye',
71                "%\bdon't know\b%" => "dinna",
72                "%\bdidn't know\b%" => "did nay know",
73                "%\bhadn't\b%" => "ha'nae",
74                "%\bdidn't\b%"=>  "di'nae",
75                "%\bwasn't\b%" => "weren't",
76                "%\bhaven't\b%" => "ha'nae",
77                '%\bfor\b%' => 'fer',
78                '%\bbetween\b%' => 'betwixt',
79                '%\baround\b%' => "aroun'",
80                '%\bto\b%' => "t'",
81                "%\bit's\b%" => "'tis",
82                '%\bwoman\b%' => 'wench',
83                '%\bwomen\b%' => 'wenches',
84                '%\blady\b%' => 'wench',
85                '%\bwife\b%' => 'lady',
86                '%\bgirl\b%' => 'lass',
87                '%\bgirls\b%' => 'lassies',
88                '%\bguy\b%' => 'lubber',
89                '%\bman\b%' => 'lubber',
90                '%\bfellow\b%' => 'lubber',
91                '%\bdude\b%' => 'lubber',
92                '%\bboy\b%' => 'lad',
93                '%\bboys\b%' => 'laddies',
94                '%\bchildren\b%' => 'little sandcrabs',
95                '%\bkids\b%' => 'minnows',
96                '%\bhim\b%' => 'that scurvey dog',
97                '%\bher\b%' => 'that comely wench',
98                '%\bhim\.\b%' => 'that drunken sailor',
99                '%\bHe\b%' => 'The ornery cuss',
100                '%\bShe\b%' => 'The winsome lass',
101                "%\bhe's\b%" => 'he be',
102                "%\bshe's\b%" => 'she be',
103                '%\bwas\b%' => "were bein'",
104                '%\bHey\b%' => 'Avast',
105                '%\bher\.\b%' => 'that lovely lass',
106                '%\bfood\b%' => 'chow',
107                '%\bmoney\b%' => 'dubloons',
108                '%\bdollars\b%' => 'pieces of eight',
109                '%\bcents\b%' => 'shillings',
110                '%\broad\b%' => 'sea',
111                '%\broads\b%' => 'seas',
112                '%\bstreet\b%' => 'river',
113                '%\bstreets\b%' => 'rivers',
114                '%\bhighway\b%' => 'ocean',
115                '%\bhighways\b%' => 'oceans',
116                '%\binterstate\b%' => 'high sea',
117                '%\bprobably\b%' => 'likely',
118                '%\bidea\b%' => 'notion',
119                '%\bcar\b%' => 'boat',
120                '%\bcars\b%' => 'boats',
121                '%\btruck\b%' => 'schooner',
122                '%\btrucks\b%' => 'schooners',
123                '%\bSUV\b%' => 'ship',
124                '%\bairplane\b%' => 'flying machine',
125                '%\bjet\b%' => 'flying machine',
126                '%\bmachine\b%' => 'contraption',
127                '%\bdriving\b%' => 'sailing',
128                '%\bunderstand\b%' => 'reckon',
129                '%\bdrive\b%' => 'sail',
130                '%\bdied\b%' => 'snuffed it',
131                '/ing\b/' => "in'",
132                '/ings\b/' => "in's",
133                // These next two do cool random substitutions
134                '/(\.\s)/e' => 'renderer_plugin_pirate::avast("$0",3)',
135                '/([!\?]\s)/e' => 'renderer_plugin_pirate::avast("$0",2)', // Greater chance after exclamation
136                );
137
138        // Replace the words:
139        $content = $this->_array_apply_regexp($patterns,$content);
140
141        return $content;
142    }
143
144    /**
145     * support function for pirate()
146     * this could probably be refactored to make it more generic, allowing
147     * different filters to pass their own patterns in.
148     */
149    function avast($stub = '',$chance = 5) {
150        $shouts = array(
151                    ", avast$stub",
152                    "$stub Ahoy!",
153                    ", and a bottle of rum!",
154                    ", by Blackbeard's sword$stub",
155                    ", by Davy Jones' locker$stub",
156                    "$stub Walk the plank!",
157                    "$stub Aarrr!",
158                    "$stub Yaaarrrrr!",
159                    ", pass the grog!",
160                    ", and dinna spare the whip!",
161                    ", with a chest full of booty$stub",
162                    ", and a bucket o' chum$stub",
163                    ", we'll keel-haul ye!",
164                    "$stub Shiver me timbers!",
165                    "$stub And hoist the mainsail!",
166                    "$stub And swab the deck!",
167                    ", ye scurvey dog$stub",
168                    "$stub Fire the cannons!",
169                    ", to be sure$stub",
170                    ", I'll warrant ye$stub",
171                    ", on a dead man's chest!",
172                    "$stub Load the cannons!",
173                    "$stub Prepare to be boarded!",
174                    ", I'll warrant ye$stub",
175                    "$stub Ye'll be sleepin' with the fishes!",
176                    "$stub The sharks will eat well tonight!",
177                    "$stub Oho!",
178                    "$stub Fetch me spyglass!",
179                    );
180
181        shuffle($shouts);
182
183        return (((1 == rand(1,$chance))?array_shift($shouts):$stub) . ' ');
184    }
185}
186
187?>
188