Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #475 from duckduckgo/jag/convert-style
Browse files Browse the repository at this point in the history
Conversions: Increase font-size to 1.5em
  • Loading branch information
mintsoft committed Jun 5, 2014
2 parents 980c03f + 9313be2 commit 4ab33d2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 61 deletions.
20 changes: 19 additions & 1 deletion lib/DDG/Goodie/Conversions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package DDG::Goodie::Conversions;
# ABSTRACT: convert between various units of measurement

use DDG::Goodie;

use HTML::Entities;
use Math::Round qw/nearest/;
use Scalar::Util qw/looks_like_number/;
use bignum;
Expand Down Expand Up @@ -55,6 +57,21 @@ my %plural_exceptions = (
'pounds force' => 'pounds force',
);

# This function adds some HTML and styling to our output
# so that we can make it prettier.
my $css = share("style.css")->slurp;
sub append_css {
my $html = shift;
return "<style type='text/css'>$css</style>$html";
}

sub wrap_html {
my ($factor, $result) = @_;
my $from = encode_entities($factor) . " <span class='unit'>" . encode_entities($result->{'from_unit'}) . "</span>";
my $to = encode_entities($result->{'result'}) . " <span class='unit'>" . encode_entities($result->{'to_unit'}) . "</span>";
return append_css("<div class='zci--conversions'>$from = $to</div>");
}

handle query_lc => sub {
# hack around issues with feet and inches for now
$_ =~ s/"/inches/;
Expand Down Expand Up @@ -137,7 +154,8 @@ handle query_lc => sub {
$result->{'result'} = defined($f_result) ? $f_result : sprintf("%.${precision}f", $result->{'result'});
$result->{'result'} =~ s/\.0{$precision}$//;

return "$factor $result->{'from_unit'} is $result->{'result'} $result->{'to_unit'}";
my $output = "$factor $result->{'from_unit'} = $result->{'result'} $result->{'to_unit'}";
return $output, html => wrap_html($factor, $result);
};


Expand Down
11 changes: 11 additions & 0 deletions share/goodie/conversions/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.zci--answer .zci--conversions {
font-size: 1.5em;
font-weight: 300;
padding-top: .25em;
padding-bottom: .25em;
color: #393939;
}

.zci--answer .zci--conversions .unit {
color: #808080;
}
120 changes: 60 additions & 60 deletions t/Conversions.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ use DDG::Test::Goodie;
zci answer_type => 'conversions';

ddg_goodie_test(
['DDG::Goodie::Conversions'],
'convert 5 oz to g' => test_zci('5 ounces is 141.747 grams',),
'convert 1 ton to long ton' => test_zci('1 ton is 0.893 long tons',),
'convert 158 ounce to lbm' => test_zci('158 ounces is 9.875 pounds',),
'convert 0.111 stone to pound' => test_zci('0.111 stone is 1.554 pounds',),
'3 kilogramme to pound' => test_zci('3 kilograms is 6.614 pounds',),
'1.3 tonnes to ton' => test_zci('1.3 metric tons is 1.433 tons',),
'2 tons to kg' => test_zci('2 tons is 1814.372 kilograms',),
'1 ton to kilos' => test_zci('1 ton is 907.186 kilograms',),
'3.9 oz in g' => test_zci('3.9 ounces is 110.563 grams',),
'2 miles to km' => test_zci('2 miles is 3.219 kilometers',),
'convert 5 feet to in' => test_zci('5 feet is 60 inches',),
'0.5 nautical mile to klick' => test_zci('0.5 nautical miles is 0.926 kilometers',),
'500 miles in metres' => test_zci('500 miles is 804672.249 meters',),
'25 cm in inches' => test_zci('25 centimeters is 9.843 inches',),
'1760 yards to miles' => test_zci('1760 yards is 1 mile',),
'3520yards to miles' => test_zci('3520 yards is 2 miles',),
'convert 1stone to lbs' => test_zci('1 stone is 14 pounds',),
'30cm in in' => test_zci('30 centimeters is 11.811 inches',),
'36 months to years' => test_zci('36 months is 3 years',),
'43200 seconds in hours' => test_zci('43200 seconds is 12 hours',),
'4 hours to minutes' => test_zci('4 hours is 240 minutes',),
'convert 5 kelvin to fahrenheit' => test_zci('5 kelvin is -450.670 degrees fahrenheit'),
'1 bar to pascal' => test_zci('1 bar is 100000 pascals',),
'1 kilopascal to psi' => test_zci('1 kilopascal is 0.145 pounds per square inch',),
'1 atm to kpa' => test_zci('1 atmosphere is 101.325 kilopascals',),
'5yrds to km' => test_zci('5 yards is 0.005 kilometers'),
'12" to cm' => test_zci('12 inches is 30.480 centimeters'),
'convert 25 inches into feet' => test_zci('25 inches is 2.083 feet'),
'42 kilowatt hours in joules' => test_zci('42 kilowatt-hours is 1.51e+08 joules'),
'2500kcal in tons of tnt' => test_zci('2500 large calories is 0.003 tons of TNT'),
'90 ps in watts' => test_zci('90 metric horsepower is 66194.888 watts'),
'1 gigawatt in horsepower' => test_zci('1 gigawatt is 1.34e+06 horsepower'),
'180 degrees in radians' => test_zci('180 degrees is 3.142 radians'),
'270 degrees in quadrants' => test_zci('270 degrees is 3 quadrants'),
'180 degrees in grads' => test_zci('180 degrees is 200 gradians'),
'45 newtons to pounds force' => test_zci('45 newtons is 10.116 pounds force'),
'8 poundal to newtons' => test_zci('8 poundals is 1.106 newtons'),
'convert 5 f to celsius' => test_zci('5 degrees fahrenheit is -15 degrees celsius'),
['DDG::Goodie::Conversions'],
'convert 5 oz to g' => test_zci('5 ounces = 141.747 grams', html => qr/.*/),
'convert 1 ton to long ton' => test_zci('1 ton = 0.893 long tons', html => qr/.*/),
'convert 158 ounce to lbm' => test_zci('158 ounces = 9.875 pounds', html => qr/.*/),
'convert 0.111 stone to pound' => test_zci('0.111 stone = 1.554 pounds', html => qr/.*/),
'3 kilogramme to pound' => test_zci('3 kilograms = 6.614 pounds', html => qr/.*/),
'1.3 tonnes to ton' => test_zci('1.3 metric tons = 1.433 tons', html => qr/.*/),
'2 tons to kg' => test_zci('2 tons = 1814.372 kilograms', html => qr/.*/),
'1 ton to kilos' => test_zci('1 ton = 907.186 kilograms', html => qr/.*/),
'3.9 oz in g' => test_zci('3.9 ounces = 110.563 grams', html => qr/.*/),
'2 miles to km' => test_zci('2 miles = 3.219 kilometers', html => qr/.*/),
'convert 5 feet to in' => test_zci('5 feet = 60 inches', html => qr/.*/),
'0.5 nautical mile to klick' => test_zci('0.5 nautical miles = 0.926 kilometers', html => qr/.*/),
'500 miles in metres' => test_zci('500 miles = 804672.249 meters', html => qr/.*/),
'25 cm in inches' => test_zci('25 centimeters = 9.843 inches', html => qr/.*/),
'1760 yards to miles' => test_zci('1760 yards = 1 mile', html => qr/.*/),
'3520yards to miles' => test_zci('3520 yards = 2 miles', html => qr/.*/),
'convert 1stone to lbs' => test_zci('1 stone = 14 pounds', html => qr/.*/),
'30cm in in' => test_zci('30 centimeters = 11.811 inches', html => qr/.*/),
'36 months to years' => test_zci('36 months = 3 years', html => qr/.*/),
'43200 seconds in hours' => test_zci('43200 seconds = 12 hours', html => qr/.*/),
'4 hours to minutes' => test_zci('4 hours = 240 minutes', html => qr/.*/),
'convert 5 kelvin to fahrenheit' => test_zci('5 kelvin = -450.670 degrees fahrenheit', html => qr/.*/),
'1 bar to pascal' => test_zci('1 bar = 100000 pascals', html => qr/.*/),
'1 kilopascal to psi' => test_zci('1 kilopascal = 0.145 pounds per square inch', html => qr/.*/),
'1 atm to kpa' => test_zci('1 atmosphere = 101.325 kilopascals', html => qr/.*/),
'5yrds to km' => test_zci('5 yards = 0.005 kilometers', html => qr/.*/),
'12" to cm' => test_zci('12 inches = 30.480 centimeters', html => qr/.*/),
'convert 25 inches into feet' => test_zci('25 inches = 2.083 feet', html => qr/.*/),
'42 kilowatt hours in joules' => test_zci('42 kilowatt-hours = 1.51e+08 joules', html => qr/.*/),
'2500kcal in tons of tnt' => test_zci('2500 large calories = 0.003 tons of TNT', html => qr/.*/),
'90 ps in watts' => test_zci('90 metric horsepower = 66194.888 watts', html => qr/.*/),
'1 gigawatt in horsepower' => test_zci('1 gigawatt = 1.34e+06 horsepower', html => qr/.*/),
'180 degrees in radians' => test_zci('180 degrees = 3.142 radians', html => qr/.*/),
'270 degrees in quadrants' => test_zci('270 degrees = 3 quadrants', html => qr/.*/),
'180 degrees in grads' => test_zci('180 degrees = 200 gradians', html => qr/.*/),
'45 newtons to pounds force' => test_zci('45 newtons = 10.116 pounds force', html => qr/.*/),
'8 poundal to newtons' => test_zci('8 poundals = 1.106 newtons', html => qr/.*/),
'convert 5 f to celsius' => test_zci('5 degrees fahrenheit = -15 degrees celsius', html => qr/.*/),
'6^2 oz to grams' => undef,
'NaN oz to stones' => undef,
'45x10 oz to stones' => undef,
Expand All @@ -59,27 +59,27 @@ ddg_goodie_test(
'use a ton of stones' => undef,
'shoot onself in the foot' => undef,
'foot in both camps' => undef,
'10 mg to tons' => test_zci('10 milligrams is 1.1e-08 tons'),
'10000 minutes in microseconds' => test_zci('10000 minutes is 6e+11 microseconds'),
'convert 5 bytes to bit' => test_zci('5 bytes is 40 bits'),
'5 GB to megabyte' => test_zci('5 gigabytes is 5000 megabytes'),
'0.013 mb in bits' => test_zci('0.013 megabytes is 104000 bits'),
'1 exabyte to pib' => test_zci('1 exabyte is 888.178 pebibytes'),
'convert 1 yb to yib' => test_zci('1 yottabyte is 0.827 yobibytes'),
'16 years in months' => test_zci('16 years is 192 months'),
'1 year in months' => test_zci('1 year is 12 months'),
'360 degrees in revolutions' => test_zci('360 degrees is 1 revolution'),
'convert km to cm' => test_zci('1 kilometer is 100000 centimeters'),
'convert 10ms to seconds' => test_zci('10 milliseconds is 0.010 seconds'),
'what is 1 inch in cm' => test_zci('1 inch is 2.540 centimeters'),
'what are 10 yards in metres' => test_zci('10 yards is 9.144 meters'),
'how long is 42 days in mins' => test_zci('42 days is 60480 minutes'),
'how much is 40 kelvin in celsius' => test_zci('40 kelvin is -233.150 degrees celsius'),
'12 degrees Celsius to Fahrenheit' => test_zci('12 degrees celsius is 53.600 degrees fahrenheit'),
'1 degrees Fahrenheit to celsius' => test_zci('1 degrees fahrenheit is -17.222 degrees celsius'),
'0 c in k' => test_zci('0 degrees celsius is 273.150 kelvin'),
'234 f to c' => test_zci('234 degrees fahrenheit is 112.222 degrees celsius'),
'234 f to k' => test_zci('234 degrees fahrenheit is 385.372 kelvin')
'10 mg to tons' => test_zci('10 milligrams = 1.1e-08 tons', html => qr/.*/),
'10000 minutes in microseconds' => test_zci('10000 minutes = 6e+11 microseconds', html => qr/.*/),
'convert 5 bytes to bit' => test_zci('5 bytes = 40 bits', html => qr/.*/),
'5 GB to megabyte' => test_zci('5 gigabytes = 5000 megabytes', html => qr/.*/),
'0.013 mb in bits' => test_zci('0.013 megabytes = 104000 bits', html => qr/.*/),
'1 exabyte to pib' => test_zci('1 exabyte = 888.178 pebibytes', html => qr/.*/),
'convert 1 yb to yib' => test_zci('1 yottabyte = 0.827 yobibytes', html => qr/.*/),
'16 years in months' => test_zci('16 years = 192 months', html => qr/.*/),
'1 year in months' => test_zci('1 year = 12 months', html => qr/.*/),
'360 degrees in revolutions' => test_zci('360 degrees = 1 revolution', html => qr/.*/),
'convert km to cm' => test_zci('1 kilometer = 100000 centimeters', html => qr/.*/),
'convert 10ms to seconds' => test_zci('10 milliseconds = 0.010 seconds', html => qr/.*/),
'what is 1 inch in cm' => test_zci('1 inch = 2.540 centimeters', html => qr/.*/),
'what are 10 yards in metres' => test_zci('10 yards = 9.144 meters', html => qr/.*/),
'how long is 42 days in mins' => test_zci('42 days = 60480 minutes', html => qr/.*/),
'how much is 40 kelvin in celsius' => test_zci('40 kelvin = -233.150 degrees celsius', html => qr/.*/),
'12 degrees Celsius to Fahrenheit' => test_zci('12 degrees celsius = 53.600 degrees fahrenheit', html => qr/.*/),
'1 degrees Fahrenheit to celsius' => test_zci('1 degrees fahrenheit = -17.222 degrees celsius', html => qr/.*/),
'0 c in k' => test_zci('0 degrees celsius = 273.150 kelvin', html => qr/.*/),
'234 f to c' => test_zci('234 degrees fahrenheit = 112.222 degrees celsius', html => qr/.*/),
'234 f to k' => test_zci('234 degrees fahrenheit = 385.372 kelvin', html => qr/.*/)
);

done_testing;

0 comments on commit 4ab33d2

Please sign in to comment.