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