2015-02-23 16:35:37 -08:00
|
|
|
package DDG::Goodie::CheatSheets;
|
2015-05-14 10:43:33 -07:00
|
|
|
# ABSTRACT: Load basic cheat sheets from JSON files
|
2015-02-23 16:35:37 -08:00
|
|
|
|
2015-02-25 18:27:24 -08:00
|
|
|
use JSON::XS;
|
2015-02-23 16:35:37 -08:00
|
|
|
use DDG::Goodie;
|
2015-04-04 10:35:33 -07:00
|
|
|
use DDP;
|
2015-08-14 04:51:09 -07:00
|
|
|
use File::Find::Rule;
|
2015-02-23 16:35:37 -08:00
|
|
|
|
2015-08-13 10:27:24 -07:00
|
|
|
no warnings 'uninitialized';
|
|
|
|
|
2015-02-23 16:35:37 -08:00
|
|
|
zci answer_type => 'cheat_sheet';
|
|
|
|
zci is_cached => 1;
|
|
|
|
|
|
|
|
triggers startend => (
|
2015-04-10 13:14:41 -07:00
|
|
|
'char', 'chars',
|
|
|
|
'character', 'characters',
|
2015-03-04 14:35:39 -08:00
|
|
|
'cheat sheet', 'cheatsheet',
|
2015-04-10 13:14:41 -07:00
|
|
|
'command', 'commands',
|
2015-03-04 14:35:39 -08:00
|
|
|
'example', 'examples',
|
|
|
|
'guide', 'help',
|
|
|
|
'quick reference', 'reference',
|
|
|
|
'shortcut', 'shortcuts',
|
2015-04-10 13:14:41 -07:00
|
|
|
'symbol', 'symbols',
|
2015-06-25 14:19:30 -07:00
|
|
|
'key bindings', 'keys', 'default keys'
|
2015-02-23 16:35:37 -08:00
|
|
|
);
|
|
|
|
|
2015-08-06 01:39:54 -07:00
|
|
|
sub getAliases {
|
|
|
|
my @files = File::Find::Rule->file()
|
|
|
|
->name("*.json")
|
|
|
|
->in(share());
|
2015-08-07 00:13:18 -07:00
|
|
|
my %results;
|
2015-08-06 01:39:54 -07:00
|
|
|
|
|
|
|
foreach my $file (@files) {
|
2015-08-07 11:30:02 -07:00
|
|
|
open my $fh, $file or warn "Error opening file: $file\n" and next;
|
2015-08-06 01:39:54 -07:00
|
|
|
my $json = do { local $/; <$fh> };
|
2015-08-17 15:19:59 -07:00
|
|
|
my $data = eval { decode_json($json) } or do {
|
2015-08-17 16:32:18 -07:00
|
|
|
warn "Failed to decode $file: $@";
|
2015-08-17 15:19:59 -07:00
|
|
|
next;
|
|
|
|
};
|
2015-08-06 01:39:54 -07:00
|
|
|
|
2015-08-07 11:30:02 -07:00
|
|
|
my $defaultName = File::Basename::fileparse($file);
|
2015-08-07 00:13:18 -07:00
|
|
|
$defaultName =~ s/-/ /g;
|
|
|
|
$defaultName =~ s/.json//;
|
|
|
|
|
2015-08-07 11:30:02 -07:00
|
|
|
$results{$defaultName} = $file;
|
2015-08-07 00:13:18 -07:00
|
|
|
|
2015-08-06 01:39:54 -07:00
|
|
|
if ($data->{'aliases'}) {
|
|
|
|
foreach my $alias (@{$data->{'aliases'}}) {
|
2015-08-07 11:30:02 -07:00
|
|
|
$results{$alias} = $file;
|
2015-08-06 01:39:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-07 00:13:18 -07:00
|
|
|
return \%results;
|
2015-08-06 01:39:54 -07:00
|
|
|
}
|
|
|
|
|
2015-08-07 00:13:18 -07:00
|
|
|
my $aliases = getAliases();
|
2015-08-06 01:39:54 -07:00
|
|
|
|
2015-02-23 16:35:37 -08:00
|
|
|
handle remainder => sub {
|
|
|
|
# If needed we could jump through a few more hoops to check
|
|
|
|
# terms against file names.
|
2015-08-07 11:30:02 -07:00
|
|
|
open my $fh, $aliases->{join(' ', split /\s+/o, lc($_))} or return;
|
2015-08-06 01:39:54 -07:00
|
|
|
|
2015-02-25 13:35:35 -08:00
|
|
|
my $json = do { local $/; <$fh> };
|
2015-02-25 18:27:24 -08:00
|
|
|
my $data = decode_json($json);
|
2015-04-04 10:35:33 -07:00
|
|
|
|
2015-04-10 13:14:41 -07:00
|
|
|
return 'Cheat Sheet', structured_answer => {
|
2015-05-01 13:13:44 -07:00
|
|
|
id => 'cheat_sheets',
|
2015-04-23 08:33:59 -07:00
|
|
|
dynamic_id => $data->{id},
|
2015-04-10 13:14:41 -07:00
|
|
|
name => 'Cheat Sheet',
|
|
|
|
data => $data,
|
|
|
|
templates => {
|
2015-04-04 10:35:33 -07:00
|
|
|
group => 'base',
|
|
|
|
item => 0,
|
|
|
|
options => {
|
2015-08-09 23:03:20 -07:00
|
|
|
content => "DDH.cheat_sheets.detail",
|
2015-04-04 10:35:33 -07:00
|
|
|
moreAt => 0
|
|
|
|
}
|
2015-04-10 13:14:41 -07:00
|
|
|
}
|
2015-02-25 13:35:35 -08:00
|
|
|
};
|
2015-02-23 16:35:37 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
1;
|