remote forking: use the original http/https url in description
[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         require_once $GLOBALS['phorkie']['cfg']['geshi'];
21         $geshi = new \GeSHi($file->getContent(), $this->getType($file));
22         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
23         $geshi->set_header_type(GESHI_HEADER_DIV);
24
25         if ($res !== null) {
26             $geshi->highlight_lines_extra(array_keys($res->annotations));
27             $geshi->set_highlight_lines_extra_style('background-color: #F2DEDE');
28         }
29
30         return '<div class="code">'
31             . str_replace('&nbsp;', '&#160;', $geshi->parse_code())
32             . '</div>';
33     }
34
35     /**
36      * Returns the type of the file, as used by Geshi
37      *
38      * @return string
39      */
40     public function getType($file)
41     {
42         $ext = $file->getExt();
43         if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
44             $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
45         }
46
47         return $ext;
48     }
49
50 }
51
52 ?>