2 package IkiWiki::Plugin::highlight;
8 # locations of highlight's files
9 my $filetypes="/etc/highlight/filetypes.conf";
10 my $langdefdir="/usr/share/highlight/langDefs";
13 hook(type => "getsetup", id => "highlight", call => \&getsetup);
14 hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
15 # this hook is used by the format plugin
16 hook(type => "htmlizefallback", id => "highlight", call =>
24 rebuild => 1, # format plugin
28 example => ".c .h .cpp .pl .py Makefile:make",
29 description => "source files to syntax highlight",
36 if (exists $config{tohighlight}) {
37 foreach my $file (split ' ', $config{tohighlight}) {
38 my @opts = $file=~s/^\.// ?
39 (keepextension => 1) :
41 my $ext = $file=~s/:(.*)// ? $1 : $file;
43 my $langfile=ext2langfile($ext);
44 if (! defined $langfile) {
45 error(sprintf(gettext(
46 "tohighlight contains unknown file type '%s'"),
55 highlight($langfile, $params{content});
57 longname => sprintf(gettext("Source code: %s"), $file),
66 my $langfile=ext2langfile($format);
68 if (! defined $langfile) {
72 return highlight($langfile, shift);
78 # Parse highlight's config file to get extension => language mappings.
79 sub read_filetypes () {
80 open (IN, $filetypes);
83 if (/^\$ext\((.*)\)=(.*)$/) {
84 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
92 return "$langdefdir/$_[0].lang";
95 # Given a filename extension, determines the language definition to
96 # use to highlight it.
97 sub ext2langfile ($) {
100 read_filetypes() unless $filetypes_read;
101 if (exists $ext2lang{$ext}) {
102 return langfile($ext2lang{$ext});
104 # If a language only has one common extension, it will not
105 # be listed in filetypes, so check the langfile.
106 elsif (-e langfile($ext)) {
107 return langfile($ext);
114 # Interface to the highlight C library.
119 eval q{use highlight};
121 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
125 my $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
126 $gen->setFragmentCode(1); # generate html fragment
127 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
128 $gen->initLanguage($langfile);
129 $gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
130 $gen->setEncoding("utf-8");
132 my $output=$gen->generateString($input);
133 highlightc::CodeGenerator_deleteInstance($gen);