PEAR2_Text_Markdown › PEAR2_Text_Markdown-0.1.0/php/PEAR2/Text/Markdown/Wiki/ColorCodeBlock.php
- PEAR2_Text_Markdown-0.1.0/
- doc/
- pear2.php.net/
- PEAR2_Text_Markdown/
- examples/
- examples/
- examples/
- PEAR2_Text_Markdown/
- pear2.php.net/
- php/
- PEAR2/
- Autoload.php
- Exception.php
- MultiErrors/
- MultiErrors.php
- Text/
- Markdown/
- Apidoc/
- Apidoc.php
- Extra/
- Extra.php
- Main.php
- Plugin/
- Plugin.php
- Wiki/
- Wiki.php
- Markdown/
- PEAR2/
- doc/
- package.xml
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
*
* Block plugin to change indented text to <pre><code>...</code></pre>
* blocks, with code colorization.
*
* @category Solar
*
* @package Markdown_Wiki
*
* @author Paul M. Jones <pmjones@solarphp.com>
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
* @version $Id: ColorCodeBlock.php 3153 2008-05-05 23:14:16Z pmjones $
*
*/
namespace PEAR2\Text;
class Markdown_Wiki_ColorCodeBlock extends Markdown_Plugin
{
/**
*
* This is a block plugin.
*
* @var bool
*
*/
protected $_is_block = true;
/**
*
* Makes <pre><code>...</code></pre> blocks and colorizes.
*
* @param string $text Portion of the Markdown source text.
*
* @return string The transformed XHTML.
*
*/
public function parse($text)
{
$tab_width = $this->_getTabWidth();
$regex = '{
\n\{\{code:(.*?)\n # $1 = the colorization type, if any
( # $2 = the code block -- one or more lines, starting with a space/tab
(?:
(?:[ ]{' . $tab_width . '} | \t) # Lines must start with a tab or a tab-width of spaces
.*\n+
)+
)
\}\}\s*?\n # end of the block
}mx';
$text = preg_replace_callback(
$regex,
array($this, '_parse'),
$text
);
return $text;
}
/**
*
* Support callback for code blocks.
*
* @param array $matches Matches from preg_replace_callback().
*
* @return string The replacement text.
*
*/
protected function _parse($matches)
{
$type = empty($matches[1]) ? '' : trim($matches[1]);
$code = $this->_outdent($matches[2]);
// trim leading newlines and trailing whitespace
$code = preg_replace(
array('/\A\n+/', '/\s+\z/'),
'',
$code
);
// colorize and escape
$color = '_color' . ucfirst(strtolower($type));
if (is_callable(array($this, $color))) {
// colorize, which should escape on its own
$code = $this->$color($code);
} else {
// simple escaping
$code = "<pre><code>" . $this->_escape($code, ENT_NOQUOTES) . "</code></pre>";
}
// done
return "\n"
. $this->_toHtmlToken($code)
. "\n";
}
/**
*
* Returns colorized PHP code.
*
* @param string $code The code block to colorize.
*
* @return string The colorized code.
*
*/
protected function _colorPhp($code)
{
// start using http://us3.php.net/manual/en/function.highlight-string.php#68274
$code = "<?php\n" . $code . "\n?>";
$code = highlight_string($code, true);
$code = str_replace(
array("<br />", " "),
array("\n", " "),
$code
);
// <code><span style="color: #000000">\n
// </span>\n</code>\n
$code = substr($code, 36, -16);
// done!
return "<pre><code>$code</code></pre>";
}
}
EOF
