zeroclickinfo-goodies/lib/DDG/Goodie/URLDecode.pm

58 lines
1.7 KiB
Perl
Raw Normal View History

package DDG::Goodie::URLDecode;
2014-09-10 14:49:31 -07:00
# ABSTRACT: Displays the decoded URL for a percent encoded uri
use DDG::Goodie;
use URI::Escape::XS qw(decodeURIComponent);
use warnings;
use strict;
2014-09-10 14:49:31 -07:00
my $trigger_words = qr#urlunescape|unescapeurl|(unescape url)|decodeurl|(decode url)|urldecode|(url decode)|(url unescape)#;
triggers query_raw => qr#%[0-9A-Fa-f]{2}#;
2014-09-10 14:49:31 -07:00
primary_example_queries 'url decode https%3A%2F%2Fduckduckgo.com%2F', 'decode url xkcd.com%2Fblag';
secondary_example_queries 'http%3A%2F%2Farstechnica.com%2F url unescape', 'linux.com%2Ftour%2F unescape url';
2014-09-10 14:49:31 -07:00
zci answer_type => 'decoded_url';
2014-10-10 02:45:06 -07:00
zci is_cached => 1;
2014-09-10 14:49:31 -07:00
name 'URLDecode';
description 'Displays the uri from a percent encoded url';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/URLDecode.pm';
category 'computing_tools';
topics 'programming', 'web_design';
attribution web => [ 'https://www.duckduckgo.com', 'DuckDuckGo' ],
github => [ 'duckduckgo', 'DuckDuckGo'],
twitter => ['duckduckgo', 'DuckDuckGo'];
handle query_raw => sub {
2014-10-07 09:38:19 -07:00
# unless trigger words exist, only answer when we have a single word
unless (m/$trigger_words/) {
return if m/\s+/;
}
2014-09-10 14:49:31 -07:00
#remove triggers and trim
s/(^$trigger_words)|($trigger_words$)//i;
2014-09-10 14:49:31 -07:00
s/(^\s+)|(\s+$)//;
2014-10-28 11:36:15 -07:00
2014-10-10 02:45:06 -07:00
my $in = $_;
my $decoded = decodeURIComponent($in);
2014-10-10 02:45:06 -07:00
if ($decoded =~ /^\s+$/) {
$decoded =~ s/\r/CReturn/;
$decoded =~ s/\n/Newline/;
$decoded =~ s/\t/Tab/;
$decoded =~ s/\s/Space/;
}
my $text = "URL Decoded: $decoded";
2014-10-10 02:45:06 -07:00
return $text,
structured_answer => {
input => [html_enc($in)],
operation => 'URL decode',
result => html_enc($decoded)
};
};
1;