FSHL tester

Enter your text
Text encoding

Select language
Additional parser options Show line numbers
Emulate tabs
Links to help pages

Download file (simple output)

Cached results (demo - 'index.php')

FSHL Version 0.4.20
Input filename index.php (10363 bytes / output 60113 bytes)
PARSE time 0.286 sec (35.44 kB/s)
  1: <?php
  2:     /* ----------------------------------------------------------------- *
  3:      * FSHL DEMO                                                         *
  4:      * ----------------------------------------------------------------- */
  5:     
  6:     function GetPostParam($param) {
  7:         return isset($_POST[$param]) ? $_POST[$param] : null;
  8:     }
  9:     
 10:     function LoadFile($filename) {
 11:         $filehandle = fopen ($filename, 'r') or die("Could not open $filename for reading.");
 12:         $text = fread($filehandle, filesize($filename));
 13:         fclose($filehandle);
 14:         return $text;
 15:     }
 16: 
 17:     function safeFileWrite($file, $mode, &$str) {
 18:         // ceate a lock file - if not possible someone else is active so wait (a while)
 19:         $err = true;
 20:         $lock = $file.'.lock';
 21:         $lf = @fopen ($lock, 'x');
 22:         $i = 0;
 23:         while ($lf === false && $i++ < 20) {
 24:             clearstatcache();
 25:             usleep(rand(5,85));
 26:             $lf = @fopen ($lock, 'x');
 27:         }
 28:         // if lockfile (finally) has been created, file is ours, else give up ...
 29:         if ($lf !== false) {
 30:             $fp = fopen($file, $mode);
 31:             if($fp !== false) {
 32:                 fwrite($fp, $str); // or use a callback
 33:                 fclose($fp);
 34:                 $err = false;
 35:             }
 36:             // and unlock
 37:             fclose($lf);
 38:             unlink($lock);
 39:         }
 40:         return $err;
 41:     }
 42:     
 43:     // -----------------------------------------------------------------
 44:     // Language array (TODO: add new languages here)
 45: 
 46:     $lang_config = array(
 47: 
 48:     // index        lang_name       option text
 49: 
 50:         "1" => array("HTML",        "HTML (with CSS, JS and PHP)"),
 51:         "2" => array("HTMLonly",    "HTML only"),
 52:         "3" => array("CSS",         "CSS only"),
 53:         "4" => array("PHP",         "PHP only"),
 54:         "5" => array("PY",          "Python"),
 55:         "6" => array("JS",          "JS only"),
 56:         "7" => array("CPP",         "C/C++"),
 57:         "8" => array("JAVA",        "JAVA"),
 58:         "9" => array("SQL",         "SQL"),
 59:         "10" => array("TEXY",       "Texy! is sexy! (and experimental)"),
 60:         "11" => array("SAFE",       "TXT (safe lexer)"),
 61:     );
 62: 
 63:     // -----------------------------------------------------------------
 64:     // FSHL DEMO Initialization
 65:     // -----------------------------------------------------------------
 66:     include ('fshl/fshl.php');
 67:     @include('online_limit.php');
 68:     
 69:     if(!defined('FSHL_LIVEDEMO_LIMIT')) {
 70:         define('FSHL_LIVEDEMO_LIMIT', 0x7fffffff);
 71:     }
 72:     
 73:     $start_language = "HTML";
 74: 
 75:     $user_lang = GetPostParam("lang_type");
 76:     $user_text = GetPostParam("test_text");
 77:     $ln_checked = GetPostParam("line_counter");
 78:     $tabs_checked = GetPostParam("emulate_tabs");
 79:     $simple_output = GetPostParam("simple_output");
 80:     $text_encoding = GetPostParam("text_encoding");
 81:     $parser_hp_links = GetPostParam("help_links");
 82:     $demo = true;
 83:     $lng = 0;
 84:     
 85:     if(!$user_text && !$user_lang && !$ln_checked && !$tabs_checked) {
 86:         $tabs_checked = $ln_checked = true;
 87:         $parser_hp_links = true;
 88:         $text_encoding = 0;
 89:     }
 90:     
 91:     $js_warning_message = "WARNING: your string is too long for this demo. Max input size is ".FSHL_LIVEDEMO_LIMIT." bytes.";
 92:     
 93:     if($user_text && $user_lang) {
 94:         // process user's text
 95:         if(isset($lang_config[$user_lang])) {
 96:             $start_language = $lang_config[$user_lang][0];
 97:             $text = get_magic_quotes_gpc() ? stripslashes($user_text) : $user_text;
 98:             $filename = "your text";
 99:             if(strlen($text) > FSHL_LIVEDEMO_LIMIT) {
100:                 $w = "\n[[ $js_warning_message ]]\n";
101:                 $text = "$w\n".substr($text, 0, FSHL_LIVEDEMO_LIMIT)."\n$w";
102:             }
103:             $lng = strlen($text);
104:             $demo = false;
105:         }
106:     }
107:     $cache_index = ($text_encoding?0:1).($tabs_checked?0:1).($ln_checked?0:1).($parser_hp_links?0:1);
108:     $cache_file = "demo_cache/$cache_index.txt";
109:     $cache_results_file = "demo_cache/$cache_index.res.txt";
110:     $out = false; $write_to_cache = false; $content_is_cached = false;
111:     if($demo) {
112:         $filename = 'index.php';                // self read
113:         if(defined('FSHL_LIVEDEMO_CACHE') && FSHL_LIVEDEMO_CACHE) {
114:             // fake cache implementation
115:             @$ts = filemtime($cache_file);
116:             @$update =  $ts < filemtime($filename) ||
117:                         $ts < filemtime('fshl/fshl.php') || 
118:                         $ts < filemtime(FSHL_CACHE.'.touch');
119:             if($ts === FALSE || $update) {
120:                 $write_to_cache = true;
121:             } else {
122:                 $out = LoadFile($cache_file);
123:                 $tmp = unserialize(LoadFile($cache_results_file));
124:                 $time = $tmp[0]; $lng = $tmp[1];
125:                 $content_is_cached = true;
126:             }
127:         }
128:         if(!$out) {
129:             $text = LoadFile($filename);
130:             $lng = strlen($text);
131:         }
132:     }
133:     // -----------------------------------------------------------------
134:     //                          LET'S GO
135:     // -----------------------------------------------------------------
136:     // make options for parser constructor
137:     $parser_opt = ($tabs_checked ? P_TAB_INDENT : 0) | ($ln_checked ? P_LINE_COUNTER : 0);
138:     $parser_output_module = false;
139:     if($parser_hp_links) {
140:         $parser_output_module = $text_encoding ? 'HTML_HP' : 'HTML_HP_UTF8';
141:     } else {
142:         $parser_output_module = $text_encoding ? 'HTML' : 'HTML_UTF8';
143:     }
144:     
145:     // create new parser object 
146:     $parser = new fshlParser($parser_output_module, $parser_opt);
147:     
148:     if(!$demo) {
149:         // classic highlight example (applied on user's text)
150:         $out = $parser->highlightString($start_language, $text);
151:         $time = $parser->getParseTime();
152:     } else {
153:         if(!$out) {
154:             // new 'per partes' highlight example
155:             $offset = 0;
156:             while($offset < strlen($text)) {
157:                 $sub_text = substr($text, $offset, 0x666);
158:                 if(!$offset) {
159:                     $counter_padding = $parser->calcCounterPadding($text);
160:                     $out  = $parser->highlightString($start_language, $sub_text, 0, $counter_padding);
161:                     $time = $parser->getParseTime();
162:                 } else {
163:                     $out .= $parser->highlightNextString($sub_text);
164:                     $time += $parser->getParseTime();
165:                 }
166:                 $offset += strlen($sub_text);
167:             }
168:         }
169:     }
170:     if($write_to_cache) {
171:         // NOTE: this simple cache method may be very unstable, use at your own risk on live servers
172:         safeFileWrite($cache_file, 'w', $out);
173:         safeFileWrite($cache_results_file, 'w', serialize(array($time, $lng)));
174:     }
175:     
176:     $out = $parser->getInternalStatistics().$out;
177:     
178:     // Show results....
179:     
180:     //<!-- cohenizator -->
181:     $encoding_str = $text_encoding ? 'windows-1250' : 'utf-8';
182:     if($simple_output) {
183:         include('simple_output.php');
184:         exit();
185:     }
186:     //<!-- /cohenizator -->
187:     echo "<?xml version=\"1.0\" encoding=\"$encoding_str\"?>\n";
188: ?>
189: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
190:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
191: <html>
192: <head>
193:   <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $encoding_str;?>" />
194:   <title>FastSHL Example - universal syntax highlighter</title>
195:   <style type="text/css">
196:   <?php include (FSHL_STYLE.'COHEN_style.css'); ?>
197: 
198:     BODY 
199:     {
200:         font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
201:         font-size: 80%;
202:         margin: 2em;
203:         background: #FFF;
204:         color: #000;
205:     }
206: 
207:     H1  { font-size: 170%;}
208:     H2  { font-size: 140%;}
209:     H3  { font-size: 120%;}
210:     H4  { font-size: 100%;}
211:     P   { font-size: 80%;  }
212: 
213:     /* hello fczbkk :) */
214: 
215:     .formular {
216:         /*width: 600px;*/
217:         margin: 0px auto;
218:         border: 2px solid #FFF;
219:         background-color: #fdc;
220:         padding: 10px;
221:     }
222:     .formular .riadok {
223:         clear: both;
224:         margin: 2px auto;
225:     }
226: 
227:     .formular .riadok .lavy {
228:         width: 20%;
229:         float: left;
230:         text-align: left;
231:     }
232:     .formular .riadok .pravy {
233:         width: 80%;
234:         float: right;
235:         text-align: left;
236:     }
237:     .formular .tlacitka {
238:         clear: both;
239:         text-align: center;
240:     }
241:     .formular .foo {
242:         clear: both;
243:     }
244: 
245:     .formular .code {
246:         font-size: 120%;
247:         clear: both;
248:         background-color: #fff;
249:     }
250:     
251:     /*.normal a {
252:         text-decoration: none;
253:     }
254:     .normal a:hover {
255:         text-decoration: underline;
256:     }*/
257: 
258:   </style>
259:   <script type="text/javascript">
260:     // thanx to johno, for great example;)
261:     function validate() {
262:         var elm = document.getElementById('test_text');
263:         if(elm && (elm.value.length > <?php echo FSHL_LIVEDEMO_LIMIT; ?> + 4)) {
264:             alert('<?php echo $js_warning_message; ?>');
265:             return false;
266:         }
267:         return true;
268:     }
269:   </script>
270: </head>
271: <body>
272: <h2><a href="http://hvge.sk/scripts/fshl">FSHL</a> tester</h2>
273: <form class="formular" method="post" action="index.php" onsubmit="return validate();">
274:     <div class="riadok">
275:         <span class="lavy">Enter your text</span>
276:         <span class="pravy">
277:             <textarea name="test_text" id="test_text" cols="60" rows="8"></textarea>
278:         </span>
279:     </div>
280: 
281:     <div class="riadok">
282:         <span class="lavy">Text encoding</span>
283:         <span class="pravy">
284:             <select name="text_encoding" onchange="this.form.submit();">
285:                 <option value="0" <?php if(!$text_encoding) echo 'selected="selected"';?>>UTF-8</option>
286:                 <option value="1" <?php if( $text_encoding) echo 'selected="selected"';?>>8-bit RAW mode</option>
287:             </select>           
288:         </span>
289:     </div>
290:     <div class="riadok">
291:         <br />
292:         <span class="lavy">Select language</span>
293:         <span class="pravy">
294:             <select name="lang_type">
295: <?php
296:             foreach($lang_config as $index => $language)
297:             {
298:                 echo "\t\t\t\t<option ";
299:                 if($index == $user_lang)
300:                     echo 'selected="selected" ';
301:                 echo "value=\"".$index."\">".$language[1]."</option>\n";
302:             }
303: ?>
304:             </select>
305:         </span>
306:     </div>
307:     
308:     <div class="riadok">
309:         <span class="lavy">Additional parser options</span>
310:         <span class="pravy">            
311:             <input type="checkbox" name="line_counter" <?php if($ln_checked) echo 'checked="checked"';?> />
312:                 Show line numbers<br />
313:             <input type="checkbox" name="emulate_tabs" <?php if($tabs_checked) echo 'checked="checked"';?> />
314:                 Emulate tabs<br />
315:             <input type="checkbox" name="help_links" <?php if($parser_hp_links) echo 'checked="checked"';?> />
316:                 Links to help pages<br />
317:         </span>
318:     </div>
319:     
320:     <div class="riadok">
321:         <span class="pravy">
322:             <br />
323:             <input type="submit" value="Highlight" />
324:             <input type="checkbox" name="simple_output" <?php if($simple_output) echo 'checked="checked"';?> /> 
325:                 Download file (simple output)
326:         </span>
327:     </div>
328:     
329:     <div class="foo">
330:     </div>
331: </form>
332: 
333: <div class="formular">
334:     <h2><?php echo ($content_is_cached ? 'Cached results' : 'Results').($demo ? " (demo - '$filename')" : null); ?></h2>
335:     <div class="riadok">
336:         <span class="lavy">FSHL Version</span>
337:         <span class="pravy"><?php echo "<strong>".FSHL_PARSER_VERSION."</strong>"; ?></span>
338:     </div>
339:     <div class="riadok">
340:         <span class="lavy">Input filename</span>
341:         <span class="pravy"><?php   
342:             echo "<strong>$filename</strong> ($lng bytes / output ".strlen($out).' bytes)'; 
343:                                     
344:         ?></span>
345:     </div>
346:     <div class="riadok">
347:         <span class="lavy">PARSE time</span>
348:         <span class="pravy"><?php printf("<strong>%0.3f</strong> sec (%0.2f kB/s)", $time, $lng/$time/1024 ); ?></span>
349:     </div>
350: 
351:     <div class="code">
352:         <pre class="normal"><?php echo $out; ?></pre>
353:     </div>
354: </div>
355: </body>
356: </html>