forked from andre-simon/highlight
-
Notifications
You must be signed in to change notification settings - Fork 4
/
testmod.php
183 lines (153 loc) · 4.74 KB
/
testmod.php
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
<?php
/* Set enable_dl = On in your php.ini and copy highlight.so in /usr/lib/php/modules/ */
if (!extension_loaded('highlight')){
if (!dl('highlight.so')) {
exit;
}
}
/**
* return value options
*
* @param $options array $options
* @param $short_opts string key short_opts
* @param $long_opts string key short_opts
* @return string value of short_opts or long_opts or false if not set
*/
function valueOptions($options,$short_opts,$long_opts) {
if (isset($options[$short_opts])) {
if ($options[$short_opts]==false) {
return true;
} else {
return $options[$short_opts];
}
} else if (isset($options[$long_opts])) {
if ($options[$long_opts]==false) {
return true;
} else {
return $options[$long_opts];
}
} else {
return false;
}
}
$help = <<<HELP
General Options:
-i, --input <file> name of single input file
-S, --syntax <type> specify type of source code
Output formatting options:
-O, --outformat <format> output file in given format
<format>=[HTML,XHTML,LATEX,TEX
ODT,RTF,ANSI,XTERM256,BBCODE,SVG]
-f, --fragment omit document header and footer
-F, --reformat <style> reformats and indents output in given style
<style>=[allman, banner, gnu,horstmann,java
kr,linux,otb,sstroustrup, whitesmith]
--inlinecss output CSS within each tag (verbose output)
-I, --includestyle include style definition
-l, --linenumbers print line numbers in output file
-m, --linenumberstart <cnt> start line numbering with cnt (assumes -l)
-s, --style <style> set colour style
HELP;
$short_opts = '';
$short_opts .= 'f';
$short_opts .= 'F:';
$short_opts .= 'i:';
$short_opts .= 'I';
$short_opts .= 'l';
$short_opts .= 'm:';
$short_opts .= 'O:';
$short_opts .= 's:';
$short_opts .= 'S:';
$long_opts = array(
'fragment',
'inlinecss',
'input:',
'includestyle',
'linenumbers',
'linenumberstart:',
'outputformat:',
'reformat:',
'style:',
'syntax:'
);
$options = getopt($short_opts,$long_opts);
if (empty($options)) {
echo $help."\n";
exit;
}
$outformat = valueOptions($options,'O','outformat');
$outformat = strtolower($outformat);
$out = 0; // default, HTML
if($outformat) {
switch ($outformat) {
case 'xhtml': $out = 1; break;
case 'tex': $out = 2; break;
case 'latex': $out = 3; break;
case 'rtf': $out = 4; break;
case 'ansi': $out = 5; break;
case 'xterm256': $out = 6; break;
case 'html32': $out = 7; break;
case 'svg': $out = 8; break;
case 'bbcode': $out = 9; break;
case 'pango': $out = 10; break;
case 'odt': $out = 11; break;
default: $out = 0;
}
}
$gen = codegenerator_getinstance($out);
// Initialize DataDir instance
$data_dir = new_datadir();
$highlight_data = datadir_lsb_data_dir_get();
// -F, --reformat <style>
$reformat = valueOptions($options,'F','reformat');
if ($reformat) {
codegenerator_initindentationscheme($gen,$reformat);
}
// --inlinecss
$inlinecss = valueOptions($options,'inlinecss','inlinecss');
if($inlinecss) {
codegenerator_sethtmlinlinecss($gen,true);
}
// -s, --style <style>
$style = valueOptions($options,'s','style');
if ($style) {
$theme_path = datadir_getthemepath($data_dir,$style.".theme");
} else {
$theme_path = datadir_getthemepath($data_dir,"seashell.theme");
}
codegenerator_inittheme($gen,$highlight_data.$theme_path);
// -i, --input <file>
$input = valueOptions($options,'i','input');
$handle = fopen($input,"r");
$filename = fread($handle,filesize($input));
// -S, --syntax <type>
$syntax = valueOptions($options,'S','syntax');
if (!$syntax) {
$ext_temp = explode('.',$input);
$syntax = end($ext_temp);
}
$lang_path = datadir_getlangpath($data_dir,$syntax.".lang");
codegenerator_loadlanguage($gen,$highlight_data.$lang_path);
// -I, --includestyle
$includestyle = valueOptions($options,'I','includestyle');
if($includestyle) {
codegenerator_setincludestyle($gen,true);
}
// -f, --fragment
$fragment = valueOptions($options,'f','fragment');
if ($fragment) {
codegenerator_setfragmentcode($gen,true);
}
// -l, --linenumbers
$linenumbers = valueOptions($options,'l','linenumbers');
if($linenumbers) {
codegenerator_setprintlinenumbers($gen,true);
}
// -m, --linenumberstart <cnt>
$linenumberstart = valueOptions($options,'m','linenumberstart');
if ($linenumberstart) {
$linenumberstart = (int) $linenumberstart; // make sure it's integer
codegenerator_setprintlinenumbers($gen,true,$linenumberstart);
}
//print!!!
echo codegenerator_generatestring($gen,$filename);