make autoloading geshi work
[phorkie.git] / src / phorkie / Renderer / Geshi.php
1 <?php
2 namespace phorkie;
3
4 class Renderer_Geshi
5 {
6     /**
7      * Converts the code to HTML
8      *
9      * @param File        $file File to render
10      * @param Tool_Result $res  Tool result to integrate
11      *
12      * @return string HTML
13      */
14     public function toHtml(File $file, Tool_Result $res = null)
15     {
16         /**
17          * Yes, geshi needs to be in your include path
18          * We use the mediawiki geshi extension package.
19          */
20         if (!class_exists('\\geshi', true)) {
21             require_once $GLOBALS['phorkie']['cfg']['geshi'];
22         }
23         $geshi = new \geshi($file->getContent(), $this->getType($file));
24         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
25         $geshi->set_header_type(GESHI_HEADER_DIV);
26
27         if ($res !== null) {
28             $geshi->highlight_lines_extra(array_keys($res->annotations));
29             $geshi->set_highlight_lines_extra_style('background-color: #F2DEDE');
30         }
31
32         return '<div class="code">'
33             . str_replace('&nbsp;', '&#160;', $geshi->parse_code())
34             . '</div>';
35     }
36
37     /**
38      * Returns the type of the file, as used by Geshi
39      *
40      * @return string
41      */
42     public function getType($file)
43     {
44         $ext = $file->getExt();
45         if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
46             $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
47         }
48
49         return $ext;
50     }
51
52 }
53
54 ?>