Index: trunk/extensions/Transliterator/Transliterator.php |
— | — | @@ -16,7 +16,8 @@ |
17 | 17 | * initial creation. |
18 | 18 | * @version 1.0.1 |
19 | 19 | * better i18n support, adjustable limits, minor formal adjustment. |
20 | | - * |
| 20 | + * @version 1.1.0 |
| 21 | + * addition of answer parameter |
21 | 22 | */ |
22 | 23 | |
23 | 24 | /** |
— | — | @@ -48,10 +49,10 @@ |
49 | 50 | |
50 | 51 | $wgExtensionCredits['parserhook'][] = array( |
51 | 52 | 'name' => 'Transliterator', |
52 | | - 'version' => '1.0.1', |
| 53 | + 'version' => '1.1.0', |
53 | 54 | 'descriptionmsg' => 'transliterator-desc', |
54 | 55 | 'author' => 'Conrad Irwin', |
55 | | - 'url' => 'http://en.wiktionary.org/wiki/User:Conrad.Irwin/Transliterator.php', |
| 56 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:Transliterator', |
56 | 57 | 'path' => __FILE__, |
57 | 58 | ); |
58 | 59 | |
— | — | @@ -173,12 +174,19 @@ |
174 | 175 | $decompose = false; |
175 | 176 | |
176 | 177 | // Split lines and remove comments and space |
177 | | - $lines = split( "\n", html_entity_decode( preg_replace( '/^(\s*#.*)?\n| */m', '', "$input" ), ENT_NOQUOTES, "UTF-8" ) ); |
| 178 | + $lines = split( "\n", html_entity_decode( preg_replace( '/^\s*(#.*)?(\n|$)| */m', '', $input ), ENT_NOQUOTES, "UTF-8" ) ); |
178 | 179 | |
| 180 | + // If the last line was a comment then there will be an empty line at the end |
| 181 | + if ( $lines[count( $lines ) - 1] == "" ) { |
| 182 | + array_pop( $lines ); |
| 183 | + } |
| 184 | + |
179 | 185 | if ( $lines[0] == "<decompose>" ) { |
180 | 186 | $map['__decompose__'] = true; |
181 | 187 | array_shift( $lines ); |
182 | 188 | $decompose = true; |
| 189 | + } else if ( $lines[0] == "<$mappage>" ) { |
| 190 | + return false; |
183 | 191 | } |
184 | 192 | |
185 | 193 | if ( count( $lines ) > $wgTransliteratorRuleCount ) |
— | — | @@ -231,11 +239,12 @@ |
232 | 240 | */ |
233 | 241 | function transliterate( $word, $map ) |
234 | 242 | { |
235 | | - $word = "^" . str_replace(" ", "$ ^", $word) . "$"; |
| 243 | + $word = "^" . str_replace( " ", "$ ^", $word ) . "$"; |
236 | 244 | if ( isset( $map["__decompose__"] ) ) { |
237 | 245 | $letters = $this->codepoints( $word ); |
238 | | - }else |
| 246 | + } else { |
239 | 247 | $letters = $this->letters( $word ); |
| 248 | + } |
240 | 249 | |
241 | 250 | $output = ""; // The output |
242 | 251 | $last_match = 0; // The position of the last character matched, or the first character of the current run |
— | — | @@ -244,7 +253,7 @@ |
245 | 254 | $count = count($letters); // The total number of characters in the string |
246 | 255 | $current = ""; // The substring that we are currently trying to find the longest match for. |
247 | 256 | |
248 | | - while ($i < $count) { |
| 257 | + while ( $i < $count ) { |
249 | 258 | |
250 | 259 | $next = $current.$letters[$i]; |
251 | 260 | |
— | — | @@ -294,17 +303,28 @@ |
295 | 304 | } |
296 | 305 | |
297 | 306 | /** |
298 | | - * {{#transliterate:<mapname>|<word>[|<format>[|<onerror>]]}} |
| 307 | + * {{#transliterate:<mapname>|<word>[|<format>[|<answer>[|<onerror>]]]}} |
299 | 308 | * |
300 | | - * It is envisaged that most usage is in the form {{#transliterate:<mapname>|<word>}} |
301 | | - * However, when in use in multi-purpose templates, it would be very ugly to have |
302 | | - * {{#if}}s around all calls to {{#transliterate}} to check whether the map |
303 | | - * exists. The further two arguments can thus give very flexible output with |
304 | | - * minimal hassle. |
| 309 | + * Direct usage will generally be of the form {{#transilterate:<mapname>|<word>}} while |
| 310 | + * generic templates may find the latter three parameters invaluable for easy use. |
| 311 | + * |
| 312 | + * $mapname is the name of the transliteration map to find. |
| 313 | + * $word is the string to transliterate (if the map was found) |
| 314 | + * $format is a string containing $1 to be replaced by the transliteration if the map exists |
| 315 | + * $answer allows for a user-specified transliteration to override the automatic one |
| 316 | + * $other is an error messsage to display if $answer is blank and an invalid map is specified |
305 | 317 | */ |
306 | | - function render( &$parser, $mapname = '', $word = '', $format = '$1', $other = '' ) { |
| 318 | + function render( &$parser, $mapname = '', $word = '', $format = '$1', $answer = '', $other = '' ) { |
307 | 319 | |
308 | | - $prefix = wfMsg('transliterator-prefix'); |
| 320 | + if ( trim( $format ) == '') { // Handle the case when people use {{#transliterate:<>|<>||<>}} |
| 321 | + $format = '$1'; |
| 322 | + } |
| 323 | + |
| 324 | + if ( trim( $answer ) != '') { |
| 325 | + return str_replace('$1', $answer, $format); |
| 326 | + } |
| 327 | + |
| 328 | + $prefix = wfMsg( 'transliterator-prefix' ); |
309 | 329 | $mappage = $prefix.$mapname; |
310 | 330 | |
311 | 331 | $map = $this->getMap( $prefix, $mapname ); |