Debian Codegraph @

extras/swig/testmod.php

raw
1 <?php
2
3 /* Set enable_dl = On in your php.ini and copy highlight.so in /usr/lib/php/modules/ */
4
5 if (!extension_loaded('highlight')){
6 if (!dl('highlight.so')) {
7 exit;
8 }
9 }
10
11 /**
12 * return value options
13 *
14 * @param $options array $options
15 * @param $short_opts string key short_opts
16 * @param $long_opts string key short_opts
17 * @return string value of short_opts or long_opts or false if not set
18 */
19 function valueOptions($options,$short_opts,$long_opts) {
20 if (isset($options[$short_opts])) {
21 if ($options[$short_opts]==false) {
22 return true;
23 } else {
24 return $options[$short_opts];
25 }
26
27 } else if (isset($options[$long_opts])) {
28 if ($options[$long_opts]==false) {
29 return true;
30 } else {
31 return $options[$long_opts];
32 }
33
34 } else {
35 return false;
36 }
37 }
38 $help = <<<HELP
39 General Options:
40
41 -i, --input <file> name of single input file
42 -S, --syntax <type> specify type of source code
43
44 Output formatting options:
45
46 -O, --outformat <format> output file in given format
47 <format>=[HTML,XHTML,LATEX,TEX
48 ODT,RTF,ANSI,XTERM256,BBCODE,SVG]
49 -f, --fragment omit document header and footer
50 -F, --reformat <style> reformats and indents output in given style
51 <style>=[allman, banner, gnu,horstmann,java
52 kr,linux,otb,sstroustrup, whitesmith]
53 --inlinecss output CSS within each tag (verbose output)
54 -I, --includestyle include style definition
55 -l, --linenumbers print line numbers in output file
56 -m, --linenumberstart <cnt> start line numbering with cnt (assumes -l)
57 -s, --style <style> set colour style
58 HELP;
59
60 $short_opts = '';
61 $short_opts .= 'f';
62 $short_opts .= 'F:';
63 $short_opts .= 'i:';
64 $short_opts .= 'I';
65 $short_opts .= 'l';
66 $short_opts .= 'm:';
67 $short_opts .= 'O:';
68 $short_opts .= 's:';
69 $short_opts .= 'S:';
70
71 $long_opts = array(
72 'fragment',
73 'inlinecss',
74 'input:',
75 'includestyle',
76 'linenumbers',
77 'linenumberstart:',
78 'outputformat:',
79 'reformat:',
80 'style:',
81 'syntax:'
82 );
83 $options = getopt($short_opts,$long_opts);
84
85 if (empty($options)) {
86 echo $help."\n";
87 exit;
88 }
89
90 $outformat = valueOptions($options,'O','outformat');
91 $outformat = strtolower($outformat);
92 $out = 0; // default, HTML
93
94
95 if($outformat) {
96 switch ($outformat) {
97 case 'xhtml': $out = 1; break;
98 case 'tex': $out = 2; break;
99 case 'latex': $out = 3; break;
100 case 'rtf': $out = 4; break;
101 case 'ansi': $out = 5; break;
102 case 'xterm256': $out = 6; break;
103 case 'html32': $out = 7; break;
104 case 'svg': $out = 8; break;
105 case 'bbcode': $out = 9; break;
106 case 'pango': $out = 10; break;
107 case 'odt': $out = 11; break;
108 default: $out = 0;
109 }
110 }
111
112 $gen = codegenerator_getinstance($out);
113
114 // Initialize DataDir instance
115 $data_dir = new_datadir();
116 $highlight_data = datadir_lsb_data_dir_get();
117
118 // -F, --reformat <style>
119 $reformat = valueOptions($options,'F','reformat');
120 if ($reformat) {
121 codegenerator_initindentationscheme($gen,$reformat);
122 }
123
124 // --inlinecss
125 $inlinecss = valueOptions($options,'inlinecss','inlinecss');
126 if($inlinecss) {
127 codegenerator_sethtmlinlinecss($gen,true);
128 }
129
130 // -s, --style <style>
131 $style = valueOptions($options,'s','style');
132 if ($style) {
133 $theme_path = datadir_getthemepath($data_dir,$style.".theme");
134
135 } else {
136 $theme_path = datadir_getthemepath($data_dir,"seashell.theme");
137 }
138 codegenerator_inittheme($gen,$highlight_data.$theme_path);
139
140 // -i, --input <file>
141 $input = valueOptions($options,'i','input');
142 $handle = fopen($input,"r");
143 $filename = fread($handle,filesize($input));
144
145 // -S, --syntax <type>
146 $syntax = valueOptions($options,'S','syntax');
147 if (!$syntax) {
148 $ext_temp = explode('.',$input);
149 $syntax = end($ext_temp);
150 }
151 $lang_path = datadir_getlangpath($data_dir,$syntax.".lang");
152 codegenerator_loadlanguage($gen,$highlight_data.$lang_path);
153
154 // -I, --includestyle
155 $includestyle = valueOptions($options,'I','includestyle');
156 if($includestyle) {
157 codegenerator_setincludestyle($gen,true);
158 }
159
160 // -f, --fragment
161 $fragment = valueOptions($options,'f','fragment');
162 if ($fragment) {
163 codegenerator_setfragmentcode($gen,true);
164 }
165
166 // -l, --linenumbers
167 $linenumbers = valueOptions($options,'l','linenumbers');
168 if($linenumbers) {
169 codegenerator_setprintlinenumbers($gen,true);
170 }
171
172 // -m, --linenumberstart <cnt>
173 $linenumberstart = valueOptions($options,'m','linenumberstart');
174 if ($linenumberstart) {
175 $linenumberstart = (int) $linenumberstart; // make sure it's integer
176 codegenerator_setprintlinenumbers($gen,true,$linenumberstart);
177 }
178
179
180
181 //print!!!
182 echo codegenerator_generatestring($gen,$filename);
183