]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Hval.pm
hval: add src_escape for highlight post-processing
[public-inbox.git] / lib / PublicInbox / Hval.pm
index 4d70d5e5008c5185ad743687437039f0f49dcceb..53810b338a0cdc801a558b936e3c7c6f79dd72d2 100644 (file)
@@ -9,7 +9,7 @@ use warnings;
 use Encode qw(find_encoding);
 use PublicInbox::MID qw/mid_clean mid_escape/;
 use base qw/Exporter/;
-our @EXPORT_OK = qw/ascii_html obfuscate_addrs to_filename/;
+our @EXPORT_OK = qw/ascii_html obfuscate_addrs to_filename src_escape/;
 
 my $enc_ascii = find_encoding('us-ascii');
 
@@ -63,6 +63,12 @@ my %xhtml_map = (
 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
 %xhtml_map = (%xhtml_map, %escape_sequence);
 
+sub src_escape ($) {
+       $_[0] =~ s/\r\n/\n/sg;
+       $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
+       $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
+}
+
 sub ascii_html {
        my ($s) = @_;
        $s =~ s/\r\n/\n/sg; # fixup bad line endings