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",
54 eval q{use highlight};
55 if (highlight::DataDir->can('new')) {
56 $data_dir=new highlight::DataDir();
57 $data_dir->searchDataDir("");
62 if (! exists $config{filetypes_conf}) {
63 $config{filetypes_conf}=
64 ($data_dir ? $data_dir->getConfDir() : "/etc/highlight/")
67 if (! exists $config{langdefdir}) {
69 ($data_dir ? $data_dir->getLangPath("")
70 : "/usr/share/highlight/langDefs");
73 if (exists $config{tohighlight} && read_filetypes()) {
74 foreach my $file (split ' ', $config{tohighlight}) {
75 my @opts = $file=~s/^\.// ?
76 (keepextension => 1) :
78 my $ext = $file=~s/:(.*)// ? $1 : $file;
80 my $langfile=ext2langfile($ext);
81 if (! defined $langfile) {
82 error(sprintf(gettext(
83 "tohighlight contains unknown file type '%s'"),
92 highlight($langfile, $file, $params{content});
94 longname => sprintf(gettext("Source code: %s"), $file),
103 my $langfile=ext2langfile($format);
105 if (! defined $langfile) {
109 return Encode::decode_utf8(highlight($langfile, $format, shift));
113 my $filetypes_read=0;
116 # Parse highlight's config file to get extension => language mappings.
117 sub read_filetypes () {
119 if (!open($f, $config{filetypes_conf})) {
120 warn($config{filetypes_conf}.": ".$!);
128 # highlight >= 3.2 format (bind-style)
129 while ($config=~m/Lang\s*=\s*\"([^"]+)\"[,\s]+Extensions\s*=\s*{([^}]+)}/sg) {
131 foreach my $bit (split ',', $2) {
132 $bit=~s/.*"(.*)".*/$1/s;
133 $ext2lang{$bit}=$lang;
137 # highlight < 3.2 format
138 if (! keys %ext2lang) {
139 foreach (split("\n", $config)) {
140 if (/^\$ext\((.*)\)=(.*)$/) {
141 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
146 return $filetypes_read=1;
150 # Given a filename extension, determines the language definition to
151 # use to highlight it.
152 sub ext2langfile ($) {
155 my $langfile="$config{langdefdir}/$ext.lang";
156 return $langfile if exists $highlighters{$langfile};
158 read_filetypes() unless $filetypes_read;
159 if (exists $ext2lang{$ext}) {
160 return "$config{langdefdir}/$ext2lang{$ext}.lang";
162 # If a language only has one common extension, it will not
163 # be listed in filetypes, so check the langfile.
164 elsif (-e $langfile) {
172 # Interface to the highlight C library.
178 eval q{use highlight};
180 print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
185 if (! exists $highlighters{$langfile}) {
186 $gen = highlight::CodeGenerator::getInstance($highlight::XHTML);
187 $gen->setFragmentCode(1); # generate html fragment
188 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
190 # new style, requires a real theme, but has no effect
191 $gen->initTheme($data_dir->getThemePath("seashell.theme"));
193 # old style, anything works.
194 $gen->initTheme("/dev/null");
196 $gen->loadLanguage($langfile); # must come after initTheme
197 $gen->setEncoding("utf-8");
198 $highlighters{$langfile}=$gen;
201 $gen=$highlighters{$langfile};
204 return "<div class=\"highlight-$extorfile\">".$gen->generateString($input)."</div>";