PublicDNS: put items in a table.
Possibly the most roundabout way to resolve issue #375. - Remove static files from share directory. - Put info into a perl structure. - Generate the two static texts from the structure. - Add minimal test for proper form. It's almost like I haven't heard of Text::Table or Template::Toolkit. Nevertheless, I think this is not too arcane for this relatively simple usage. On the other hand, I am going to feel like a fool for not just editing the two static files when it turns out there are never any changes to the data.master
parent
4f3813e6c3
commit
47da8db500
|
@ -2,6 +2,8 @@ package DDG::Goodie::PublicDNS;
|
|||
|
||||
use DDG::Goodie;
|
||||
|
||||
use List::Util qw(max);
|
||||
|
||||
primary_example_queries 'public dns';
|
||||
description 'list common public DNS servers and their IP addresses';
|
||||
name 'Public DNS';
|
||||
|
@ -13,14 +15,124 @@ attribution github => ['https://github.com/warthurton', 'warthurton'];
|
|||
|
||||
triggers end => "public dns", "dns servers";
|
||||
|
||||
zci is_cached => 1;
|
||||
zci is_cached => 1;
|
||||
zci answer_type => "public_dns";
|
||||
|
||||
my $text = share('publicdns.txt')->slurp;
|
||||
my $html = share('publicdns.html')->slurp;
|
||||
# Let them add new entries anywhere, but store them lexically.
|
||||
# Done like this to allow for manual (or other) sorting should the
|
||||
# need arise.
|
||||
my @ordered_servers = sort { $a->{provider} cmp $b->{provider} } ({
|
||||
provider => 'Comodo Secure DNS',
|
||||
ip4 => ['8.26.56.26', '8.20.247.20'],
|
||||
ip6 => [],
|
||||
info_url => 'http://www.comodo.com/secure-dns/',
|
||||
},
|
||||
{
|
||||
provider => 'DNS Advantage',
|
||||
ip4 => ['156.154.70.1', '156.154.71.1'],
|
||||
ip6 => [],
|
||||
info_url => 'http://dnsadvantage.com',
|
||||
},
|
||||
{
|
||||
provider => 'Google Public DNS',
|
||||
ip4 => ['8.8.8.8', '8.8.4.4'],
|
||||
ip6 => ['2001:4860:4860::8888', '2001:4860:4860::8844'],
|
||||
info_url => 'http://code.google.com/speed/public-dns/',
|
||||
},
|
||||
{
|
||||
provider => 'Norton DNS',
|
||||
ip4 => ['198.153.192.1', '198.153.194.1'],
|
||||
ip6 => [],
|
||||
info_url => 'http://dns.norton.com',
|
||||
},
|
||||
{
|
||||
provider => 'OpenDNS',
|
||||
ip4 => ['208.67.222.222', '208.67.220.220'],
|
||||
ip6 => [],
|
||||
info_url => 'http://opendns.com/',
|
||||
},
|
||||
);
|
||||
|
||||
# Today we could just change and use the key names, but I have no idea what the future holds.
|
||||
my @display_cols = ({
|
||||
key => 'provider',
|
||||
title => 'Provider',
|
||||
},
|
||||
{
|
||||
key => 'ip4',
|
||||
title => 'IPv4',
|
||||
},
|
||||
{
|
||||
key => 'ip6',
|
||||
title => 'IPv6',
|
||||
},
|
||||
);
|
||||
|
||||
my $layout = {
|
||||
extra_space => 1, # How many extra spaces in the text table?
|
||||
text_corner => '+',
|
||||
text_line => '-',
|
||||
text_col => '|',
|
||||
text_array => ', ',
|
||||
html_array => '<br/>',
|
||||
};
|
||||
|
||||
foreach my $column (@display_cols) {
|
||||
$column->{width} = max(map { _max_str_len($_->{$column->{key}}) } @ordered_servers);
|
||||
$column->{text_spacer} = sub {
|
||||
my $to_show = shift;
|
||||
# Everything is left-aligned
|
||||
return
|
||||
$layout->{text_col}
|
||||
. (' ' x $layout->{extra_space})
|
||||
. $to_show
|
||||
. (' ' x max(0, $column->{width} - length($to_show) + $layout->{extra_space}));
|
||||
};
|
||||
}
|
||||
|
||||
my $table_spacer =
|
||||
join($layout->{text_corner}, '', (map { $layout->{text_line} x ($_->{width} + $layout->{extra_space} * 2) } @display_cols), '') . "\n";
|
||||
|
||||
# Actually build the output.. finally!
|
||||
my $text = $table_spacer;
|
||||
my $css = share("style.css")->slurp;
|
||||
my $html = '<style type="text/css">' . $css . '</style><table class="publicdns">';
|
||||
|
||||
# First the headers
|
||||
$text .= join('', map { $_->{text_spacer}->($_->{title}) } @display_cols);
|
||||
$html .= '<tr><th>' . join('</th><th>', map { $_->{title} } @display_cols) . '</th></tr>';
|
||||
$text .= "|\n" . $table_spacer;
|
||||
|
||||
# And now the content
|
||||
foreach my $server (@ordered_servers) {
|
||||
$html .= "<tr>";
|
||||
foreach my $column (@display_cols) {
|
||||
if ($column->{key} ne 'provider') {
|
||||
# Assuming we aren't displaying any non-provider string types!
|
||||
$text .= $column->{text_spacer}->(join($layout->{text_array}, @{$server->{$column->{key}}}));
|
||||
$html .= '<td>' . (join($layout->{html_array}, @{$server->{$column->{key}}})) . '</td>';
|
||||
} else {
|
||||
# Special-case provider
|
||||
$text .= $column->{text_spacer}->($server->{$column->{key}});
|
||||
$html .= '<td><a href="' . $server->{info_url} . '">' . $server->{$column->{key}} . '</a></td>';
|
||||
}
|
||||
}
|
||||
$text .= $layout->{text_col} . "\n";
|
||||
$html .= '</tr>';
|
||||
}
|
||||
|
||||
$text .= $table_spacer;
|
||||
$html .= '</table>';
|
||||
|
||||
handle sub {
|
||||
$text, html => $html;
|
||||
};
|
||||
|
||||
sub _max_str_len {
|
||||
my $item = shift;
|
||||
|
||||
# We only have strings or arrays, so the max length of a string is the length of the string itself.
|
||||
return (ref $item eq 'ARRAY') ? length(join($layout->{text_array}, @$item)) : length $item;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
<i>Comodo Secure DNS:</i>
|
||||
8.26.56.26,
|
||||
8.20.247.20
|
||||
[<a href="http://www.comodo.com/secure-dns/">info</a>]
|
||||
|
||||
<br><i>DNS Advantage:</i>
|
||||
156.154.70.1,
|
||||
156.154.71.1
|
||||
[<a href="http://dnsadvantage.com">info</a>]
|
||||
|
||||
<br><i>Google Public DNS:</i>
|
||||
8.8.8.8,
|
||||
8.8.4.4,
|
||||
2001:4860:4860::8888,
|
||||
2001:4860:4860::8844
|
||||
[<a href="http://code.google.com/speed/public-dns/">info</a>]
|
||||
|
||||
<br><i>Norton DNS:</i>
|
||||
198.153.192.1,
|
||||
198.153.194.1
|
||||
[<a href="http://dns.norton.com">info</a>]
|
||||
|
||||
<br><i>OpenDNS:</i>
|
||||
208.67.222.222, 208.67.220.220
|
||||
[<a href="http://opendns.com/">info</a>]
|
|
@ -1,5 +0,0 @@
|
|||
Comodo Secure DNS: 8.26.56.26, 8.20.247.20
|
||||
DNS Advantage: 156.154.70.1, 156.154.71.1
|
||||
Google Public DNS: 8.8.8.8, 8.8.4.4, 2001:4860:4860::8888, 2001:4860:4860::8844
|
||||
Norton DNS: 198.153.192.1, 198.153.194.1
|
||||
OpenDNS: 208.67.222.222, 208.67.220.220
|
|
@ -0,0 +1,11 @@
|
|||
#zero_click_abstract table.publicdns td {
|
||||
white-space: nowrap;
|
||||
padding: 0.5ex;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
|
||||
#zero_click_abstract table.publicdns th {
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
border: 1px solid #ddd;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Test::More;
|
||||
use DDG::Test::Goodie;
|
||||
|
||||
zci answer_type => "public_dns";
|
||||
zci is_cached => 1;
|
||||
|
||||
# We don't want to test too specifically on the included data, so just confirm
|
||||
# we got an answer with something approaching the correct form.
|
||||
# Hopefully, some one has eyeballed the output to make sure its got the right data.
|
||||
my $text_table = qr#^\+-+.*-+\+#m;
|
||||
my $html_table = qr#<table class="publicdns">.*</table>$#m;
|
||||
|
||||
ddg_goodie_test([qw( DDG::Goodie::PublicDNS)], map { ("$_" => test_zci($text_table, html => $html_table,)) } ('public dns', 'dns servers',));
|
||||
|
||||
done_testing;
|
Loading…
Reference in New Issue