* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ namespace PHP_CodeSniffer\Generators; use PHP_CodeSniffer\Config; class HTML extends Generator { /** * Generates the documentation for a standard. * * @return void * @see processSniff() */ public function generate() { ob_start(); $this->printHeader(); $this->printToc(); foreach ($this->docFiles as $file) { $doc = new \DOMDocument(); $doc->load($file); $documentation = $doc->getElementsByTagName('documentation')->item(0); $this->processSniff($documentation); } $this->printFooter(); $content = ob_get_contents(); ob_end_clean(); echo $content; }//end generate() /** * Print the header of the HTML page. * * @return void */ protected function printHeader() { $standard = $this->ruleset->name; echo ''.PHP_EOL; echo ' '.PHP_EOL; echo " $standard Coding Standards".PHP_EOL; echo ' '.PHP_EOL; echo ' '.PHP_EOL; echo ' '.PHP_EOL; echo "

$standard Coding Standards

".PHP_EOL; }//end printHeader() /** * Print the table of contents for the standard. * * The TOC is just an unordered list of bookmarks to sniffs on the page. * * @return void */ protected function printToc() { echo '

Table of Contents

'.PHP_EOL; echo ' '.PHP_EOL; }//end printToc() /** * Print the footer of the HTML page. * * @return void */ protected function printFooter() { // Turn off errors so we don't get timezone warnings if people // don't have their timezone set. $errorLevel = error_reporting(0); echo '
'; echo 'Documentation generated on '.date('r'); echo ' by PHP_CodeSniffer '.Config::VERSION.''; echo '
'.PHP_EOL; error_reporting($errorLevel); echo ' '.PHP_EOL; echo ''.PHP_EOL; }//end printFooter() /** * Process the documentation for a single sniff. * * @param \DOMNode $doc The DOMNode object for the sniff. * It represents the "documentation" tag in the XML * standard file. * * @return void */ public function processSniff(\DOMNode $doc) { $title = $this->getTitle($doc); echo ' '.PHP_EOL; echo "

$title

".PHP_EOL; foreach ($doc->childNodes as $node) { if ($node->nodeName === 'standard') { $this->printTextBlock($node); } else if ($node->nodeName === 'code_comparison') { $this->printCodeComparisonBlock($node); } } }//end processSniff() /** * Print a text block found in a standard. * * @param \DOMNode $node The DOMNode object for the text block. * * @return void */ protected function printTextBlock(\DOMNode $node) { $content = trim($node->nodeValue); $content = htmlspecialchars($content); // Allow em tags only. $content = str_replace('<em>', '', $content); $content = str_replace('</em>', '', $content); echo "

$content

".PHP_EOL; }//end printTextBlock() /** * Print a code comparison block found in a standard. * * @param \DOMNode $node The DOMNode object for the code comparison block. * * @return void */ protected function printCodeComparisonBlock(\DOMNode $node) { $codeBlocks = $node->getElementsByTagName('code'); $firstTitle = $codeBlocks->item(0)->getAttribute('title'); $first = trim($codeBlocks->item(0)->nodeValue); $first = str_replace('', $first); $first = str_replace(' ', ' ', $first); $first = str_replace('', '', $first); $first = str_replace('', '', $first); $secondTitle = $codeBlocks->item(1)->getAttribute('title'); $second = trim($codeBlocks->item(1)->nodeValue); $second = str_replace('', $second); $second = str_replace(' ', ' ', $second); $second = str_replace('', '', $second); $second = str_replace('', '', $second); echo ' '.PHP_EOL; echo ' '.PHP_EOL; echo " ".PHP_EOL; echo " ".PHP_EOL; echo ' '.PHP_EOL; echo ' '.PHP_EOL; echo " ".PHP_EOL; echo " ".PHP_EOL; echo ' '.PHP_EOL; echo '
$firstTitle$secondTitle
$first$second
'.PHP_EOL; }//end printCodeComparisonBlock() }//end class