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))
12 > I haven't tested them, but those changes look sensible to me. -- [[Will]]
14 I hit a wall the following example (the last commit in the above repo).
16 \[[!meta title="Solutions to assignment 1"]]
23 > I haven't actually tested this to see what the problem is. How does this fail?
24 > Does source-highlight barf on the non-c++ content? Is there a wiki URL that shows the failure? -- [[Will]]
25 >> Here is the content div from the output page
30 <li><div id="sourcecode"></li>
32 2beb4fd7289998159f61976143f66bb6</p>
42 package IkiWiki::Plugin::sourcecode;
47 use open qw{:utf8 :std};
52 hook(type => "getsetup", id => "sourcecode", call => \&getsetup);
53 hook(type => "checkconfig", id => "sourcecode", call => \&checkconfig);
54 hook(type => "pagetemplate", id => "sourcecode", call => \&pagetemplate);
61 rebuild => 1, # format plugin
63 sourcecode_command => {
65 example => "/usr/bin/source-highlight",
66 description => "The command to execute to run source-highlight",
72 example => "c,cpp,h,java",
73 description => "Comma separated list of suffixes to recognise as source code",
77 sourcecode_linenumbers => {
80 description => "Should we add line numbers to the source code",
86 example => "sourcecode_style",
87 description => "page to use as css file for source",
94 if (! $config{sourcecode_lang}) {
95 error("The sourcecode plugin requires a list of suffixes in the 'sourcecode_lang' config option");
98 if (! $config{sourcecode_command}) {
99 $config{sourcecode_command} = "source-highlight";
102 if (! length `which $config{sourcecode_command} 2>/dev/null`) {
103 error("The sourcecode plugin is unable to find the $config{sourcecode_command} command");
106 if (! $config{sourcecode_css}) {
107 $config{sourcecode_css} = "sourcecode_style";
110 if (! defined $config{sourcecode_linenumbers}) {
111 $config{sourcecode_linenumbers} = 1;
116 open(LANGS, "$config{sourcecode_command} --lang-list|");
118 if ($_ =~ /(\w+) = .+\.lang/) {
124 foreach my $lang (split(/[, ]+/, $config{sourcecode_lang})) {
126 hook(type => "htmlize", id => $lang, call => \&htmlize, keepextension => 1);
128 error("Your installation of source-highlight cannot handle sourcecode language $lang!");
136 my $page = $params{page};
138 eval q{use FileHandle};
140 eval q{use IPC::Open2};
143 local(*SPS_IN, *SPS_OUT); # Create local handles
147 if ($config{sourcecode_linenumbers}) {
148 push @args, '--line-number= ';
151 my $pid = open2(*SPS_IN, *SPS_OUT, $config{sourcecode_command},
152 '-s', IkiWiki::pagetype($pagesources{$page}),
153 '-c', $config{sourcecode_css}, '--no-doc',
157 error("Unable to open $config{sourcecode_command}") unless $pid;
159 print SPS_OUT $params{content};
167 my $stylesheet=bestlink($page, $config{sourcecode_css}.".css");
168 if (length $stylesheet) {
169 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).'"'.
171 ' type="text/css" />';
174 return '<div id="sourcecode">'."\r\n".join("\r\n",@html)."\r\n</div>\n";
177 sub pagetemplate (@) {
180 my $page=$params{page};
181 my $template=$params{template};
183 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
184 # avoid duplicate meta lines
186 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));