Code.php 4.89 KB
Newer Older
zYne's avatar
zYne committed
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 59 60
<?php
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
/**
 * Code rule end renderer for Xhtml
 *
 * PHP versions 4 and 5
 *
 * @category   Text
 * @package    Text_Wiki
 * @author     Paul M. Jones <pmjones@php.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    CVS: $Id: Code.php,v 1.13 2006/02/10 23:07:03 toggg Exp $
 * @link       http://pear.php.net/package/Text_Wiki
 */

/**
 * This class renders code blocks in XHTML.
 *
 * @category   Text
 * @package    Text_Wiki
 * @author     Paul M. Jones <pmjones@php.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    Release: @package_version@
 * @link       http://pear.php.net/package/Text_Wiki
 */
class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render {

    var $conf = array(
        'css'      => null, // class for <pre>
        'css_code' => null, // class for generic <code>
        'css_php'  => null, // class for PHP <code>
        'css_html' => null, // class for HTML <code>
        'css_filename' => null // class for optional filename <div>
    );

    /**
    *
    * Renders a token into text matching the requested format.
    *
    * @access public
    *
    * @param array $options The "options" portion of the token (second
    * element).
    *
    * @return string The text rendered from the token options.
    *
    */

    function token($options)
    {
        $text = $options['text'];
        $attr = $options['attr'];
        $type = strtolower($attr['type']);

        $css      = $this->formatConf(' class="%s"', 'css');
        $css_code = $this->formatConf(' class="%s"', 'css_code');
        $css_php  = $this->formatConf(' class="%s"', 'css_php');
        $css_html = $this->formatConf(' class="%s"', 'css_html');
        $css_filename = $this->formatConf(' class="%s"', 'css_filename');

zYne's avatar
zYne committed
61
        if ($type == 'php') {
zYne's avatar
zYne committed
62 63 64
            if (substr($options['text'], 0, 5) != '<?php') {
                // PHP code example:
                // add the PHP tags
zYne's avatar
zYne committed
65 66
                $text = "<?php 
                " .  $options['text'] . "\n?>"; // <?php
zYne's avatar
zYne committed
67 68 69
            }

            // convert tabs to four spaces
zYne's avatar
zYne committed
70
            $text = str_replace("\t", "&nbsp;&nbsp;%nbsp;&nbsp;", $text);
zYne's avatar
zYne committed
71 72 73

            // colorize the code block (also converts HTML entities and adds
            // <code>...</code> tags)
zYne's avatar
zYne committed
74 75 76
            $h = new PHP_Highlight;
            $h->loadString($text);
            $text = $h->toHtml(true);
zYne's avatar
zYne committed
77 78 79 80 81

            // replace <br /> tags with simple newlines.
            // replace non-breaking space with simple spaces.
            // translate HTML <font> and color to XHTML <span> and style.
            // courtesy of research by A. Kalin :-).
zYne's avatar
zYne committed
82
            /**
zYne's avatar
zYne committed
83 84 85
            $map = array(
                '<br />'  => "\n",
                '&nbsp;'  => ' ',
zYne's avatar
zYne committed
86
                "\n"      => "<br \>",
zYne's avatar
zYne committed
87 88 89 90
                '<font'   => '<span',
                '</font>' => '</span>',
                'color="' => 'style="color:'
            );
zYne's avatar
zYne committed
91

zYne's avatar
zYne committed
92
            $text = strtr($text, $map);
zYne's avatar
zYne committed
93
            */
zYne's avatar
zYne committed
94 95 96 97 98 99 100 101 102 103 104 105 106

            // get rid of the last newline inside the code block
            // (becuase higlight_string puts one there)
            if (substr($text, -8) == "\n</code>") {
                $text = substr($text, 0, -8) . "</code>";
            }

            // replace all <code> tags with classed tags
            if ($css_php) {
                $text = str_replace('<code>', "<code$css_php>", $text);
            }

            // done
zYne's avatar
zYne committed
107 108 109 110
            $text = "<table border=1 class='dashed' cellpadding=0 cellspacing=0>
                     <tr><td><b>$text
                     </b></td></tr>
                     </table>";
zYne's avatar
zYne committed
111 112 113 114 115 116 117 118 119 120 121 122

        } elseif ($type == 'html' || $type == 'xhtml') {

            // HTML code example:
            // add <html> opening and closing tags,
            // convert tabs to four spaces,
            // convert entities.
            $text = str_replace("\t", "    ", $text);
            $text = "<html>\n$text\n</html>";
            $text = $this->textEncode($text);
            $text = "<pre$css><code$css_html>$text</code></pre>";

zYne's avatar
zYne committed
123 124 125 126 127 128 129 130
        } elseif ($type == 'sql') {
            // HTML code example:
            // add <html> opening and closing tags,
            // convert tabs to four spaces,
            // convert entities.
            $text = str_replace("\t", "    ", $text);
            $text = $this->textEncode($text);
            $text = "<div class='sql'>$text</div>";
zYne's avatar
zYne committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        } else {
            // generic code example:
            // convert tabs to four spaces,
            // convert entities.
            $text = str_replace("\t", "    ", $text);
            $text = $this->textEncode($text);
            $text = "<pre$css><code$css_code>$text</code></pre>";
        }

        if ($css_filename && isset($attr['filename'])) {
            $text = "<div$css_filename>" .
                $attr['filename'] . '</div>' . $text;
        }

        return "\n$text\n\n";
    }
}
?>