HexToASCII: improve and standardize output
- With code tags (thanks, @mintsoft!), we'll count on the user agent rendering the codes slightly differently, so we do not need to style. - Returning both 'pure ASCII' and 'html' representations. The code layout now feels either slightly over-clever or slightly under-clever and I am not sure which!master
parent
f6588301cf
commit
91117a9eb7
|
@ -26,9 +26,7 @@ handle remainder => sub {
|
|||
my $value = $_;
|
||||
$value =~ s/^\s+//;
|
||||
$value =~ s/\s+$//;
|
||||
if ( $value =~ /^(?:[0\\]x)?([0-9a-f]+)$/i
|
||||
or $value =~ /^([0-9a-f]+)h?$/i)
|
||||
{
|
||||
if ($value =~ /^(?:[0\\]x)?([0-9a-f]+)$/i or $value =~ /^([0-9a-f]+)h?$/i) {
|
||||
my @digits = $1 =~ /(..)/g;
|
||||
my @chars;
|
||||
foreach my $digit (@digits) {
|
||||
|
@ -38,9 +36,15 @@ handle remainder => sub {
|
|||
}
|
||||
# Don't let long strings make the output untidy
|
||||
if (scalar @chars > MAX_INPUT_CHARS) {
|
||||
@chars = (@chars[0 .. MAX_INPUT_CHARS], '...');
|
||||
@chars = (
|
||||
@chars[0 .. MAX_INPUT_CHARS],
|
||||
{
|
||||
pure => '...',
|
||||
html => '…',
|
||||
});
|
||||
}
|
||||
return join('', @chars) . " (ASCII)";
|
||||
my $addendum = ' (ASCII)';
|
||||
return join('', map { $_->{pure} } @chars) . $addendum, html => join('', map { $_->{html} } @chars) . $addendum;
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
@ -86,14 +90,15 @@ my %invisibles = (
|
|||
sub printable_chr {
|
||||
my ($hex) = @_;
|
||||
|
||||
my $printable;
|
||||
my $representation;
|
||||
if (my $char_info = $invisibles{$hex}) {
|
||||
$printable = '<span style="background-color: #ddd" title="' . $char_info->{desc} . '">[' . $char_info->{abbr} . ']</span>';
|
||||
$representation->{html} = '<code title="' . $char_info->{desc} . '">[' . $char_info->{abbr} . ']</code>';
|
||||
$representation->{pure} = ''; # Don't want to add any printable whitespace and wonder what happened.
|
||||
} else {
|
||||
$printable = chr $hex;
|
||||
$representation->{html} = $representation->{pure} = chr $hex;
|
||||
}
|
||||
|
||||
return $printable;
|
||||
return $representation;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -12,9 +12,9 @@ ddg_goodie_test(
|
|||
[qw(
|
||||
DDG::Goodie::HexToASCII
|
||||
)],
|
||||
'ascii 0x7465007374' => test_zci('te<span style="background-color: #ddd" title="null">[NUL]</span>st (ASCII)'),
|
||||
'ascii 0x74657374' => test_zci('test (ASCII)'),
|
||||
'ascii 0x5468697320697320612074657374' => test_zci('This is a test (ASCII)'),
|
||||
'ascii 0x7465007374' => test_zci('test (ASCII)', html => 'te<code title="null">[NUL]</code>st (ASCII)'),
|
||||
'ascii 0x74657374' => test_zci('test (ASCII)', html => 'test (ASCII)'),
|
||||
'ascii 0x5468697320697320612074657374' => test_zci('This is a test (ASCII)', html => 'This is a test (ASCII)'),
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue