2 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::Spawn qw(which spawn);
8 use IO::Handle; # ->autoflush
10 eval { require highlight } or
11 plan skip_all => "failed to load highlight.pm for $0";
12 use_ok 'PublicInbox::HlMod';
13 my $hls = PublicInbox::HlMod->new;
14 ok($hls, 'initialized OK');
15 is($hls->_shebang2lang(\"#!/usr/bin/perl -w\n"), 'perl', 'perl shebang OK');
16 is($hls->{-ext2lang}->{'pm'}, 'perl', '.pm suffix OK');
17 is($hls->{-ext2lang}->{'pl'}, 'perl', '.pl suffix OK');
18 is($hls->_path2lang('Makefile'), 'make', 'Makefile OK');
19 my $str = do { local $/; open(my $fh, __FILE__); <$fh> };
23 my $ref = $hls->do_hl(\$str, 'foo.perl');
24 is(ref($ref), 'SCALAR', 'got a scalar reference back');
25 ok(utf8::valid($$ref), 'resulting string is utf8::valid');
26 like($$ref, qr/I can see you!/, 'we can see ourselves in output');
27 like($$ref, qr/&&/, 'escaped &&');
28 my $lref = $hls->do_hl_lang(\$str, 'perl');
29 is($$ref, $$lref, 'do_hl_lang matches do_hl');
32 which('w3m') or skip 'w3m(1) missing to check output', 1;
33 my $cmd = [ qw(w3m -T text/html -dump -config /dev/null) ];
34 open my $in, '+>', undef or die;
35 open my $out, '+>', undef or die;
36 my $rdr = { 0 => fileno($in), 1 => fileno($out) };
38 print $in '<pre>', $$ref, '</pre>' or die;
39 $in->seek(0, SEEK_SET) or die;
40 my $pid = spawn($cmd, undef, $rdr);
42 # expand tabs and normalize whitespace,
43 # w3m doesn't preserve tabs
45 $out->seek(0, SEEK_SET) or die;
46 $out = do { local $/; <$out> };
49 is($out, $orig, 'w3m output matches');
53 if ('experimental, only for help text') {
61 $hls->do_hl_text(\$tmp);
62 my @hl = split(/^/m, $tmp);
63 is($hl[0], ":>\n", 'first line escaped');
64 is($hl[1], "```perl\n", '2nd line preserved');
65 like($hl[2], qr/<span\b/, 'code highlighted');
66 like($hl[2], qr/&/, 'ampersand escaped');
67 is($hl[3], "```\n", '4th line preserved');
68 is($hl[4], ":<\n", '5th line escaped');
69 is(scalar(@hl), 5, 'no extra line');