PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/search_code/spice.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 66 lines | 47 code | 17 blank | 2 comment | 15 complexity | 510eaecc3aa3b753ece6165b02bb1088 MD5 | raw file
Possible License(s): Apache-2.0
  1. function ddg_spice_search_code(data) {
  2. if(data.results.length > 0) {
  3. var searchterm; // holds the search term
  4. var result; // holds the main result
  5. for (var i=0; i<data.results.length; i++) {
  6. var tmp_result = data.results[i];
  7. if (!DDG.isRelevant(tmp_result.name + ' ' + tmp_result.displayname + ' ' + tmp_result.namespace, '', 2)) continue;
  8. result = tmp_result;
  9. break;
  10. }
  11. if (!result) return;
  12. searchterm = data.query;
  13. var div = d.createElement('div');
  14. var div2 = d.createElement('div');
  15. YAHOO.util.Dom.addClass(div2,'zero_click_searchcode');
  16. div2.innerHTML = search_codeFormatZeroClick(result);
  17. div.appendChild(div2);
  18. items = [[]];
  19. items[0]['a'] = div.innerHTML;
  20. items[0]['h'] = search_codeFormatName(result);
  21. items[0]['s'] = 'search[code]';
  22. items[0]['u'] = 'http://searchco.de/?q='+encodeURIComponent(searchterm);
  23. // DDG rendering function is nra.
  24. nra(items);
  25. }
  26. }
  27. function search_codeFormatName(result) {
  28. var formatted_name = result.name;
  29. if (result.displayname != '' || result.namespace != '') {
  30. formatted_name += ' (';
  31. if (result.displayname != '') formatted_name += result.displayname;
  32. if (result.namespace != '') formatted_name += (result.displayname ? ', ' : '') + result.namespace;
  33. formatted_name += ')';
  34. }
  35. return formatted_name;
  36. }
  37. function search_codeFormatZeroClick(result) {
  38. if(result.synopsis != '') {
  39. return '<pre><code>' + result.synopsis + '</code></pre>' + result.description + '<br>[<a href="'+result.url+'">Reference</a>]';
  40. } else {
  41. return result.description + '<br>[<a href="'+result.url+'">Reference</a>]';
  42. }
  43. }
  44. // This uses the browser to strip HTML, possibly YUI2 has better way to do this
  45. function search_codeStrip(html) {
  46. var tmp = document.createElement("div");
  47. tmp.innerHTML = html;
  48. return tmp.textContent||tmp.innerText;
  49. }