2 package IkiWiki::Plugin::highlight;
10 hook(type => "getsetup", id => "highlight", call => \&getsetup);
11 hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
12 # this hook is used by the format plugin
13 hook(type => "htmlizeformat", id => "highlight",
14 call => \&htmlizeformat, last => 1);
21 rebuild => 1, # format plugin
26 example => ".c .h .cpp .pl .py Makefile:make",
27 description => "types of source files to syntax highlight",
33 example => "/etc/highlight/filetypes.conf",
34 description => "location of highlight's filetypes.conf",
40 example => "/usr/share/highlight/langDefs",
41 description => "location of highlight's langDefs directory",
48 if (! exists $config{filetypes_conf}) {
49 $config{filetypes_conf}="/etc/highlight/filetypes.conf";
51 if (! exists $config{langdefdir}) {
52 $config{langdefdir}="/usr/share/highlight/langDefs";
54 if (exists $config{tohighlight}) {
55 foreach my $file (split ' ', $config{tohighlight}) {
56 my @opts = $file=~s/^\.// ?
57 (keepextension => 1) :
59 my $ext = $file=~s/:(.*)// ? $1 : $file;
61 my $langfile=ext2langfile($ext);
62 if (! defined $langfile) {
63 error(sprintf(gettext(
64 "tohighlight contains unknown file type '%s'"),
73 highlight($langfile, $params{content});
75 longname => sprintf(gettext("Source code: %s"), $file),
84 my $langfile=ext2langfile($format);
86 if (! defined $langfile) {
90 return Encode::decode_utf8(highlight($langfile, shift));
97 # Parse highlight's config file to get extension => language mappings.
98 sub read_filetypes () {
99 open (IN, $config{filetypes_conf}) || error("$config{filetypes_conf}: $!");
102 if (/^\$ext\((.*)\)=(.*)$/) {
103 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
111 # Given a filename extension, determines the language definition to
112 # use to highlight it.
113 sub ext2langfile ($) {
116 my $langfile="$config{langdefdir}/$ext.lang";
117 return $langfile if exists $highlighters{$langfile};
119 read_filetypes() unless $filetypes_read;
120 if (exists $ext2lang{$ext}) {
121 return "$config{langdefdir}/$ext2lang{$ext}.lang";
123 # If a language only has one common extension, it will not
124 # be listed in filetypes, so check the langfile.
125 elsif (-e $langfile) {
133 # Interface to the highlight C library.
138 eval q{use highlight};
140 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
145 if (! exists $highlighters{$langfile}) {
146 $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
147 $gen->setFragmentCode(1); # generate html fragment
148 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
149 $gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
150 $gen->initLanguage($langfile); # must come after initTheme
151 $gen->setEncoding("utf-8");
152 $highlighters{$langfile}=$gen;
155 $gen=$highlighters{$langfile};
158 return $gen->generateString($input);