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
|
<?php
/**This PHP class serves as interface to the highlight utility.
Input and output streams are handled with pipes.
Command line parameters are validated before use.
*/
class HighlightPipe {
// alter these members to control highlight output
// see manpage for the options
var $hl_option = array(
'hl_bin' => 'highlight', // configure path of highlight binary
'syntax' => 'c',
'theme' => 'kwrite',
'force' => 1,
'line-numbers' => 0,
'line-number-length' => 4,
'line-number-start' => 0,
'zeroes' => 0,
'wraptype' => 0,
'line-length' => 0,
'reformat' => '',
'kw-case' => '',
'replace-tabs' => 0,
'encoding' => '',
'enclose-pre' => 1,
'inline-css' => 1,
'fragment' => 1,
);
// this member contains the input source code
var $input='';
// this member will contain the command string after getResult() was called
var $hl_cmd_str='';
function getInfo(){
return array(
'author' => 'Andre Simon',
'url' => 'http://www.andre-simon.de/',
'version' => '1.2',
);
}
// call this method to generate highlighted HTML code
function getResult() {
foreach ($this->hl_option as $key => $value) {
$this->hl_option[$key] = $this->validate( $value );
}
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w")
);
$this->hl_cmd_str = $this->hl_option['hl_bin'];
if ($this->hl_option['line-numbers']){
$this->hl_cmd_str .= " -l -m 1";
if ($this->hl_option['zeroes']){
$this->hl_cmd_str .= " -z ";
}
if ($this->hl_option['line-number-length']!='0' && is_numeric($this->hl_option['line-number-length'])) {
$this->hl_cmd_str .= ' -j ';
$this->hl_cmd_str .=$this->hl_option['line-number-length'];
}
}
if (is_numeric($this->hl_option['replace-tabs']) and $this->hl_option['replace-tabs']>0) {
$this->hl_cmd_str .= " -t ";
$this->hl_cmd_str .= $this->hl_option['replace-tabs'];
}
if ($this->hl_option['wraptype']>0){
$this->hl_cmd_str .= ($this->hl_option['wraptype'] == 1)? ' -V ':' -W ';
if ($this->hl_option['line-length']>0 && is_numeric($this->hl_option['line-length'])) {
$this->hl_cmd_str .= " -J ";
$this->hl_cmd_str .= $this->hl_option['line-length'];
}
}
if (strlen($this->hl_option['reformat'])>1){
$this->hl_cmd_str .= " -F ";
$this->hl_cmd_str .= $this->hl_option['reformat'];
}
if (strlen($this->hl_option['kw-case'])>1){
$this->hl_cmd_str .= " --kw-case ";
$this->hl_cmd_str .= $this->hl_option['kw-case'];
}
if ($this->hl_option['force']){
$this->hl_cmd_str .= " --force ";
}
if ($this->hl_option['inline-css']){
$this->hl_cmd_str .= " --inline-css ";
}
if ($this->hl_option['fragment']){
$this->hl_cmd_str .= " -f ";
}
if ($this->hl_option['theme']){
$this->hl_cmd_str .= " -s ";
$this->hl_cmd_str .= $this->hl_option['theme'];
}
if ($this->hl_option['encoding']){
$this->hl_cmd_str .= " -u ";
$this->hl_cmd_str .= $this->hl_option['encoding'];
}
if ($this->hl_option['enclose-pre']){
$this->hl_cmd_str .= " --enclose-pre ";
}
$this->hl_cmd_str .= " -S ";
$this->hl_cmd_str .= $this->hl_option['syntax'];
$process = proc_open($this->hl_cmd_str, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $this->input);
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);
}
return $output;
}
// PRIVATE STUFF
var $special = array(' ', '/','!','&','*','\\', '.', '|', 'ยด','\'', '<', '>');
function validate($string) {
return (strlen($string)>50)? "" : str_replace($this->special,"",$string);
}
}
/*****************************************************************************/
/*
// Sample code:
$generator = new HighlightPipe;
$generator->input='int main () { return 0; }';
$generator->hl_option['theme']='neon';
$generator->hl_option['syntax']='c';
$result= $generator->getResult();
print $result;
*/
?>
|