Skip to content

Instantly share code, notes, and snippets.

@moollaza moollaza/WordCount.pm
Last active Mar 26, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.