Handle French typographical rules for spaces before and after punctuation
[lhc/web/wiklou.git] / includes / Tokenizer.php
1 <?php
2 class Tokenizer {
3 /* private */ var $mText, # Text to be processed by the tokenizer
4 $mPos, # current position of tokenizer in text
5 $mTextLength, # Length of $mText
6 $mQueuedToken; # Tokens that were already found, but not
7 # returned yet.
8
9 /* private */ function Tokenizer()
10 {
11 global $wgLang;
12
13 $this->mPos=0;
14 $this->mTokenQueue=array();
15 $this->linkPrefixExtension = $wgLang->linkPrefixExtension();
16 }
17
18 # factory function
19 function newFromString( $s )
20 {
21 $fname = "Tokenizer::newFromString";
22 wfProfileIn( $fname );
23
24 $t = new Tokenizer();
25 $t->mText = $s;
26 $t->mTextLength = strlen( $s );
27
28 wfProfileOut( $fname );
29 return $t;
30 }
31
32
33 // Return the next token, but do not increase the pointer. The next call
34 // to previewToken or nextToken will return the same token again.
35 // Actually, the pointer is increased, but the token is queued. The next
36 // call to previewToken or nextToken will check the queue and return
37 // the stored token.
38 function previewToken()
39 {
40 $fname = "Tokenizer::previewToken";
41 wfProfileIn( $fname );
42
43 if ( count( $this->mQueuedToken ) != 0 ) {
44 // still one token from the last round around. Return that one first.
45 $token = $this->mQueuedToken[0];
46 } else {
47 $token = $this->nextToken();
48 array_unshift( $this->mQueuedToken, $token );
49 }
50
51 wfProfileOut( $fname );
52 return $token;
53 }
54
55
56 // get the next token
57 // proceeds character by character through the text, looking for characters needing
58 // special attention. Those are currently: I, R, ', [, ], newline
59 //
60 // TODO: handling of French blanks not yet implemented
61 function nextToken()
62 {
63 $fname = "Tokenizer::nextToken";
64 wfProfileIn( $fname );
65
66 if ( count( $this->mQueuedToken ) != 0 ) {
67 // still one token from the last round around. Return that one first.
68 $token = array_shift( $this->mQueuedToken );
69 } else if ( $this->mPos > $this->mTextLength ) {
70 // If no text is left, return "false".
71 $token = false;
72 } else {
73
74 $token["text"]="";
75 $token["type"]="text";
76
77 while ( $this->mPos <= $this->mTextLength ) {
78 switch ( @$ch = $this->mText[$this->mPos] ) {
79 case 'R': // for "RFC "
80 if ( $this->continues("FC ") ) {
81 $queueToken["type"] = $queueToken["text"] = "RFC ";
82 $this->mQueuedToken[] = $queueToken;
83 $this->mPos += 3;
84 break 2; // switch + while
85 }
86 break;
87 case 'I': // for "ISBN "
88 if ( $this->continues("SBN ") ) {
89 $queueToken["type"] = $queueToken["text"] = "ISBN ";
90 $this->mQueuedToken[] = $queueToken;
91 $this->mPos += 4;
92 break 2; // switch + while
93 }
94 break;
95 case "[": // for links "[["
96 if ( $this->continues("[[") ) {
97 $queueToken["type"] = "[[[";
98 $queueToken["text"] = "";
99 $this->mQueuedToken[] = $queueToken;
100 $this->mPos += 3;
101 break 2; // switch + while
102 } else if ( $this->continues("[") ) {
103 $queueToken["type"] = "[[";
104 $queueToken["text"] = "";
105 // Check for a "prefixed link", e.g. Al[[Khazar]]
106 // Mostly for arabic wikipedia
107 if ( $this->linkPrefixExtension ) {
108 while ( $this->linkPrefixExtension
109 && ($len = strlen( $token["text"] ) ) > 0
110 && !ctype_space( $token["text"][$len-1] ) )
111 {
112 //prepend the character to the link's open tag
113 $queueToken["text"] = $token["text"][$len-1] . $queueToken["text"];
114 //remove character from the end of the text token
115 $token["text"] = substr( $token["text"], 0, -1);
116 }
117 }
118 $this->mQueuedToken[] = $queueToken;
119 $this->mPos += 2;
120 break 2; // switch + while
121 }
122 break;
123 case "]": // for end of links "]]"
124 if ( $this->continues("]") ) {
125 $queueToken["type"] = "]]";
126 $queueToken["text"] = "";
127 $this->mQueuedToken[] = $queueToken;
128 $this->mPos += 2;
129 break 2; // switch + while
130 }
131 break;
132 case "'": // for all kind of em's and strong's
133 if ( $this->continues("'") ) {
134 $queueToken["type"] = "'";
135 $queueToken["text"] = "";
136 while( ($this->mPos+1 < $this->mTextLength)
137 && $this->mText[$this->mPos+1] == "'" )
138 {
139 $queueToken["type"] .= "'";
140 $this->mPos ++;
141 }
142
143 $this->mQueuedToken[] = $queueToken;
144 $this->mPos ++;
145 break 2; // switch + while
146 }
147 break;
148 case "\n": // for block levels, actually, only "----" is handled.
149 case "\r":
150 if ( $this->continues( "----" ) )
151 {
152 $queueToken["type"] = "----";
153 $queueToken["text"] = "";
154 $this->mQueuedToken[] = $queueToken;
155 $this->mPos += 5;
156 while ( $this->mPos<$this->mTextLength
157 and $this->mText[$this->mPos] == "-" )
158 {
159 $this->mPos ++;
160 }
161 break 2;
162 }
163 break;
164 case "!": // French spacing rules have a space before exclamation
165 case "?": // and question marks. Those have to become &nbsp;
166 case ":": // And colons, Hashar says ...
167 if ( $this->preceeded( " " ) )
168 {
169 // strip blank from Token
170 $token["text"] = substr( $token["text"], 0, -1 );
171 $queueToken["type"] = "blank";
172 $queueToken["text"] = " {$ch}";
173 $this->mQueuedToken[] = $queueToken;
174 $this->mPos ++;
175 break 2; // switch + while
176 }
177 break;
178 case "0": // A space between two numbers is used to ease reading
179 case "1": // of big numbers, e.g. 1 000 000. Those spaces need
180 case "2": // to be unbreakable
181 case "3":
182 case "4":
183 case "5":
184 case "6":
185 case "7":
186 case "8":
187 case "9":
188 if ( ($this->mTextLength >= $this->mPos +2)
189 && ($this->mText[$this->mPos+1] == " ")
190 && ctype_digit( $this->mText[$this->mPos+2] ) )
191 {
192 $queueToken["type"] = "blank";
193 $queueToken["text"] = $ch . " ";
194 $this->mQueuedToken[] = $queueToken;
195 $this->mPos += 2;
196 break 2; // switch + while
197 }
198 break;
199 case "\302": // first byte of UTF-8 Character Guillemet-left
200 if ( $this->continues( "\253 ") ) // second byte and a blank
201 {
202 $queueToken["type"] = "blank";
203 $queueToken["text"] = "\302\253 ";
204 $this->mQueuedToken[] = $queueToken;
205 $this->mPos += 3;
206 break 2; // switch + while
207 }
208 break;
209 case "\273": //last byte of UTF-8 Character Guillemet-right
210 if ( $this->preceeded( " \302" ) )
211 {
212 $queueToken["type"] = "blank";
213 $queueToken["text"] = " \302\273";
214 $token["text"] = substr( $token["text"], 0, -2 );
215 $this->mQueuedToken[] = $queueToken;
216 $this->mPos ++;
217 break 2; // switch + while
218 }
219 break;
220
221 } /* switch */
222 $token["text"].=$ch;
223 $this->mPos ++;
224 // echo $this->mPos . "<br>\n";
225 } /* while */
226 } /* if (nothing left in queue) */
227
228 wfProfileOut( $fname );
229 return $token;
230 }
231
232 // function continues
233 // checks whether the mText continues with $cont from mPos+1
234 /* private */ function continues( $cont )
235 {
236 // If string is not long enough to contain $cont, return false
237 if ( $this->mTextLength < $this->mPos + strlen( $cont ) )
238 return false;
239 for ( $i=0; $i < strlen( $cont ); $i++ )
240 {
241 if ( $this->mText[$this->mPos+1+$i] != $cont[$i] )
242 return false;
243 }
244 return true;
245 }
246
247 // function preceeded
248 // checks whether the mText is preceeded by $prec at position mPos
249 /* private */ function preceeded( $prec )
250 {
251 $len = strlen( $prec );
252 // if $prec is longer than the text up to mPos, return false
253 if ( $this->mPos < $len )
254 return false;
255 return ( 0 == strcmp( $prec, substr($this->mText, $this->mPos-$len, $len) ) );
256 }
257 }
258