Add pakefile as replacement for makefile
[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-2015 G. Giunta
7  */
8 function highlight($file)
9 {
10     $starttag = '<pre class="programlisting">';
11     $endtag = '</pre>';
12
13     $content = file_get_contents($file);
14     $last = 0;
15     $out = '';
16     while (($start = strpos($content, $starttag, $last)) !== false) {
17         $end = strpos($content, $endtag, $start);
18         $code = substr($content, $start + strlen($starttag), $end - $start - strlen($starttag));
19         if ($code[strlen($code) - 1] == "\n") {
20             $code = substr($code, 0, -1);
21         }
22
23         $code = str_replace(array('&gt;', '&lt;'), array('>', '<'), $code);
24         $code = highlight_string('<?php ' . $code, true);
25         $code = str_replace('<span style="color: #0000BB">&lt;?php&nbsp;<br />', '<span style="color: #0000BB">', $code);
26
27         $out = $out . substr($content, $last, $start + strlen($starttag) - $last) . $code . $endtag;
28         $last = $end + strlen($endtag);
29     }
30     $out .= substr($content, $last, strlen($content));
31
32     return $out;
33 }
34
35 $dir = $argv[1];
36
37 $files = scandir($dir);
38 foreach ($files as $file) {
39     if (substr($file, -5, 5) == '.html') {
40         $out = highlight($dir . '/' . $file);
41         file_put_contents($dir . '/' . $file, $out);
42     }
43 }