|
1 <?php |
|
2 // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: |
|
3 /** |
|
4 * Code rule end renderer for Xhtml |
|
5 * |
|
6 * PHP versions 4 and 5 |
|
7 * |
|
8 * @category Text |
|
9 * @package Text_Wiki |
|
10 * @author Paul M. Jones <pmjones@php.net> |
|
11 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|
12 * @version CVS: $Id: Code.php,v 1.13 2006/02/10 23:07:03 toggg Exp $ |
|
13 * @link http://pear.php.net/package/Text_Wiki |
|
14 */ |
|
15 |
|
16 /** |
|
17 * This class renders code blocks in XHTML. |
|
18 * |
|
19 * @category Text |
|
20 * @package Text_Wiki |
|
21 * @author Paul M. Jones <pmjones@php.net> |
|
22 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|
23 * @version Release: @package_version@ |
|
24 * @link http://pear.php.net/package/Text_Wiki |
|
25 */ |
|
26 class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render { |
|
27 |
|
28 var $conf = array( |
|
29 'css' => null, // class for <pre> |
|
30 'css_code' => null, // class for generic <code> |
|
31 'css_php' => null, // class for PHP <code> |
|
32 'css_html' => null, // class for HTML <code> |
|
33 'css_filename' => null // class for optional filename <div> |
|
34 ); |
|
35 |
|
36 /** |
|
37 * |
|
38 * Renders a token into text matching the requested format. |
|
39 * |
|
40 * @access public |
|
41 * |
|
42 * @param array $options The "options" portion of the token (second |
|
43 * element). |
|
44 * |
|
45 * @return string The text rendered from the token options. |
|
46 * |
|
47 */ |
|
48 |
|
49 function token($options) |
|
50 { |
|
51 $text = $options['text']; |
|
52 $attr = $options['attr']; |
|
53 $type = strtolower($attr['type']); |
|
54 |
|
55 $css = $this->formatConf(' class="%s"', 'css'); |
|
56 $css_code = $this->formatConf(' class="%s"', 'css_code'); |
|
57 $css_php = $this->formatConf(' class="%s"', 'css_php'); |
|
58 $css_html = $this->formatConf(' class="%s"', 'css_html'); |
|
59 $css_filename = $this->formatConf(' class="%s"', 'css_filename'); |
|
60 |
|
61 if ($type == 'php') { |
|
62 if (substr($options['text'], 0, 5) != '<?php') { |
|
63 // PHP code example: |
|
64 // add the PHP tags |
|
65 $text = "<?php\n" . $options['text'] . "\n?>"; // <?php |
|
66 } |
|
67 |
|
68 // convert tabs to four spaces |
|
69 $text = str_replace("\t", " ", $text); |
|
70 |
|
71 // colorize the code block (also converts HTML entities and adds |
|
72 // <code>...</code> tags) |
|
73 ob_start(); |
|
74 highlight_string($text); |
|
75 $text = ob_get_contents(); |
|
76 ob_end_clean(); |
|
77 |
|
78 // replace <br /> tags with simple newlines. |
|
79 // replace non-breaking space with simple spaces. |
|
80 // translate HTML <font> and color to XHTML <span> and style. |
|
81 // courtesy of research by A. Kalin :-). |
|
82 $map = array( |
|
83 '<br />' => "\n", |
|
84 ' ' => ' ', |
|
85 '<font' => '<span', |
|
86 '</font>' => '</span>', |
|
87 'color="' => 'style="color:' |
|
88 ); |
|
89 $text = strtr($text, $map); |
|
90 |
|
91 // get rid of the last newline inside the code block |
|
92 // (becuase higlight_string puts one there) |
|
93 if (substr($text, -8) == "\n</code>") { |
|
94 $text = substr($text, 0, -8) . "</code>"; |
|
95 } |
|
96 |
|
97 // replace all <code> tags with classed tags |
|
98 if ($css_php) { |
|
99 $text = str_replace('<code>', "<code$css_php>", $text); |
|
100 } |
|
101 |
|
102 // done |
|
103 $text = "<pre$css>$text</pre>"; |
|
104 |
|
105 } elseif ($type == 'html' || $type == 'xhtml') { |
|
106 |
|
107 // HTML code example: |
|
108 // add <html> opening and closing tags, |
|
109 // convert tabs to four spaces, |
|
110 // convert entities. |
|
111 $text = str_replace("\t", " ", $text); |
|
112 $text = "<html>\n$text\n</html>"; |
|
113 $text = $this->textEncode($text); |
|
114 $text = "<pre$css><code$css_html>$text</code></pre>"; |
|
115 |
|
116 } else { |
|
117 // generic code example: |
|
118 // convert tabs to four spaces, |
|
119 // convert entities. |
|
120 $text = str_replace("\t", " ", $text); |
|
121 $text = $this->textEncode($text); |
|
122 $text = "<pre$css><code$css_code>$text</code></pre>"; |
|
123 } |
|
124 |
|
125 if ($css_filename && isset($attr['filename'])) { |
|
126 $text = "<div$css_filename>" . |
|
127 $attr['filename'] . '</div>' . $text; |
|
128 } |
|
129 |
|
130 return "\n$text\n\n"; |
|
131 } |
|
132 } |
|
133 ?> |