diff --git a/src/Components/Blocks/FencedCode.php b/src/Components/Blocks/FencedCode.php index 74b7608..24bad21 100644 --- a/src/Components/Blocks/FencedCode.php +++ b/src/Components/Blocks/FencedCode.php @@ -85,19 +85,15 @@ final class FencedCode implements ContinuableBlock $newCode = $this->code; - if ($Context->previousEmptyLines() > 0) { - $newCode .= \str_repeat("\n", $Context->previousEmptyLines()); - } + $newCode .= $Context->previousEmptyLinesText(); if (($len = \strspn($Context->line()->text(), $this->marker)) >= $this->openerLength && \chop(\substr($Context->line()->text(), $len), ' ') === '' ) { - $newCode = \substr($newCode, 1); - return new self($newCode, $this->infostring, $this->marker, $this->openerLength, true); } - $newCode .= "\n" . $Context->line()->rawLine(); + $newCode .= $Context->line()->rawLine() . "\n"; return new self($newCode, $this->infostring, $this->marker, $this->openerLength, false); } diff --git a/src/Components/Blocks/IndentedCode.php b/src/Components/Blocks/IndentedCode.php index 53d426a..22850a9 100644 --- a/src/Components/Blocks/IndentedCode.php +++ b/src/Components/Blocks/IndentedCode.php @@ -8,6 +8,7 @@ use Erusev\Parsedown\Components\ContinuableBlock; use Erusev\Parsedown\Html\Renderables\Element; use Erusev\Parsedown\Html\Renderables\Text; use Erusev\Parsedown\Parsing\Context; +use Erusev\Parsedown\Parsing\Line; use Erusev\Parsedown\State; final class IndentedCode implements ContinuableBlock @@ -44,7 +45,7 @@ final class IndentedCode implements ContinuableBlock return null; } - return new self($Context->line()->ltrimBodyUpto(4)); + return new self($Context->line()->ltrimBodyUpto(4) . "\n"); } /** @@ -59,11 +60,19 @@ final class IndentedCode implements ContinuableBlock $newCode = $this->code; + $offset = $Context->line()->indentOffset(); + if ($Context->previousEmptyLines() > 0) { - $newCode .= \str_repeat("\n", $Context->previousEmptyLines()); + foreach (\explode("\n", $Context->previousEmptyLinesText()) as $line) { + $newCode .= (new Line($line, $offset))->ltrimBodyUpto(4) . "\n"; + } + + $newCode = \substr($newCode, 0, -1); } - return new self($newCode . "\n" . $Context->line()->ltrimBodyUpto(4)); + $newCode .= $Context->line()->ltrimBodyUpto(4) . "\n"; + + return new self($newCode); } /** diff --git a/src/Parsing/Context.php b/src/Parsing/Context.php index 9091e2d..a100327 100644 --- a/src/Parsing/Context.php +++ b/src/Parsing/Context.php @@ -4,24 +4,24 @@ namespace Erusev\Parsedown\Parsing; final class Context { - /** - * @var Line - */ + /** @var Line */ private $Line; - /** - * @var int - */ + /** @var int */ private $previousEmptyLines; + /** @var string */ + private $previousEmptyLinesText; + /** * @param Line $Line - * @param int $previousEmptyLines + * @param string $previousEmptyLinesText */ - public function __construct($Line, $previousEmptyLines) + public function __construct($Line, $previousEmptyLinesText) { $this->Line = $Line; - $this->previousEmptyLines = \max($previousEmptyLines, 0); + $this->previousEmptyLinesText = $previousEmptyLinesText; + $this->previousEmptyLines = \substr_count($previousEmptyLinesText, "\n"); } /** @return Line */ @@ -35,4 +35,10 @@ final class Context { return $this->previousEmptyLines; } + + /** @return string */ + public function previousEmptyLinesText() + { + return $this->previousEmptyLinesText; + } } diff --git a/src/Parsing/Lines.php b/src/Parsing/Lines.php index fe92794..2d4a4b4 100644 --- a/src/Parsing/Lines.php +++ b/src/Parsing/Lines.php @@ -10,20 +10,24 @@ final class Lines /** @var bool */ private $containsBlankLines; + /** @var string */ + private $trailingBlankLinesText; + /** @var int */ private $trailingBlankLines; /** * @param Context[] $Contexts - * @param int $trailingBlankLines + * @param string $trailingBlankLinesText */ - private function __construct($Contexts, $trailingBlankLines) + private function __construct($Contexts, $trailingBlankLinesText) { $this->Contexts = $Contexts; - $this->trailingBlankLines = $trailingBlankLines; + $this->trailingBlankLinesText = $trailingBlankLinesText; + $this->trailingBlankLines = \substr_count($trailingBlankLinesText, "\n"); $this->containsBlankLines = ( - ($trailingBlankLines > 0) + ($this->trailingBlankLines > 0) || \array_reduce( $Contexts, /** @@ -42,7 +46,7 @@ final class Lines /** @return self */ public static function none() { - return new self([], 0); + return new self([], ''); } /** @@ -56,23 +60,23 @@ final class Lines $text = \str_replace(["\r\n", "\r"], "\n", $text); $Contexts = []; - $sequentialBreaks = 0; + $sequentialLines = ''; foreach (\explode("\n", $text) as $line) { if (\chop($line) === '') { - $sequentialBreaks += 1; + $sequentialLines .= $line . "\n"; continue; } $Contexts[] = new Context( new Line($line, $indentOffset), - $sequentialBreaks + $sequentialLines ); - $sequentialBreaks = 0; + $sequentialLines = ''; } - return new self($Contexts, $sequentialBreaks); + return new self($Contexts, $sequentialLines); } /** @return bool */ @@ -116,6 +120,7 @@ final class Lines } $Lines = clone($this); + $Lines->trailingBlankLinesText .= \str_repeat("\n", $count); $Lines->trailingBlankLines += $count; $Lines->containsBlankLines = $Lines->containsBlankLines || ($count > 0); @@ -135,6 +140,7 @@ final class Lines if (\count($NextLines->Contexts) === 0) { $Lines->trailingBlankLines += $NextLines->trailingBlankLines; + $Lines->trailingBlankLinesText .= $NextLines->trailingBlankLinesText; $Lines->containsBlankLines = $Lines->containsBlankLines || ($Lines->trailingBlankLines > 0) @@ -145,12 +151,13 @@ final class Lines $NextLines->Contexts[0] = new Context( $NextLines->Contexts[0]->line(), - $NextLines->Contexts[0]->previousEmptyLines() + $Lines->trailingBlankLines + $NextLines->Contexts[0]->previousEmptyLinesText() . $Lines->trailingBlankLinesText ); $Lines->Contexts = \array_merge($Lines->Contexts, $NextLines->Contexts); $Lines->trailingBlankLines = $NextLines->trailingBlankLines; + $Lines->trailingBlankLinesText = $NextLines->trailingBlankLinesText; $Lines->containsBlankLines = $Lines->containsBlankLines || $NextLines->containsBlankLines @@ -166,7 +173,7 @@ final class Lines $Context = new Context( $Context->line(), - $Context->previousEmptyLines() + $Lines->trailingBlankLines + $Context->previousEmptyLinesText() . $Lines->trailingBlankLinesText ); if ($Context->previousEmptyLines() > 0) { @@ -174,6 +181,7 @@ final class Lines } $Lines->trailingBlankLines = 0; + $Lines->trailingBlankLinesText = ''; $Lines->Contexts[] = $Context; diff --git a/tests/commonmark/108-Fenced_code_blocks.html b/tests/commonmark/108-Fenced_code_blocks.html index f6c67db..bccd082 100644 --- a/tests/commonmark/108-Fenced_code_blocks.html +++ b/tests/commonmark/108-Fenced_code_blocks.html @@ -1,4 +1,3 @@ -

foo

-
bar
-
-

baz

\ No newline at end of file +
aaa
+~~~ ~~
+
\ No newline at end of file diff --git a/tests/commonmark/108-Fenced_code_blocks.md b/tests/commonmark/108-Fenced_code_blocks.md index 0540b9d..38450cc 100644 --- a/tests/commonmark/108-Fenced_code_blocks.md +++ b/tests/commonmark/108-Fenced_code_blocks.md @@ -1,5 +1,3 @@ -foo -``` -bar -``` -baz \ No newline at end of file +~~~~~~ +aaa +~~~ ~~ \ No newline at end of file diff --git a/tests/commonmark/81-Indented_code_blocks.html b/tests/commonmark/81-Indented_code_blocks.html new file mode 100644 index 0000000..cb1d989 --- /dev/null +++ b/tests/commonmark/81-Indented_code_blocks.html @@ -0,0 +1,4 @@ +
chunk1
+  
+  chunk2
+
\ No newline at end of file diff --git a/tests/commonmark/81-Indented_code_blocks.md b/tests/commonmark/81-Indented_code_blocks.md new file mode 100644 index 0000000..7c8395d --- /dev/null +++ b/tests/commonmark/81-Indented_code_blocks.md @@ -0,0 +1,3 @@ + chunk1 + + chunk2 \ No newline at end of file diff --git a/tests/commonmark/96-Fenced_code_blocks.html b/tests/commonmark/96-Fenced_code_blocks.html new file mode 100644 index 0000000..af46fb3 --- /dev/null +++ b/tests/commonmark/96-Fenced_code_blocks.html @@ -0,0 +1,4 @@ +

+```
+aaa
+
\ No newline at end of file diff --git a/tests/commonmark/96-Fenced_code_blocks.md b/tests/commonmark/96-Fenced_code_blocks.md new file mode 100644 index 0000000..f904121 --- /dev/null +++ b/tests/commonmark/96-Fenced_code_blocks.md @@ -0,0 +1,4 @@ +````` + +``` +aaa \ No newline at end of file diff --git a/tests/commonmark/97-Fenced_code_blocks.html b/tests/commonmark/97-Fenced_code_blocks.html index 8b957e4..6307015 100644 --- a/tests/commonmark/97-Fenced_code_blocks.html +++ b/tests/commonmark/97-Fenced_code_blocks.html @@ -1,3 +1,5 @@ -

-  
-
\ No newline at end of file +
+
aaa
+
+
+

bbb

\ No newline at end of file diff --git a/tests/commonmark/97-Fenced_code_blocks.md b/tests/commonmark/97-Fenced_code_blocks.md index ac1b221..c24a0ce 100644 --- a/tests/commonmark/97-Fenced_code_blocks.md +++ b/tests/commonmark/97-Fenced_code_blocks.md @@ -1,4 +1,4 @@ -``` +> ``` +> aaa - -``` \ No newline at end of file +bbb \ No newline at end of file diff --git a/tests/data/code_block.html b/tests/data/code_block.html index 1cf90ed..d7ee39c 100644 --- a/tests/data/code_block.html +++ b/tests/data/code_block.html @@ -1,20 +1,24 @@
<?php
 
 $message = 'Hello World!';
-echo $message;
+echo $message; +
> not a quote
 - not a list item
-[not a reference]: http://foo.com
+[not a reference]: http://foo.com +
foo
 
 
-bar
+bar +
\ No newline at end of file diff --git a/tests/data/escaping.html b/tests/data/escaping.html index c8fa585..f298f52 100644 --- a/tests/data/escaping.html +++ b/tests/data/escaping.html @@ -1,6 +1,7 @@

escaped *emphasis*.

escaped \*emphasis\* in a code span

-
escaped \*emphasis\* in a code block
+
escaped \*emphasis\* in a code block
+

\ ` * _ { } [ ] ( ) > # + - . !

one_two one_two

one*two one*two

\ No newline at end of file diff --git a/tests/data/fenced_code_block.html b/tests/data/fenced_code_block.html index 2c0eeb2..fd91256 100644 --- a/tests/data/fenced_code_block.html +++ b/tests/data/fenced_code_block.html @@ -1,18 +1,25 @@
<?php
 
 $message = 'fenced code block';
-echo $message;
-
tilde
-
echo 'language identifier';
-
echo 'language identifier with non words';
+echo $message; + +
tilde
+
+
echo 'language identifier';
+
+
echo 'language identifier with non words';
+
<?php
 echo "Hello World";
 ?>
-<a href="http://auraphp.com" >Aura Project</a>
+<a href="http://auraphp.com" >Aura Project</a> +
the following isn't quite enough to close
 ```
-still a fenced code block
+still a fenced code block +
foo
 
 
-bar
\ No newline at end of file +bar + \ No newline at end of file diff --git a/tests/data/tab-indented_code_block.html b/tests/data/tab-indented_code_block.html index 0ea64e6..39c35f2 100644 --- a/tests/data/tab-indented_code_block.html +++ b/tests/data/tab-indented_code_block.html @@ -3,4 +3,5 @@ $message = 'Hello World!'; echo $message; -echo "following a blank line"; \ No newline at end of file +echo "following a blank line"; + \ No newline at end of file diff --git a/tests/data/whitespace.html b/tests/data/whitespace.html index f2dd7a0..9c78fe9 100644 --- a/tests/data/whitespace.html +++ b/tests/data/whitespace.html @@ -1 +1,2 @@ -
code
\ No newline at end of file +
code
+
\ No newline at end of file