2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon; use IO::Handle; # ->autoflush
6 require_mods 'highlight';
7 use_ok 'PublicInbox::HlMod';
8 my $hls = PublicInbox::HlMod->new;
9 ok($hls, 'initialized OK');
10 is($hls->_shebang2lang(\"#!/usr/bin/perl -w\n"), 'perl', 'perl shebang OK');
11 is($hls->{-ext2lang}->{'pm'}, 'perl', '.pm suffix OK');
12 is($hls->{-ext2lang}->{'pl'}, 'perl', '.pl suffix OK');
13 like($hls->_path2lang('Makefile'), qr/\Amake/, 'Makefile OK');
14 my $str = do { local $/; open(my $fh, __FILE__); <$fh> };
18 my $ref = $hls->do_hl(\$str, 'foo.perl');
19 is(ref($ref), 'SCALAR', 'got a scalar reference back');
20 ok(utf8::valid($$ref), 'resulting string is utf8::valid');
21 like($$ref, qr/I can see you!/, 'we can see ourselves in output');
22 like($$ref, qr/&&/, 'escaped &&');
23 my $lref = $hls->do_hl_lang(\$str, 'perl');
24 is($$ref, $$lref, 'do_hl_lang matches do_hl');
27 my $w3m = require_cmd('w3m', 1) or
28 skip('w3m(1) missing to check output', 1);
29 my $cmd = [ $w3m, qw(-T text/html -dump -config /dev/null) ];
30 my $in = '<pre>' . $$ref . '</pre>';
31 my $out = xqx($cmd, undef, { 0 => \$in });
32 # expand tabs and normalize whitespace,
33 # w3m doesn't preserve tabs
37 is($out, $orig, 'w3m output matches');
41 if ('experimental, only for help text') {
49 $hls->do_hl_text(\$tmp);
50 my @hl = split(/^/m, $tmp);
51 is($hl[0], ":>\n", 'first line escaped');
52 is($hl[1], "```perl\n", '2nd line preserved');
53 like($hl[2], qr/<span\b/, 'code highlighted');
54 like($hl[2], qr/&/, 'ampersand escaped');
55 is($hl[3], "```\n", '4th line preserved');
56 is($hl[4], ":<\n", '5th line escaped');
57 is(scalar(@hl), 5, 'no extra line');