1 Here is another [[patch]] for this. It is more up to date than either of the patches linked on the previous page. It is most similar to [[plugins/contrib/sourcehighlight]].
3 Updated to use fix noted in [[bugs/multiple_pages_with_same_name]].
8 I was trying to replace sourcehighlight with sourcecode. I had to modify the
9 htmlize call slightly so that it would work in a format directive.
10 ([modified version](http://pivot.cs.unb.ca/git/?p=ikiplugins.git;a=blob_plain;f=IkiWiki/Plugin/sourcecode.pm;hb=21fc57091edb9))
11 I hit a wall the following example (the last commit in the above repo).
13 \[[!meta title="Solutions to assignment 1"]]
24 package IkiWiki::Plugin::sourcecode;
29 use open qw{:utf8 :std};
34 hook(type => "getsetup", id => "sourcecode", call => \&getsetup);
35 hook(type => "checkconfig", id => "sourcecode", call => \&checkconfig);
36 hook(type => "pagetemplate", id => "sourcecode", call => \&pagetemplate);
43 rebuild => 1, # format plugin
45 sourcecode_command => {
47 example => "/usr/bin/source-highlight",
48 description => "The command to execute to run source-highlight",
54 example => "c,cpp,h,java",
55 description => "Comma separated list of suffixes to recognise as source code",
59 sourcecode_linenumbers => {
62 description => "Should we add line numbers to the source code",
68 example => "sourcecode_style",
69 description => "page to use as css file for source",
76 if (! $config{sourcecode_lang}) {
77 error("The sourcecode plugin requires a list of suffixes in the 'sourcecode_lang' config option");
80 if (! $config{sourcecode_command}) {
81 $config{sourcecode_command} = "source-highlight";
84 if (! length `which $config{sourcecode_command} 2>/dev/null`) {
85 error("The sourcecode plugin is unable to find the $config{sourcecode_command} command");
88 if (! $config{sourcecode_css}) {
89 $config{sourcecode_css} = "sourcecode_style";
92 if (! defined $config{sourcecode_linenumbers}) {
93 $config{sourcecode_linenumbers} = 1;
98 open(LANGS, "$config{sourcecode_command} --lang-list|");
100 if ($_ =~ /(\w+) = .+\.lang/) {
106 foreach my $lang (split(/[, ]+/, $config{sourcecode_lang})) {
108 hook(type => "htmlize", id => $lang, call => \&htmlize, keepextension => 1);
110 error("Your installation of source-highlight cannot handle sourcecode language $lang!");
118 my $page = $params{page};
120 eval q{use FileHandle};
122 eval q{use IPC::Open2};
125 local(*SPS_IN, *SPS_OUT); # Create local handles
129 if ($config{sourcecode_linenumbers}) {
130 push @args, '--line-number= ';
133 my $pid = open2(*SPS_IN, *SPS_OUT, $config{sourcecode_command},
134 '-s', IkiWiki::pagetype($pagesources{$page}),
135 '-c', $config{sourcecode_css}, '--no-doc',
139 error("Unable to open $config{sourcecode_command}") unless $pid;
141 print SPS_OUT $params{content};
149 my $stylesheet=bestlink($page, $config{sourcecode_css}.".css");
150 if (length $stylesheet) {
151 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).'"'.
153 ' type="text/css" />';
156 return '<div id="sourcecode">'."\r\n".join("\r\n",@html)."\r\n</div>\n";
159 sub pagetemplate (@) {
162 my $page=$params{page};
163 my $template=$params{template};
165 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
166 # avoid duplicate meta lines
168 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));