source
Yoz.sk | Highlighted syntax
PHP
Script delimiters
(513x)
$
Variables
(177x)
Quotation 2
(150x)
Reserved words
(92x)
Quotation 1
(75x)
Built-in functions
(57x)
Number
(13x)
Line comment
(2x)
Escaped character
(1x)
Block comment
(1x)
Line comment 2
<?php
/*
* SynHi - Syntax Highlighter, v 0.8.3, 2004/12/07
*
* Copyright © 2004 Jozef Chúťka <[email protected]>
* Visit www.inspired.sk & studio.inspired.sk.
*
*
* Syntax highlighter for any scripting language based on .stx syntax files.
* Download .stx syntax files from www.editplus.com (http://www.editplus.com/files.html)
* and copy them to the $_STX_directory folder ($_STX_directory="" means
* same folder as this .php file).
*
* 1. Default CSS styles attached. If you can see underlined text (with default
* CSS styles), please report this bug.
* 2. JavaScript function ClipBoard() let you copy source directly to clip board by
* clicking on the showed button.
* 3. Use Verdana 7px font for editing this file, with Tab 4.
*
*
* @author Jozef Chúťka <[email protected]>
* @version Revision: 2.0
* (Tested with php, as, css, html syntax files.)
*
*
***************************************************************
* CSS style:
*
* <style type="text/css" media="screen" title="Default SynHi">
* <!--
* DIV.CSShighlightedCode,
* BUTTON.CSShighlightedCode,
* PRE.CSShighlightedCode {font: 9px "Verdana", sans-serif; background: #fff; margin: 5px; color: #000;}
* DIV.CSShighlightedCode {float: left; padding: 5px; border: 1px dashed #ccc;}
* PRE.CSShighlightedCode {clear: both; padding: 5px; border: 1px dashed gray;}
* .CSShighlightedCode H3 {margin: 0;}
* .CSShighlightedCode SPAN.CSSscriptMark {color: #fff; background: #00f;}
* .CSShighlightedCode SPAN.CSSescape {color: #999;}
* .CSShighlightedCode SPAN.CSScomment {color: #090;}
* .CSShighlightedCode SPAN.CSScomment2 {color: #090;}
* .CSShighlightedCode SPAN.CSScomment3 {color: #090;}
* .CSShighlightedCode SPAN.CSScomment4 {color: #090;}
* .CSShighlightedCode SPAN.CSSblockComment {color: #090;}
* .CSShighlightedCode SPAN.CSSblockComment2 {color: #090;}
* .CSShighlightedCode SPAN.CSSblockComment3 {color: #090;}
* .CSShighlightedCode SPAN.CSSblockComment4 {color: #090;}
* .CSShighlightedCode SPAN.CSSquot {color: #f0f;}
* .CSShighlightedCode SPAN.CSSprefix1 {color: #00f;}
* .CSShighlightedCode SPAN.CSSprefix2 {color: #00f;}
* .CSShighlightedCode SPAN.CSSprefix3 {color: #00f;}
* .CSShighlightedCode SPAN.CSSprefix4 {color: #00f;}
* .CSShighlightedCode SPAN.CSSprefix5 {color: #00f;}
* .CSShighlightedCode SPAN.CSSprefix6 {color: #00f;}
* .CSShighlightedCode SPAN.CSSkeyword1 {color: #00f;}
* .CSShighlightedCode SPAN.CSSkeyword2 {color: #f00;}
* .CSShighlightedCode SPAN.CSSkeyword3 {color: #088;}
* .CSShighlightedCode SPAN.CSSkeyword4 {color: #900;}
* .CSShighlightedCode SPAN.CSSkeyword5 {color: #f60;}
* .CSShighlightedCode SPAN.CSSkeyword6 {color: #f06;}
* .CSShighlightedCode SPAN.CSSnumber {color: #909;}
* .CSShighlightedCode SPAN.CSSstatisticsCount {color: #999;}
* .CSShighlightedCode SPAN SPAN {text-decoration: underline;}
* BUTTON.CSShighlightedCode {background: #ccc; border: 1px solid #999; margin-top: -6px;}
* -->
* </style>
*
*
***************************************************************
* JavaScript:
* <script type="text/javascript">
* <!--
* function getElementById(id){
* var element=document.getElementById && document.getElementById(id) || document.all && document.all(id);
* return element;
* }
* function ClipBoard(id){
* getElementById('IDSynHi'+id+'Holdtext').innerText=getElementById('IDSynHi'+id).innerText;
* if(getElementById('IDSynHi'+id+'Holdtext').createTextRange){
* Copied=getElementById('IDSynHi'+id+'Holdtext').createTextRange();
* Copied.execCommand("RemoveFormat");
* Copied.execCommand("Copy");
* }else{
* alert('Unable to copy to clipboard.');
* }
* }
* // -->
* </script>
*
*
***************************************************************
* DEMOS:
* --- DEMO 1 -----------------------------------------------------
* (local file, autodetect scripting language, shows legend)
* $SynHi=new SynHi();
* echo $SynHi->ViewFile(dirname(__FILE__).'/file.php', true);
* echo $c;
* --- DEMO 2 -----------------------------------------------------
* (webpage html, function Legend() must be called after ViewFile() or View() function!!!)
* $SynHi=new SynHi('html');
* echo $SynHi->ViewFile('http://www.google.com');
* echo $SynHi->Legend();
* --- DEMO 3 -----------------------------------------------------
* (script in string, shows legend)
* $buffer="public function serialize(what:Object):String{\n";
* $buffer.=" var buffer:String = new String('hi there');\n";
* $buffer.=" buffer = serialize_internal(\"hallo\", what);\n";
* $buffer.=" UNIQUESORT\n";
* $buffer.=" return buffer;\n";
* $buffer.="}; // serialize function";
* $SynHi=new SynHi('as');
* echo $SynHi->View($buffer, true);
*
*
***************************************************************
* IGNORED STX FUNCTIONS:
* #SKIP_QUOTE=y (???)
* #SPECIAL_STX=cpp (???)
* #SPECIAL_STX=html (???)
* #SPECIAL_STX=perl (???)
* #SUFFIX{x}=
*
*/
class SynHi {
var $_ver="0.8.3";
var $_STX_directory="/stx/";
var $_MarkBegin="{-{--{-{";
var $_MarkClose="}--}";
var $_MarkEnd="}-}--}-}";
var $_STX_path;
var $_STX_file;
var $_STX_KEY_array;
var $_STX_VAL_array;
var $_Pattern, $_PatternReplace;
var $_CSS_array;
var $_LegendStatistics_array;
var $_ScriptLanguage;
var $_UglyStrings_array;
var $_EmbededLanguagePattern_array;
var $_EmbededSynHi_array;
function SynHi($_ScriptLanguage=false){
if($this->_ScriptLanguage=$_ScriptLanguage) $this->Initialize($_ScriptLanguage);
$this->_EmbededLanguagePattern_array['html']['modifiers']="sie";
$this->_EmbededLanguagePattern_array['html']['languages']['css']['begin']="<style[^>]*?".">";
$this->_EmbededLanguagePattern_array['html']['languages']['css']['end']="<\/style>";
$this->_EmbededLanguagePattern_array['html']['languages']['js']['begin']="<script[^>]*?".">";
$this->_EmbededLanguagePattern_array['html']['languages']['js']['end']="<\/script>";
}
function Initialize($_ScriptLanguage){
$filename=dirname(__FILE__).$this->_STX_directory.$_ScriptLanguage.".stx";
if(!is_file($filename)) $filename=dirname(__FILE__).$this->_STX_directory.strtolower($_ScriptLanguage).".stx";
if(!is_file($filename)) $filename=dirname(__FILE__).$this->_STX_directory.preg_replace("/(.*?)[0-9]$/","$1",$_ScriptLanguage).".stx";
if(!is_file($filename)) $filename=dirname(__FILE__).$this->_STX_directory.strtolower(preg_replace("/(.*?)[0-9]$/","$1",$_ScriptLanguage)).".stx";
if(is_file($filename)){
$this->_STX_path=$filename;
$this->_STX_file=file($filename);
$this->ParseSTX();
$this->DefinePattern();
}else die("Unable to find STX file (<b>$filename</b>).");
}
function ParseSTX(){
$supple_keys=0;
foreach($this->_STX_file as $line_content){
if(preg_match("/\#(.+?)=(.+)?/", $line_content, $matches)){
$matches[1]=strtoupper($matches[1]);
if($matches[1]=="KEYWORD"){
$this->_STX_KEY_array['KEYWORD'][]=$matches[2];
$actual_variable=array('KEYWORD', count($this->_STX_KEY_array['KEYWORD'])-1);
}else{
if(preg_match("/^(?:suffix|prefix)([0-9]+)/i", $matches[1], $number))
if($supple_keys<$number[1]) $supple_keys=$number[1];
$this->_STX_KEY_array[$matches[1]]=trim($matches[2]);
$actual_variable=$matches[1];
}
}elseif(preg_match("/^([a-z0-9]+.*?)(\\r|\\n|$)/i", $line_content, $matches)){
if(is_array($actual_variable)){
$this->_STX_VAL_array[$actual_variable[0]][$actual_variable[1]][]=trim($matches[1]);
}elseif($actual_variable){
$this->_STX_VAL_array[$actual_variable][]=trim($matches[1]);
}
}
}
while(count($this->_STX_KEY_array['KEYWORD'])<$supple_keys){
$this->_STX_KEY_array['KEYWORD'][]='';
}
}
function PatternMark($match, $class=false){
if(is_array($match)){
foreach($match as $key=>$val){
if($val!=''){
$content.=$this->PatternMark($val, $this->_CSS_array[$key]);
if(is_array($this->_LegendStatistics_array[$key])) $this->_LegendStatistics_array[$key][1]++;}}
return $content;
}elseif($class) return $this->_MarkBegin.$class.$this->_MarkClose.$match.$this->_MarkEnd;
else return $match;
}
function ViewFile($path, $legend=false){
if(!$this->_ScriptLanguage){
$pathinfo=pathinfo($path);
$this->Initialize($pathinfo['extension']);
}
if($buffer=@implode("", @file($path))) return $this->View($buffer, $legend);
else die("Unable to read file <a href='$path'>$path</a>.");
}
function View($content, $legend=false){
$content=$this->HideUglyString($content, $this->_MarkBegin);
$content=$this->HideUglyString($content, $this->_MarkClose);
$content=$this->HideUglyString($content, $this->_MarkEnd);
if($this->_STX_KEY_array['SCRIPT_BEGIN'] && $this->_STX_KEY_array['SCRIPT_END']){
// #SCRIPT_BEGIN=< ?
// #SCRIPT_END=? >
$scriptBegin=$this->AddPatternSlashes($this->_STX_KEY_array['SCRIPT_BEGIN']);
$scriptEnd=$this->AddPatternSlashes($this->_STX_KEY_array['SCRIPT_END']);
$pattern="/($scriptBegin)(.*?)($scriptEnd)/se";
$replace="$"."this->PatternMark('$1', 'CSSscriptMark')".".";
$replace.="$"."this->Highlight($"."this->AdjustQuotes('$2'))".".";
$replace.="$"."this->PatternMark('$3', 'CSSscriptMark')";
$content=preg_replace($pattern, $replace, $content);
if($this->_STX_KEY_array['HTML_EMBEDDED']=='y'){
// #HTML_EMBEDDED=y
$SynHi_HTML=new SynHi('html');
$pattern="/(?=^|$scriptEnd)(.*?)(?=$scriptBegin|$)/se";
$replace="$"."SynHi_HTML->Highlight($"."this->AdjustQuotes('$1'))";
$content=preg_replace($pattern, $replace, $content);
$this->_Legend_array[]=$SynHi_HTML->Legend();
}
}else $content=$this->Highlight($content);
$content=$this->HtmlSource($content);
$content=preg_replace("/".$this->_MarkBegin."(.*?)".$this->_MarkClose."/", "<span class='$1'>", $content);
$content=str_replace($this->_MarkEnd, "</span>", $content);
$this->ShowUglyStrings($content);
$id=rand(1000,2000);
$content="\n\n\n<!-- SynHi v".($this->_ver)." -->\n\n"
."<pre id='IDSynHi$id' class='CSShighlightedCode'>$content</pre>\n"
."<textarea id='IDSynHi$id"."Holdtext' style='display:none;' rows='1' cols='1'></textarea>\n"
."<button class='CSShighlightedCode' onclick='javascript:ClipBoard($id);'>Copy to Clipboard</button>\n"
."\n\n<!-- End of SynHi -->\n\n\n";
if($legend) $content=$this->Legend().$content;
return $content;
}
function Highlight($content, $skip=false){
if(!$skip && $ELP=$this->_EmbededLanguagePattern_array[$this->_ScriptLanguage]){
foreach($ELP['languages'] as $language=>$value_array){
if(!$this->_EmbededSynHi_array[$language]){
$this->_EmbededSynHi_array[$language]=new SynHi($language);
}
$pattern_array[]="(.*?".$value_array['begin'].")(.*?)(".$value_array['end'].".*?)";
$replace_array[]="'".$language."'=>'$".($k*3+2)."'";
$replace_htmlprev.="$".($k*3+1);$replace_script.="$".($k*3+2);$replace_htmlnext.="$".($k++*3+3);
}
$pattern="/^".implode("|", $pattern_array)."|(.*?)$/".$ELP['modifiers'];
$replace="$"."this->Highlight($"."this->AdjustQuotes('$replace_htmlprev$".(count($pattern_array)*3+1)."'), true)".".";
$replace.="(('$replace_script')?";
$replace.="($"."this->_EmbededSynHi_array[$"."this->ValuedKey(array(".implode(",",$replace_array)."))]";
$replace.="->Highlight($"."this->AdjustQuotes('$replace_script')))";
$replace.=":null).";
$replace.="$"."this->Highlight($"."this->AdjustQuotes('$replace_htmlnext'), true)";
return preg_replace($pattern, $replace, $content);
}else return $this->AdjustQuotes(preg_replace($this->_Pattern, $this->_PatternReplace, $content));
}
function DefinePattern(){
$k=1;
$delimiter=$this->AddPatternSlashes($this->_STX_KEY_array['DELIMITER']);
if(is_array($this->_STX_KEY_array['KEYWORD'])){
foreach($this->_STX_KEY_array['KEYWORD'] as $i=>$KEYWORD_array_array){
if($prefix=$this->_STX_KEY_array['PREFIX'.($i+1)]) $prefixes.=$this->AddPatternSlashes($prefix);
}
}
if($escape=$this->_STX_KEY_array['ESCAPE']){
$key='escape'; // #ESCAPE=\
$escape=$this->AddPatternSlashes($this->AddPatternSlashes($escape));
$escape=str_replace("\\", "\\\\", $escape);
$pattern_array[]="([$escape].)";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]='CSSescape';
$this->_LegendStatistics_array[$key]=array("<span class='CSSescape'>Escaped character</span>", 0);
$not_escaped="(?:[^$escape])";
}
$s=1;while($quot=$this->_STX_KEY_array['QUOTATION'.$s]){
$key='quot'.$s; // #QUOTATION{n}='
$quot=$this->AddPatternSlashes($quot);
$end=($this->_STX_KEY_array['CONTINUE_QUOTE']=='n'?"\\r|\\n|":'')."$quot|$";
$pattern_array[]="($quot(?:(?:$end)|.*?$not_escaped(?:$end)))";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]='CSSquot';
$this->_LegendStatistics_array[$key]=array("<span class='CSSquot'>Quotation $s</span>", 0);
$s++;
}
if($comment=$this->_STX_KEY_array['LINECOMMENT']){
$key='linecomment'; // #LINECOMMENT=//
$comment=$this->AddPatternSlashes($comment);
$pattern_array[]="($comment.*?$not_escaped(?:\\r|\\n|$))";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]='CSScomment';
$this->_LegendStatistics_array[$key]=array("<span class='CSScomment'>Line comment</span>", 0);
}
$s=2;while($comment=$this->_STX_KEY_array['LINECOMMENT'.$s]){
$key='linecomment'.$s; // #LINECOMMENT{n}=#
$comment=$this->AddPatternSlashes($comment);
$pattern_array[]="($comment.*?$not_escaped(?:\\r|\\n|$))";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]='CSScomment'.$s;
$this->_LegendStatistics_array[$key]=array("<span class='CSScomment$s'>Line comment $s</span>", 0);
$s++;
}
if($this->_STX_KEY_array['COMMENTON'] && $this->_STX_KEY_array['COMMENTOFF']){
$key='blockcomment'; // #COMMENTON=/*, #COMMENTOFF=*/
$commentOn=$this->AddPatternSlashes($this->_STX_KEY_array['COMMENTON']);
$commentOff=$this->AddPatternSlashes($this->_STX_KEY_array['COMMENTOFF']);
$pattern_array[]="($commentOn(?:(?:$commentOff|$)|.*?$not_escaped(?:$commentOff|$)))";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]='CSSblockComment';
$this->_LegendStatistics_array[$key]=array("<span class='CSSblockComment'>Block comment</span>", 0);
}
$s=2;while($this->_STX_KEY_array['COMMENTON'.$s] || $this->_STX_KEY_array['COMMENTOFF'.$s]){
if($this->_STX_KEY_array['COMMENTON'.$s] && $this->_STX_KEY_array['COMMENTOFF'.$s]){
$key='blockcomment'.$s; #COMMENTON{n}=<<<TEXT , #COMMENTOFF{n}=TEXT;
$commentOn=$this->AddPatternSlashes($this->_STX_KEY_array['COMMENTON'.$s]);
$commentOff=$this->AddPatternSlashes($this->_STX_KEY_array['COMMENTOFF'.$s]);
$pattern_array[]="($commentOn(?:(?:$commentOff|$)|.*?$not_escaped(?:$commentOff|$)))";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]='CSSblockComment'.$s;
$this->_LegendStatistics_array[$key]=array("<span class='CSSblockComment$s'>Block comment $s</span>", 0);}
$s++;
}
if($this->_STX_KEY_array['NUMBER_PATTERN']=='cpp'){
$key='number'; // #NUMBER_PATTERN=cpp
$pattern_array[]="([\s$delimiter])([0-9]+)";
$pattern_replace_array[]="numberdelimiter=>'$".$k++."'";
$pattern_replace_array[]="$key=>'$".$k++."'";
$this->_CSS_array[$key]="CSSnumber";
$this->_LegendStatistics_array[$key]=array("<span class='CSSnumber'>Number</span>", 0);
}
if(is_array($this->_STX_KEY_array['KEYWORD'])){
// #KEYWORD
foreach($this->_STX_KEY_array['KEYWORD'] as $i=>$KEYWORD_array_array){
$n=$i+1;
unset($keyword_array, $prefix);
if(is_array($this->_STX_VAL_array['KEYWORD'][$i]))
foreach($this->_STX_VAL_array['KEYWORD'][$i] as $j=>$keyword){$keyword_array[]=$keyword;}
if($prefix=$this->_STX_KEY_array['PREFIX'.$n]) $prefix=$this->AddPatternSlashes($prefix);
if($prefix || is_array($keyword_array)){
$key="keyword$n";
$label=$this->_STX_KEY_array['KEYWORD'][$i];
if($prefix){
if(is_array($keyword_array)) $pattern_array[]="(?:($prefix)(".implode("|", $keyword_array).")(?=[\s$delimiter$prefixes]))";
else $pattern_array[]="(?:($prefix)([^\s$delimiter$prefixes]*))";
$key1="prefix$n";
$this->_CSS_array[$key1]="CSSprefix$n";
$this->_LegendStatistics_array[$key]=array("<span class='CSSprefix$n'>".stripslashes($prefix)."</span><span class='CSSkeyword$n'>".($label?$label:'Unknown')."</span>", 0);
}else{
$key1="keyworddelimiter$n";
$pattern_array[]="(?:(^|[\s$delimiter])(".implode("|", $keyword_array).")(?=[\s$delimiter$prefixes]|$))";
$this->_LegendStatistics_array[$key]=array("<span class='CSSkeyword$n'>".($label?$label:'Unknown')."</span>", 0);
}
$this->_CSS_array[$key]="CSSkeyword$n";
$pattern_replace_array[]="$key1=>'$".$k++."'";
$pattern_replace_array[]="$key=>'$".$k++."'";
}
}
}
if(is_array($pattern_array)){
// #CASE=y
$this->_Pattern="/(?:".implode("|", $pattern_array).")/se".(strstr($this->_STX_KEY_array['CASE'],'y')?false:'i');
$this->_PatternReplace="$"."this->PatternMark(array(".implode(",",$pattern_replace_array)."))";
}
}
function Legend(){
// Embeded Legends
if(is_array($this->_EmbededSynHi_array)){
foreach($this->_EmbededSynHi_array as $key=>$val){
$this->_Legend_array[]=$this->_EmbededSynHi_array[$key]->Legend();
}
}
// Self Legend
if(!function_exists("SortStatistics")){function SortStatistics($a, $b){if($a[1]==$b[1]) return 0;return ($a[1]>$b[1])?-1:1;}}
usort($this->_LegendStatistics_array,"SortStatistics");
if($this->_STX_KEY_array['SCRIPT_BEGIN'] && $this->_STX_KEY_array['SCRIPT_END']) $legend[]="<span class='CSSscriptMark'>Script delimiters</span>";
foreach($this->_LegendStatistics_array as $stat_array){
if($stat_array[0] && $stat_array[1]) $legend[]="<span class='CSSstatisticsCount'>(".$stat_array[1]."x)</span> ".$stat_array[0];
}
if(is_array($legend)){
$head="<h3><a href='".$this->_STX_path."' title='".$this->_STX_KEY_array['TITLE']." (.stx file)'>".$this->_STX_KEY_array['TITLE']."</a></h3>";
$legend="<div class='CSShighlightedCode'>$head".implode("<br />\n",$legend)."</div>";
$this->_Legend_array[]=$legend;
}
if(is_array($this->_Legend_array)) return implode("\n", array_reverse($this->_Legend_array)); else return false;
}
function AddPatternSlashes($val){return addcslashes($val, "\",(){}[]-+*%/='~!&|<>?:;.@#$");}
function AdjustQuotes($content){return str_replace('\"', '"', $content);}
function ValuedKey($a){foreach($a as $k=>$v){if($v!=''){return $k;break;}}}
function HideUglyString($content, $a){
if(strstr($content, $a)){
$a1=str_replace("--", "---", $a);
while(strstr($content, $a1)){$a1=str_replace("--", "---", $a1);}
$this->_UglyStrings_array[]=array($a,$a1);
return str_replace($a, $a1, $content);
}else return $content;
}
function ShowUglyStrings(&$content){
if(is_array($this->_UglyStrings_array)){
foreach($this->_UglyStrings_array as $ugly_string){$content=str_replace($ugly_string[1], $ugly_string[0], $content);}}
return $content;
}
function HtmlSource($html){
$html=str_replace("&","&",$html);
$html=str_replace("<","<",$html);
$html=str_replace(">",">",$html);
$html=str_replace('"',""",$html);
$html=str_replace(" "," ",$html);
$html=str_replace("\r\n","\n",$html);
return $html;
}
function dump($var){
echo "\n\n\n<!-- DUMP v1.1 -->\n\n<pre>\n";
print_r($this->HtmlSource($var));
echo "\n</pre>\n\n<!-- END DUMP v1.1 -->\n\n\n";
}
}
?>Generated in 2.0091 sec.