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

Commit 4ab33d2

Browse files
committed
Merge pull request #475 from duckduckgo/jag/convert-style
Conversions: Increase font-size to 1.5em
2 parents 980c03f + 9313be2 commit 4ab33d2

File tree

3 files changed

+90
-61
lines changed

3 files changed

+90
-61
lines changed

lib/DDG/Goodie/Conversions.pm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package DDG::Goodie::Conversions;
22
# ABSTRACT: convert between various units of measurement
33

44
use DDG::Goodie;
5+
6+
use HTML::Entities;
57
use Math::Round qw/nearest/;
68
use Scalar::Util qw/looks_like_number/;
79
use bignum;
@@ -55,6 +57,21 @@ my %plural_exceptions = (
5557
'pounds force' => 'pounds force',
5658
);
5759

60+
# This function adds some HTML and styling to our output
61+
# so that we can make it prettier.
62+
my $css = share("style.css")->slurp;
63+
sub append_css {
64+
my $html = shift;
65+
return "<style type='text/css'>$css</style>$html";
66+
}
67+
68+
sub wrap_html {
69+
my ($factor, $result) = @_;
70+
my $from = encode_entities($factor) . " <span class='unit'>" . encode_entities($result->{'from_unit'}) . "</span>";
71+
my $to = encode_entities($result->{'result'}) . " <span class='unit'>" . encode_entities($result->{'to_unit'}) . "</span>";
72+
return append_css("<div class='zci--conversions'>$from = $to</div>");
73+
}
74+
5875
handle query_lc => sub {
5976
# hack around issues with feet and inches for now
6077
$_ =~ s/"/inches/;
@@ -137,7 +154,8 @@ handle query_lc => sub {
137154
$result->{'result'} = defined($f_result) ? $f_result : sprintf("%.${precision}f", $result->{'result'});
138155
$result->{'result'} =~ s/\.0{$precision}$//;
139156

140-
return "$factor $result->{'from_unit'} is $result->{'result'} $result->{'to_unit'}";
157+
my $output = "$factor $result->{'from_unit'} = $result->{'result'} $result->{'to_unit'}";
158+
return $output, html => wrap_html($factor, $result);
141159
};
142160

143161

share/goodie/conversions/style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.zci--answer .zci--conversions {
2+
font-size: 1.5em;
3+
font-weight: 300;
4+
padding-top: .25em;
5+
padding-bottom: .25em;
6+
color: #393939;
7+
}
8+
9+
.zci--answer .zci--conversions .unit {
10+
color: #808080;
11+
}

t/Conversions.t

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,45 @@ use DDG::Test::Goodie;
99
zci answer_type => 'conversions';
1010

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

8585
done_testing;

0 commit comments

Comments
 (0)