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 => "types of 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);
79 # Parse highlight's config file to get extension => language mappings.
80 sub read_filetypes () {
81 open (IN, $filetypes);
84 if (/^\$ext\((.*)\)=(.*)$/) {
85 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
93 # Given a filename extension, determines the language definition to
94 # use to highlight it.
95 sub ext2langfile ($) {
98 my $langfile="$langdefdir/$ext.lang";
99 return $langfile if exists $highlighters{$langfile};
101 read_filetypes() unless $filetypes_read;
102 if (exists $ext2lang{$ext}) {
103 return "$langdefdir/$ext2lang{$ext}.lang";
105 # If a language only has one common extension, it will not
106 # be listed in filetypes, so check the langfile.
107 elsif (-e $langfile) {
115 # Interface to the highlight C library.
120 eval q{use highlight};
122 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
127 if (! exists $highlighters{$langfile}) {
128 $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
129 $gen->setFragmentCode(1); # generate html fragment
130 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
131 $gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
132 $gen->initLanguage($langfile); # must come after initTheme
133 $gen->setEncoding("utf-8");
134 $highlighters{$langfile}=$gen;
137 $gen=$highlighters{$langfile};
140 return $gen->generateString($input);