aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Renderer/Geshi.php
blob: 18f95acdba9577a338f57d523117222b9cb1f8f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace phorkie;

class Renderer_Geshi
{
    /**
     * Converts the code to HTML
     *
     * @param File        $file File to render
     * @param Tool_Result $res  Tool result to integrate
     *
     * @return string HTML
     */
    public function toHtml(File $file, Tool_Result $res = null)
    {
        /**
         * Yes, geshi needs to be in your include path
         * We use the geshi pear package.
         */
        if (!class_exists('\\GeSHi', true)) {
            require_once $GLOBALS['phorkie']['cfg']['geshi'];
        }
        $geshi = new \GeSHi($file->getContent(), $this->getType($file));
        $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
        $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
        $geshi->enable_classes();
        $geshi->set_line_style('color: #DDD;');

        if ($res !== null) {
            $geshi->highlight_lines_extra(array_keys($res->annotations));
            $geshi->set_highlight_lines_extra_style('background-color: #F2DEDE');
        }

        return '<style type="text/css">'
            . $geshi->get_stylesheet() . '</style>'
            . '<div class="code">'
            . str_replace('&nbsp;', '&#160;', $geshi->parse_code())
            . '</div>';
    }

    /**
     * Returns the type of the file, as used by Geshi
     *
     * @return string
     */
    public function getType($file)
    {
        $ext = $file->getExt();
        if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
            $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
        }

        return $ext;
    }

}

?>