- welcome 2014
[plcapi.git] / doc / highlight.php
1 <?php
2 /**
3  * takes a dir as arg, highlights all php code found in html files inside
4  *
5  * @author Gaetano Giunta
6  * @copyright (c) 2007-2014 G. Giunta
7  */
8
9 function highlight($file)
10 {
11   $starttag = '<pre class="programlisting">';
12   $endtag = '</pre>';
13
14   $content = file_get_contents($file);
15   $last = 0;
16   $out = '';
17   while(($start = strpos($content, $starttag, $last)) !== false)
18   {
19     $end = strpos($content, $endtag, $start);
20         $code = substr($content, $start+strlen($starttag), $end-$start-strlen($starttag));
21         if ($code[strlen($code)-1] == "\n") {
22                 $code = substr($code, 0, -1);
23         }
24 //var_dump($code);
25         $code = str_replace(array('&gt;', '&lt;'), array('>', '<'), $code);
26     $code = highlight_string('<?php '.$code, true);
27     $code = str_replace('<span style="color: #0000BB">&lt;?php&nbsp;<br />', '<span style="color: #0000BB">', $code);
28 //echo($code);
29     $out = $out . substr($content, $last, $start+strlen($starttag)-$last) . $code . $endtag;
30     $last = $end+strlen($endtag);
31   }
32   $out .= substr($content, $last, strlen($content));
33   return $out;
34 }
35
36 $dir = $argv[1];
37
38 $files = scandir($dir);
39 foreach($files as $file)
40 {
41         if (substr($file, -5, 5) == '.html')
42         {
43                 $out = highlight($dir.'/'.$file);
44                 file_put_contents($dir.'/'.$file, $out);
45         }
46 }
47
48 ?>