5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* A doc generator that outputs documentation in one big HTML file.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: HTML.php,v 1.5 2008/12/07 21:40:02 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
require_once 'PHP/CodeSniffer/DocGenerators/Generator.php';
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* A doc generator that outputs documentation in one big HTML file.
|
|
|
21 |
*
|
|
|
22 |
* Output is in one large HTML file and is designed for you to style with
|
|
|
23 |
* your own stylesheet. It contains a table of contents at the top with anchors
|
|
|
24 |
* to each sniff.
|
|
|
25 |
*
|
|
|
26 |
* @category PHP
|
|
|
27 |
* @package PHP_CodeSniffer
|
|
|
28 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
29 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
30 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
31 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
32 |
* @version Release: 1.2.0RC1
|
|
|
33 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
34 |
*/
|
|
|
35 |
class PHP_CodeSniffer_DocGenerators_HTML extends PHP_CodeSniffer_DocGenerators_Generator
|
|
|
36 |
{
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Generates the documentation for a standard.
|
|
|
41 |
*
|
|
|
42 |
* @return void
|
|
|
43 |
* @see processSniff()
|
|
|
44 |
*/
|
|
|
45 |
public function generate()
|
|
|
46 |
{
|
|
|
47 |
ob_start();
|
|
|
48 |
$this->printHeader();
|
|
|
49 |
|
|
|
50 |
$standardFiles = $this->getStandardFiles();
|
|
|
51 |
$this->printToc($standardFiles);
|
|
|
52 |
|
|
|
53 |
foreach ($standardFiles as $standard) {
|
|
|
54 |
$doc = new DOMDocument();
|
|
|
55 |
$doc->load($standard);
|
|
|
56 |
$documentation = $doc->getElementsByTagName('documentation')->item(0);
|
|
|
57 |
$this->processSniff($documentation);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$this->printFooter();
|
|
|
61 |
|
|
|
62 |
$content = ob_get_contents();
|
|
|
63 |
ob_end_clean();
|
|
|
64 |
|
|
|
65 |
echo $content;
|
|
|
66 |
|
|
|
67 |
}//end generate()
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Print the header of the HTML page.
|
|
|
72 |
*
|
|
|
73 |
* @return void
|
|
|
74 |
*/
|
|
|
75 |
protected function printHeader()
|
|
|
76 |
{
|
|
|
77 |
$standard = $this->getStandard();
|
|
|
78 |
echo '<html>'.PHP_EOL;
|
|
|
79 |
echo ' <head>'.PHP_EOL;
|
|
|
80 |
echo " <title>$standard Coding Standards</title>".PHP_EOL;
|
|
|
81 |
echo ' <style>
|
|
|
82 |
body {
|
|
|
83 |
background-color: #FFFFFF;
|
|
|
84 |
font-size: 14px;
|
|
|
85 |
font-family: Arial, Helvetica, sans-serif;
|
|
|
86 |
color: #000000;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
h1 {
|
|
|
90 |
color: #666666;
|
|
|
91 |
font-size: 20px;
|
|
|
92 |
font-weight: bold;
|
|
|
93 |
margin-top: 0px;
|
|
|
94 |
background-color: #E6E7E8;
|
|
|
95 |
padding: 20px;
|
|
|
96 |
border: 1px solid #BBBBBB;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
h2 {
|
|
|
100 |
color: #00A5E3;
|
|
|
101 |
font-size: 16px;
|
|
|
102 |
font-weight: normal;
|
|
|
103 |
margin-top: 50px;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
.code-comparison {
|
|
|
107 |
width: 100%;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
.code-comparison td {
|
|
|
111 |
border: 1px solid #CCCCCC;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
.code-comparison-title, .code-comparison-code {
|
|
|
115 |
font-family: Arial, Helvetica, sans-serif;
|
|
|
116 |
font-size: 12px;
|
|
|
117 |
color: #000000;
|
|
|
118 |
vertical-align: top;
|
|
|
119 |
padding: 4px;
|
|
|
120 |
width: 50%;
|
|
|
121 |
background-color: #F1F1F1;
|
|
|
122 |
line-height: 15px;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
.code-comparison-code {
|
|
|
126 |
font-family: Courier;
|
|
|
127 |
background-color: #F9F9F9;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
.code-comparison-highlight {
|
|
|
131 |
background-color: #DDF1F7;
|
|
|
132 |
border: 1px solid #00A5E3;
|
|
|
133 |
line-height: 15px;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
.tag-line {
|
|
|
137 |
text-align: center;
|
|
|
138 |
width: 100%;
|
|
|
139 |
margin-top: 30px;
|
|
|
140 |
font-size: 12px;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
.tag-line a {
|
|
|
144 |
color: #000000;
|
|
|
145 |
}
|
|
|
146 |
</style>'.PHP_EOL;
|
|
|
147 |
echo ' </head>'.PHP_EOL;
|
|
|
148 |
echo ' <body>'.PHP_EOL;
|
|
|
149 |
echo " <h1>$standard Coding Standards</h1>".PHP_EOL;
|
|
|
150 |
|
|
|
151 |
}//end printHeader()
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Print the table of contents for the standard.
|
|
|
156 |
*
|
|
|
157 |
* The TOC is just an unordered list of bookmarks to sniffs on the page.
|
|
|
158 |
*
|
|
|
159 |
* @param array $standardFiles An array of paths to the XML standard files.
|
|
|
160 |
*
|
|
|
161 |
* @return void
|
|
|
162 |
*/
|
|
|
163 |
protected function printToc($standardFiles)
|
|
|
164 |
{
|
|
|
165 |
echo ' <h2>Table of Contents</h2>'.PHP_EOL;
|
|
|
166 |
echo ' <ul class="toc">'.PHP_EOL;
|
|
|
167 |
|
|
|
168 |
foreach ($standardFiles as $standard) {
|
|
|
169 |
$doc = new DOMDocument();
|
|
|
170 |
$doc->load($standard);
|
|
|
171 |
$documentation = $doc->getElementsByTagName('documentation')->item(0);
|
|
|
172 |
$title = $this->getTitle($documentation);
|
|
|
173 |
echo ' <li><a href="#'.str_replace(' ', '-', $title)."\">$title</a></li>".PHP_EOL;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
echo ' </ul>'.PHP_EOL;
|
|
|
177 |
|
|
|
178 |
}//end printToc()
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Print the footer of the HTML page.
|
|
|
183 |
*
|
|
|
184 |
* @return void
|
|
|
185 |
*/
|
|
|
186 |
protected function printFooter()
|
|
|
187 |
{
|
|
|
188 |
// Turn off strict errors so we don't get timezone warnings if people
|
|
|
189 |
// don't have their timezone set.
|
|
|
190 |
error_reporting(E_ALL);
|
|
|
191 |
echo ' <div class="tag-line">';
|
|
|
192 |
echo 'Documentation generated on '.date('r');
|
|
|
193 |
echo ' by <a href="http://pear.php.net/package/PHP_CodeSniffer">PHP_CodeSniffer 1.2.0RC1</a>';
|
|
|
194 |
echo '</div>'.PHP_EOL;
|
|
|
195 |
error_reporting(E_ALL | E_STRICT);
|
|
|
196 |
|
|
|
197 |
echo ' </body>'.PHP_EOL;
|
|
|
198 |
echo '</html>'.PHP_EOL;
|
|
|
199 |
|
|
|
200 |
}//end printFooter()
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Process the documentation for a single sniff.
|
|
|
205 |
*
|
|
|
206 |
* @param DOMNode $doc The DOMNode object for the sniff.
|
|
|
207 |
* It represents the "documentation" tag in the XML
|
|
|
208 |
* standard file.
|
|
|
209 |
*
|
|
|
210 |
* @return void
|
|
|
211 |
*/
|
|
|
212 |
public function processSniff(DOMNode $doc)
|
|
|
213 |
{
|
|
|
214 |
$title = $this->getTitle($doc);
|
|
|
215 |
echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL;
|
|
|
216 |
echo " <h2>$title</h2>".PHP_EOL;
|
|
|
217 |
|
|
|
218 |
foreach ($doc->childNodes as $node) {
|
|
|
219 |
if ($node->nodeName === 'standard') {
|
|
|
220 |
$this->printTextBlock($node);
|
|
|
221 |
} else if ($node->nodeName === 'code_comparison') {
|
|
|
222 |
$this->printCodeComparisonBlock($node);
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
}//end processSniff()
|
|
|
227 |
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Print a text block found in a standard.
|
|
|
231 |
*
|
|
|
232 |
* @param DOMNode $node The DOMNode object for the text block.
|
|
|
233 |
*
|
|
|
234 |
* @return void
|
|
|
235 |
*/
|
|
|
236 |
protected function printTextBlock($node)
|
|
|
237 |
{
|
|
|
238 |
$content = trim($node->nodeValue);
|
|
|
239 |
$content = htmlspecialchars($content);
|
|
|
240 |
|
|
|
241 |
// Allow em tags only.
|
|
|
242 |
$content = str_replace('<em>', '<em>', $content);
|
|
|
243 |
$content = str_replace('</em>', '</em>', $content);
|
|
|
244 |
|
|
|
245 |
echo " <p class=\"text\">$content</p>".PHP_EOL;
|
|
|
246 |
|
|
|
247 |
}//end printTextBlock()
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Print a code comparison block found in a standard.
|
|
|
252 |
*
|
|
|
253 |
* @param DOMNode $node The DOMNode object for the code comparison block.
|
|
|
254 |
*
|
|
|
255 |
* @return void
|
|
|
256 |
*/
|
|
|
257 |
protected function printCodeComparisonBlock($node)
|
|
|
258 |
{
|
|
|
259 |
$codeBlocks = $node->getElementsByTagName('code');
|
|
|
260 |
|
|
|
261 |
$firstTitle = $codeBlocks->item(0)->getAttribute('title');
|
|
|
262 |
$first = trim($codeBlocks->item(0)->nodeValue);
|
|
|
263 |
$first = str_replace("\n", '</br>', $first);
|
|
|
264 |
$first = str_replace(' ', ' ', $first);
|
|
|
265 |
$first = str_replace('<em>', '<span class="code-comparison-highlight">', $first);
|
|
|
266 |
$first = str_replace('</em>', '</span>', $first);
|
|
|
267 |
|
|
|
268 |
$secondTitle = $codeBlocks->item(1)->getAttribute('title');
|
|
|
269 |
$second = trim($codeBlocks->item(1)->nodeValue);
|
|
|
270 |
$second = str_replace("\n", '</br>', $second);
|
|
|
271 |
$second = str_replace(' ', ' ', $second);
|
|
|
272 |
$second = str_replace('<em>', '<span class="code-comparison-highlight">', $second);
|
|
|
273 |
$second = str_replace('</em>', '</span>', $second);
|
|
|
274 |
|
|
|
275 |
echo ' <table class="code-comparison">'.PHP_EOL;
|
|
|
276 |
echo ' <tr>'.PHP_EOL;
|
|
|
277 |
echo " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
|
|
|
278 |
echo " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
|
|
|
279 |
echo ' </tr>'.PHP_EOL;
|
|
|
280 |
echo ' <tr>'.PHP_EOL;
|
|
|
281 |
echo " <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
|
|
|
282 |
echo " <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
|
|
|
283 |
echo ' </tr>'.PHP_EOL;
|
|
|
284 |
echo ' </table>'.PHP_EOL;
|
|
|
285 |
|
|
|
286 |
}//end printCodeComparisonBlock()
|
|
|
287 |
|
|
|
288 |
|
|
|
289 |
}//end class
|
|
|
290 |
|
|
|
291 |
?>
|