add DDG::Goodie::DuckDuckGo, make HTMLEntities respond properly to control characters
parent
eb6ad375b1
commit
58b8900ca2
|
@ -0,0 +1,33 @@
|
|||
package DDG::Goodie::DuckDuckGo;
|
||||
# ABSTRACT: Return hard-coded descriptions for DuckDuckGo terms
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
my %data = (
|
||||
zeroclickinfo => "Zero Click Info is the term DuckDuckGo uses for these boxes, which often provide useful instant answers above traditional results.",
|
||||
zeroclick => \"zeroclickinfo",
|
||||
help => "DuckDuckGo's help website: http://help.duckduckgo.com/",
|
||||
help_html => "DuckDuckGo's help website: <a href='http://help.duckduckgo.com/'>http://help.duckduckgo.com/</a>",
|
||||
);
|
||||
|
||||
triggers start => keys %data, qw/zero/;
|
||||
|
||||
zci is_cached => 1;
|
||||
|
||||
handle query_nowhitespace_nodash => sub {
|
||||
$_ = lc;
|
||||
s/\W//g;
|
||||
return unless exists $data{$_};
|
||||
my $answer = $data{$_};
|
||||
my $answerhtml = exists $data{"${_}_html"} ? $data{"${_}_html"} : 0;
|
||||
|
||||
if (ref $answer eq 'SCALAR') {
|
||||
$answer = $data{$$answer};
|
||||
$answerhtml = exists $data{"${answer}_html"} ? $data{"${answer}_html"} : 0;
|
||||
}
|
||||
|
||||
return $answer unless $answerhtml;
|
||||
return $answer, html => $answerhtml;
|
||||
};
|
||||
|
||||
1;
|
|
@ -3,21 +3,31 @@ package DDG::Goodie::HTMLEntities;
|
|||
|
||||
use DDG::Goodie;
|
||||
use HTML::Entities;
|
||||
use Unicode::UCD 'charinfo';
|
||||
|
||||
zci answer_type => 'html_entity';
|
||||
|
||||
zci is_cached => 1;
|
||||
|
||||
triggers query_nowhitespace => qr/^(?:html|entity|htmlentity)?&#?\w+;?$/i;
|
||||
triggers query_nowhitespace => qr/^(?:html|entity|htmlentity)?(&#?\w+;?)$/i;
|
||||
|
||||
handle query_nowhitespace => sub {
|
||||
s/^(?:html)?(?:entity)?//i;
|
||||
s/;?$/;/; # append a semicolon (some entities like &mdash do not work without one)
|
||||
my $decoded = decode_entities($_);
|
||||
handle matches => sub {
|
||||
my $entity = $_[0];
|
||||
$entity =~ s/;?$/;/; # append a semicolon (some entities like &mdash do not work without one)
|
||||
my $decoded = decode_entities($entity);
|
||||
my $decoded_html = $decoded;
|
||||
my $decimal = ord($decoded);
|
||||
|
||||
my $info = charinfo($decimal);
|
||||
if( $$info{name} eq '<control>' ) {
|
||||
$decoded_html = "<a href='https://en.wikipedia.org/wiki/Unicode_control_characters'>Unicode control character</a> (no visual representation)";
|
||||
$decoded = "Unicode control character (no visual representation)";
|
||||
}
|
||||
|
||||
|
||||
my $hex = sprintf("%04x", $decimal);
|
||||
return "Decoded HTML Entity: $decoded, decimal: $decimal, hexadecimal: $hex",
|
||||
html => "Decoded HTML Entity: $decoded, decimal: $decimal, hexadecimal: <a href=\"/?q=U%2B$hex\">$hex</a>" unless $_ eq $decoded; # decode_entities will return the input if it cannot be decoded
|
||||
html => "Decoded HTML Entity: $decoded_html, decimal: $decimal, hexadecimal: <a href=\"/?q=U%2B$hex\">$hex</a>" unless $entity eq $decoded; # decode_entities will return the input if it cannot be decoded
|
||||
return;
|
||||
};
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ ddg_goodie_test(
|
|||
DDG::Goodie::CurrencyIn
|
||||
DDG::Goodie::DaysBetween
|
||||
DDG::Goodie::Dice
|
||||
DDG::Goodie::DuckDuckGo
|
||||
DDG::Goodie::EmToPx
|
||||
DDG::Goodie::FlipText
|
||||
DDG::Goodie::GUID
|
||||
|
@ -80,6 +81,9 @@ ddg_goodie_test(
|
|||
# Dice
|
||||
'throw dice' => test_zci(qr/\d \d/, answer_type => 'dice_roll', is_cached => 0),
|
||||
|
||||
# DuckDuckGo
|
||||
'zero click info' => test_zci("Zero Click Info is the term DuckDuckGo uses for these boxes, which often provide useful instant answers above traditional results.", is_cached => 1, answer_type => 'duckduckgo'),
|
||||
|
||||
# EmToPx
|
||||
'10 px to em' => test_zci('0.625 em in 10 px', answer_type => 'conversion', is_cached => 1),
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Test::More;
|
||||
use DDG::Test::Goodie;
|
||||
|
||||
zci answer_type => 'duckduckgo';
|
||||
zci is_cached => 1;
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(
|
||||
DDG::Goodie::DuckDuckGo
|
||||
)],
|
||||
'Zero-Click Info' => test_zci("Zero Click Info is the term DuckDuckGo uses for these boxes, which often provide useful instant answers above traditional results."),
|
||||
'zeroclick' => test_zci("Zero Click Info is the term DuckDuckGo uses for these boxes, which often provide useful instant answers above traditional results."),
|
||||
'help' => test_zci("DuckDuckGo's help website: http://help.duckduckgo.com/", html => "DuckDuckGo's help website: <a href='http://help.duckduckgo.com/'>http://help.duckduckgo.com/</a>"),
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
||||
|
Loading…
Reference in New Issue