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