From 7da60e67f3d9b715110760d00db97313f20052c0 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 18 Mar 2021 18:38:48 -0400 Subject: [PATCH 1/2] Adding a span around code blocks that start with $ --- src/Renderers/CodeNodeRenderer.php | 4 ++++ .../expected/blocks/code-blocks/terminal.html | 14 +++++++++++++- .../source/blocks/code-blocks/terminal.rst | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Renderers/CodeNodeRenderer.php b/src/Renderers/CodeNodeRenderer.php index b3625a9..08c6739 100644 --- a/src/Renderers/CodeNodeRenderer.php +++ b/src/Renderers/CodeNodeRenderer.php @@ -74,6 +74,10 @@ public function render(): string $highlightedCode = str_replace('$', '$', $highlightedCode); } + if ('terminal' === $language && str_starts_with($highlightedCode, '$')) { + $highlightedCode = '$' . substr($highlightedCode, 1); + } + $numOfLines = \count(preg_split('/\r\n|\r|\n/', $highlightedCode)); $lines = implode("\n", range(1, $numOfLines - 1)); diff --git a/tests/fixtures/expected/blocks/code-blocks/terminal.html b/tests/fixtures/expected/blocks/code-blocks/terminal.html index 6bde18a..6f839e5 100644 --- a/tests/fixtures/expected/blocks/code-blocks/terminal.html +++ b/tests/fixtures/expected/blocks/code-blocks/terminal.html @@ -15,5 +15,17 @@ +
+
+
1
+
+           
+               $ cowsay
+               'eat more chicken'
+           
+       
+
+
+ - \ No newline at end of file + diff --git a/tests/fixtures/source/blocks/code-blocks/terminal.rst b/tests/fixtures/source/blocks/code-blocks/terminal.rst index 295a151..57b6000 100644 --- a/tests/fixtures/source/blocks/code-blocks/terminal.rst +++ b/tests/fixtures/source/blocks/code-blocks/terminal.rst @@ -1,3 +1,7 @@ .. code-block:: terminal git clone git@github.com:symfony/symfony.git + +.. code-block:: terminal + + $ cowsay 'eat more chicken' From fd77b3bb3a9364ac28da13986d32900cd228b5e3 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 18 Mar 2021 20:57:17 -0400 Subject: [PATCH 2/2] Adding Windows terminal support and test case for comments --- src/Renderers/CodeNodeRenderer.php | 5 +++-- .../expected/blocks/code-blocks/terminal.html | 19 ++++++++++++++++++- .../source/blocks/code-blocks/terminal.rst | 7 +++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Renderers/CodeNodeRenderer.php b/src/Renderers/CodeNodeRenderer.php index 08c6739..2812d71 100644 --- a/src/Renderers/CodeNodeRenderer.php +++ b/src/Renderers/CodeNodeRenderer.php @@ -74,8 +74,9 @@ public function render(): string $highlightedCode = str_replace('$', '$', $highlightedCode); } - if ('terminal' === $language && str_starts_with($highlightedCode, '$')) { - $highlightedCode = '$' . substr($highlightedCode, 1); + if ('terminal' === $language) { + $highlightedCode = preg_replace('/^\$/m', '$', $highlightedCode); + $highlightedCode = preg_replace('/^C:\\\>/m', 'C:\>', $highlightedCode); } $numOfLines = \count(preg_split('/\r\n|\r|\n/', $highlightedCode)); diff --git a/tests/fixtures/expected/blocks/code-blocks/terminal.html b/tests/fixtures/expected/blocks/code-blocks/terminal.html index 6f839e5..bde9415 100644 --- a/tests/fixtures/expected/blocks/code-blocks/terminal.html +++ b/tests/fixtures/expected/blocks/code-blocks/terminal.html @@ -17,11 +17,28 @@
-
1
+
1
+2
            
                $ cowsay
                'eat more chicken'
+               $ cowsay
+               'mmmm'
+           
+       
+
+
+ +
+
+
1
+2
+3
+
+           
+               C:\> CIV
+               # Civilization for DOS - my first computer game!
            
        
diff --git a/tests/fixtures/source/blocks/code-blocks/terminal.rst b/tests/fixtures/source/blocks/code-blocks/terminal.rst index 57b6000..ede3e22 100644 --- a/tests/fixtures/source/blocks/code-blocks/terminal.rst +++ b/tests/fixtures/source/blocks/code-blocks/terminal.rst @@ -5,3 +5,10 @@ .. code-block:: terminal $ cowsay 'eat more chicken' + $ cowsay 'mmmm' + +.. code-block:: terminal + + C:\> CIV + + # Civilization for DOS - my first computer game!