-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdefault.php
More file actions
93 lines (71 loc) · 2.17 KB
/
default.php
File metadata and controls
93 lines (71 loc) · 2.17 KB
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
<?php
/*----------------------------------------------------------------------------*/
class BitterFormatDefault extends BitterFormat {
protected $tabsize = 4;
protected $line = 1;
protected $output = '';
public function process($source) {
$this->output = $source;
$this->processTabs();
$this->processLines();
return sprintf(
'<pre>%s</pre>',
$this->output
);
}
protected function processTabs() {
if (!function_exists('__expander')) eval("
function __expander(\$matches) {
return \$matches[1] . str_repeat(
' ', strlen(\$matches[2]) * {$this->tabsize} - (strlen(\$matches[1]) % {$this->tabsize})
);
}
");
while (strstr($this->output, "\t")) {
$this->output = preg_replace_callback('%^([^\t\n]*)(\t+)%m', '__expander', $this->output);
}
}
protected function processLines() {
$tokens = preg_split('%(<span class=".*?">|</span>)%', $this->output, 0, PREG_SPLIT_DELIM_CAPTURE);
$stack = array(); $this->output = '';
$this->startLine();
foreach ($tokens as $token) {
// Close:
if (preg_match('%^</%', $token)) {
array_pop($stack);
$this->output .= $token;
}
// Open:
else if (preg_match('%^<%', $token)) {
array_push($stack, $token);
$this->output .= $token;
}
else {
$characters = preg_split('//', $token);
foreach ($characters as $character) {
if ($character == "\n") {
$this->endLine();
foreach ($stack as $alt_token) $this->output .= '</span>';
}
$this->output .= $character;
if ($character == "\n") {
$this->startLine();
foreach ($stack as $alt_token) $this->output .= $alt_token;
}
}
}
}
$this->endLine();
}
protected function startLine() {
$this->output .= "<code class=\"line line-{$this->line}\">";
}
protected function endLine() {
$this->line++;
$this->output .= '</code>';
}
}
/*----------------------------------------------------------------------------*/
return new BitterFormatDefault();
/*----------------------------------------------------------------------------*/
?>