also tag 'patch/core', considering that over half of the changes are there
[ikiwiki] / doc / todo / automatic_use_of_syntax_plugin_on_source_code_files / discussion.mdwn
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]].
2
3 Updated to use fix noted in [[bugs/multiple_pages_with_same_name]].
4
5 -- [[Will]]
6
7 ----
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
12 > I haven't tested them, but those changes look sensible to me. -- [[Will]]
13
14 I hit a wall the following example (the last commit in the above repo).
15
16     \[[!meta title="Solutions to assignment 1"]]
17
18     - [[!format cc """
19     test
20     """]]
21
22
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
26 >> [[DavidBremner]]
27
28      <div id="content">
29      <p><ul>
30      <li><div id="sourcecode"></li>
31      </ul>
32      2beb4fd7289998159f61976143f66bb6</p>
33
34      <p></div></p>
35
36      </div>
37
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:
41
42     <div id="content">
43     <ul>
44     <li><div id="sourcecode"></li>
45     </ul>
46     
47     <pre><tt><span class="linenum">00001:</span> <span class="normal">test</span></tt></pre>
48     
49     <p></div></p>
50     
51     </div>
52
53 >>> I don't know what is going wrong for you... source-highlight, Markdown or something else.
54 >>>> It's a well-known bug in old versions of markdown. --[[Joey]] 
55 >>> I do find it interesting the way the sourcecode `div` and the list get interleaved.  That
56 >>> just looks like a Markdown thing though.
57 >>> In any case, I've updated the patch below to include most of your changes.  -- [[Will]]
58
59 ----
60
61     #!/usr/bin/perl
62     # markup source files
63     # Originally by Will Uther
64     # With modifications by David Bremner
65     package IkiWiki::Plugin::sourcecode;
66     
67     use warnings;
68     use strict;
69     use IkiWiki 2.00;
70     use open qw{:utf8 :std};
71     
72     my %metaheaders;
73     
74     sub import {
75         hook(type => "getsetup", id => "sourcecode", call => \&getsetup);
76         hook(type => "checkconfig", id => "sourcecode", call => \&checkconfig);
77         hook(type => "pagetemplate", id => "sourcecode", call => \&pagetemplate);
78     }
79     
80     sub getsetup () {
81         return 
82             plugin => {
83                 safe => 1,
84                 rebuild => 1, # format plugin
85             },
86             sourcecode_command => {
87                 type => "string",
88                 example => "/usr/bin/source-highlight",
89                 description => "The command to execute to run source-highlight",
90                 safe => 0,
91                 rebuild => 1,
92             },
93             sourcecode_lang => {
94                 type => "string",
95                 example => "c,cpp,h,java",
96                 description => "Comma separated list of suffixes to recognise as source code",
97                 safe => 1,
98                 rebuild => 1,
99             },
100             sourcecode_linenumbers => {
101                 type => "boolean",
102                 example => 1,
103                 description => "Should we add line numbers to the source code",
104                 safe => 1,
105                 rebuild => 1,
106             },
107             sourcecode_css => {
108                 type => "string",
109                 example => "sourcecode_style",
110                 description => "page to use as css file for source",
111                 safe => 1,
112                 rebuild => 1,
113             },
114     }
115     
116     sub checkconfig () {
117         if (! $config{sourcecode_lang}) {
118             error("The sourcecode plugin requires a list of suffixes in the 'sourcecode_lang' config option");
119         }
120     
121         if (! $config{sourcecode_command}) {
122             $config{sourcecode_command} = "source-highlight";
123         }
124     
125         if (! length `which $config{sourcecode_command} 2>/dev/null`) {
126             error("The sourcecode plugin is unable to find the $config{sourcecode_command} command");
127         }
128     
129         if (! $config{sourcecode_css}) {
130             $config{sourcecode_css} = "sourcecode_style";
131         }
132     
133         if (! defined $config{sourcecode_linenumbers}) {
134             $config{sourcecode_linenumbers} = 1;
135         }
136     
137         my %langs = ();
138     
139         open(LANGS, "$config{sourcecode_command} --lang-list|");
140         while (<LANGS>) {
141             if ($_ =~ /(\w+) = .+\.lang/) {
142                 $langs{$1} = 1;
143             }
144         }
145         close(LANGS);
146     
147         foreach my $lang (split(/[, ]+/, $config{sourcecode_lang})) {
148             if ($langs{$lang}) {
149                 hook(type => "htmlize", id => $lang, no_override=>1,
150                  call => sub { htmlize(lang=>$lang, @_) }, 
151                  keepextension => 1);
152             } else {
153                 error("Your installation of source-highlight cannot handle sourcecode language $lang!");
154             }
155         }
156     }
157     
158     sub htmlize (@) {
159         my %params=@_;
160     
161         my $page = $params{page};
162     
163         eval q{use FileHandle};
164         error($@) if $@;
165         eval q{use IPC::Open2};
166         error($@) if $@;
167     
168         local(*SPS_IN, *SPS_OUT);  # Create local handles
169     
170         my @args;
171     
172         if ($config{sourcecode_linenumbers}) {
173             push @args, '--line-number';
174         }
175     
176         my $pid = open2(*SPS_IN, *SPS_OUT, $config{sourcecode_command},
177                         '-s', $params{lang},
178                         '-c', $config{sourcecode_css}, '--no-doc',
179                         '-f', 'xhtml',
180                         @args);
181     
182         error("Unable to open $config{sourcecode_command}") unless $pid;
183     
184         print SPS_OUT $params{content};
185         close SPS_OUT;
186     
187         my @html = <SPS_IN>;
188         close SPS_IN;
189     
190         waitpid $pid, 0;
191     
192         my $stylesheet=bestlink($page, $config{sourcecode_css}.".css");
193         if (length $stylesheet) {
194             push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).'"'.
195                 ' rel="stylesheet"'.
196                 ' type="text/css" />';
197         }
198     
199         return '<div id="sourcecode">'."\r\n".join("",@html)."\r\n</div>\r\n";
200     }
201     
202     sub pagetemplate (@) {
203         my %params=@_;
204     
205         my $page=$params{page};
206         my $template=$params{template};
207     
208         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
209             # avoid duplicate meta lines
210             my %seen;
211             $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
212         }
213     }
214     
215     1