Changeset 139364
- Timestamp:
- 07/26/2009 07:55:48 AM (17 years ago)
- Location:
- unit-converter/trunk
- Files:
-
- 3 edited
-
UnitConverterTest.php (modified) (1 diff)
-
readme.txt (modified) (3 diffs)
-
unit-converter.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
unit-converter/trunk/UnitConverterTest.php
r125120 r139364 113 113 $this->assertEquals($expected, $actual); 114 114 } 115 116 public function testCelsiusToFahrenheit() 117 { 118 $data = '5 C 10 F'; 119 $actual = UnitConverter::filter($data); 120 $expected = '5 C (41 degrees Fahrenheit) 10 F (-12.22 degrees Celsius)'; 121 $this->assertEquals($expected, $actual); 122 } 123 124 public function testDegreeSymbols() 125 { 126 // Test regular degrees symbol. 127 $data = '5°C'; 128 $actual = UnitConverter::filter($data); 129 $expected = '5°C (41 degrees Fahrenheit)'; 130 $this->assertEquals($expected, $actual); 131 // Test HTML entity. 132 $data = '5°C'; 133 $actual = UnitConverter::filter($data); 134 $expected = '5°C (41 degrees Fahrenheit)'; 135 $this->assertEquals($expected, $actual); 136 // Test Unicode symbol of each. 137 $data = '5℃ 10℉'; 138 $actual = UnitConverter::filter($data); 139 $expected = '5℃ (41 degrees Fahrenheit) 10℉ (-12.22 degrees Celsius)'; 140 $this->assertEquals($expected, $actual); 141 142 } 115 143 } -
unit-converter/trunk/readme.txt
r133773 r139364 4 4 Tags: metric, customary, imperial, standard, unit, convert, text, cooking, nutrition, fitness 5 5 Requires at least: 2.7 6 Tested up to: 2.8. 17 Stable tag: 0. 46 Tested up to: 2.8.2 7 Stable tag: 0.5 8 8 9 9 Detects units of measurement in your blog text and automatically displays the metric or US customary equivalent in one of several possible ways. … … 20 20 = Currently Supported Conversions = 21 21 22 * Celsius <-> Fahrenheit 23 * Centimetres <-> inches 24 * Grams <-> ounces 22 25 * Kilograms <-> pounds 23 * Centimetres <-> inches 26 * Kilometres <-> miles 27 * Kilojoules <-> (food) calories 28 * Litres <-> gallons 24 29 * Metres <-> feet 25 * Kilometres <-> miles26 * Litres <-> gallons27 * Kilojoules <-> (food) calories28 * Grams <-> ounces29 30 * Millilitres <-> fluid ounces 30 31 … … 37 38 38 39 == Changelog == 40 41 = 0.5 - 2009-07-26 = 42 * Added Celsius <-> Fahrenheit converter. 43 * Tested up to WordPress 2.8.2. 39 44 40 45 = 0.4 - 2009-06-12 = -
unit-converter/trunk/unit-converter.php
r125120 r139364 4 4 Plugin URI: http://miknight.com/projects/unit-converter 5 5 Description: Detects units of measurement in your blog text and automatically displays the metric or US customary equivalent in one of several possible ways. 6 Version: 0. 46 Version: 0.5 7 7 Author: Michael Knight 8 8 Author URI: http://miknight.com … … 75 75 $this->addMaps('millilitre', array('mL')); 76 76 $this->addMaps('fluid ounce', array('fl oz', 'fl. oz.', 'oz. fl.')); 77 78 // Celsius <-> Fahrenheit 79 $this->addConversionFunc( 80 'degree Celsius', 81 'degree Fahrenheit', 82 create_function('$c', 'return $c * (9/5) + 32;'), 83 create_function('$f', 'return ($f - 32) * (5/9);') 84 ); 85 $this->addMaps('degree Celsius', array('C', '°C', '℃')); 86 $this->addMaps('degree Fahrenheit', array('F', '°F', '℉')); 77 87 } 78 88 … … 195 205 { 196 206 $convert = $this->getComplementaryUnit($this->getCanonicalUnit($unit)); 197 $new_amount = $amount * $convert['multiplier']; 207 if (isset($convert['multiplier'])) { 208 $new_amount = $amount * $convert['multiplier']; 209 } elseif ($convert['func']) { 210 $new_amount = call_user_func($convert['func'], $amount); 211 } else { 212 die("Unit '{$unit}' has no multiplier or function for conversion."); 213 } 198 214 $new_amount = round($new_amount, 2); // TODO: configure the DP. 199 215 return array('amount' => $new_amount, 'unit' => $convert['to']); … … 231 247 232 248 /** 233 * Adds a metric <-> imperial conversion mapping. 234 * It will also add an alias mapping for the canonical name and plural version of the canonical name. 249 * Adds alias mappings for the canonical name of a unit and plural version of the canonical name. 250 * 251 * @param string $metric Canonical name of the metric measurement (e.g. 'kilometre'). 252 * @param string $imperial Canonical name of the imperial measurement (e.g. 'mile'). 253 * 254 * @return void Does not return anything. 255 */ 256 private function addSelfMappings($metric, $imperial) 257 { 258 // Add self and plural version to the map 259 $this->addMaps($metric, array($metric, self::plural($metric))); 260 $this->addMaps($imperial, array($imperial, self::plural($imperial))); 261 } 262 263 /** 264 * Adds a metric <-> imperial conversion multiplier mapping. 235 265 * 236 266 * @param string $metric Canonical name of the metric measurement (e.g. 'kilometre'). … … 250 280 'multiplier' => 1/$multiplier, 251 281 ); 252 // Add self and plural version to the map 253 $this->addMaps($metric, array($metric, self::plural($metric))); 254 $this->addMaps($imperial, array($imperial, self::plural($imperial))); 282 $this->addSelfMappings($metric, $imperial); 283 } 284 285 /** 286 * Adds a metric <-> imperial conversion with functions instead of a multiplier. 287 * 288 * @param string $metric Canonical name of the metric measurement (e.g. 'kilometre'). 289 * @param string $imperial Canonical name of the imperial measurement (e.g. 'mile'). 290 * @param string $metric The multiplier to apply to the metric unit to obtain the imperial unit. 291 * 292 * @return void Does not return anything. 293 */ 294 private function addConversionFunc($metric, $imperial, $met_to_imp, $imp_to_met) 295 { 296 $this->units[$metric] = array( 297 'to' => $imperial, 298 'func' => $met_to_imp, 299 ); 300 $this->units[$imperial] = array( 301 'to' => $metric, 302 'func' => $imp_to_met, 303 ); 304 $this->addSelfMappings($metric, $imperial); 255 305 } 256 306 … … 266 316 foreach ($aliases as $alias) { 267 317 $this->maps[$alias] = $unit; 318 // In order to detect the mapping in HTML, it may be necessary to check the HTML entity version of the unit. 319 $alias_html = htmlentities(utf8_decode($alias)); 320 if ($alias_html != $alias) { 321 $this->maps[$alias_html] = $unit; 322 } 268 323 } 269 324 } … … 279 334 { 280 335 switch ($singular) { 336 case 'degree Celsius': 337 case 'degree Fahrenheit': 338 return str_replace('degree', 'degrees', $singular); 281 339 case 'foot': 282 340 return 'feet'; … … 317 375 } 318 376 377 /** 378 * Generates the text to insert in place of the original text. 379 * 380 * @param string $original The original measurement. 381 * @param string $converted The converted measurement. 382 * 383 * @return string The resulting text containing the original measurement with the converted measurement. 384 */ 319 385 private function generateReplacement($original, $converted) 320 386 { … … 331 397 } 332 398 399 // Hook in the plugin to the following WordPress actions. 333 400 add_action('the_content', 'UnitConverter::filter'); 334 401 add_action('the_content_rss', 'UnitConverter::filter');
Note: See TracChangeset
for help on using the changeset viewer.