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);
22 rebuild => 1, # format plugin
26 example => ".c, .h, .cpp, .pl, .py, Makefile:make",
27 description => "source files to syntax highlight",
34 if (exists $config{tohighlight}) {
35 foreach my $file (split /, /, $config{tohighlight}) {
36 my @opts = $file=~s/^\.// ?
37 (keepextension => 1) :
39 my $ext = $file=~s/:(.*)// ? $1 : $file;
41 my $langfile=ext2langfile($ext);
42 if (! defined $langfile) {
43 error(sprintf(gettext(
44 "tohighlight contains unknown file type '%s'"),
53 highlight($langfile, $params{content});
55 longname => sprintf(gettext("Source code: %s"), $file),
65 # Parse highlight's config file to get extension => language mappings.
66 sub read_filetypes () {
67 open (IN, $filetypes);
70 if (/^\$ext\((.*)\)=(.*)$/) {
71 $ext2lang{$_}=$1 foreach $1, split ' ', $2;
79 return "$langdefdir/$_[0].lang";
82 # Given a filename extension, determines the language definition to
83 # use to highlight it.
84 sub ext2langfile ($) {
87 read_filetypes() unless $filetypes_read;
88 if (exists $ext2lang{$ext}) {
89 return langfile($ext2lang{$ext});
91 # If a language only has one common extension, it will not
92 # be listed in filetypes, so check the langfile.
93 elsif (-e langfile($ext)) {
94 return langfile($ext);
101 # Interface to the highlight C library.
106 my $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
107 $gen->setFragmentCode(1); # generate html fragment
108 $gen->setHTMLEnclosePreTag(1); # include stylish <pre>
109 $gen->initLanguage($langfile);
110 $gen->initTheme("/dev/null"); # theme is not needed because CSS is not emitted
111 $gen->setEncoding("utf-8");
113 my $output=$gen->generateString($input);
114 highlightc::CodeGenerator_deleteInstance($gen);