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
30 example => ".c .h .cpp .pl .py Makefile:make",
31 description => "types of source files to syntax highlight",
38 if (exists $config{tohighlight}) {
39 foreach my $file (split ' ', $config{tohighlight}) {
40 my @opts = $file=~s/^\.// ?
41 (keepextension => 1) :
43 my $ext = $file=~s/:(.*)// ? $1 : $file;
45 my $langfile=ext2langfile($ext);
46 if (! defined $langfile) {
47 error(sprintf(gettext(
48 "tohighlight contains unknown file type '%s'"),
57 highlight($langfile, $params{content});
59 longname => sprintf(gettext("Source code: %s"), $file),
68 my $langfile=ext2langfile($format);
70 if (! defined $langfile) {
74 return Encode::decode_utf8(highlight($langfile, shift));
81 # Parse highlight's config file to get extension => language mappings.
82 sub read_filetypes () {
83 open (IN, $filetypes);
86 if (/^\$ext\((.*)\)=(.*)$/) {
87 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
95 # Given a filename extension, determines the language definition to
96 # use to highlight it.
97 sub ext2langfile ($) {
100 my $langfile="$langdefdir/$ext.lang";
101 return $langfile if exists $highlighters{$langfile};
103 read_filetypes() unless $filetypes_read;
104 if (exists $ext2lang{$ext}) {
105 return "$langdefdir/$ext2lang{$ext}.lang";
107 # If a language only has one common extension, it will not
108 # be listed in filetypes, so check the langfile.
109 elsif (-e $langfile) {
117 # Interface to the highlight C library.
122 eval q{use highlight};
124 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
129 if (! exists $highlighters{$langfile}) {
130 $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
131 $gen->setFragmentCode(1); # generate html fragment
132 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
133 $gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
134 $gen->initLanguage($langfile); # must come after initTheme
135 $gen->setEncoding("utf-8");
136 $highlighters{$langfile}=$gen;
139 $gen=$highlighters{$langfile};
142 return $gen->generateString($input);