diff --git a/lib/DDG/Goodie/AsciiTable.pm b/lib/DDG/Goodie/AsciiTable.pm new file mode 100644 index 00000000000..b72ae4b4046 --- /dev/null +++ b/lib/DDG/Goodie/AsciiTable.pm @@ -0,0 +1,44 @@ +package DDG::Goodie::AsciiTable; +# ABSTRACT: Shows Ascii Table + +use DDG::Goodie; +use YAML::XS 'LoadFile'; +use strict; +use warnings; + +zci answer_type => 'ascii_table'; + +zci is_cached => 1; + +triggers start => 'ascii table', 'ascii tables', 'ascii reference table', + 'ascii reference'; + +my $ascii = LoadFile(share('data.yml')); + +handle remainder => sub { + return unless $_ eq ''; + + return '', + structured_answer => { + id => 'ascii_table', + name => 'ASCII Table', + data => { + title => 'ASCII Table', + table => $ascii + }, + meta => { + sourceName => 'asciitable.com', + sourceUrl => 'http://www.asciitable.com/' + }, + templates => { + group => 'list', + item => 0, + options => { + content => 'DDH.ascii_table.content', + moreAt => 1 + } + } + }; +}; + +1; diff --git a/share/goodie/ascii_table/ascii_table.css b/share/goodie/ascii_table/ascii_table.css new file mode 100644 index 00000000000..f2dd012266a --- /dev/null +++ b/share/goodie/ascii_table/ascii_table.css @@ -0,0 +1,3 @@ +.zci--ascii_table .c-list__content.chomp { + max-height: 14em; +} diff --git a/share/goodie/ascii_table/ascii_table.js b/share/goodie/ascii_table/ascii_table.js new file mode 100644 index 00000000000..9f7ce66078b --- /dev/null +++ b/share/goodie/ascii_table/ascii_table.js @@ -0,0 +1,14 @@ +DDH.ascii_table = DDH.ascii_table || {}; + +DDH.ascii_table.build = function(ops) { + "use strict"; + + Spice.registerHelper('get_value', function(obj, key) { + if (obj) { + if (key in obj) { + return obj[key]; + } + } + return ''; + }); +}; \ No newline at end of file diff --git a/share/goodie/ascii_table/content.handlebars b/share/goodie/ascii_table/content.handlebars new file mode 100644 index 00000000000..71cf91fabef --- /dev/null +++ b/share/goodie/ascii_table/content.handlebars @@ -0,0 +1,18 @@ + + + + {{#each table.header}} + + {{/each}} + + + + {{#each table.body}} + + {{#each ../table.header}} + + {{/each}} + + {{/each}} + +
{{ abbr }}
{{ get_value ../this this.abbr }}
diff --git a/share/goodie/ascii_table/data.yml b/share/goodie/ascii_table/data.yml new file mode 100644 index 00000000000..b8d19c0d1e3 --- /dev/null +++ b/share/goodie/ascii_table/data.yml @@ -0,0 +1,653 @@ +--- +header: + - key: Decimal + abbr: Dec + - key: Hexadecimal + abbr: Hex + - key: Octal + abbr: Oct + - key: 'Html Encoding' + abbr: Html + - key: 'Character Representation' + abbr: Char +body: + - Dec: 0 + Hex: 0 + Oct: 000 + Html: '' + Char: 'NUL' + - Dec: 1 + Hex: 1 + Oct: 001 + Html: '' + Char: 'SOH' + - Dec: 2 + Hex: 2 + Oct: 002 + Html: '' + Char: 'STX' + - Dec: 3 + Hex: 3 + Oct: 003 + Html: '' + Char: 'ETX' + - Dec: 4 + Hex: 4 + Oct: 004 + Html: '' + Char: 'EOT' + - Dec: 5 + Hex: 5 + Oct: 005 + Html: '' + Char: 'ENQ' + - Dec: 6 + Hex: 6 + Oct: 006 + Html: '' + Char: 'ACK' + - Dec: 7 + Hex: 7 + Oct: 007 + Html: '' + Char: 'BEL' + - Dec: 8 + Hex: 8 + Oct: 010 + Html: '' + Char: 'BS' + - Dec: 9 + Hex: 9 + Oct: 011 + Html: '' + Char: 'TAB' + - Dec: 10 + Hex: A + Oct: 012 + Html: '' + Char: 'LF' + - Dec: 11 + Hex: B + Oct: 013 + Html: '' + Char: 'VT' + - Dec: 12 + Hex: C + Oct: 014 + Html: '' + Char: 'FF' + - Dec: 13 + Hex: D + Oct: 015 + Html: '' + Char: 'CR' + - Dec: 14 + Hex: E + Oct: 016 + Html: '' + Char: 'SO' + - Dec: 15 + Hex: F + Oct: 017 + Html: '' + Char: 'SI' + - Dec: 16 + Hex: 10 + Oct: 020 + Html: '' + Char: 'DLE' + - Dec: 17 + Hex: 11 + Oct: 021 + Html: '' + Char: 'DC1' + - Dec: 18 + Hex: 12 + Oct: 022 + Html: '' + Char: 'DC2' + - Dec: 19 + Hex: 13 + Oct: 023 + Html: '' + Char: 'DC3' + - Dec: 20 + Hex: 14 + Oct: 024 + Html: '' + Char: 'DC4' + - Dec: 21 + Hex: 15 + Oct: 025 + Html: '' + Char: 'NAK' + - Dec: 22 + Hex: 16 + Oct: 026 + Html: '' + Char: 'SYN' + - Dec: 23 + Hex: 17 + Oct: 027 + Html: '' + Char: 'ETB' + - Dec: 24 + Hex: 18 + Oct: 030 + Html: '' + Char: 'CAN' + - Dec: 25 + Hex: 19 + Oct: 031 + Html: '' + Char: 'EM' + - Dec: 26 + Hex: 1A + Oct: 032 + Html: '' + Char: 'SUB' + - Dec: 27 + Hex: 1B + Oct: 033 + Html: '' + Char: 'ESC' + - Dec: 28 + Hex: 1C + Oct: 034 + Html: '' + Char: 'FS' + - Dec: 29 + Hex: 1D + Oct: 035 + Html: '' + Char: 'GS' + - Dec: 30 + Hex: 1E + Oct: 036 + Html: '' + Char: 'RS' + - Dec: 31 + Hex: 1F + Oct: 037 + Html: '' + Char: 'US' + - Dec: 32 + Hex: 20 + Oct: 040 + Html: ' ' + Char: 'Space' + - Dec: 33 + Hex: 21 + Oct: 041 + Html: '!' + Char: '!' + - Dec: 34 + Hex: 22 + Oct: 042 + Html: '"' + Char: '\"' + - Dec: 35 + Hex: 23 + Oct: 043 + Html: '#' + Char: '#' + - Dec: 36 + Hex: 24 + Oct: 044 + Html: '$' + Char: '$' + - Dec: 37 + Hex: 25 + Oct: 045 + Html: '%' + Char: '%' + - Dec: 38 + Hex: 26 + Oct: 046 + Html: '&' + Char: '&' + - Dec: 39 + Hex: 27 + Oct: 047 + Html: ''' + Char: "'" + - Dec: 40 + Hex: 28 + Oct: 050 + Html: '(' + Char: '(' + - Dec: 41 + Hex: 29 + Oct: 051 + Html: ')' + Char: ')' + - Dec: 42 + Hex: 2A + Oct: 052 + Html: '*' + Char: '*' + - Dec: 43 + Hex: 2B + Oct: 053 + Html: '+' + Char: '+' + - Dec: 44 + Hex: 2C + Oct: 054 + Html: ',' + Char: ',' + - Dec: 45 + Hex: 2D + Oct: 055 + Html: '-' + Char: '-' + - Dec: 46 + Hex: 2E + Oct: 056 + Html: '.' + Char: '.' + - Dec: 47 + Hex: 2F + Oct: 057 + Html: '/' + Char: '/' + - Dec: 48 + Hex: 30 + Oct: 060 + Html: '0' + Char: '0' + - Dec: 49 + Hex: 31 + Oct: 061 + Html: '1' + Char: '1' + - Dec: 50 + Hex: 32 + Oct: 062 + Html: '2' + Char: '2' + - Dec: 51 + Hex: 33 + Oct: 063 + Html: '3' + Char: '3' + - Dec: 52 + Hex: 34 + Oct: 064 + Html: '4' + Char: '4' + - Dec: 53 + Hex: 35 + Oct: 065 + Html: '5' + Char: '5' + - Dec: 54 + Hex: 36 + Oct: 066 + Html: '6' + Char: '6' + - Dec: 55 + Hex: 37 + Oct: 067 + Html: '7' + Char: '7' + - Dec: 56 + Hex: 38 + Oct: 070 + Html: '8' + Char: '8' + - Dec: 57 + Hex: 39 + Oct: 071 + Html: '9' + Char: '9' + - Dec: 58 + Hex: 3A + Oct: 072 + Html: ':' + Char: ':' + - Dec: 59 + Hex: 3B + Oct: 073 + Html: ';' + Char: ';' + - Dec: 60 + Hex: 3C + Oct: 074 + Html: '<' + Char: '<' + - Dec: 61 + Hex: 3D + Oct: 075 + Html: '=' + Char: '=' + - Dec: 62 + Hex: 3E + Oct: 076 + Html: '>' + Char: '>' + - Dec: 63 + Hex: 3F + Oct: 077 + Html: '?' + Char: '?' + - Dec: 64 + Hex: 40 + Oct: 100 + Html: '@' + Char: '@' + - Dec: 65 + Hex: 41 + Oct: 101 + Html: 'A' + Char: 'A' + - Dec: 66 + Hex: 42 + Oct: 102 + Html: 'B' + Char: 'B' + - Dec: 67 + Hex: 43 + Oct: 103 + Html: 'C' + Char: 'C' + - Dec: 68 + Hex: 44 + Oct: 104 + Html: 'D' + Char: 'D' + - Dec: 69 + Hex: 45 + Oct: 105 + Html: 'E' + Char: 'E' + - Dec: 70 + Hex: 46 + Oct: 106 + Html: 'F' + Char: 'F' + - Dec: 71 + Hex: 47 + Oct: 107 + Html: 'G' + Char: 'G' + - Dec: 72 + Hex: 48 + Oct: 110 + Html: 'H' + Char: 'H' + - Dec: 73 + Hex: 49 + Oct: 111 + Html: 'I' + Char: 'I' + - Dec: 74 + Hex: 4A + Oct: 112 + Html: 'J' + Char: 'J' + - Dec: 75 + Hex: 4B + Oct: 113 + Html: 'K' + Char: 'K' + - Dec: 76 + Hex: 4C + Oct: 114 + Html: 'L' + Char: 'L' + - Dec: 77 + Hex: 4D + Oct: 115 + Html: 'M' + Char: 'M' + - Dec: 78 + Hex: 4E + Oct: 116 + Html: 'N' + Char: 'N' + - Dec: 79 + Hex: 4F + Oct: 117 + Html: 'O' + Char: 'O' + - Dec: 80 + Hex: 50 + Oct: 120 + Html: 'P' + Char: 'P' + - Dec: 81 + Hex: 51 + Oct: 121 + Html: 'Q' + Char: 'Q' + - Dec: 82 + Hex: 52 + Oct: 122 + Html: 'R' + Char: 'R' + - Dec: 83 + Hex: 53 + Oct: 123 + Html: 'S' + Char: 'S' + - Dec: 84 + Hex: 54 + Oct: 124 + Html: 'T' + Char: 'T' + - Dec: 85 + Hex: 55 + Oct: 125 + Html: 'U' + Char: 'U' + - Dec: 86 + Hex: 56 + Oct: 126 + Html: 'V' + Char: 'V' + - Dec: 87 + Hex: 57 + Oct: 127 + Html: 'W' + Char: 'W' + - Dec: 88 + Hex: 58 + Oct: 130 + Html: 'X' + Char: 'X' + - Dec: 89 + Hex: 59 + Oct: 131 + Html: 'Y' + Char: 'Y' + - Dec: 90 + Hex: 5A + Oct: 132 + Html: 'Z' + Char: 'Z' + - Dec: 91 + Hex: 5B + Oct: 133 + Html: '[' + Char: '[' + - Dec: 92 + Hex: 5C + Oct: 134 + Html: '\' + Char: '\' + - Dec: 93 + Hex: 5D + Oct: 135 + Html: ']' + Char: ']' + - Dec: 94 + Hex: 5E + Oct: 136 + Html: '^' + Char: '^' + - Dec: 95 + Hex: 5F + Oct: 137 + Html: '_' + Char: '_' + - Dec: 96 + Hex: 60 + Oct: 140 + Html: '`' + Char: '`' + - Dec: 97 + Hex: 61 + Oct: 141 + Html: 'a' + Char: 'a' + - Dec: 98 + Hex: 62 + Oct: 142 + Html: 'b' + Char: 'b' + - Dec: 99 + Hex: 63 + Oct: 143 + Html: 'c' + Char: 'c' + - Dec: 100 + Hex: 64 + Oct: 144 + Html: 'd' + Char: 'd' + - Dec: 101 + Hex: 65 + Oct: 145 + Html: 'e' + Char: 'e' + - Dec: 102 + Hex: 66 + Oct: 146 + Html: 'f' + Char: 'f' + - Dec: 103 + Hex: 67 + Oct: 147 + Html: 'g' + Char: 'g' + - Dec: 104 + Hex: 68 + Oct: 150 + Html: 'h' + Char: 'h' + - Dec: 105 + Hex: 69 + Oct: 151 + Html: 'i' + Char: 'i' + - Dec: 106 + Hex: 6A + Oct: 152 + Html: 'j' + Char: 'j' + - Dec: 107 + Hex: 6B + Oct: 153 + Html: 'k' + Char: 'k' + - Dec: 108 + Hex: 6C + Oct: 154 + Html: 'l' + Char: 'l' + - Dec: 109 + Hex: 6D + Oct: 155 + Html: 'm' + Char: 'm' + - Dec: 110 + Hex: 6E + Oct: 156 + Html: 'n' + Char: 'n' + - Dec: 111 + Hex: 6F + Oct: 157 + Html: 'o' + Char: 'o' + - Dec: 112 + Hex: 70 + Oct: 160 + Html: 'p' + Char: 'p' + - Dec: 113 + Hex: 71 + Oct: 161 + Html: 'q' + Char: 'q' + - Dec: 114 + Hex: 72 + Oct: 162 + Html: 'r' + Char: 'r' + - Dec: 115 + Hex: 73 + Oct: 163 + Html: 's' + Char: 's' + - Dec: 116 + Hex: 74 + Oct: 164 + Html: 't' + Char: 't' + - Dec: 117 + Hex: 75 + Oct: 165 + Html: 'u' + Char: 'u' + - Dec: 118 + Hex: 76 + Oct: 166 + Html: 'v' + Char: 'v' + - Dec: 119 + Hex: 77 + Oct: 167 + Html: 'w' + Char: 'w' + - Dec: 120 + Hex: 78 + Oct: 170 + Html: 'x' + Char: 'x' + - Dec: 121 + Hex: 79 + Oct: 171 + Html: 'y' + Char: 'y' + - Dec: 122 + Hex: 7A + Oct: 172 + Html: 'z' + Char: 'z' + - Dec: 123 + Hex: 7B + Oct: 173 + Html: '{' + Char: '{' + - Dec: 124 + Hex: 7C + Oct: 174 + Html: '|' + Char: '|' + - Dec: 125 + Hex: 7D + Oct: 175 + Html: '}' + Char: '}' + - Dec: 126 + Hex: 7E + Oct: 176 + Html: '~' + Char: '~' + - Dec: 127 + Hex: 7F + Oct: 177 + Html: '' + Char: 'DEL' \ No newline at end of file diff --git a/t/AsciiTable.t b/t/AsciiTable.t new file mode 100644 index 00000000000..ee39788024a --- /dev/null +++ b/t/AsciiTable.t @@ -0,0 +1,88 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use Test::Deep; +use DDG::Test::Goodie; +use YAML::XS 'LoadFile'; + +zci answer_type => 'ascii_table'; +zci is_cached => 1; + +my $ascii = LoadFile('share/goodie/ascii_table/data.yml'); + +sub build_structured_answer { + my $result = { + title => 'ASCII Table', + table => $ascii + }; + + # Check if type of header data is an Array + isa_ok($result->{table}->{header}, 'ARRAY'); + + # Check if type of body data is an Array + isa_ok($result->{table}->{body}, 'ARRAY'); + + # Check if each Header Hash has required keys + for (my $i = 0; $i < $#{$result->{table}->{header}}; $i++) { + cmp_deeply( + $result->{table}->{header}->[$i], + { + key => ignore(), + abbr => ignore() + } + ); + } + + # Check if each Body Hash has required keys + for (my $i = 0; $i < $#{$result->{table}->{body}}; $i++) { + cmp_deeply( + $result->{table}->{body}->[$i], + { + Dec => ignore(), + Hex => ignore(), + Oct => ignore(), + Html => ignore(), + Char => ignore() + } + ); + } + + # Check if title is correct or not + is($result->{title}, 'ASCII Table'); + + return '', + structured_answer => { + id => 'ascii_table', + name => 'ASCII Table', + data => $result, + meta => { + sourceName => 'asciitable.com', + sourceUrl => 'http://www.asciitable.com/' + }, + templates => { + group => 'list', + item => 0, + options => { + content => 'DDH.ascii_table.content', + moreAt => 1 + } + } + }; +} + +sub build_test { test_zci(build_structured_answer(@_)) } + +ddg_goodie_test( + [qw( DDG::Goodie::AsciiTable )], + 'ascii table' => build_test(), + 'ascii reference table' => build_test(), + 'ascii reference' => build_test(), + 'ascii convertor' => undef, + 'ascii conversion' => undef, + 'convert ascii' => undef, + 'dont run for this' => undef +); + +done_testing;