comparison vendor/squizlabs/php_codesniffer/src/Generators/Markdown.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php
2 /**
3 * A doc generator that outputs documentation in Markdown format.
4 *
5 * @author Stefano Kowalke <blueduck@gmx.net>
6 * @copyright 2014 Arroba IT
7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8 */
9
10 namespace PHP_CodeSniffer\Generators;
11
12 use PHP_CodeSniffer\Config;
13
14 class Markdown extends Generator
15 {
16
17
18 /**
19 * Generates the documentation for a standard.
20 *
21 * @return void
22 * @see processSniff()
23 */
24 public function generate()
25 {
26 ob_start();
27 $this->printHeader();
28
29 foreach ($this->docFiles as $file) {
30 $doc = new \DOMDocument();
31 $doc->load($file);
32 $documentation = $doc->getElementsByTagName('documentation')->item(0);
33 $this->processSniff($documentation);
34 }
35
36 $this->printFooter();
37 $content = ob_get_contents();
38 ob_end_clean();
39
40 echo $content;
41
42 }//end generate()
43
44
45 /**
46 * Print the markdown header.
47 *
48 * @return void
49 */
50 protected function printHeader()
51 {
52 $standard = $this->ruleset->name;
53
54 echo "# $standard Coding Standard".PHP_EOL;
55
56 }//end printHeader()
57
58
59 /**
60 * Print the markdown footer.
61 *
62 * @return void
63 */
64 protected function printFooter()
65 {
66 // Turn off errors so we don't get timezone warnings if people
67 // don't have their timezone set.
68 error_reporting(0);
69 echo 'Documentation generated on '.date('r');
70 echo ' by [PHP_CodeSniffer '.Config::VERSION.'](https://github.com/squizlabs/PHP_CodeSniffer)'.PHP_EOL;
71
72 }//end printFooter()
73
74
75 /**
76 * Process the documentation for a single sniff.
77 *
78 * @param \DOMNode $doc The DOMNode object for the sniff.
79 * It represents the "documentation" tag in the XML
80 * standard file.
81 *
82 * @return void
83 */
84 protected function processSniff(\DOMNode $doc)
85 {
86 $title = $this->getTitle($doc);
87 echo "## $title".PHP_EOL;
88
89 foreach ($doc->childNodes as $node) {
90 if ($node->nodeName === 'standard') {
91 $this->printTextBlock($node);
92 } else if ($node->nodeName === 'code_comparison') {
93 $this->printCodeComparisonBlock($node);
94 }
95 }
96
97 }//end processSniff()
98
99
100 /**
101 * Print a text block found in a standard.
102 *
103 * @param \DOMNode $node The DOMNode object for the text block.
104 *
105 * @return void
106 */
107 protected function printTextBlock(\DOMNode $node)
108 {
109 $content = trim($node->nodeValue);
110 $content = htmlspecialchars($content);
111
112 $content = str_replace('&lt;em&gt;', '*', $content);
113 $content = str_replace('&lt;/em&gt;', '*', $content);
114
115 echo $content.PHP_EOL;
116
117 }//end printTextBlock()
118
119
120 /**
121 * Print a code comparison block found in a standard.
122 *
123 * @param \DOMNode $node The DOMNode object for the code comparison block.
124 *
125 * @return void
126 */
127 protected function printCodeComparisonBlock(\DOMNode $node)
128 {
129 $codeBlocks = $node->getElementsByTagName('code');
130
131 $firstTitle = $codeBlocks->item(0)->getAttribute('title');
132 $first = trim($codeBlocks->item(0)->nodeValue);
133 $first = str_replace("\n", "\n ", $first);
134 $first = str_replace('<em>', '', $first);
135 $first = str_replace('</em>', '', $first);
136
137 $secondTitle = $codeBlocks->item(1)->getAttribute('title');
138 $second = trim($codeBlocks->item(1)->nodeValue);
139 $second = str_replace("\n", "\n ", $second);
140 $second = str_replace('<em>', '', $second);
141 $second = str_replace('</em>', '', $second);
142
143 echo ' <table>'.PHP_EOL;
144 echo ' <tr>'.PHP_EOL;
145 echo " <th>$firstTitle</th>".PHP_EOL;
146 echo " <th>$secondTitle</th>".PHP_EOL;
147 echo ' </tr>'.PHP_EOL;
148 echo ' <tr>'.PHP_EOL;
149 echo '<td>'.PHP_EOL.PHP_EOL;
150 echo " $first".PHP_EOL.PHP_EOL;
151 echo '</td>'.PHP_EOL;
152 echo '<td>'.PHP_EOL.PHP_EOL;
153 echo " $second".PHP_EOL.PHP_EOL;
154 echo '</td>'.PHP_EOL;
155 echo ' </tr>'.PHP_EOL;
156 echo ' </table>'.PHP_EOL;
157
158 }//end printCodeComparisonBlock()
159
160
161 }//end class