2 package IkiWiki::Plugin::highlight;
9 # locations of highlight's files
10 my $filetypes="/etc/highlight/filetypes.conf";
11 my $langdefdir="/usr/share/highlight/langDefs";
14 hook(type => "getsetup", id => "highlight", call => \&getsetup);
15 hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
16 # this hook is used by the format plugin
17 hook(type => "htmlizefallback", id => "highlight", call =>
25 rebuild => 1, # format plugin
29 example => ".c .h .cpp .pl .py Makefile:make",
30 description => "types of source files to syntax highlight",
37 if (exists $config{tohighlight}) {
38 foreach my $file (split ' ', $config{tohighlight}) {
39 my @opts = $file=~s/^\.// ?
40 (keepextension => 1) :
42 my $ext = $file=~s/:(.*)// ? $1 : $file;
44 my $langfile=ext2langfile($ext);
45 if (! defined $langfile) {
46 error(sprintf(gettext(
47 "tohighlight contains unknown file type '%s'"),
56 highlight($langfile, $params{content});
58 longname => sprintf(gettext("Source code: %s"), $file),
67 my $langfile=ext2langfile($format);
69 if (! defined $langfile) {
73 return Encode::decode_utf8(highlight($langfile, shift));
80 # Parse highlight's config file to get extension => language mappings.
81 sub read_filetypes () {
82 open (IN, $filetypes);
85 if (/^\$ext\((.*)\)=(.*)$/) {
86 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
94 # Given a filename extension, determines the language definition to
95 # use to highlight it.
96 sub ext2langfile ($) {
99 my $langfile="$langdefdir/$ext.lang";
100 return $langfile if exists $highlighters{$langfile};
102 read_filetypes() unless $filetypes_read;
103 if (exists $ext2lang{$ext}) {
104 return "$langdefdir/$ext2lang{$ext}.lang";
106 # If a language only has one common extension, it will not
107 # be listed in filetypes, so check the langfile.
108 elsif (-e $langfile) {
116 # Interface to the highlight C library.
121 eval q{use highlight};
123 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
128 if (! exists $highlighters{$langfile}) {
129 $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
130 $gen->setFragmentCode(1); # generate html fragment
131 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
132 $gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
133 $gen->initLanguage($langfile); # must come after initTheme
134 $gen->setEncoding("utf-8");
135 $highlighters{$langfile}=$gen;
138 $gen=$highlighters{$langfile};
141 return $gen->generateString($input);