Complete rewrite of Shorcut plugin: added context and used Wikipedia as resource.

master
dariog88a 2013-07-30 00:20:18 -03:00
parent acc573eb26
commit 5b02bd5748
2 changed files with 81 additions and 56 deletions

View File

@ -1,71 +1,90 @@
package DDG::Goodie::Shortcut; package DDG::Goodie::Shortcut;
# ABSTRACT: Display keyboard shortcut for an action or display action invoked by a key combination. # ABSTRACT: Display keyboard shortcut for an action.
use DDG::Goodie; use DDG::Goodie;
use LWP::UserAgent;
use HTML::TreeBuilder;
triggers any => 'shortcut','keyboard shortcut'; triggers any => 'shortcut','keyboard shortcut', 'key combination';
zci is_cached => 1; zci is_cached => 1;
zci answer_type => 'shortcut'; zci answer_type => 'shortcut';
primary_example_queries "rename shortcut"; primary_example_queries 'windows show desktop shortcut';
secondary_example_queries "f11 shortcut", "paste shortcut"; secondary_example_queries 'ubuntu shortcut new folder', 'paste keyboard shortcut';
description "keyboard shortcuts"; description 'keyboard shortcuts';
name "Shortcut"; name 'Shortcut';
topics "computing"; topics 'computing';
category "computing_info"; category 'computing_info';
attribution github => ['https://github.com/dariog88a','dariog88a'], attribution github => ['https://github.com/dariog88a','dariog88a'],
email => [ 'mailto:dariog88@gmail.com', 'dariog88' ],
twitter => ['http://twitter.com/dariog88','dariog88']; twitter => ['http://twitter.com/dariog88','dariog88'];
# not using query_lc because I need to keep the whitespace handle remainder_lc => sub {
handle remainder => sub { #replace all OS words with starting char
s/windows|win|xp|vista|seven/W/gi;
s/mac|osx/M/gi;
s/linux|ubuntu|debian|fedora|kde|gnome/L/gi;
my $search = lc $_; sub trim($) {
my $s = shift;
$s =~ s/ +/ /gs;
$s =~ s/^\s+|\s+$//g;
return $s;
}
my %keys = ( #get OS char (if any)
'undo' => 'ctrl z', my $search = $_;
'redo' => 'ctrl y', #[qw('ctrl y' 'ctrl shift z')], $search =~ s/[WML]//g; #remove all OS chars from search
'cut' => 'ctrl x', my $os = substr($_,0,1); #save first char
'copy' => 'ctrl c', $os =~ s/[^WML]//g; #remove if not an OS char
'paste' => 'ctrl v', $search = trim($search);
'select all' => 'ctrl a',
'new' => 'ctrl n', #get wikipedia page content
'open' => 'ctrl o', my $url = 'https://en.wikipedia.org/wiki/Table_of_keyboard_shortcuts';
'close' => 'alt f4', my $content = LWP::UserAgent->new()->get($url)->content;
'save' => 'ctrl s', defined $content or return;
'print' => 'ctrl p',
'bold' => 'ctrl b', #find the first row that starts with the searched action
'italic' => 'ctrl i', my $tree = HTML::TreeBuilder->new_from_content($content);
'underline' => 'ctrl u', my $heading = $tree->look_down(
'help' => 'f1', _tag => 'th',
'rename' => 'f2', sub {
'find' => 'f3', #[qw('f3' 'ctrl f')], $_[0]->as_trimmed_text() =~ /^$search/i #matches only the start
'refresh' => 'f5', }
'reload' => 'ctrl r',
'address bar' => 'f6',
'location bar' => 'ctrl l',
'fullscreen' => 'f11'
); );
my %actions = reverse %keys;
my @answer; #return if no row was found
my $char; if (!defined $heading) { return; }
if (exists $actions{$search}) {
@answer = split(' ',$actions{$search}); my %columns = (W=>'Windows',M=>'Mac OS',L=>'KDE/GNOME');
$char = ' '; my $answer = 'The shortcut for ' . $heading->as_trimmed_text();
my $keys;
#get the (not empty) column content for the searched OS or for all of them if OS not specified
if ($os eq 'W') {
$keys = $heading->right()->as_trimmed_text();
if (!$keys) { return; }
} elsif ($os eq 'M') {
$keys = $heading->right()->right()->as_trimmed_text();
if (!$keys) { return; }
} elsif ($os eq 'L') {
$keys = $heading->right()->right()->right()->as_trimmed_text();
if (!$keys) { return; }
} else {
$answer .= ' is:';
$keys = $heading->right()->as_trimmed_text();
if ($keys) { $answer .= "\n" . $columns{W} . ': ' . $keys; }
$keys = $heading->right()->right()->as_trimmed_text();
if ($keys) { $answer .= "\n" . $columns{M} . ': ' . $keys; }
$keys = $heading->right()->right()->right()->as_trimmed_text();
if ($keys) { $answer .= "\n" . $columns{L} . ': ' . $keys; }
} }
if (exists $keys{$search}) { if ($os) { $answer .= ' in ' . $columns{$os} . ' is ' . $keys; }
@answer = split(' ',$keys{$search});
$char = '+';
}
foreach(@answer) { my $source = "\n" . '<a href="' . $url . '">More at Wikipedia</a>';
$_ = ucfirst $_; return $answer, html => "$answer $source";
}
return join($char,@answer) if @answer;
return;
}; };
1; 1;

View File

@ -8,14 +8,20 @@ use DDG::Test::Goodie;
zci answer_type => 'shortcut'; zci answer_type => 'shortcut';
zci is_cached => 1; zci is_cached => 1;
my $start = 'The shortcut for ';
my $link = '<a href="https://en.wikipedia.org/wiki/Table_of_keyboard_shortcuts">More at Wikipedia</a>';
my $first = 'Show desktop in Windows is ⊞ Win+D or⊞ Win+M (then use ⊞ Win+⇧ Shift+M to bring back all windows)';
my $second = 'New folder in KDE/GNOME is Ctrl+⇧ Shift+N';
my $third = "Paste contents of clipboard at cursor is:\nWindows: Ctrl+V, or ⇧ Shift+Ins\nMac OS: ⌘ Cmd+V\nKDE/GNOME: Ctrl+V";
my $fourth = "Paste special is:\nWindows: Ctrl+Alt+V\nKDE/GNOME: Ctrl+⇧ Shift+V";
ddg_goodie_test( ddg_goodie_test(
[qw( [qw( DDG::Goodie::Shortcut
DDG::Goodie::Shortcut
)], )],
'undo keyboard shortcut' => test_zci('Ctrl+Z'), 'windows show desktop shortcut' => test_zci($start . $first, html => $start . $first ." \n" . $link),
'ctrl c shortcut' => test_zci('Copy'), 'ubuntu shortcut new folder' => test_zci($start . $second, html => $start . $second . " \n" . $link),
'F6 shortcut' => test_zci('Address Bar'), 'paste keyboard shortcut' => test_zci($start . $third, html => $start . $third . " \n" . $link),
'location bar shortcut' => test_zci('Ctrl+L'), 'paste special shortcut' => test_zci($start . $fourth, html => $start . $fourth . " \n" . $link)
); );
done_testing; done_testing;