00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 class HFile
00022 {
00023
00024 function HFile()
00025 {
00026 # Language name.
00027 $this->lang_name = "";
00028 # Language configuration hashtable.
00029 $this->config = array();
00030 $color = 0;
00031 $parsing = "";
00032 $this->validkeys = array();
00033
00034 $this->indent = array();
00035 $this->unindent = array();
00036 $this->blockcommenton = array();
00037 $this->blockcommentoff = array();
00038 $this->linecommenton = array();
00039 $this->preprocessors = array();
00040
00041 # Zoning
00042 $this->zones = array();
00043
00044 # Hashtable of keywords - maps from keyword to colour.
00045 $this->keywords = array();
00046
00047 # Used for conversion from keyword -> regexp.
00048 $this->keypats = array();
00049
00050 # Which colours to use for each 'level'.
00051 $this->colours = array("blue", "purple", "gray", "brown", "blue", "purple", "gray", "brown");
00052 $this->quotecolour = "blue";
00053 $this->blockcommentcolour = "green";
00054 $this->linecommentcolour = "green";
00055 $this->stringchars = array();
00056 $this->delimiters = array();
00057 $this->escchar = "\\";
00058
00059 # Caches for the harvesters.
00060 $this->comcache = "";
00061 $this->stringcache = "";
00062
00063 # Toggles
00064 $this->perl = 0;
00065 $this->notrim = 0;
00066 $this->nocase = 0;
00067 }
00068
00069 function parse_file($file)
00070 {
00071 print "This method should not be called from here, but from an extending configuration class.<br>";
00072 }
00073
00074 function to_perl($stub, $tofile = 1)
00075 {
00076 $this->used_categories = $this->_get_categories();
00077 print "
00078 package HFile::$stub;
00079
00080 ###############################
00081 #
00082 # Beautifier Perl HFile
00083 # Language: $stub
00084 #
00085 ###############################
00086
00087 use Beautifier::HFile;
00088 @ISA = qw(HFile);
00089 sub new
00090 {
00091 my( \$class ) = @_;
00092 my \$self = {};
00093 bless \$self, \$class;
00094
00095 # Flags:
00096 ";
00097 $this->_dump_var($this->nocase, " \$self->{nocase} ");
00098 $this->_dump_var($this->notrim, " \$self->{notrim} ");
00099 $this->_dump_var($this->perl, " \$self->{perl} ");
00100 $this->_dump_perl_array($this->indent, " \$self->{indent} ");
00101 $this->_dump_perl_array($this->unindent, " \$self->{unindent} ");
00102 $this->_dump_perl_array($this->stringchars, " \$self->{stringchars} ");
00103 $this->_dump_perl_array($this->delimiters, " \$self->{delimiters} ");
00104 $this->_dump_var($this->escchar, " \$self->{escchar} ");
00105 $this->_dump_perl_array($this->linecommenton, " \$self->{linecommenton} ");
00106 $this->_dump_perl_array($this->blockcommenton, " \$self->{blockcommenton} ");
00107 $this->_dump_perl_array($this->blockcommentoff, " \$self->{blockcommentoff}");
00108 $this->_dump_perl_hash($this->keywords, " \$self->{keywords} ");
00109 $this->_dump_perl_linkscripts();
00110 print "
00111 return \$self;
00112 }
00113
00114 ";
00115
00116 $this->_dump_perl_defaultscripts();
00117 print "1;\n";
00118 }
00119
00120 function to_php($stub, $tofile = 1)
00121 {
00122
00123
00124 $this->used_categories = $this->_get_categories();
00125
00126 if ($tofile) print "<?php\n"; else print "<?\n";
00127 print "require_once('psh.php');\n";
00128 print "class psh_$stub extends psh\n{\n";
00129 print "function psh_$stub(){\n";
00130 print "\$this->psh();\n";
00131 print "######################################\n";
00132 print "# Beautifier Highlighting Configuration File \n";
00133 print "# $this->lang_name\n";
00134 print "######################################\n";
00135 print "# Flags\n\n";
00136 $this->_dump_var($this->nocase, "\$this->nocase ");
00137 $this->_dump_var($this->notrim, "\$this->notrim ");
00138 $this->_dump_var($this->perl, "\$this->perl ");
00139 print "\n# Colours\n\n";
00140 $this->_dump_colours();
00141 $this->_dump_var("blue", "\$this->quotecolour ");
00142 $this->_dump_var("green", "\$this->blockcommentcolour");
00143 $this->_dump_var("green", "\$this->linecommentcolour ");
00144 print "\n# Indent Strings\n\n";
00145 $this->_dump_array($this->indent, "\$this->indent ");
00146 $this->_dump_array($this->unindent, "\$this->unindent ");
00147 print "\n# String characters and delimiters\n\n";
00148 $this->_dump_array($this->stringchars, "\$this->stringchars ");
00149 $this->_dump_array($this->delimiters, "\$this->delimiters ");
00150 $this->_dump_var ($this->escchar, "\$this->escchar ");
00151 print "\n# Comment settings\n\n";
00152 $this->_dump_array($this->linecommenton, "\$this->linecommenton ");
00153 $this->_dump_var ($this->blockcommenton, "\$this->blockcommenton ");
00154 $this->_dump_var ($this->blockcommentoff, "\$this->blockcommentoff ");
00155 print "\n# Keywords (keyword mapping to colour number)\n\n";
00156 $this->_dump_hash ($this->keywords, "\$this->keywords ");
00157 print "\n# Special extensions\n";
00158 $this->_dump_linkscripts();
00159 print "}\n";
00160 if ($tofile) print "\n?>"; else print "\n?>";
00161 }
00162
00163 function _get_categories()
00164 {
00165 $usedcats = array();
00166
00167 foreach(array_keys($this->keywords) as $k)
00168 {
00169 $cat = $this->keywords[$k];
00170 if (!in_array($cat, $usedcats)) array_push($usedcats, $cat);
00171 }
00172 return $usedcats;
00173 }
00174
00175 function _dump_linkscripts()
00176 {
00177 print "
00178 # Each category can specify a PHP function that takes in the function name, and returns a string
00179 # to put in its place. This can be used to generate links, images, etc.\n\n";
00180
00181 $linkhash = array();
00182
00183 foreach($this->used_categories as $c)
00184 {
00185 $linkhash{$c} = "donothing";
00186 }
00187
00188 $this->_dump_hash($linkhash, "\$this->linkscripts ");
00189 print "}\n";
00190 print "\n# DoNothing link function\n\n";
00191 print "function donothing(\$keywordin)\n{\n return \$keywordin;\n}\n";
00192 }
00193
00194 function _dump_perl_linkscripts()
00195 {
00196 print "
00197 # Each category can specify a Perl function that takes in the function name, and returns a string
00198 # to put in its place. This can be used to generate links, images, etc.\n\n";
00199
00200 $linkhash = array();
00201 foreach($this->used_categories as $c)
00202 {
00203 $linkhash{$c} = "donothing";
00204 }
00205
00206 $this->_dump_perl_hash($linkhash, "\$self->{linkscripts} ");
00207 }
00208
00209 function _dump_perl_defaultscripts()
00210 {
00211 print "\n# DoNothing link function\n\n";
00212 print "sub donothing\n{\nmy ( \$self ) = @_;\nreturn;\n}\n";
00213 }
00214
00215 function _dump_colours()
00216 {
00217 $usedcols = array();
00218 foreach($this->used_categories as $c)
00219 {
00220 array_push($usedcols, $this->colours[$c-1]);
00221 }
00222 $this->_dump_array($usedcols, "\$this->colours ");
00223 }
00224
00225 function _dump_var($variable, $name)
00226 {
00227 print $name." = \"".addslashes($variable)."\";\n";
00228 }
00229
00230 function _dump_array($array, $name)
00231 {
00232 $first = 1;
00233 print $name." = array(";
00234 foreach($array as $a)
00235 {
00236 if (!$first) print ", "; else $first = 0;
00237 print "\"".addslashes($a)."\"";
00238 }
00239 print ");\n";
00240 }
00241
00242 function _dump_perl_array($array, $name)
00243 {
00244 $first = 1;
00245 print $name." = [";
00246 foreach($array as $a)
00247 {
00248 if (!$first) print ", "; else $first = 0;
00249 print "\"".addslashes($a)."\"";
00250 }
00251 print "];\n";
00252 }
00253
00254 function _dump_hash($hash, $name)
00255 {
00256 $first = 1;
00257 print $name." = array(";
00258 foreach(array_keys($hash) as $k)
00259 {
00260 if (!$first) print ", "; else $first = 0;
00261 print "\n \"".addslashes($k)."\"";
00262 print " => \"".addslashes($hash[$k])."\"";
00263 }
00264 print ");\n";
00265 }
00266
00267 function _dump_perl_hash($hash, $name)
00268 {
00269 $first = 1;
00270 print $name." = {";
00271 foreach(array_keys($hash) as $k)
00272 {
00273 if (!$first) print ", "; else $first = 0;
00274 print "\n \"".addslashes($k)."\"";
00275 print " => \"".addslashes($hash[$k])."\"";
00276 }
00277 print "};\n";
00278 }
00279 }
00280
00281 ?>