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
|
|
|
|
|
|
|
# locations of highlight's files
|
|
|
|
my $filetypes="/etc/highlight/filetypes.conf";
|
|
|
|
my $langdefdir="/usr/share/highlight/langDefs";
|
|
|
|
|
|
|
|
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
|
|
|
|
hook(type => "htmlizefallback", id => "highlight", call =>
|
|
|
|
\&htmlizefallback);
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sub checkconfig () {
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-23 11:17:26 +02:00
|
|
|
sub htmlizefallback {
|
|
|
|
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 () {
|
|
|
|
open (IN, $filetypes);
|
|
|
|
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;
|
|
|
|
|
2009-05-24 00:39:49 +02:00
|
|
|
my $langfile="$langdefdir/$ext.lang";
|
|
|
|
return $langfile if exists $highlighters{$langfile};
|
|
|
|
|
2009-05-23 04:57:03 +02:00
|
|
|
read_filetypes() unless $filetypes_read;
|
|
|
|
if (exists $ext2lang{$ext}) {
|
2009-05-24 00:39:49 +02:00
|
|
|
return "$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
|