Subversion Repositories Applications.papyrus

Rev

Rev 1688 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 jpm 1
<?php
2
 
3
class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render {
4
 
5
    var $conf = array(
6
        'css'      => null, // class for <pre>
7
        'css_code' => null, // class for generic <code>
8
        'css_php'  => null, // class for PHP <code>
9
        'css_html' => null // class for HTML <code>
10
    );
11
 
12
    /**
13
    *
14
    * Renders a token into text matching the requested format.
15
    *
16
    * @access public
17
    *
18
    * @param array $options The "options" portion of the token (second
19
    * element).
20
    *
21
    * @return string The text rendered from the token options.
22
    *
23
    */
24
 
25
    function token($options)
26
    {
27
        $text = $options['text'];
28
        $attr = $options['attr'];
29
           $type = strtolower($attr['type']);
30
 
31
        $css      = $this->formatConf(' class="%s"', 'css');
32
        $css_code = $this->formatConf(' class="%s"', 'css_code');
33
        $css_php  = $this->formatConf(' class="%s"', 'css_php');
34
        $css_html = $this->formatConf(' class="%s"', 'css_html');
35
 
36
        if ($type == 'php') {
37
 
38
            // PHP code example:
39
            // add the PHP tags
40
            $text = "<?php\n" . $options['text'] . "\n?>"; // <?php
41
 
42
            // convert tabs to four spaces
43
            $text = str_replace("\t", "    ", $text);
44
 
45
            // colorize the code block (also converts HTML entities and adds
46
            // <code>...</code> tags)
47
            ob_start();
48
            highlight_string($text);
49
            $text = ob_get_contents();
50
            ob_end_clean();
51
 
52
            // replace <br /> tags with simple newlines.
53
            // replace non-breaking space with simple spaces.
54
            // translate HTML <font> and color to XHTML <span> and style.
55
            // courtesy of research by A. Kalin :-).
56
            $map = array(
57
                '<br />'  => "\n",
58
                '&nbsp;'  => ' ',
59
                '<font'   => '<span',
60
                '</font>' => '</span>',
61
                'color="' => 'style="color:'
62
            );
63
            $text = strtr($text, $map);
64
 
65
            // get rid of the last newline inside the code block
66
            // (becuase higlight_string puts one there)
67
            if (substr($text, -8) == "\n</code>") {
68
                $text = substr($text, 0, -8) . "</code>";
69
            }
70
 
71
            // replace all <code> tags with classed tags
72
            if ($css_php) {
73
                $text = str_replace('<code>', "<code$css_php>", $text);
74
            }
75
 
76
            // done
77
            $text = "<pre$css>$text</pre>";
78
 
79
        } elseif ($type == 'html' || $type == 'xhtml') {
80
 
81
            // HTML code example:
82
            // add <html> opening and closing tags,
83
            // convert tabs to four spaces,
84
            // convert entities.
85
            $text = str_replace("\t", "    ", $text);
86
            $text = "<html>\n$text\n</html>";
87
            $text = htmlentities($text);
88
            $text = "<pre$css><code$css_html>$text</code></pre>";
89
 
90
        } else {
91
            // generic code example:
92
            // convert tabs to four spaces,
93
            // convert entities.
94
            $text = str_replace("\t", "    ", $text);
95
            $text = htmlentities($text);
96
            $text = "<pre$css><code$css_code>$text</code></pre>";
97
        }
98
 
99
        return "\n$text\n\n";
100
    }
101
}
102
?>