1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaloa\Renderer\Xml\Rule; |
4
|
|
|
|
5
|
|
|
use DOMElement; |
6
|
|
|
use Kaloa\Renderer\Xml\Rule\AbstractRule; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
final class ListingsRule extends AbstractRule |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $listingCount; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $positionCache; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
1 |
|
public function init() |
29
|
|
|
{ |
30
|
1 |
|
$this->listingCount = 0; |
31
|
1 |
|
$this->positionCache = array(); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
1 |
|
public function render() |
38
|
|
|
{ |
39
|
1 |
|
foreach ($this->runXpathQuery('//listing') as $node) { |
40
|
|
|
/* @var $node DOMElement */ |
41
|
1 |
|
$parent = $node->parentNode; |
42
|
|
|
|
43
|
|
|
|
44
|
1 |
|
$fragment = $this->getDocument()->createDocumentFragment(); |
45
|
|
|
|
46
|
1 |
|
$language = (string) $node->getAttribute('language'); |
47
|
1 |
|
$caption = (string) $node->getAttribute('caption'); |
48
|
|
|
|
49
|
1 |
|
$file = (string) $node->getAttribute('file'); |
50
|
1 |
|
$from = $node->getAttribute('from'); |
51
|
1 |
|
$length = $node->getAttribute('length'); |
52
|
|
|
|
53
|
1 |
|
if ($from === '') { |
54
|
1 |
|
$from = null; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
if ($length === '') { |
58
|
1 |
|
$length = null; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
$source = ''; |
|
|
|
|
62
|
|
|
|
63
|
1 |
|
if ('' === $file) { |
64
|
1 |
|
$source = $node->nodeValue; |
65
|
1 |
|
} else { |
66
|
|
|
$resourceBasePath = $this->renderer |
|
|
|
|
67
|
|
|
->getConfig()->getResourceBasePath(); |
68
|
|
|
$file = $resourceBasePath . '/' . $file; |
69
|
|
|
|
70
|
|
|
$lines = file($file, FILE_IGNORE_NEW_LINES); |
71
|
|
|
if ($from === null && $length === null) { |
72
|
|
|
$from = 1; |
73
|
|
|
$length = count($lines); |
74
|
|
|
} elseif ($from !== null && $length === null) { |
75
|
|
|
$length = count($lines) - $from + 1; |
76
|
|
|
} elseif ($from === null && $length !== null) { |
77
|
|
|
if (isset($this->positionCache[$file])) { |
78
|
|
|
$from = $this->positionCache[$file]; |
79
|
|
|
} else { |
80
|
|
|
$from = 1; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$source = implode("\n", array_splice($lines, $from - 1, $length)); |
85
|
|
|
|
86
|
|
|
$this->positionCache[$file] = $from + $length; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Smart trim code |
90
|
1 |
|
$source = preg_replace('/(?:\s*\n)?(.*)$/s', '$1', $source); |
91
|
1 |
|
$source = rtrim($source); |
92
|
|
|
|
93
|
1 |
|
if ($language === 'xsl' || $language === 'xslt') { |
94
|
|
|
$language = 'xml'; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
if ($language !== '') { |
98
|
1 |
|
$source = $this->shHighlight($source, $language); |
99
|
1 |
|
} else { |
100
|
1 |
|
$source = '<pre><span class="preformatted">' . $this->escape($source) . '</span></pre>'; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$s = '<div>'; |
104
|
|
|
|
105
|
1 |
|
if ($caption !== '') { |
106
|
1 |
|
$this->listingCount++; |
107
|
1 |
|
$s .= '<p class="caption">Listing ' . $this->listingCount . ': ' . $caption . '</p>'; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
1 |
|
$s .= $source; |
111
|
|
|
|
112
|
1 |
|
$s .= '</div>'; |
113
|
|
|
|
114
|
1 |
|
$fragment->appendXML($s); |
115
|
|
|
|
116
|
1 |
|
$parent->replaceChild($fragment, $node); |
117
|
1 |
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @param string $source |
123
|
|
|
* @param string $language |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
1 |
|
private function shHighlight($source, $language) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
/*$this->geshi->set_language($language); |
|
|
|
|
129
|
|
|
$this->geshi->set_source($source); |
130
|
|
|
|
131
|
|
|
$this->geshi->highlight_lines_extra(array(-1));*/ |
132
|
|
|
|
133
|
|
|
//$source = $this->sh->highlight($source, $language); |
|
|
|
|
134
|
|
|
|
135
|
1 |
|
$source = '<pre>' . htmlspecialchars($source, ENT_QUOTES, 'UTF-8') . '</pre>'; |
136
|
|
|
|
137
|
|
|
#$source = $this->geshi->parse_code(); |
|
|
|
|
138
|
|
|
|
139
|
|
|
/** @todo Otherwise, DOMDocument would warn about unknown 'nbsp' entities */ |
140
|
|
|
//$source = str_replace(' ', ' ', $source); |
|
|
|
|
141
|
|
|
|
142
|
|
|
//$source = preg_replace('/^(<pre[^>]*>)(.*)(<\/pre>)$/ms', '$1<code>$2</code>$3', $source); |
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
// Post process Geshi output |
146
|
|
|
// Get stuff between <pre>...</pre> |
147
|
|
|
|
148
|
1 |
|
$preTop = preg_replace('/(<pre[^>]*>).*/s', '\\1', $source); |
149
|
1 |
|
$code = preg_replace('/<pre[^>]*>(.*)<\/pre>/s', '\\1', $source); |
150
|
|
|
|
151
|
1 |
|
$code = explode("\n", $code); |
152
|
1 |
|
$count = count($code); |
153
|
1 |
|
$parsedCode = ''; |
154
|
|
|
|
155
|
1 |
|
for ($i = 0; $i < $count; $i++) { |
156
|
|
|
/** @todo mermshaus Downstream hack */ |
157
|
1 |
|
if (true) { |
|
|
|
|
158
|
1 |
|
$c = 0; |
159
|
1 |
|
if ($code[$i] === ' ') { |
160
|
|
|
// Empty line |
161
|
|
|
$c = 6; |
|
|
|
|
162
|
|
|
} else { |
163
|
1 |
|
while (substr($code[$i], $c, 1) === ' ') { |
164
|
1 |
|
$c++; |
165
|
1 |
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/*$code[$i] = substr($code[$i], 0, $c) |
|
|
|
|
169
|
|
|
. '<span style="display: block; background: #eee;" id="' . $geshi->overall_id . '-' . $i . '">' |
170
|
|
|
. substr($code[$i], $c) |
171
|
|
|
. '</span>';*/ |
172
|
|
|
|
173
|
1 |
|
$class = ''; |
|
|
|
|
174
|
1 |
|
if (in_array($i, array(5, 9, 12))) { |
175
|
1 |
|
$class = 'line hl'; |
176
|
1 |
|
} else { |
177
|
1 |
|
$class = 'line'; |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
$code[$i] = '<span class="' . $class . '" id="hic-svnt-dracones">' |
181
|
1 |
|
. $code[$i] |
182
|
1 |
|
. '</span>'; |
183
|
|
|
|
184
|
|
|
/*$code[$i] = '<span class="'.$class.'" id="' . $geshi->overall_id . '-' . $i . '">' |
|
|
|
|
185
|
|
|
. $code[$i] |
186
|
|
|
. '</span>';*/ |
187
|
1 |
|
} |
188
|
|
|
|
189
|
|
|
//$code[$i] = preg_replace_callback('/>(.*?)</s', function ($matches) { return '>' |
|
|
|
|
190
|
|
|
//. str_replace(' ', ' <wbr/>', $matches[1]) . '<'; }, $code[$i]); |
|
|
|
|
191
|
|
|
|
192
|
1 |
|
$parsedCode .= $code[$i] . "\n"; |
193
|
1 |
|
} |
194
|
|
|
|
195
|
1 |
|
$parsedCode = $preTop . '<code>' . rtrim($parsedCode) . '</code></pre>'; |
196
|
|
|
|
197
|
1 |
|
return $parsedCode; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.