Skip to content

Commit

Permalink
Preserve whitespace as it was first specified.
Browse files Browse the repository at this point in the history
Prevents Textile from collapsing white-space inside
code blocks and removes added extra hard-coded
newlines that only work because they are inside
inline HTML elements, e.g. code.

White-space is trimmed from the end of the text,
because otherwise the last extended block will include the last stranded empty line. This then removes the
need for the trim + hard-coded LF, and allows us
to actually join the blocks with the original separating
whitespace.

This fixes the formatting inside bc, notextile, bq
and pre tags, and/or any other extended blocks.

Closes #111, fixes #109
  • Loading branch information
Jukka Svahn committed Oct 25, 2013
1 parent 3d36dad commit 00e2cbe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
40 changes: 28 additions & 12 deletions src/Netcarver/Textile/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1843,18 +1843,30 @@ protected function blocks($text)
{
$blocktags = join('|', $this->blocktag_whitelist);

$text = explode("\n\n", $text);
$text = preg_split('/(\n{2,})/', $text, null, PREG_SPLIT_DELIM_CAPTURE);

$tag = 'p';
$atts = '';
$cite = '';
$graf = '';
$ext = '';
$eat = false;
$whitespace = '';
$eatWhitespace = false;

$out = array();

foreach ($text as $line) {
foreach ($text as $key => $line) {

// Line is just whitespace, keep it for the next block.
if (trim($line) === '') {
if ($eatWhitespace === false) {
$whitespace .= $line;
}
continue;
}

$eatWhitespace = false;
$anon = 0;
if (preg_match("/^($blocktags)($this->a$this->c)\.(\.?)(?::(\S+))? (.*)$/Ss", $line, $m)) {
// Last block was extended, so close it
Expand Down Expand Up @@ -1889,14 +1901,20 @@ protected function blocks($text)
}

$line = $this->doPBr($line);
$line = preg_replace('/<br>/', '<br />', $line);
$line = $whitespace . preg_replace('/<br>/', '<br />', $line);

if ($ext && $anon) {
$out[count($out)-1] .= "\n".$line;
$out[count($out)-1] .= $line;
} elseif (!$eat) {
$out[] = $line;
}

if ($eat) {
$eatWhitespace = true;
} else {
$whitespace = '';
}

if (!$ext) {
$tag = 'p';
$atts = '';
Expand All @@ -1910,7 +1928,7 @@ protected function blocks($text)
$out[count($out)-1] .= $c1;
}

return join("\n\n", $out);
return join('', $out);
}

/**
Expand Down Expand Up @@ -1985,15 +2003,15 @@ protected function fBlock($m)
$o2 = "<code>";
$c2 = "</code>";
$c1 = "</pre>";
$content = $this->shelve($this->rEncodeHTML(rtrim($content, "\n")."\n"));
$content = $this->shelve($this->rEncodeHTML($content));
} elseif ($tag == 'notextile') {
$content = $this->shelve($content);
$o1 = '';
$o2 = '';
$c1 = '';
$c2 = '';
} elseif ($tag == 'pre') {
$content = $this->shelve($this->rEncodeHTML(rtrim($content, "\n")."\n"));
$content = $this->shelve($this->rEncodeHTML($content));
$o1 = "<pre$atts>";
$o2 = '';
$c2 = '';
Expand Down Expand Up @@ -2857,12 +2875,10 @@ protected function cleanWhiteSpace($text)
$out = preg_replace("/^\xEF\xBB\xBF|\x1A/", '', $text);
// Replaces CRLF and CR with single LF.
$out = preg_replace("/\r\n?/", "\n", $out);
// Replaces line endings containing only whitespace with single LF.
// Removes leading tabs and spaces, if the line is otherwise empty.
$out = preg_replace("/^[ \t]*\n/m", "\n", $out);
// Trim three or more LFs down to 2.
$out = preg_replace("/\n{3,}/", "\n\n", $out);
// Removes leading blank lines.
$out = preg_replace("/^\n*/", "", $out);
// Removes leading and ending blank lines.
$out = trim($out, "\n");
return $out;
}

Expand Down
36 changes: 28 additions & 8 deletions test/fixtures/basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,7 @@ HTML Comments :
Comment
--></p>
<pre><code>&lt;!-- Here is a comment block in a code block. --&gt;
</code></pre>
<pre><code>&lt;!-- Here is a comment block in a code block. --&gt;</code></pre>
URLs as non-links :
notes : Straight URL text sequences should not be converted to a hyperlink.
Expand Down Expand Up @@ -1329,10 +1328,21 @@ Code blocks, pre and exclusions :
<txp:image_info type="caption" />
</txp:category>
bc.. Code block
with three empty lines
p(regular#somestuff). _Aaaand_, *__relax__*.
bc.. Paragraph 1
Paragraph 2
Paragraph 3
expect : |
<pre class="input"><code>You can&#39;t _*touch*_ *_this_*... Hammer time.
</code></pre>
<pre class="input"><code>You can&#39;t _*touch*_ *_this_*... Hammer time.</code></pre>
<p>And keep your <code>(expect)mitts _*off*_ *_me_*</code> as well. About the <code>&lt;hr /&gt;</code> tag.</p>
Expand All @@ -1342,11 +1352,22 @@ Code blocks, pre and exclusions :
&lt;txp:image class=&quot;prawns&quot; something=&quot;summat else&quot; /&gt;
&lt;br /&gt;
&lt;txp:image_info type=&quot;caption&quot; /&gt;
&lt;/txp:category&gt;
</code></pre>
&lt;/txp:category&gt;</code></pre>
<pre><code>Code block</code>
<code>with three empty lines</code></pre>
<p class="regular" id="somestuff"><em>Aaaand</em>, <strong><i>relax</i></strong>.</p>
<pre><code>Paragraph 1</code>
<code>Paragraph 2</code>
<code>Paragraph 3</code></pre>
Basic span modifiers & glyphs:
input: |
This is the "start" and 'end'; and I'm stuck in '88!: With smd_g4llery's attributes where hëll_won't__fr33ze_over if I use apostrophes inside odd words and_underscores (Pah).
Expand Down Expand Up @@ -1438,8 +1459,7 @@ Make sure these don't get turned into lists... :
; Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0
; Expires: Sat, 24 Jul 2003 05:00:00 GMT
; Last-Modified: Wed, 1 Jan 2025 05:00:00 GMT
; Pragma: no-cache
</code></pre>
; Pragma: no-cache</code></pre>
<p><strong>123 test</strong></p>
Expand Down

0 comments on commit 00e2cbe

Please # to comment.