]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HlMod.pm
hlmod: make into a singleton
[public-inbox.git] / lib / PublicInbox / HlMod.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # I have no idea how stable or safe this is for handling untrusted
5 # input, but it seems to have been around for a while, and the
6 # highlight(1) executable is supported by gitweb and cgit.
7 #
8 # I'm also unsure about API stability, but highlight 3.x seems to
9 # have been around a few years and ikiwiki (apparently the only
10 # user of the SWIG/Perl bindings, at least in Debian) hasn't needed
11 # major changes to support it in recent years.
12 #
13 # Some code stolen from ikiwiki (GPL-2.0+)
14 # wrapper for SWIG-generated highlight.pm bindings
15 package PublicInbox::HlMod;
16 use strict;
17 use warnings;
18 use highlight; # SWIG-generated stuff
19 my $hl;
20
21 sub _parse_filetypes ($) {
22         my $ft_conf = $_[0]->searchFile('filetypes.conf') or
23                                 die 'filetypes.conf not found by highlight';
24         open my $fh, '<', $ft_conf or die "failed to open($ft_conf): $!";
25         local $/;
26         my $cfg = <$fh>;
27         my %ext2lang;
28         my @shebang; # order matters
29
30         # Hrm... why isn't this exposed by the highlight API?
31         # highlight >= 3.2 format (bind-style) (from ikiwiki)
32         while ($cfg =~ /\bLang\s*=\s*\"([^"]+)\"[,\s]+
33                          Extensions\s*=\s*{([^}]+)}/sgx) {
34                 my $lang = $1;
35                 foreach my $bit (split(/,/, $2)) {
36                         $bit =~ s/.*"(.*)".*/$1/s;
37                         $ext2lang{$bit} = $lang;
38                 }
39         }
40         # AFAIK, all the regexps used by in filetypes.conf distributed
41         # by highlight work as Perl REs
42         while ($cfg =~ /\bLang\s*=\s*\"([^"]+)\"[,\s]+
43                         Shebang\s*=\s*\[\s*\[([^}]+)\s*\]\s*\]\s*}\s*,/sgx) {
44                 my ($lang, $re) = ($1, $2);
45                 eval {
46                         my $perl_re = qr/$re/;
47                         push @shebang, [ $lang, $perl_re ];
48                 };
49                 if ($@) {
50                         warn "$lang shebang=[[$re]] did not work in Perl: $@";
51                 }
52         }
53         (\%ext2lang, \@shebang);
54 }
55
56 # We only need one instance, so we don't need to do
57 # highlight::CodeGenerator::deleteInstance
58 sub new {
59         my ($class) = @_;
60         $hl ||= do {
61                 my $dir = highlight::DataDir->new;
62                 $dir->initSearchDirectories('');
63                 my ($ext2lang, $shebang) = _parse_filetypes($dir);
64                 bless {
65                         -dir => $dir,
66                         -ext2lang => $ext2lang,
67                         -shebang => $shebang,
68                 }, $class;
69         };
70 }
71
72 sub _shebang2lang ($$) {
73         my ($self, $str) = @_;
74         my $shebang = $self->{-shebang};
75         foreach my $s (@$shebang) {
76                 return $s->[0] if $$str =~ $s->[1];
77         }
78         undef;
79 }
80
81 sub _path2lang ($$) {
82         my ($self, $path) = @_;
83         my ($ext) = ($path =~ m!([^\\/\.]+)\z!);
84         $ext = lc($ext);
85         $self->{-ext2lang}->{$ext} || $ext;
86 }
87
88 sub do_hl {
89         my ($self, $str, $path) = @_;
90         my $lang = _path2lang($self, $path) if defined $path;
91         do_hl_lang($self, $str, $lang);
92 }
93
94 sub do_hl_lang {
95         my ($self, $str, $lang) = @_;
96
97         my $dir = $self->{-dir};
98         my $langpath;
99
100         if (defined $lang) {
101                 $langpath = $dir->getLangPath("$lang.lang") or return;
102                 $lang = undef unless -f $langpath
103         }
104         unless (defined $lang) {
105                 $lang = _shebang2lang($self, $str) or return;
106                 $langpath = $dir->getLangPath("$lang.lang") or return;
107                 return unless -f $langpath
108         }
109         my $gen = $self->{$langpath} ||= do {
110                 my $g = highlight::CodeGenerator::getInstance($highlight::HTML);
111                 $g->setFragmentCode(1); # generate html fragment
112
113                 # whatever theme works
114                 my $themepath = $dir->getThemePath('print.theme');
115                 $g->initTheme($themepath);
116                 $g->loadLanguage($langpath);
117                 $g->setEncoding('utf-8');
118                 $g;
119         };
120
121         # we assume $$str is valid UTF-8, but the SWIG binding doesn't
122         # know that, so ensure it's marked as UTF-8 even if it isnt...
123         my $out = $gen->generateString($$str);
124         utf8::decode($out);
125         \$out;
126 }
127
128 1;