|
| 1 | +package DDG::Goodie::FIGlet; |
| 2 | +# ABSTRACT: Uses FIGlet to make large letters out of ordinary text. |
| 3 | + |
| 4 | +use DDG::Goodie; |
| 5 | +use Text::FIGlet; |
| 6 | + |
| 7 | +triggers startend => "figlet", "bigtext", "big text"; |
| 8 | +primary_example_queries 'figlet DuckDuckGo'; |
| 9 | +secondary_example_queries 'figlet computer DuckDuckGo'; |
| 10 | + |
| 11 | +name 'FIGlet'; |
| 12 | +description 'Uses FIGlet to make large letters out of ordinary text.'; |
| 13 | +category 'transformations'; |
| 14 | +topics 'words_and_games'; |
| 15 | +code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Figlet.pm'; |
| 16 | +attribution |
| 17 | + web => ['http://engvik.nu', 'Lars Jansøn Engvik'], |
| 18 | + github => [ 'larseng', 'Lars Jansøn Engvik']; |
| 19 | + |
| 20 | +zci answer_type => 'figlet'; |
| 21 | +zci is_cached => 1; |
| 22 | + |
| 23 | +my $width = 800; |
| 24 | +my $css = share("style.css")->slurp; |
| 25 | + |
| 26 | +# Fetch available fonts. |
| 27 | +opendir DIR, share(); |
| 28 | +my @fonts = readdir(DIR); |
| 29 | +closedir DIR; |
| 30 | + |
| 31 | +# Renders a figlet. |
| 32 | +sub render_figlet { |
| 33 | + my ($font, $text) = @_; |
| 34 | + return Text::FIGlet->new(-f=>$font, -d=>share())->figify(-w=>$width, -A=>$text); |
| 35 | +} |
| 36 | + |
| 37 | +# Apply CSS. |
| 38 | +sub apply_css { |
| 39 | + my ($html) = @_; |
| 40 | + return "<style type='text/css'>$css</style>\n" . $html; |
| 41 | +} |
| 42 | + |
| 43 | +handle query => sub { |
| 44 | + my $font; |
| 45 | + my $text; |
| 46 | + my $figlet; |
| 47 | + my $html; |
| 48 | + |
| 49 | + # Return if no input provided. |
| 50 | + return if ((lc $_ eq 'figlet') || (lc $_ eq 'bigtext') || (lc $_ eq 'big text')); |
| 51 | + |
| 52 | + # Parse query. |
| 53 | + $_ =~ m/^(?:figlet|bigtext|big text)(?:\-|\s+)(.*)|(.*)\s+(?:figlet|bigtext|big text)$/i; |
| 54 | + $text = $1 if $1; |
| 55 | + $text = $2 if $2; |
| 56 | + |
| 57 | + # Checks if the first word is a font. |
| 58 | + $text =~ m/^\s*(\w+)/; |
| 59 | + $font = lc $1 if grep /\b$1\b/i, @fonts; |
| 60 | + |
| 61 | + # Strip the font from the text to render if we're using a font. |
| 62 | + if ($font && $font ne $text) { |
| 63 | + $text = substr $text, length ($font)+1, length $text; |
| 64 | + } else { |
| 65 | + $font = "standard"; |
| 66 | + } |
| 67 | + |
| 68 | + # Render the FIGlet |
| 69 | + $figlet = render_figlet($font, $text); |
| 70 | + |
| 71 | + $html = "<div id='figlet-wrapper'><span>Font: </span><span id='figlet-font'>$font</span><pre contenteditable='true'>$figlet</pre></div>"; |
| 72 | + |
| 73 | + return $figlet, html => apply_css($html) if $figlet; |
| 74 | + return; |
| 75 | +}; |
| 76 | + |
| 77 | +1; |
0 commit comments