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>
38 >>> That is quite strange. I tested your version of the plugin. I had to revert one your changes to get it to
39 >>> work: the linenumber argument should not have a space at the end of it. Once I made that change,
40 >>> everything worked as expected. The output I get for your example is below:
44 <li><div id="sourcecode"></li>
47 <pre><tt><span class="linenum">00001:</span> <span class="normal">test</span></tt></pre>
53 >>> I don't know what is going wrong for you... source-highlight, Markdown or something else.
54 >>> I do find it interesting the way the sourcecode `div` and the list get interleaved. That
55 >>> just looks like a Markdown thing though.
56 >>> In any case, I've updated the patch below to include most of your changes. -- [[Will]]
62 # Originally by Will Uther
63 # With modifications by David Bremner
64 package IkiWiki::Plugin::sourcecode;
69 use open qw{:utf8 :std};
74 hook(type => "getsetup", id => "sourcecode", call => \&getsetup);
75 hook(type => "checkconfig", id => "sourcecode", call => \&checkconfig);
76 hook(type => "pagetemplate", id => "sourcecode", call => \&pagetemplate);
83 rebuild => 1, # format plugin
85 sourcecode_command => {
87 example => "/usr/bin/source-highlight",
88 description => "The command to execute to run source-highlight",
94 example => "c,cpp,h,java",
95 description => "Comma separated list of suffixes to recognise as source code",
99 sourcecode_linenumbers => {
102 description => "Should we add line numbers to the source code",
108 example => "sourcecode_style",
109 description => "page to use as css file for source",
116 if (! $config{sourcecode_lang}) {
117 error("The sourcecode plugin requires a list of suffixes in the 'sourcecode_lang' config option");
120 if (! $config{sourcecode_command}) {
121 $config{sourcecode_command} = "source-highlight";
124 if (! length `which $config{sourcecode_command} 2>/dev/null`) {
125 error("The sourcecode plugin is unable to find the $config{sourcecode_command} command");
128 if (! $config{sourcecode_css}) {
129 $config{sourcecode_css} = "sourcecode_style";
132 if (! defined $config{sourcecode_linenumbers}) {
133 $config{sourcecode_linenumbers} = 1;
138 open(LANGS, "$config{sourcecode_command} --lang-list|");
140 if ($_ =~ /(\w+) = .+\.lang/) {
146 foreach my $lang (split(/[, ]+/, $config{sourcecode_lang})) {
148 hook(type => "htmlize", id => $lang, no_override=>1,
149 call => sub { htmlize(lang=>$lang, @_) },
152 error("Your installation of source-highlight cannot handle sourcecode language $lang!");
160 my $page = $params{page};
162 eval q{use FileHandle};
164 eval q{use IPC::Open2};
167 local(*SPS_IN, *SPS_OUT); # Create local handles
171 if ($config{sourcecode_linenumbers}) {
172 push @args, '--line-number';
175 my $pid = open2(*SPS_IN, *SPS_OUT, $config{sourcecode_command},
177 '-c', $config{sourcecode_css}, '--no-doc',
181 error("Unable to open $config{sourcecode_command}") unless $pid;
183 print SPS_OUT $params{content};
191 my $stylesheet=bestlink($page, $config{sourcecode_css}.".css");
192 if (length $stylesheet) {
193 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).'"'.
195 ' type="text/css" />';
198 return '<div id="sourcecode">'."\r\n".join("",@html)."\r\n</div>\r\n";
201 sub pagetemplate (@) {
204 my $page=$params{page};
205 my $template=$params{template};
207 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
208 # avoid duplicate meta lines
210 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));