PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pear/PHP/CodeSniffer/DocGenerators/HTML.php

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