]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/automatic_use_of_syntax_plugin_on_source_code_files/discussion.mdwn
strange output from sourcecode plugin
[ikiwiki.git] / 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 ----
39
40     #!/usr/bin/perl
41     # markup source files
42     package IkiWiki::Plugin::sourcecode;
43     
44     use warnings;
45     use strict;
46     use IkiWiki 2.00;
47     use open qw{:utf8 :std};
48     
49     my %metaheaders;
50     
51     sub import {
52         hook(type => "getsetup", id => "sourcecode", call => \&getsetup);
53         hook(type => "checkconfig", id => "sourcecode", call => \&checkconfig);
54         hook(type => "pagetemplate", id => "sourcecode", call => \&pagetemplate);
55     }
56     
57     sub getsetup () {
58         return 
59                 plugin => {
60                         safe => 1,
61                         rebuild => 1, # format plugin
62                 },
63                 sourcecode_command => {
64                         type => "string",
65                         example => "/usr/bin/source-highlight",
66                         description => "The command to execute to run source-highlight",
67                         safe => 0,
68                         rebuild => 1,
69                 },
70                 sourcecode_lang => {
71                         type => "string",
72                         example => "c,cpp,h,java",
73                         description => "Comma separated list of suffixes to recognise as source code",
74                         safe => 1,
75                         rebuild => 1,
76                 },
77                 sourcecode_linenumbers => {
78                         type => "boolean",
79                         example => 1,
80                         description => "Should we add line numbers to the source code",
81                         safe => 1,
82                         rebuild => 1,
83                 },
84                 sourcecode_css => {
85                         type => "string",
86                         example => "sourcecode_style",
87                         description => "page to use as css file for source",
88                         safe => 1,
89                         rebuild => 1,
90                 },
91     }
92     
93     sub checkconfig () {
94         if (! $config{sourcecode_lang}) {
95                 error("The sourcecode plugin requires a list of suffixes in the 'sourcecode_lang' config option");
96         }
97         
98         if (! $config{sourcecode_command}) {
99                 $config{sourcecode_command} = "source-highlight";
100         }
101         
102         if (! length `which $config{sourcecode_command} 2>/dev/null`) {
103                 error("The sourcecode plugin is unable to find the $config{sourcecode_command} command");
104         }
105     
106         if (! $config{sourcecode_css}) {
107                 $config{sourcecode_css} = "sourcecode_style";
108         }
109         
110         if (! defined $config{sourcecode_linenumbers}) {
111                 $config{sourcecode_linenumbers} = 1;
112         }
113         
114         my %langs = ();
115         
116         open(LANGS, "$config{sourcecode_command} --lang-list|");
117         while (<LANGS>) {
118                 if ($_ =~ /(\w+) = .+\.lang/) {
119                         $langs{$1} = 1;
120                 }
121         }
122         close(LANGS);
123         
124         foreach my $lang (split(/[, ]+/, $config{sourcecode_lang})) {
125                 if ($langs{$lang}) {
126                         hook(type => "htmlize", id => $lang, call => \&htmlize, keepextension => 1);
127                 } else {
128                         error("Your installation of source-highlight cannot handle sourcecode language $lang!");
129                 }
130         }
131     }
132     
133     sub htmlize (@) {
134         my %params=@_;
135     
136         my $page = $params{page};
137     
138         eval q{use FileHandle};
139         error($@) if $@;
140         eval q{use IPC::Open2};
141         error($@) if $@;
142     
143         local(*SPS_IN, *SPS_OUT);  # Create local handles
144     
145         my @args;
146     
147         if ($config{sourcecode_linenumbers}) {
148                 push @args, '--line-number= ';
149         }
150     
151         my $pid = open2(*SPS_IN, *SPS_OUT, $config{sourcecode_command},
152                                         '-s', IkiWiki::pagetype($pagesources{$page}),
153                                         '-c', $config{sourcecode_css}, '--no-doc',
154                                         '-f', 'xhtml',
155                                         @args);
156     
157         error("Unable to open $config{sourcecode_command}") unless $pid;
158     
159         print SPS_OUT $params{content};
160         close SPS_OUT;
161     
162         my @html = <SPS_IN>;
163         close SPS_IN;
164         
165         waitpid $pid, 0;
166     
167         my $stylesheet=bestlink($page, $config{sourcecode_css}.".css");
168         if (length $stylesheet) {
169                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).'"'.
170                         ' rel="stylesheet"'.
171                         ' type="text/css" />';
172         }
173     
174         return '<div id="sourcecode">'."\r\n".join("\r\n",@html)."\r\n</div>\n";
175     }
176     
177     sub pagetemplate (@) {
178         my %params=@_;
179     
180         my $page=$params{page};
181         my $template=$params{template};
182     
183         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
184                 # avoid duplicate meta lines
185                 my %seen;
186                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
187         }
188     }
189     
190     1