Plugin Directory

Changeset 139364


Ignore:
Timestamp:
07/26/2009 07:55:48 AM (17 years ago)
Author:
miknight
Message:

Added the ability to use functions instead of just multipliers, allowing the addition of Celsius <-> Fahrenheit conversion.
Also updated the plugin for 0.5 release.

Location:
unit-converter/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • unit-converter/trunk/UnitConverterTest.php

    r125120 r139364  
    113113        $this->assertEquals($expected, $actual);
    114114    }
     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&deg;C';
     133        $actual = UnitConverter::filter($data);
     134        $expected = '5&deg;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    }
    115143}
  • unit-converter/trunk/readme.txt

    r133773 r139364  
    44Tags: metric, customary, imperial, standard, unit, convert, text, cooking, nutrition, fitness
    55Requires at least: 2.7
    6 Tested up to: 2.8.1
    7 Stable tag: 0.4
     6Tested up to: 2.8.2
     7Stable tag: 0.5
    88
    99Detects units of measurement in your blog text and automatically displays the metric or US customary equivalent in one of several possible ways.
     
    2020= Currently Supported Conversions =
    2121
     22* Celsius <-> Fahrenheit
     23* Centimetres <-> inches
     24* Grams <-> ounces
    2225* Kilograms <-> pounds
    23 * Centimetres <-> inches
     26* Kilometres <-> miles
     27* Kilojoules <-> (food) calories
     28* Litres <-> gallons
    2429* Metres <-> feet
    25 * Kilometres <-> miles
    26 * Litres <-> gallons
    27 * Kilojoules <-> (food) calories
    28 * Grams <-> ounces
    2930* Millilitres <-> fluid ounces
    3031
     
    3738
    3839== Changelog ==
     40
     41= 0.5 - 2009-07-26 =
     42* Added Celsius <-> Fahrenheit converter.
     43* Tested up to WordPress 2.8.2.
    3944
    4045= 0.4 - 2009-06-12 =
  • unit-converter/trunk/unit-converter.php

    r125120 r139364  
    44Plugin URI: http://miknight.com/projects/unit-converter
    55Description: 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.4
     6Version: 0.5
    77Author: Michael Knight
    88Author URI: http://miknight.com
     
    7575        $this->addMaps('millilitre', array('mL'));
    7676        $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', '℉'));
    7787    }
    7888
     
    195205    {
    196206        $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        }
    198214        $new_amount = round($new_amount, 2); // TODO: configure the DP.
    199215        return array('amount' => $new_amount, 'unit' => $convert['to']);
     
    231247
    232248    /**
    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.
    235265     *
    236266     * @param string $metric Canonical name of the metric measurement (e.g. 'kilometre').
     
    250280            'multiplier' => 1/$multiplier,
    251281            );
    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);
    255305    }
    256306
     
    266316        foreach ($aliases as $alias) {
    267317            $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            }
    268323        }
    269324    }
     
    279334    {
    280335        switch ($singular) {
     336            case 'degree Celsius':
     337            case 'degree Fahrenheit':
     338                return str_replace('degree', 'degrees', $singular);
    281339            case 'foot':
    282340                return 'feet';
     
    317375    }
    318376
     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     */
    319385    private function generateReplacement($original, $converted)
    320386    {
     
    331397}
    332398
     399// Hook in the plugin to the following WordPress actions.
    333400add_action('the_content', 'UnitConverter::filter');
    334401add_action('the_content_rss', 'UnitConverter::filter');
Note: See TracChangeset for help on using the changeset viewer.