Build Code Syntax Highlighter with PHP

Code syntax highlighter is a tool or library that adds color and formatting to source code, making it easier to read and understand. It highlights different parts of the code based on their syntax, such as keywords, variables, comments, and strings, by applying different colors or styles to each element.It helps programmers quickly identify and differentiate between different language constructs, which can aid in detecting errors and understanding the code structure.

You can get the complete code from Github

highlighter.class.php
1 <?php
2 include_once("keywords.php");
3 class Highlighter {
4 	private $fileName;
5 	private $fileExtension;
6 	private $showFileName;
7 	
8 	public function __construct() {
9 		$this->fileName = "";
10 		$this->fileExtension = "";
11 		$this->showFileName = true;
12 	}
13 	
14 	public function showfilename($value) {
15 		$this->showFileName = $value;
16 	}
17 
18 	public function applycolor($fileLocation = "") {
19 		if($fileLocation == "") {
20 			return;
21 		}
22 		else
23 		{
24 			if(file_exists$fileLocation) {
25 				$temp = explode("/",$fileLocation);
26 				$this->fileName = trim(end$temp);
27 				$temp = explode(".",$this->fileName);
28 				$this->fileExtension = trim(end$temp);
29 				$fileContent = trim(file_get_contents$fileLocation, true);
30 				$fileContent = htmlentities($fileContent,ENT_NOQUOTES);
31 				if($fileContent == "") {	
32 					return;	
33 				}
34 			}
35 			else
36 			{	
37 				return; 
38 			}
39 			
40 			$parenthesisFound = 0;
41 			$bracketFound = 0;
42 			$foundCharacter = "";
43 
44 			$line = 1;
45 			$counter = 0;
46 			$contentSize = strlen($fileContent);
47 
48 			$content = "<font class='lI'>".$line."</font> ";
49 			while($counter < $contentSize) {
50 				$character = $fileContent[$counter];
51 				$code = intval(ord$character);
52 				if($code >= 97 && $code <= 122 || $code >= 65 && $code <= 90) { //Identify only alphabet(Capital/Small) characters
53 					$characterBuffer .= $character;	
54 				}
55 				else
56 				{
57 					if($characterBuffer != "") {	
58 						$content .= $this->checker($characterBuffer);
59 						$characterBuffer = "";
60 					}
61 
62 					if($character == "/" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/") { //Identify single comment or multiple comments
63 						$content .= "<font class='cC'>".$fileContent[$counter].$fileContent[$counter+1];
64 						if($fileContent[$counter+1] == "*") {
65 							$counter += 2;
66 							while($counter < $contentSize) {
67 								$character = $fileContent[$counter];
68 								$code = intval(ord$character);
69 								if($code != 10) { //Identify not '\n' character
70 									if($character == "*" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "/") {
71 										$counter++;
72 										$content .= $character.$fileContent[$counter]."</font>";
73 										break;
74 									}
75 									else
76 									{	
77 										$content .= $character;	
78 									}
79 								}
80 								else
81 								{
82 									$line++;
83 									$content .= "</font>".$character."<font class='lI'>".$line."</font> <font class='cC'>";
84 								}
85 								$counter++;
86 							}
87 						}
88 						else
89 						{
90 							$counter += 2;
91 							while($counter < $contentSize) {
92 								$character = $fileContent[$counter];
93 								$code = intval(ord$character);
94 								if($code == 10) { //Identify '\n' character
95 									$content .= "</font>";
96 									$counter--;
97 									break;
98 								}
99 								$content .= $character;
100 								$counter++;
101 							}
102 						}
103 					}
104 					else if($character == "'" || $character == "\"") { //Identify sigle quote or double quote character
105 						$foundCharacter = $character;
106 						$content .= "<font class='qC'>".$foundCharacter;
107 						$counter++;
108 						while($counter < $contentSize) {
109 							$character = $fileContent[$counter];
110 							$code = intval(ord$character);
111 							if($foundCharacter == $character) {
112 								if($foundCharacter == "\"") {
113 									if($fileContent[$counter-1] != "\\") {
114 										$content .= $foundCharacter."</font>";
115 										break;
116 									}
117 									else if($fileContent[$counter-2] == "\\" && $fileContent[$counter-1] == "\\") {
118 										$content .= $foundCharacter."</font>";
119 										break;
120 									}
121 									else
122 									{
123 										$content .= $character;
124 									}
125 								}
126 								else
127 								{
128 									$content .= $foundCharacter."</font>";
129 									break;
130 								}
131 							}
132 							else if($code == 10) { //Identify '\n' character
133 								$line++;
134 								$content .= $character;
135 								$content .= "<font class='lI'>".$line."</font> ";
136 							}
137 							else
138 							{
139 								$content .= $character;
140 							}
141 							$counter++;
142 						}
143 					}
144 					else if($character == "(" || $character == ")") { //Identify parenthesis character
145 						if($parenthesisFound == 0) {
146 							$content .= "<font class='pC'>".$character."</font><font class='iPC'>";
147 						}
148 						if($character == "(") {
149 							$parenthesisFound++;
150 						}
151 						else if($character == ")") {
152 							$parenthesisFound--;
153 						}
154 						if($parenthesisFound == 0) {
155 							$content .= "</font><font class='pC'>".$character."</font>";
156 						}
157 					}
158 					else if($character == "[" || $character == "]") { //Identify bracket character
159 						if($bracketFound == 0) {
160 							$content .= "<font class='bC'>".$character."</font><font class='iBC'>";
161 						}
162 						if($character == "[") {
163 							$bracketFound++;
164 						}
165 						else if($character == "]") {
166 							$bracketFound--;
167 						}
168 						if($bracketFound == 0) {
169 							$content .= "</font><font class='bC'>".$character."</font>";
170 						}
171 					}
172 					else if($code == 10) { //Identify '\n' character
173 						$line++;
174 						$content .= $character;
175 						$content .= "<font class='lI'>".$line."</font> ";
176 					}
177 					else
178 					{
179 						$content .= $character;
180 					}
181 				}
182 				$counter++;
183 			}
184 			
185 			$output = "<div class='codediv'>";
186 			if($this->showFileName == true) {
187 				$output .= "<div class='fN'>".$this->fileName."</div>";
188 			}
189 			$output .= "<div class='code'><pre><code>".$content."</code></pre></div>";
190 			$output .= "</div>";
191 			return $output;
192 		}
193 	}
194 
195 	private function checker($value) {
196 		global $languageKeywords;		
197 		$value = trim($value);
198 		if(isset$languageKeywords[$this->fileExtension]) { //Identify file type extension			
199 			if(in_array$value,$languageKeywords[$this->fileExtension]) { //Identify keywords				
200 				$value = "<font class='kC'>".$value."</font>";	
201 			}
202 		}	
203 		return $value;
204 	}
205 }
206 ?>


keywords.php
1 <?php
2 //Programming language keywords
3 $languageKeywords["php"] = array("function","if","else","isset","unset","foreach","for","while","do","new","private","public","protected");
4 $languageKeywords["cpp"] = array("void","char","int","main","static","if","else","for","while","do","cout","cin","struct","unsigned","long","return");
5 $languageKeywords["java"] = array("if","else","for","while","do","return","public","private","static","void","import","class","try","catch","package","main","this","int","null","new"); 
6 $languageKeywords["js"] = array("var","function");
7 ?>


style.css
1 /* Code Highlighter Start */
2 
3 /* Code Div */
4 .codediv {
5 font-size:16px;
6 padding:0px;
7 margin-top:5px;
8 margin-bottom:5px;
9 }
10 
11 /* File Name Color */
12 .fN {
13 color:#21094e;
14 margin-bottom:5px;
15 }
16 
17 /* Code */
18 .code {
19 line-height:normal;
20 padding:15px;
21 overflow:auto;
22 color:#85d9eb;
23 background-color:#191e1e;
24 border-left:5px solid #605a56;    
25 }
26 
27 /* Paragraph */
28 pre {
29 margin:0px;
30 padding:0px;
31 }
32 
33 /* Line Number Color */
34 .lI {
35 color:#ffffff;
36 }
37 
38 /* Keyword Color */
39 .kC {
40 color:#56e27a;
41 }
42 
43 /* Single Line & Mulitple Lines Comment Color */
44 .cC {
45 color:#b8b0b0;
46 }
47 
48 /* Single & Double Quotes Color */
49 .qC {
50 color:#d4f088;
51 }
52 
53 /* Parenthesis Color */
54 .pC {
55 color:#ffffff;
56 }
57 
58 /* Inner Parenthesis Color */
59 .iPC {
60 color:#d4f088;
61 }
62 
63 /* Bracket Color */
64 .bC {
65 color:#ffffff;
66 }
67 
68 /* Inner Bracket Color */
69 .iBC {
70 color:#d4f088;
71 }
72 
73 /* Code Highlighter End */


index.php
1 <link href="style.css" rel="stylesheet" />
2 <?php
3 include_once("highlighter.class.php");
4 $colorObj = new highlighter();
5 $fileContent = $colorObj->applycolor("highlighter.class.php");
6 echo $fileContent;
7 $colorObj->showfilename(false);
8 $fileContent = $colorObj->applycolor("index.php");
9 echo $fileContent;
10 ?>

No comments:

Post a Comment