zeroclickinfo-goodies/lib/DDG/Goodie/TitleCase.pm

45 lines
1.3 KiB
Perl
Raw Normal View History

2012-03-06 20:35:45 -08:00
package DDG::Goodie::TitleCase;
use DDG::Goodie;
triggers startend => 'titlecase', 'ucfirst', 'title case', 'capitalize';
2012-11-06 13:56:31 -08:00
primary_example_queries 'titlecase test';
description 'return the query in title case';
name 'Title Case';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/TitleCase.pm';
category 'transformations';
topics 'words_and_games';
attribution github => ['https://github.com/moollaza', 'moollaza'],
github => ['https://github.com/maxluzuriaga', 'Max Luzuriaga'];
2012-11-06 13:56:31 -08:00
2012-03-06 20:35:45 -08:00
zci is_cached => 1;
2012-03-20 21:08:12 -07:00
zci answer_type => "title_case";
2012-03-06 20:35:45 -08:00
# http://blog.apastyle.org/apastyle/2012/03/title-case-and-sentence-case-capitalization-in-apa-style.html
my @exceptions = ("a", "an", "and", "the", "by", "but", "for", "or", "nor", "yet", "so", "as", "at", "in", "of", "on", "per", "to");
handle remainder => sub {
return unless $_;
my @words = split(/ /, $_);
@words = map {
2014-01-09 18:26:16 -08:00
my $word = $words[$_];
if ($_ == 0) {
2014-01-09 18:26:16 -08:00
ucfirst $word # Capitalize first word
} else {
2014-01-09 18:26:16 -08:00
if (grep { $_ eq $word } @exceptions) {
$word # Don't capitalize minor words
} else {
2014-01-09 18:26:16 -08:00
join('-', map { ucfirst $_ } split(/-/, $word) ) # Capitalize every part of a hyphenated word
}
}
} 0 .. $#words;
return join(' ', @words);
};
2012-03-06 20:35:45 -08:00
1;