2009-05-23 04:57:03 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::highlight;
|
|
|
|
|
2010-12-29 12:18:14 +01:00
|
|
|
# This has been tested with highlight 2.16 and highlight 3.2+svn19.
|
|
|
|
# In particular version 3.2 won't work. It detects the different
|
|
|
|
# versions by the presence of the the highlight::DataDir class.
|
|
|
|
|
2009-05-23 04:57:03 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki 3.00;
|
2009-07-07 22:31:18 +02:00
|
|
|
use Encode;
|
2009-05-23 04:57:03 +02:00
|
|
|
|
2010-12-29 12:18:14 +01:00
|
|
|
my $data_dir;
|
|
|
|
|
2009-05-23 04:57:03 +02:00
|
|
|
sub import {
|
|
|
|
hook(type => "getsetup", id => "highlight", call => \&getsetup);
|
|
|
|
hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
|
2009-05-23 11:17:26 +02:00
|
|
|
# this hook is used by the format plugin
|
2010-10-26 05:00:32 +02:00
|
|
|
hook(type => "htmlizeformat", id => "highlight",
|
|
|
|
call => \&htmlizeformat, last => 1);
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub getsetup () {
|
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1, # format plugin
|
2010-02-12 07:10:36 +01:00
|
|
|
section => "format",
|
2009-05-23 04:57:03 +02:00
|
|
|
},
|
|
|
|
tohighlight => {
|
|
|
|
type => "string",
|
2009-05-23 06:09:14 +02:00
|
|
|
example => ".c .h .cpp .pl .py Makefile:make",
|
2009-05-23 11:33:08 +02:00
|
|
|
description => "types of source files to syntax highlight",
|
2009-05-23 04:57:03 +02:00
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2010-08-30 19:15:49 +02:00
|
|
|
filetypes_conf => {
|
|
|
|
type => "string",
|
|
|
|
example => "/etc/highlight/filetypes.conf",
|
|
|
|
description => "location of highlight's filetypes.conf",
|
|
|
|
safe => 0,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
|
|
|
langdefdir => {
|
|
|
|
type => "string",
|
|
|
|
example => "/usr/share/highlight/langDefs",
|
|
|
|
description => "location of highlight's langDefs directory",
|
|
|
|
safe => 0,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub checkconfig () {
|
2010-12-29 12:18:14 +01:00
|
|
|
eval q{use highlight};
|
2011-11-23 21:13:16 +01:00
|
|
|
if (highlight::DataDir->can('new')) {
|
2010-12-29 12:18:14 +01:00
|
|
|
$data_dir=new highlight::DataDir();
|
|
|
|
$data_dir->searchDataDir("");
|
|
|
|
} else {
|
|
|
|
$data_dir=undef;
|
|
|
|
}
|
|
|
|
|
2010-08-30 19:15:49 +02:00
|
|
|
if (! exists $config{filetypes_conf}) {
|
2014-08-04 16:35:03 +02:00
|
|
|
if (! $data_dir ) {
|
|
|
|
$config{filetypes_conf}= "/etc/highlight/filetypes.conf";
|
|
|
|
} elsif ( $data_dir -> can('searchFile') ) {
|
|
|
|
# 3.18 +
|
|
|
|
$config{filetypes_conf}=
|
|
|
|
$data_dir -> searchFile("filetypes.conf");
|
|
|
|
} else {
|
|
|
|
# 3.9 +
|
|
|
|
$config{filetypes_conf}=
|
|
|
|
$data_dir -> getConfDir() . "/filetypes.conf";
|
|
|
|
}
|
2010-08-30 19:15:49 +02:00
|
|
|
}
|
2014-08-04 17:26:52 +02:00
|
|
|
# note that this is only used for old versions of highlight
|
|
|
|
# where $data_dir will not be defined.
|
2010-08-30 19:15:49 +02:00
|
|
|
if (! exists $config{langdefdir}) {
|
2014-08-04 17:26:52 +02:00
|
|
|
$config{langdefdir}= "/usr/share/highlight/langDefs";
|
2010-12-29 12:18:14 +01:00
|
|
|
|
2010-08-30 19:15:49 +02:00
|
|
|
}
|
2010-12-29 12:18:14 +01:00
|
|
|
if (exists $config{tohighlight} && read_filetypes()) {
|
2009-05-23 06:09:14 +02:00
|
|
|
foreach my $file (split ' ', $config{tohighlight}) {
|
2009-05-23 04:57:03 +02:00
|
|
|
my @opts = $file=~s/^\.// ?
|
|
|
|
(keepextension => 1) :
|
|
|
|
(noextension => 1);
|
|
|
|
my $ext = $file=~s/:(.*)// ? $1 : $file;
|
|
|
|
|
|
|
|
my $langfile=ext2langfile($ext);
|
|
|
|
if (! defined $langfile) {
|
|
|
|
error(sprintf(gettext(
|
|
|
|
"tohighlight contains unknown file type '%s'"),
|
|
|
|
$ext));
|
|
|
|
}
|
|
|
|
|
|
|
|
hook(
|
|
|
|
type => "htmlize",
|
|
|
|
id => $file,
|
|
|
|
call => sub {
|
|
|
|
my %params=@_;
|
2013-02-12 16:27:29 +01:00
|
|
|
highlight($langfile, $file, $params{content});
|
2009-05-23 04:57:03 +02:00
|
|
|
},
|
|
|
|
longname => sprintf(gettext("Source code: %s"), $file),
|
|
|
|
@opts,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-26 04:37:34 +02:00
|
|
|
sub htmlizeformat {
|
2009-05-23 11:17:26 +02:00
|
|
|
my $format=lc shift;
|
|
|
|
my $langfile=ext2langfile($format);
|
|
|
|
|
|
|
|
if (! defined $langfile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-12 16:27:29 +01:00
|
|
|
return Encode::decode_utf8(highlight($langfile, $format, shift));
|
2009-05-23 11:17:26 +02:00
|
|
|
}
|
|
|
|
|
2009-05-23 04:57:03 +02:00
|
|
|
my %ext2lang;
|
|
|
|
my $filetypes_read=0;
|
2009-05-24 00:39:49 +02:00
|
|
|
my %highlighters;
|
2009-05-23 04:57:03 +02:00
|
|
|
|
|
|
|
# Parse highlight's config file to get extension => language mappings.
|
|
|
|
sub read_filetypes () {
|
2010-12-29 12:18:14 +01:00
|
|
|
my $f;
|
|
|
|
if (!open($f, $config{filetypes_conf})) {
|
|
|
|
warn($config{filetypes_conf}.": ".$!);
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2010-11-20 17:55:26 +01:00
|
|
|
local $/=undef;
|
|
|
|
my $config=<$f>;
|
|
|
|
close $f;
|
|
|
|
|
|
|
|
# highlight >= 3.2 format (bind-style)
|
|
|
|
while ($config=~m/Lang\s*=\s*\"([^"]+)\"[,\s]+Extensions\s*=\s*{([^}]+)}/sg) {
|
|
|
|
my $lang=$1;
|
|
|
|
foreach my $bit (split ',', $2) {
|
|
|
|
$bit=~s/.*"(.*)".*/$1/s;
|
|
|
|
$ext2lang{$bit}=$lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# highlight < 3.2 format
|
|
|
|
if (! keys %ext2lang) {
|
|
|
|
foreach (split("\n", $config)) {
|
|
|
|
if (/^\$ext\((.*)\)=(.*)$/) {
|
|
|
|
$ext2lang{$_}=$1 foreach $1, split ' ', $2;
|
|
|
|
}
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-20 17:55:26 +01:00
|
|
|
|
2010-12-29 12:18:14 +01:00
|
|
|
return $filetypes_read=1;
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-04 17:26:52 +02:00
|
|
|
sub searchlangdef {
|
|
|
|
my $lang=shift;
|
|
|
|
|
|
|
|
if ($data_dir) {
|
|
|
|
return $data_dir->getLangPath($lang . ".lang");
|
|
|
|
} else {
|
|
|
|
return "$config{langdefdir}/$lang.lang";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-05-23 04:57:03 +02:00
|
|
|
# Given a filename extension, determines the language definition to
|
|
|
|
# use to highlight it.
|
|
|
|
sub ext2langfile ($) {
|
|
|
|
my $ext=shift;
|
|
|
|
|
2014-08-04 17:26:52 +02:00
|
|
|
my $langfile=searchlangdef($ext);
|
2009-05-24 00:39:49 +02:00
|
|
|
return $langfile if exists $highlighters{$langfile};
|
|
|
|
|
2009-05-23 04:57:03 +02:00
|
|
|
read_filetypes() unless $filetypes_read;
|
|
|
|
if (exists $ext2lang{$ext}) {
|
2014-08-04 17:26:52 +02:00
|
|
|
return searchlangdef($ext2lang{$ext});
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
# If a language only has one common extension, it will not
|
|
|
|
# be listed in filetypes, so check the langfile.
|
2009-05-24 00:39:49 +02:00
|
|
|
elsif (-e $langfile) {
|
|
|
|
return $langfile;
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Interface to the highlight C library.
|
|
|
|
sub highlight ($$) {
|
|
|
|
my $langfile=shift;
|
2013-02-12 16:27:29 +01:00
|
|
|
my $extorfile=shift;
|
2009-05-23 04:57:03 +02:00
|
|
|
my $input=shift;
|
|
|
|
|
2009-05-23 11:17:26 +02:00
|
|
|
eval q{use highlight};
|
|
|
|
if ($@) {
|
|
|
|
print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2009-05-24 00:39:49 +02:00
|
|
|
my $gen;
|
|
|
|
if (! exists $highlighters{$langfile}) {
|
2016-01-18 09:45:18 +01:00
|
|
|
no warnings 'once';
|
2010-12-29 12:18:14 +01:00
|
|
|
$gen = highlight::CodeGenerator::getInstance($highlight::XHTML);
|
2016-01-18 09:45:18 +01:00
|
|
|
use warnings;
|
2009-05-24 00:39:49 +02:00
|
|
|
$gen->setFragmentCode(1); # generate html fragment
|
|
|
|
$gen->setHTMLEnclosePreTag(1); # include stylish <pre>
|
2010-12-29 12:18:14 +01:00
|
|
|
if ($data_dir){
|
|
|
|
# new style, requires a real theme, but has no effect
|
|
|
|
$gen->initTheme($data_dir->getThemePath("seashell.theme"));
|
|
|
|
} else {
|
|
|
|
# old style, anything works.
|
|
|
|
$gen->initTheme("/dev/null");
|
|
|
|
}
|
|
|
|
$gen->loadLanguage($langfile); # must come after initTheme
|
2009-05-24 00:39:49 +02:00
|
|
|
$gen->setEncoding("utf-8");
|
|
|
|
$highlighters{$langfile}=$gen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$gen=$highlighters{$langfile};
|
|
|
|
}
|
2009-05-23 04:57:03 +02:00
|
|
|
|
2013-02-12 16:27:29 +01:00
|
|
|
return "<div class=\"highlight-$extorfile\">".$gen->generateString($input)."</div>";
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1
|