Initial add of defaria.com
[clearscm.git] / defaria.com / php / phpcode.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 // 
4 // phpcode: Displays or returns an HTML'ized version of a php file
5 //
6 ////////////////////////////////////////////////////////////////////////////////
7 function phpcode ($file, $funclink = true, $return = false) {
8   $data = @highlight_file ($file, true)
9     or die ("Unable to open file $file");
10
11   // Init
12   $data = explode ('<br />', $data);
13   $start = '<span style="color: #aaa;">';
14   $end   = '</span>';
15   $i = 1;
16   $text = '';
17  
18   // Loop
19   foreach ($data as $line) {
20     $nbr = $i;
21     if ($i < 10000) {
22       $nbr = "&nbsp;$i";
23     } elseif ($i < 1000) {
24       $nbr = "&nbsp;&nbsp;$i";
25     } elseif ($i < 100) {
26       $nbr = "&nbsp;&nbsp;&nbsp;$i";
27     } elseif ($i < 10) {
28       $nbr = "&nbsp;&nbsp;&nbsp;&nbsp;$i";
29     } // if      
30     $text .= sprintf("%s %s:%s %s<br>\n",
31                      $start, $nbr, $end, str_replace("\n", '', $line));
32     ++$i;
33   } // foreach
34
35   // replace <code><span>... with <pre><span>... to justify numbers
36   $html_col = ini_get('highlight.html');
37   $text = preg_replace("/(<code><span style=\"color: $html_col\">)?/",
38                        "", $text);
39   $text = preg_replace("/(<\/span><\/code>)?/",
40                        "", $text);
41   $text = "<font family=fixed><span style=\"color: $html_col\">".$text."</span></font>"; 
42
43   // Wrap with div class=code
44   $text = '<div class="code">' . $text . '</div>';
45   // Optional function linking
46   if ($funclink === true) {
47     $keyword_col = ini_get('highlight.keyword');
48     $manual = 'http://www.php.net/function.';
49     $text = preg_replace(
50       // Match a highlighted keyword
51       '~([\w_]+)(\s*</font>)'.
52       // Followed by a bracket
53       '(\s*<font\s+color="' . $keyword_col . '">\s*\()~m',
54       // Replace with a link to the manual
55       '<a href="' . $manual . '$1">$1</a>$2$3', $text); 
56   } // if
57     
58   // Return mode
59   if ($return === false) {
60     echo $text;
61   } else {
62     return $text;
63   } // if
64 } // phpcode
65 ?>