Subject: | Add escaping option for output in a webpage |
Hi,
thanks for String::Diff, a very useful and straightforward module.
I have a suggestion: Usually if you want to display diffs on a webpage,
you'd html-escape the content. But the escaping can't be done before
the diff since one wants to compare the raw text not the html entities.
But if I do it afterwards, I would also escape the delimiters.
The only solution I have at the moment is using a unique delimiter for
the remove_open, ... options, and after the diff and html-escaping turn
them into <del> etc. back again.
It would be cool if you could add an escape callback.
Example:
my $diff = diff($x, $y,
remove_open => '<del>',
...
escape => sub { encode_entities($_[0] }, # for example
);
The _str subroutine would then look like this:
sub _str {
my($diff, %opts) = @_;
my $str = '';
my $esc = $opts{escape};
for my $parts (@{ $diff }) {
if ($parts->[0] eq '-') {
$str .= $opts{remove_open}. ($esc ? $esc->($parts->[1]) :
$parts->[1]) . $opts{remove_close};
} elsif ($parts->[0] eq '+') {
$str .= $opts{append_open}. ($esc ? $esc->($parts->[1]) :
$parts->[1]) . $opts{append_close};
} else {
$str .= $esc ? $esc->($parts->[1]) : $parts->[1];
}
}
$str;
}
I would just inherit from the module but _str is a private subroutine.
Overwriting _str myself is probably not a goof idea since its
implementation could change.
Do you think you could add that option?
thanks,
tina