]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HlMod.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / HlMod.pm
1 # Copyright (C) 2019-2020 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 use PublicInbox::Hval qw(src_escape ascii_html);
20 my $hl;
21
22 sub _parse_filetypes ($) {
23         my $ft_conf = $_[0]->getFiletypesConfPath('filetypes') or
24                                 die 'filetypes.conf not found by highlight';
25         open my $fh, '<', $ft_conf or die "failed to open($ft_conf): $!";
26         local $/;
27         my $cfg = <$fh>;
28         my %ext2lang;
29         my @shebang; # order matters
30
31         # Hrm... why isn't this exposed by the highlight API?
32         # highlight >= 3.2 format (bind-style) (from ikiwiki)
33         while ($cfg =~ /\bLang\s*=\s*\"([^"]+)\"[,\s]+
34                          Extensions\s*=\s*{([^}]+)}/sgx) {
35                 my $lang = $1;
36                 foreach my $bit (split(/,/, $2)) {
37                         $bit =~ s/.*"(.*)".*/$1/s;
38                         $ext2lang{$bit} = $lang;
39                 }
40         }
41         # AFAIK, all the regexps used by in filetypes.conf distributed
42         # by highlight work as Perl REs
43         while ($cfg =~ /\bLang\s*=\s*\"([^"]+)\"[,\s]+
44                         Shebang\s*=\s*\[\s*\[([^}]+)\s*\]\s*\]\s*}\s*,/sgx) {
45                 my ($lang, $re) = ($1, $2);
46                 eval {
47                         my $perl_re = qr/$re/;
48                         push @shebang, [ $lang, $perl_re ];
49                 };
50                 if ($@) {
51                         warn "$lang shebang=[[$re]] did not work in Perl: $@";
52                 }
53         }
54         (\%ext2lang, \@shebang);
55 }
56
57 # We only need one instance, so we don't need to do
58 # highlight::CodeGenerator::deleteInstance
59 sub new {
60         my ($class) = @_;
61         $hl ||= do {
62                 my $dir = highlight::DataDir->new;
63                 $dir->initSearchDirectories('');
64                 my ($ext2lang, $shebang) = _parse_filetypes($dir);
65                 bless {
66                         -dir => $dir,
67                         -ext2lang => $ext2lang,
68                         -shebang => $shebang,
69                 }, $class;
70         };
71 }
72
73 sub _shebang2lang ($$) {
74         my ($self, $str) = @_;
75         my $shebang = $self->{-shebang};
76         foreach my $s (@$shebang) {
77                 return $s->[0] if $$str =~ $s->[1];
78         }
79         undef;
80 }
81
82 sub _path2lang ($$) {
83         my ($self, $path) = @_;
84         my ($ext) = ($path =~ m!([^\\/\.]+)\z!);
85         $ext = lc($ext);
86         $self->{-ext2lang}->{$ext} || $ext;
87 }
88
89 sub do_hl {
90         my ($self, $str, $path) = @_;
91         my $lang = _path2lang($self, $path) if defined $path;
92         do_hl_lang($self, $str, $lang);
93 }
94
95 sub do_hl_lang {
96         my ($self, $str, $lang) = @_;
97
98         my $dir = $self->{-dir};
99         my $langpath;
100
101         if (defined $lang) {
102                 $langpath = $dir->getLangPath("$lang.lang") or return;
103                 $lang = undef unless -f $langpath
104         }
105         unless (defined $lang) {
106                 $lang = _shebang2lang($self, $str) or return;
107                 $langpath = $dir->getLangPath("$lang.lang") or return;
108                 return unless -f $langpath
109         }
110         my $gen = $self->{$langpath} ||= do {
111                 my $g = highlight::CodeGenerator::getInstance($highlight::HTML);
112                 $g->setFragmentCode(1); # generate html fragment
113
114                 # whatever theme works
115                 my $themepath = $dir->getThemePath('print.theme');
116                 $g->initTheme($themepath);
117                 $g->loadLanguage($langpath);
118                 $g->setEncoding('utf-8');
119                 $g;
120         };
121
122         # we assume $$str is valid UTF-8, but the SWIG binding doesn't
123         # know that, so ensure it's marked as UTF-8 even if it isnt...
124         my $out = $gen->generateString($$str);
125         utf8::decode($out);
126         src_escape($out);
127         \$out;
128 }
129
130 # Highlight text, but support Markdown "```$LANG" notation
131 # while preserving WYSIWYG of plain-text documentation.
132 # This is NOT to be enabled by default or encouraged for parsing
133 # emails, since it is NOT stable and can lead to standards
134 # proliferation of email.
135 sub do_hl_text {
136         my ($self, $str) = @_;
137
138         $$str = join('', map {
139                 if (/\A(``` ?)(\w+)\s*?\n(.+)(^```\s*)\z/sm) {
140                         my ($pfx, $lang, $code, $post) = ($1, $2, $3, $4);
141                         my $hl = do_hl_lang($self, \$code, $lang) || \$code;
142                         $pfx . $lang . "\n" . $$hl . $post;
143                 } else {
144                         ascii_html($_);
145                 }
146         } split(/(^``` ?\w+\s*?\n.+?^```\s*$)/sm, $$str));
147 }
148
149 1;