From 4f3c0488ff95a7ca4dae3a27f294ee633f610085 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Tue, 2 Apr 2024 11:39:33 +0200 Subject: [PATCH] Add Inline Theme --- README.md | 11 ++-- UPGRADING.md | 5 ++ src/CommonMark/CodeBlockRenderer.php | 19 +++--- src/Themes/InlineTheme.php | 69 ++++++++++++++++++++++ src/Themes/solarized-dark.css | 2 +- src/WithPre.php | 12 ++++ tests/CommonMark/CodeBlockRendererTest.php | 29 +++++++++ tests/HighlighterTest.php | 10 ++++ tests/index.php | 11 ++-- tests/targets/test.md | 26 ++++---- 10 files changed, 155 insertions(+), 39 deletions(-) create mode 100644 src/Themes/InlineTheme.php create mode 100644 src/WithPre.php diff --git a/README.md b/README.md index 1fcfbee..3404e46 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ You can read about why I started this package [here](https://stitcher.io/blog/a- - [Quickstart](#quickstart) - [Supported languages](#supported-languages) - [Themes](#themes) - - [For the web](#for-the-web) - - [For the terminal](#for-the-terminal) - [Gutter](#gutter) - [Special highlighting tags](#special-highlighting-tags) - [Emphasize strong and blur](#emphasize-strong-and-blur) @@ -37,18 +35,17 @@ All supported languages can be found in the [Languages folder](./src/Languages). ## Themes -### For the web - -For HTML rendering, you can use one of the provided themes that comes with this package: +There are a [bunch of themes](./src/Themes) included in this package. You can load them either by importing the correct CSS file into your project's CSS file, or you can manually copy a stylesheet. ```css @import "../vendor/tempest/highlight/src/Themes/highlight-light-lite.css"; -@import "../vendor/tempest/highlight/src/Themes/highlight-dark-lite.css"; ``` You can build your own CSS theme with just a couple of classes, copy over [the base stylesheet](./src/Themes/highlight-light-lite.css), and make adjustments however you like. Note that `pre` tag styling isn't included in this package. -### For the terminal +### Terminal themes + +Terminal themes are more limited because of their limited styling options. Right now there's one terminal theme provided: `LightTerminalTheme`. More terminal themes are planned to be added in the future. ```php use Tempest\Highlight\Highlighter; diff --git a/UPGRADING.md b/UPGRADING.md index 2d34ccd..af081d7 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,3 +1,8 @@ +## 1.2.0 + +- Added a collection of themes (#87) +- Added a new `InlineTheme`, which doesn't require loading a CSS style sheet (#88) + ## 1.1.0 - Added Gutter support: diff --git a/src/CommonMark/CodeBlockRenderer.php b/src/CommonMark/CodeBlockRenderer.php index f534fd7..1f3d0eb 100644 --- a/src/CommonMark/CodeBlockRenderer.php +++ b/src/CommonMark/CodeBlockRenderer.php @@ -9,8 +9,8 @@ use League\CommonMark\Node\Node; use League\CommonMark\Renderer\ChildNodeRendererInterface; use League\CommonMark\Renderer\NodeRendererInterface; -use League\CommonMark\Util\HtmlElement; use Tempest\Highlight\Highlighter; +use Tempest\Highlight\WithPre; final class CodeBlockRenderer implements NodeRendererInterface { @@ -31,13 +31,14 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer) $this->highlighter->withGutter((int)$startAt); } - return new HtmlElement( - 'pre', - [], - $this->highlighter->parse( - content: $node->getLiteral(), - language: $matches['language'], - ), - ); + $parsed = $this->highlighter->parse($node->getLiteral(), $matches['language']); + + $theme = $this->highlighter->getTheme(); + + if ($theme instanceof WithPre) { + return $theme->preBefore() . $parsed . $theme->preAfter(); + } else { + return '
' . $parsed . '
'; + } } } diff --git a/src/Themes/InlineTheme.php b/src/Themes/InlineTheme.php new file mode 100644 index 0000000..a93de0f --- /dev/null +++ b/src/Themes/InlineTheme.php @@ -0,0 +1,69 @@ +[\w,\.\s\-]+){(?