diff --git a/lib/DDG/Goodie/TextConverter.pm b/lib/DDG/Goodie/TextConverter.pm index fdf1a2468..7167954bb 100644 --- a/lib/DDG/Goodie/TextConverter.pm +++ b/lib/DDG/Goodie/TextConverter.pm @@ -35,6 +35,14 @@ my @merged_triggers = (@single_triggers, @triggers); my $triggers_re = join "|", @merged_triggers; my $generics_re = join "|", @generics; +# for static language based triggers e.g. 'base64 decode' +# these words mean we want to go from the type in the query to text +my @from_words = ('decoder', 'decode', 'converter', 'translator'); +my $from_words_re = join '|', @from_words; +# these words mean we want to go from text to the type in the query +my @to_words = ('encode', 'encoder', 'translation', 'translate', 'convert', 'conversion'); +my $to_words_re = join '|', @to_words; + for my $trig (@triggers) { push @lang_triggers, map { "$trig $_" } @generics; } @@ -89,7 +97,11 @@ handle query_lc => sub { # check to see if query is a static language based trigger # eg. binary converter, hex encoder if(grep(/^$query$/, @lang_triggers)) { - $to_type = get_type_information($query); + if ($query =~ /${from_words_re}/gi) { + $from_type = get_type_information($query); + } elsif ($query =~ /${to_words_re}/gi) { + $to_type = get_type_information($query); + } return '', structured_answer => { diff --git a/t/TextConverter.t b/t/TextConverter.t index e2f14c38c..623bf077c 100644 --- a/t/TextConverter.t +++ b/t/TextConverter.t @@ -50,22 +50,22 @@ ddg_goodie_test( 'binary converter' => test_zci( '', structured_answer => build_structured_answer({ - from_type => '', - to_type => 'binary' + from_type => 'binary', + to_type => '' }) ), 'hex converter' => test_zci( '', structured_answer => build_structured_answer({ - from_type => '', - to_type => 'hexadecimal' + from_type => 'hexadecimal', + to_type => '' }) ), 'ascii converter' => test_zci( '', structured_answer => build_structured_answer({ - from_type => '', - to_type => 'text' + from_type => 'text', + to_type => '' }) ), @@ -83,6 +83,27 @@ ddg_goodie_test( }) ), + 'base64 decoder' => test_zci( + '', structured_answer => build_structured_answer({ + from_type => 'base64', + to_type => '' + }) + ), + + 'hex translator' => test_zci( + '', structured_answer => build_structured_answer({ + from_type => 'hexadecimal', + to_type => '' + }) + ), + + 'binary translation' => test_zci( + '', structured_answer => build_structured_answer({ + from_type => '', + to_type => 'binary' + }) + ), + ## ## 2. LANGUAGE BASED QUERIES ##