Programming :  Student Freelance Forum For Work Experience Builders' (CertificationPoint) The fastest message board... ever.
 
PHP Syntax Highlighter
Posted by: adcertpoint (Moderator)
Date: April 29, 2020 04:23PM

By Mike Jewell

on July 9, 2001

-
Version: 1.02

Type: Full Script

Category: HTML

License: Other

Description: PSH highlights and indents source code using highlight configuration files (which are similar to Ultraedit highlighting files). As such, it currently supports C, C#, Java, Perl, PHP, Pascal, Lisp, Mumps, Eiffel, Euphoria, and x86 Assembler, amongst others. PSH handles line/block comments, keyword highlighting, correct indentation, and string highlighting, as well as the harvesting of comment text to allow for easy source code indexing.





?


<?
# Language number: Only really used in Ultraedit. [DOOMED]
$lang_num = 0;
# Language name.
$lang_name = "";
# Language configuration hashtable.
$config = array();
$color = 0;
$parsing = "";
$indent = array();
$unindent = array();
# Hashtable of keywords - maps from keyword to colour.
$keywords = array();
# Used for conversion from keyword -> regexp.
$keypats = array();
# Which colours to use for each 'level'.
$colours = array("blue", "purple", "gray", "brown", "blue", "purple", "gray", "brown"winking smiley;
$stringchars = array();

# Caches for the harvesters.
$comcache = "";
$stringcache = "";

# Used by the configuration file, this takes in the first line (which
# is used to describe general language traits).

function parse_ufile_config($config_line)
{

$valid_keys = "";

# Keywords that can be used in 'Foo = Bar' context.
$valid_keys = "Escape Char|Line Comment Num|Line Comment Alt|Line Comment|Block Comment On|Block Comment Off|Block Comment On Alt|Block Comment Off Alt|String Chars";

# Keywords that should be used on their own. Note that the _LANG
# ones are fairly doomed.
$valid_keywords = "Notrim|Nocase|Noquote|PERL|HTML_LANG|FORTRAN_LANG|LATEX_LANG";
$done = 0;
$out = array();
while (!$done)
{
$config_line = ltrim($config_line);

# bla = bla matched.
if (preg_match("/^*(".$valid_keys."winking smiley*=(.*)/", $config_line, $matches))
{
$key = $matches[1];
$val = $matches[2];
$val = trim($val);
}
# Single keyword matched.
else if (preg_match("/^*(".$valid_keywords."winking smiley*(.*)/", $config_line, $matches))
{
$out[$matches[1]] = 1;
$config_line = $matches[2];
continue;
}
else
{
return array();
}

# We've got a 'foo = bar' case, so strip out the the string up to the
# next valid keyword, and recreate the config line.
$reg = "/^(.*?)s*(".$valid_keys."|".$valid_keywords."winking smiley(.+)$/";
if (preg_match($reg, $val, $matches))
{
$value = $matches[1];
$config_line = $matches[2].$matches[3];
$done = 0;
}
else
{
$value = $val;
$done = 1;
}
if (!isset($value)) {$value = 1;}
$out[$key] = $value;
}
return $out;
}

# Go through the string, highlighting keywords. At the moment this is a bit kludgy, with
# 'fwble' (voted least likely to be a keyword yawning smiley) being used to act as a placeholder for
# 'font' and 'r', 'n' being used for < and > respectively.
function munge($tomunge)
{
global $keypats, $colours, $keywords;

$tomunge = str_replace("&gt;", ">", $tomunge);
$tomunge = str_replace("&lt;","<", $tomunge);
foreach (array_keys($keypats) as $keyobj)
{
$keyobj = str_replace("/","/",$keyobj);

if (preg_match("/".$keyobj."/", $tomunge, $matches))
{
$matched = $matches[1];
$color = $colours[(int)($keywords[$matched])-1];
$tomunge = preg_replace("/".$keyobj."/","nfwble="$color"r$matchedn/fontr", $tomunge);

}
}
# Replace all symbols.
$tomunge = str_replace(">","&gt;",$tomunge);
$tomunge = str_replace("<","&lt;",$tomunge);
$tomunge = str_replace("n","<",$tomunge);
$tomunge = str_replace("r",">",$tomunge);
$tomunge = str_replace("fwble","font color",$tomunge);
return $tomunge;
}

# Takes in a filename of a highlight file, and creates the necessary settings.
function psh_parse_file($highlightfile)
{
global $keywords;
global $indent;
global $unindent;
global $keypats;
global $colours;
global $config;
$filehandle = fopen ($highlightfile, "r"winking smiley or die("Unable to open syntax file: $!"winking smiley;


while(!feof ($filehandle))
{
$parsing = fgets($filehandle, 4096);
# Grab language number, name, and config string.
if (preg_match("//L([0-9]+)"([^"]*)"(.*)/", $parsing, $matches))
{
$lang_num = $matches[1];
$lang_name = $matches[2];
$config = parse_ufile_config($matches[3]);
continue;
}
# Move onto a new colour category.
else if (preg_match("/^/C([0-9]+)s*(.*)/", $parsing, $matches))
{
$color = $matches[1];
continue;
}
# Set indenting strings. This nifty reg-exp removes the outer quotes, so
# the explode can do its work.
else if (preg_match("/^/Indent Stringss*=s*"(.*)"$/", $parsing, $matches))
{
$indent = explode("" "", $matches[1]);
continue;
}
# As above, but for unindenting strings.
else if (preg_match("/^/Unindent Stringss*=s*"(.*)"$/", $parsing, $matches))
{
$unindent = explode("" "", $matches[1]);
continue;
}
# Split any keywords up.
$keylist = preg_split("/s+/", $parsing, -1, PREG_SPLIT_NO_EMPTY);

foreach($keylist as $k)
{
$keywords[$k] = $color;
}

}
fclose($filehandle);
$keyword_keys = array_keys($keywords);

# Create the regexps for the keywords. 'b' specifies a word boundary.
foreach($keyword_keys as $k)
{
$keypats["b(".preg_quote($k)."winking smileyb"] = $k;
}
}

# A handy function to find the longest element of the array that is present at the beginning
# of the provided string. For example, given an array of 'foo' and 'foot' and the string 'football',
# this returns 'foot'.
function starts_with($text, $array)
{
$ml = 0;
$curr = "";

foreach($array as $i)
{
$l = strlen($i);
if (substr($text, 0, $l)==$i && ($text[$l]==" " || $l==1 || $text[$l]=="n" || $text[$l]=="t" || $text[$l]=="." || $text[$l]==";" || $l==strlen($text)))
{
if ($l>$ml)
{
$curr = $i;
$ml = $l;
}
}
}
return $curr;
}

# Load a file and return it as a (big

Options: ReplyQuote


Sorry, only registered users may post in this forum.
This forum powered by Phorum.