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

/CodeSniffer/DocGenerators/HTML.php

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