2 package IkiWiki::Plugin::highlight;
4 # This has been tested with highlight 2.16 and highlight 3.2+svn19.
5 # In particular version 3.2 won't work. It detects the different
6 # versions by the presence of the the highlight::DataDir class.
16 hook(type => "getsetup", id => "highlight", call => \&getsetup);
17 hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
18 # this hook is used by the format plugin
19 hook(type => "htmlizeformat", id => "highlight",
20 call => \&htmlizeformat, last => 1);
27 rebuild => 1, # format plugin
32 example => ".c .h .cpp .pl .py Makefile:make",
33 description => "types of source files to syntax highlight",
39 example => "/etc/highlight/filetypes.conf",
40 description => "location of highlight's filetypes.conf",
46 example => "/usr/share/highlight/langDefs",
47 description => "location of highlight's langDefs directory",
55 eval q{use highlight};
57 print STDERR "Failed to load highlight. Configuring anyway.\n";
60 if (highlight::DataDir->can('new')){
61 $data_dir=new highlight::DataDir();
62 $data_dir->searchDataDir("");
67 if (! exists $config{filetypes_conf}) {
68 $config{filetypes_conf}=
69 ($data_dir ? $data_dir->getConfDir() : "/etc/highlight/")
72 if (! exists $config{langdefdir}) {
74 ($data_dir ? $data_dir->getLangPath("")
75 : "/usr/share/highlight/langDefs");
78 if (exists $config{tohighlight} && read_filetypes()) {
79 foreach my $file (split ' ', $config{tohighlight}) {
80 my @opts = $file=~s/^\.// ?
81 (keepextension => 1) :
83 my $ext = $file=~s/:(.*)// ? $1 : $file;
85 my $langfile=ext2langfile($ext);
86 if (! defined $langfile) {
87 error(sprintf(gettext(
88 "tohighlight contains unknown file type '%s'"),
97 highlight($langfile, $params{content});
99 longname => sprintf(gettext("Source code: %s"), $file),
108 my $langfile=ext2langfile($format);
110 if (! defined $langfile) {
114 return Encode::decode_utf8(highlight($langfile, shift));
118 my $filetypes_read=0;
121 # Parse highlight's config file to get extension => language mappings.
122 sub read_filetypes () {
124 if (!open($f, $config{filetypes_conf})) {
125 warn($config{filetypes_conf}.": ".$!);
133 # highlight >= 3.2 format (bind-style)
134 while ($config=~m/Lang\s*=\s*\"([^"]+)\"[,\s]+Extensions\s*=\s*{([^}]+)}/sg) {
136 foreach my $bit (split ',', $2) {
137 $bit=~s/.*"(.*)".*/$1/s;
138 $ext2lang{$bit}=$lang;
142 # highlight < 3.2 format
143 if (! keys %ext2lang) {
144 foreach (split("\n", $config)) {
145 if (/^\$ext\((.*)\)=(.*)$/) {
146 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
151 return $filetypes_read=1;
155 # Given a filename extension, determines the language definition to
156 # use to highlight it.
157 sub ext2langfile ($) {
160 my $langfile="$config{langdefdir}/$ext.lang";
161 return $langfile if exists $highlighters{$langfile};
163 read_filetypes() unless $filetypes_read;
164 if (exists $ext2lang{$ext}) {
165 return "$config{langdefdir}/$ext2lang{$ext}.lang";
167 # If a language only has one common extension, it will not
168 # be listed in filetypes, so check the langfile.
169 elsif (-e $langfile) {
177 # Interface to the highlight C library.
182 eval q{use highlight};
184 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
189 if (! exists $highlighters{$langfile}) {
190 $gen = highlight::CodeGenerator::getInstance($highlight::XHTML);
191 $gen->setFragmentCode(1); # generate html fragment
192 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
194 # new style, requires a real theme, but has no effect
195 $gen->initTheme($data_dir->getThemePath("seashell.theme"));
197 # old style, anything works.
198 $gen->initTheme("/dev/null");
200 $gen->loadLanguage($langfile); # must come after initTheme
201 $gen->setEncoding("utf-8");
202 $highlighters{$langfile}=$gen;
205 $gen=$highlighters{$langfile};
208 return $gen->generateString($input);