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