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