2009-05-23 04:57:03 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::highlight;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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-08-30 19:15:49 +02:00
|
|
|
if (! exists $config{filetypes_conf}) {
|
|
|
|
$config{filetypes_conf}="/etc/highlight/filetypes.conf";
|
|
|
|
}
|
|
|
|
if (! exists $config{langdefdir}) {
|
|
|
|
$config{langdefdir}="/usr/share/highlight/langDefs";
|
|
|
|
}
|
2009-05-23 04:57:03 +02:00
|
|
|
if (exists $config{tohighlight}) {
|
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=@_;
|
|
|
|
highlight($langfile, $params{content});
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-07 22:31:18 +02:00
|
|
|
return Encode::decode_utf8(highlight($langfile, 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-08-30 19:15:49 +02:00
|
|
|
open (IN, $config{filetypes_conf}) || error("$config{filetypes_conf}: $!");
|
2009-05-23 04:57:03 +02:00
|
|
|
while (<IN>) {
|
|
|
|
chomp;
|
|
|
|
if (/^\$ext\((.*)\)=(.*)$/) {
|
|
|
|
$ext2lang{$_}=$1 foreach $1, split ' ', $2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close IN;
|
|
|
|
$filetypes_read=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Given a filename extension, determines the language definition to
|
|
|
|
# use to highlight it.
|
|
|
|
sub ext2langfile ($) {
|
|
|
|
my $ext=shift;
|
|
|
|
|
2010-08-30 19:15:49 +02:00
|
|
|
my $langfile="$config{langdefdir}/$ext.lang";
|
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}) {
|
2010-08-30 19:15:49 +02:00
|
|
|
return "$config{langdefdir}/$ext2lang{$ext}.lang";
|
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;
|
|
|
|
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}) {
|
|
|
|
$gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
|
|
|
|
$gen->setFragmentCode(1); # generate html fragment
|
|
|
|
$gen->setHTMLEnclosePreTag(1); # include stylish <pre>
|
|
|
|
$gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
|
2009-05-26 23:04:49 +02:00
|
|
|
$gen->initLanguage($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
|
|
|
|
2009-05-24 00:39:49 +02:00
|
|
|
return $gen->generateString($input);
|
2009-05-23 04:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1
|