1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
<?php
/*
Plugin Name: Highlight
Plugin URI: http://www.andre-simon.de
Description: Plugin which uses the highlight utility to generate coloured source code
Author: André Simon
Version: 1.2
Author URI: http://www.andre-simon.de
This file is part of Highlight.
Highlight is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Highlight is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Highlight. If not, see <http://www.gnu.org/licenses/>.
*/
/*
== Description ==
The highlight utility converts source code of 120 programming languages to HTML
with syntax highlighting. This plugin pipes the content of <highlight>-Tags associated
with a lang parameter to highlight, and returns the output code which is included
in the Wiki page.
Usage:
Paste the following in the edit section of the wiki editing form:
<highlight cpp>#include <stdio.h>
int main (void){
printf("This is some random code");
return 0;
}</highlight>
Use the lang parameter to define the programming language (c, php, py, xml, etc).
See the highlight documentation to learn all possible languages.
See the syntax.php file for some formatting options (line numbering, code
indentation, line wrapping etc).
== Installation ==
1. Install highlight (www.andre-simon.de) on your host
2. Unzip the dokuwiki_highlight.zip file and upload the content to the
`lib/plugins/` directory
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
class syntax_plugin_highlight extends DokuWiki_Syntax_Plugin {
function getInfo(){
return array(
'author' => 'Andre Simon',
'email' => 'andre.simon1@gmx.de',
'date' => '2007-05-29',
'name' => 'Highlight Plugin',
'desc' => 'Plugin which uses the highlight utility (http://www.andre-simon.de) instead of Geshi for source code highlighting',
'url' => 'http://wiki.splitbrain.org/wiki:plugins',
);
}
/**
* What kind of syntax are we?
*/
function getType(){
return 'formatting';
}
/**
* What kind of syntax do we allow (optional)
*/
// function getAllowedTypes() {
// return array();
// }
/**
* What about paragraphs? (optional)
*/
function getPType(){
return 'block';
}
/**
* Where to sort in?
*/
function getSort(){
return 199;
}
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addEntryPattern('<highlight(?=[^\r\n]*?>.*?</highlight>)',$mode,'plugin_highlight');
}
function postConnect() {
$this->Lexer->addExitPattern('</highlight>','plugin_highlight');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler){
switch ($state) {
case DOKU_LEXER_ENTER :
break;
case DOKU_LEXER_UNMATCHED :
list($lang, $content) = preg_split('/>/u',$match,2);
$lang = trim($lang);
return array($state, $lang, $content);
case DOKU_LEXER_EXIT :
break;
}
return array();
}
/**
* Create output
*/
function render($mode, &$renderer, $data) {
// Highlight options:
$hl_options=array();
$hl_options['hl_binary']='/highlight'; // path to the highlight binary
$hl_options['linenumbers']=false;
$hl_options['theme']='kwrite'; // set color theme
$hl_options['linenumber-start']=1;
$hl_options['linenumber-zeroes']=1; // set to 1 if line numbers should be padded with zeros
$hl_options['linenumber-length']=2;
$hl_options['reformat-style']="gnu"; // possible values for C, C++ and Java Code: ansi, gnu, java, kr, linux
$hl_options['replace-tabs']=4; //number of spaces which replace a tab
$hl_options['wrap-style']=2; //0 -> disable, 1-> wrap, 2-> wrap and indent function names
$hl_options['wrap-line-length']=80; // if wrap-style <>0, this defines the line length before wrapping takes place
if($mode == 'xhtml'){
list($state, $lang, $input_code) = $data;
$lang = preg_replace("'[^a-zA-Z]'","",$lang);
switch ($state) {
case DOKU_LEXER_ENTER :
break;
case DOKU_LEXER_UNMATCHED : // $renderer->doc .= $renderer->_xmlEntities($match); break;
$search = array("&",""", "<", ">","\","'"," ");
$replace = array("&","\"", "<", ">","\\","\'", " ");
$input_code = str_replace($search, $replace, $input_code);
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w") // stdout is a pipe that the child will write to
);
$hl_cmd_str = $hl_options['hl_binary'];
$hl_cmd_str .= ' --inline-css -f ';
if ($hl_options['linenumbers']){
$hl_cmd_str .= " -l -m ";
$hl_cmd_str .= $hl_options['linenumber-start'];
if ($hl_options['linenumber-zeroes']){
$hl_cmd_str .= " -z -j ";
$hl_cmd_str .= $hl_options['linenumber-length'];
}
}
if ($hl_options['replace-tabs']) {
$hl_cmd_str .= " -t ";
$hl_cmd_str .= $hl_options['replace-tabs'];
}
if ($hl_options['wrap-style']){
$hl_cmd_str .= ($hl_options['wrap-style'] == 1)? ' -V ':' -W ';
$hl_cmd_str .= " -J ";
$hl_cmd_str .= $hl_options['wrap-line-length'];
}
if ($hl_options['reformat-style']){
$hl_cmd_str .= " -F ";
$hl_cmd_str .= $hl_options['reformat-style'];
}
if ($hl_options['theme']){
$hl_cmd_str .= " -s ";
$hl_cmd_str .= $hl_options['theme'];
}
$hl_cmd_str .= " -S $lang ";
$process = proc_open($hl_cmd_str, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $input_code);
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
proc_close($process);
}
if (!strlen($output)) {
$renderer->doc .= "<small>ERROR: Execution of highlight ($hl_cmd_str) failed or missing input. Check binary path.</small>";
$renderer->doc .= "<pre style=\"font-size:9pt;\">";
$renderer->doc .= $renderer->_xmlEntities($input_code);
$renderer->doc .= "</pre>";
} else {
$renderer->doc .= "<pre style=\"font-size:9pt;\">";
$renderer->doc .= $output;
$renderer->doc .= "</pre>";
}
//$renderer->doc .= "<br>cmd=".$hl_cmd_str;
break;
case DOKU_LEXER_EXIT :
break;
}
return true;
}
return false;
}
}
|