]> Sergey Matveev's repositories - public-inbox.git/commitdiff
hval: add src_escape for highlight post-processing
authorEric Wong <e@80x24.org>
Sun, 27 Jan 2019 11:43:56 +0000 (11:43 +0000)
committerEric Wong <e@80x24.org>
Mon, 28 Jan 2019 06:14:53 +0000 (06:14 +0000)
We need to post-process "highlight" output to ensure it doesn't
contain odd bytes which cause "wide character" warnings or
require odd glyphs in source form.

lib/PublicInbox/Hval.pm
lib/PublicInbox/ViewVCS.pm
t/hval.t

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
index a8aa0b61e2934b96b3fd4eff8fdba151017a074d..63e503d8f33066df1593d360dbe7b1cb2a16264b 100644 (file)
@@ -20,7 +20,7 @@ use Encode qw(find_encoding);
 use PublicInbox::SolverGit;
 use PublicInbox::WwwStream;
 use PublicInbox::Linkify;
-use PublicInbox::Hval qw(ascii_html to_filename);
+use PublicInbox::Hval qw(ascii_html to_filename src_escape);
 my $hl = eval {
        require PublicInbox::HlMod;
        PublicInbox::HlMod->new;
@@ -96,6 +96,8 @@ sub solve_result {
        $l->linkify_1($$blob);
        my $ok = $hl->do_hl($blob, $path) if $hl;
        if ($ok) {
+               $$ok = $enc_utf8->decode($$ok);
+               src_escape($$ok);
                $blob = $ok;
        } else {
                $$blob = ascii_html($$blob);
index a193c296649094cb67263017aa438495c9cbcf3a..bfc9a856f50ba83886388f34e57ad1f3fa91ddbe 100644 (file)
--- a/t/hval.t
+++ b/t/hval.t
@@ -43,5 +43,8 @@ is('foo-bar', PublicInbox::Hval::to_filename("foo   bar\nanother line\n"),
 is('foo.bar', PublicInbox::Hval::to_filename("foo....bar"),
        'to_filename squeezes -');
 
+my $s = "\0\x07\n";
+PublicInbox::Hval::src_escape($s);
+is($s, "\\0\\a\n", 'src_escape works as intended');
 
 done_testing();