forked from cakephp/debug_kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTidyHelper.php
More file actions
183 lines (167 loc) · 4.56 KB
/
Copy pathTidyHelper.php
File metadata and controls
183 lines (167 loc) · 4.56 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
App::uses('File', 'Utility');
/**
* Tidy helper - passes html through tidy on the command line, and reports markup errors
*
* PHP version 4 and 5
*
* Copyright (c) 2009, Andy Dawson
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2009, Andy Dawson
* @link www.ad7six.com
* @package debug_kit
* @subpackage debug_kit.views.helpers
* @since v 1.0 (22-Jun-2009)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* TidyHelper class
*
* @uses AppHelper
* @package debug_kit
* @subpackage debug_kit.views.helpers
*/
class TidyHelper extends AppHelper {
/**
* helpers property
*
* @var array
* @access public
*/
public $helpers = array('DebugKit.Toolbar');
/**
* results property
*
* @var mixed null
* @access public
*/
public $results = null;
/**
* Return a nested array of errors for the passed html string
* Fudge the markup slightly so that the tag which is invalid is highlighted
*
* @param string $html ''
* @param string $out ''
* @return array
* @access public
*/
public function process($html = '', &$out = '') {
$errors = $this->tidyErrors($html, $out);
if (!$errors) {
return;
}
$result = array('Error' => array(), 'Warning' => array(), 'Misc' => array());
$errors = explode("\n", $errors);
$markup = explode("\n", $out);
foreach ($errors as $error) {
preg_match('@line (\d+) column (\d+) - (\w+): (.*)@', $error, $matches);
if ($matches) {
list($original, $line, $column, $type, $message) = $matches;
$line = $line - 1;
$string = '</strong>';
if (isset($markup[$line - 1])) {
$string .= h($markup[$line - 1]);
}
$string .= '<strong>' . h(@$markup[$line]) . '</strong>';
if (isset($markup[$line + 1])) {
$string .= h($markup[$line + 1]);
}
$string .= '</strong>';
$result[$type][$string][] = h($message);
} elseif ($error) {
$message = $error;
$result['Misc'][h($message)][] = h($message);
}
}
$this->results = $result;
return $result;
}
/**
* report method
*
* Call process if a string is passed, or no prior results exist - and return the results using
* the toolbar helper to generate a nested navigatable array
*
* @param mixed $html null
* @return string
* @access public
*/
public function report($html = null) {
if ($html) {
$this->process($html);
} elseif ($this->results === null) {
$this->process($this->_View->output);
}
if (!$this->results) {
return '<p>' . __d('debug_kit', 'No markup errors found') . '</p>';
}
foreach ($this->results as &$results) {
foreach ($results as $type => &$messages) {
foreach ($messages as &$message) {
$message = html_entity_decode($message, ENT_COMPAT, Configure::read('App.encoding'));
}
}
}
return $this->Toolbar->makeNeatArray(array_filter($this->results), 0, 0, false);
}
/**
* Run the html string through tidy, and return the (raw) errors. pass back a reference to the
* normalized string so that the error messages can be linked to the line that caused them.
*
* @param string $in ''
* @param string $out ''
* @return string
* @access public
*/
public function tidyErrors($in = '', &$out = '') {
$out = preg_replace('@>\s*<@s', ">\n<", $in);
// direct access? windows etc
if (function_exists('tidy_parse_string')) {
$tidy = tidy_parse_string($out, array(), 'UTF8');
$tidy->cleanRepair();
$errors = $tidy->errorBuffer . "\n";
return $errors;
}
// cli
$File = new File(rtrim(TMP, DS) . DS . rand() . '.html', true);
$File->write($out);
$path = $File->pwd();
$errors = $path . '.err';
$this->_exec("tidy -eq -utf8 -f $errors $path");
$File->delete();
if (!file_exists($errors)) {
return;
}
$Error = new File($errors);
$errors = $Error->read();
$Error->delete();
return $errors;
}
/**
* exec method
*
* @param mixed $cmd
* @param mixed $out null
* @return void
* @access protected
*/
protected function _exec($cmd, &$out = null) {
if (DS === '/') {
$_out = exec($cmd . ' 2>&1', $out, $return);
} else {
$_out = exec($cmd, $out, $return);
}
if (Configure::read('debug')) {
$source = Debugger::trace(array('depth' => 1, 'start' => 2)) . "\n";
//CakeLog::write('system_calls_' . date('Y-m-d'), "\n" . $source . Debugger::exportVar(compact('cmd','out','return')));
//CakeLog::write('system_calls', "\n" . $source . Debugger::exportVar(compact('cmd','out','return')));
}
if ($return) {
return false;
}
return $_out ? $_out : true;
}
}