highlighting of errorneous lines in from tool output
[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 'MediaWiki/geshi/geshi/geshi.php';
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 $geshi->parse_code();
31     }
32
33     /**
34      * Returns the type of the file, as used by Geshi
35      *
36      * @return string
37      */
38     public function getType($file)
39     {
40         $ext = $file->getExt();
41         if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
42             $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
43         }
44
45         return $ext;
46     }
47
48 }
49
50 ?>