Skip to content

Commit ca4081c

Browse files
committed
Generators/Markdown: fix line break and indentation handling
As things were, line breaks in a `<standard>` block were not respected for proper display in markdown. As a side-effect of this, an indented text line in a `<standard>` block directly after a blank line, would unintentionally cause the markdown to display the line as a code block (via the indentation). This has now been fixed by: * Trimming indentation off each line. * Adding two spaces to the end of each line as long as the line is not followed by a blank line or at the end of the standard block. The two spaces will ensure markdown will recognize the line break (in most markdown flavours). Includes updated test expectations. Refs: * https://www.markdownguide.org/basic-syntax/#paragraph-best-practices * https://www.markdownguide.org/basic-syntax/#line-break-best-practices * https://daringfireball.net/projects/markdown/syntax#p
1 parent 941a00e commit ca4081c

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

src/Generators/Markdown.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,34 @@ protected function printTextBlock(DOMNode $node)
117117
{
118118
$content = trim($node->nodeValue);
119119
$content = htmlspecialchars($content, (ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401));
120-
121-
// Use the correct line endings based on the OS.
122-
$content = str_replace("\n", PHP_EOL, $content);
123-
124120
$content = str_replace('&lt;em&gt;', '*', $content);
125121
$content = str_replace('&lt;/em&gt;', '*', $content);
126122

127-
echo $content.PHP_EOL;
123+
$nodeLines = explode("\n", $content);
124+
$lineCount = count($nodeLines);
125+
$lines = [];
126+
127+
for ($i = 0; $i < $lineCount; $i++) {
128+
$currentLine = trim($nodeLines[$i]);
129+
if ($currentLine === '') {
130+
// The text contained a blank line. Respect this.
131+
$lines[] = '';
132+
continue;
133+
}
134+
135+
// Check if the _next_ line is blank.
136+
if (isset($nodeLines[($i + 1)]) === false
137+
|| trim($nodeLines[($i + 1)]) === ''
138+
) {
139+
// Next line is blank, just add the line.
140+
$lines[] = $currentLine;
141+
} else {
142+
// Ensure that line breaks are respected in markdown.
143+
$lines[] = $currentLine.' ';
144+
}
145+
}
146+
147+
echo implode(PHP_EOL, $lines).PHP_EOL;
128148

129149
}//end printTextBlock()
130150

tests/Core/Generators/Expectations/ExpectedOutputCodeComparisonLineLength.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Code Comparison, line length
44

5-
Ensure there is no PHP &quot;Warning: str_repeat(): Second argument has to be greater than or equal to 0&quot;.
6-
Ref: squizlabs/PHP_CodeSniffer#2522
5+
Ensure there is no PHP &quot;Warning: str_repeat(): Second argument has to be greater than or equal to 0&quot;.
6+
Ref: squizlabs/PHP_CodeSniffer#2522
77
<table>
88
<tr>
99
<th>Valid: contains line which is too long.</th>

tests/Core/Generators/Expectations/ExpectedOutputStandardBlankLines.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
There is a blank line at the start of this standard.
66

7-
And the above blank line is also deliberate to test part of the logic.
7+
And the above blank line is also deliberate to test part of the logic.
88

9-
Let&#039;s also end on a blank line to test that too.
9+
Let&#039;s also end on a blank line to test that too.
1010

1111
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)

tests/Core/Generators/Expectations/ExpectedOutputStandardEncoding.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Standard Element, handling of HTML tags
44

5-
The use of *tags* in standard descriptions is allowed and their handling should be *safeguarded*.
6-
Other tags, like &lt;a href=&quot;example.com&quot;&gt;link&lt;/a&gt;, &lt;b&gt;bold&lt;/bold&gt;, &lt;script&gt;&lt;/script&gt; are not allowed and will be encoded for display when the HTML or Markdown report is used.
5+
The use of *tags* in standard descriptions is allowed and their handling should be *safeguarded*.
6+
Other tags, like &lt;a href=&quot;example.com&quot;&gt;link&lt;/a&gt;, &lt;b&gt;bold&lt;/bold&gt;, &lt;script&gt;&lt;/script&gt; are not allowed and will be encoded for display when the HTML or Markdown report is used.
77

88
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)

tests/Core/Generators/Expectations/ExpectedOutputStandardIndent.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## Standard Element, indentation should be ignored
44

5-
This line has no indentation.
6-
This line has 4 spaces indentation.
7-
This line has 8 spaces indentation.
8-
This line has 4 spaces indentation.
5+
This line has no indentation.
6+
This line has 4 spaces indentation.
7+
This line has 8 spaces indentation.
8+
This line has 4 spaces indentation.
99

1010
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)

tests/Core/Generators/Expectations/ExpectedOutputStandardLineWrapping.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Standard Element, line wrapping handling
44

5-
This line has to be exactly 99 chars to test part of the logic.------------------------------------
6-
And this line has to be exactly 100 chars.----------------------------------------------------------
7-
And here we have a line which should start wrapping as it is longer than 100 chars. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean pellentesque iaculis enim quis hendrerit. Morbi ultrices in odio pharetra commodo.
5+
This line has to be exactly 99 chars to test part of the logic.------------------------------------
6+
And this line has to be exactly 100 chars.----------------------------------------------------------
7+
And here we have a line which should start wrapping as it is longer than 100 chars. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean pellentesque iaculis enim quis hendrerit. Morbi ultrices in odio pharetra commodo.
88

99
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)

0 commit comments

Comments
 (0)