Skip to content

Instantly share code, notes, and snippets.

@moollaza
Last active March 26, 2017 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moollaza/ce1472183ddf8fd4f947 to your computer and use it in GitHub Desktop.
Save moollaza/ce1472183ddf8fd4f947 to your computer and use it in GitHub Desktop.
DuckDuckHack Webcast - Making a "Wordcount" Goodie Instant Answer with JavaScript & Templates
<div>Word: {{word}}</div>
<div>Length: {{length}}</div>
DDH.word_count = DDH.word_count || {};
(function(DDH) {
"use strict";
console.log("DDH.word_count.build"); // remove this before submitting pull request
DDH.word_count.build = function(ops) {
$.each(ops.data.list, function(k, v) {
ops.data.list[k] = {
word: v.word,
length: v.word.length
};
});
if (!ops.data.list_view){
ops.data.title = "There is only 1 word";
ops.templates = { group: "text" };
return ops;
}
};
})(DDH);
package DDG::Goodie::WordCount;
use DDG::Goodie;
use strict;
zci answer_type => 'word_count';
zci is_cached => 1;
triggers start => 'word count', 'wordcount', 'how many words in';
handle remainder => sub {
my $remainder = $_;
return unless $remainder;
my @words = split " ", $remainder;
my $count = scalar @words;
my @list = map { word => $_ }, @words;
my $list_view = $count > 1;
return "plain text response",
structured_answer => {
id => 'word_count',
name => 'Answer',
data => {
title => "There are $count words",
subtitle => "Wordcount for: $remainder",
list => \@list,
list_view => $list_view
},
templates => {
group => 'list',
options => {
list_content => "DDH.word_count.list_content"
}
}
};
};
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment