PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/SearchCode.pm

https://github.com/robertbrook/zeroclickinfo-spice
Perl | 51 lines | 32 code | 10 blank | 9 comment | 4 complexity | 33789d953f10ca84aa269d61f5090ca6 MD5 | raw file
  1. package DDG::Spice::SearchCode;
  2. use DDG::Spice;
  3. name "SearchCode";
  4. description "search through APIs and open source repositories";
  5. source "Search[code]";
  6. primary_example_queries "underscore.js bind";
  7. secondary_example_queries "php print_r";
  8. category "reference";
  9. topics "programming";
  10. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/SearchCode.pm";
  11. icon_url "/i/searchco.de.ico";
  12. attribution twitter => ["https://twitter.com/boyter", "boyter"],
  13. github => ["https://github.com/boyter", "Ben Boyter"];
  14. # known bad queries
  15. my %skip_queries = map { $_ => undef } (
  16. 'sql server with',
  17. );
  18. my @triggers = share('triggers.txt')->slurp;
  19. triggers startend => @triggers;
  20. spice to => 'http://searchco.de/api/jsonp_search_IV/?q=$1&callback={{callback}}';
  21. # use list of trigger words to create regex
  22. # and strip newline characters
  23. my $words = join "|", @triggers;
  24. $words =~ s/\n//g;
  25. handle query_raw => sub {
  26. return if exists $skip_queries{lc($_)};
  27. # don't trigger on:
  28. # app (for Quixey)
  29. # code/example (for CodeSearch)
  30. # jobs (for Github jobs)
  31. return if m/(^|\s+)(app|code|example|jobs)(\s+|$)/ig;
  32. # make sure there is more to the
  33. # query besides the trigger itself
  34. if (m/$words/i){
  35. my $query = $_;
  36. s/$words//ig;
  37. return $query if length $_ > 1;
  38. }
  39. return;
  40. };
  41. 1;