diff --git a/.gitignore b/.gitignore index e286955..864a81b 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ Icon # --------------- /kirby +/site/plugins diff --git a/site/plugins/kirby-highlighter/LICENSE b/site/plugins/kirby-highlighter/LICENSE deleted file mode 100644 index 1bd7d12..0000000 --- a/site/plugins/kirby-highlighter/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2020-PRESENT Johann Schopplich - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/site/plugins/kirby-highlighter/README.md b/site/plugins/kirby-highlighter/README.md deleted file mode 100644 index a2dfa34..0000000 --- a/site/plugins/kirby-highlighter/README.md +++ /dev/null @@ -1,135 +0,0 @@ -# Kirby Highlighter - -Server-side code highlighting available as [custom block](https://getkirby.com/docs/reference/panel/fields/blocks) and for [KirbyText](https://getkirby.com/docs/guide/content/text-formatting#kirbytext). - -Built upon [highlight.php](http://www.highlightjs.org) which itself is a port of [highlight.js](http://www.highlightjs.org). - -## Key Features - -- 🏗 Works with Kirby's [`code` block](https://getkirby.com/docs/reference/panel/blocks/code) -- 🏳️‍🌈 Supports 189 languages -- 💫 94 styles available -- ⛳️ Automatic language detection for KirbyText - -## Requirements - -- Kirby 3.8+ - -## Installation - -### Composer - -``` -composer require johannschopplich/kirby-highlighter -``` - -### Download - -Download and copy this repository to `/site/plugins/kirby-highlighter`. - -## Usage - -### With Kirby Blocks Field - -This plugin overwrites Kirby's internal [`code` block](https://getkirby.com/docs/reference/panel/blocks/code). Thus, you won't have to change a thing. - -Use the `code` block just like before, the output will be highlighted automatically: - -```yaml -fields: - example: - label: Paste code here - type: blocks - fieldsets: - - code -``` - -### Within KirbyText - -Create a code block in your KirbyText field and optionally set the code language: - -
```css
-.currentColor {
-  color: currentColor;
-}
-```
-
- -Or use the new `code`-KirbyTag from this plugin with a base64 encoded code string: - -``` -(code: LmN1cnJlbnRDb2xvciB7CiAgY29sb3I6IGN1cnJlbnRDb2xvcjsKfQ== lang: css) -``` - -Which outputs: - -```html -
.currentColor {
-    color: currentColor;
-}
-``` - -The syntax highlighting functionality can be changed. You can choose between two highlighting modes: - -1. Explicit mode (default) -2. Automatic language detection mode (opt-in) - -#### Explicit Mode - -In explicit mode, you have to define which language the code block is. Otherwise highlighting will be skipped. - -#### Automatic Language Detection - -Alternatively you can use the automatic detection mode, which highlights your code with the language the library thinks is best. It is highly recommended you explicitly choose the language or limit the number of languages to automatically detect from. This reduces the number of inaccuracies and skips this extremely inefficient selection process. - -To enable automatic language detection, set: - -- `johannschopplich.highlighter.autodetect` to `true` -- `johannschopplich.highlighter.languages` to an array of names from which languages should be chosen - -## Options - -| Option |  Default | Description | -| --------------------------------------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | -| `johannschopplich.highlighter.class` | `hljs` | Style class for Highlight to be added to the `pre` element. | -| `johannschopplich.highlighter.autodetect` | `false` | Indicates if the library should define which language thinks is best. Only applies when no language was set on the KirbyText code block. | -| `johannschopplich.highlighter.languages` | `[]` | Array of language names to be auto-detected. If empty, every language will be auto-detectable. | -| `johannschopplich.highlighter.line-numbering` | `false` | Indicates if the library should split up the highlighted code on each new line and wrap it in a `` element. | -| `johannschopplich.highlighter.line-numbering-class` | `hljs-code-line` | CSS class applied to highlighted code lines, respectively `` elements. | - -## Styling in the Frontend - -Since this plugin handles highlighting code only and thus just wraps span's around code, you have to link styles in your frontend yourself. I recommend choosing one of the available themes directly from the highlight.js project: [highlight.js/src/styles/](https://github.com/highlightjs/highlight.js/tree/master/src/styles) - -The CSS files over at the repository are maintained and new ones arrive from time to time, therefore it would be redundant to include a copy in this repository. - -One of my favorite themes is [Night Owl by Sarah Drasner](https://github.com/highlightjs/highlight.js/blob/master/src/styles/night-owl.css). -For example you could download the CSS file and save it in your Kirby project under `assets/css/hljs-night-owl.css`. Now you just have to include it in your template ``. Alternatively, use a CSS bundler of your choice. - -### Line Numbering - -If you choose to activate the line numbering option, you will need to include additional CSS style to display line numbering. - -A basic example using [pseudo-elements](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements): - -```css -pre.hljs .hljs-code-line { - counter-increment: line; -} - -pre.hljs .hljs-code-line::before { - content: counter(line); - display: inline-block; - margin-right: 1em; - opacity: 0.5; -} -``` - -## Credits - -- Geert Bergman and contributors for the awesome [highlight.php](https://github.com/scrivo/highlight.php) port. -- Martin Folkers for his [Kirby Highlight](https://github.com/S1SYPHOS/kirby3-highlight) plugin which built the base of this package. - -## License - -[MIT](./LICENSE) License © 2020-PRESENT [Johann Schopplich](https://github.com/johannschopplich) diff --git a/site/plugins/kirby-highlighter/classes/JohannSchopplich/HTML5DOMDocument.php b/site/plugins/kirby-highlighter/classes/JohannSchopplich/HTML5DOMDocument.php deleted file mode 100644 index 0b2cdab..0000000 --- a/site/plugins/kirby-highlighter/classes/JohannSchopplich/HTML5DOMDocument.php +++ /dev/null @@ -1,74 +0,0 @@ -tempRoot}>" . $convertedSource . "tempRoot}>", $options); - } - - /** - * Strip the temporarily added root element - */ - private function unwrapTempRoot(string $output): string - { - if ($this->firstChild->nodeName === $this->tempRoot) { - return substr($output, strlen($this->tempRoot) + 2, -strlen($this->tempRoot) - 4); - } - - return $output; - } - - /** - * Dump the internal document into a HTML string - */ - #[\ReturnTypeWillChange] - public function saveHTML(DOMNode|null $node = null, bool $entities = false): string|false - { - $html = parent::saveHTML($node); - - if ($entities === false) { - $html = html_entity_decode($html); - } - - if ($node === null) { - $html = $this->unwrapTempRoot($html); - } - - return $html; - } -} diff --git a/site/plugins/kirby-highlighter/composer.json b/site/plugins/kirby-highlighter/composer.json deleted file mode 100644 index 212531a..0000000 --- a/site/plugins/kirby-highlighter/composer.json +++ /dev/null @@ -1,49 +0,0 @@ -{ - "name": "johannschopplich/kirby-highlighter", - "description": "Server-side syntax highlighting for Kirby CMS", - "type": "kirby-plugin", - "version": "3.1.0", - "keywords": [ - "kirby", - "highlight", - "highlighter", - "hljs" - ], - "license": "MIT", - "homepage": "https://github.com/johannschopplich/kirby-highlighter#readme", - "authors": [ - { - "name": "Johann Schopplich", - "email": "pkg@johannschopplich.com", - "homepage": "https://johannschopplich.com" - } - ], - "require": { - "getkirby/composer-installer": "^1.2", - "scrivo/highlight.php": "^9.18" - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "@stable", - "getkirby/cms": "^4", - "phpunit/phpunit": "^9.0" - }, - "autoload": { - "psr-4": { - "JohannSchopplich\\": "classes/JohannSchopplich/" - } - }, - "scripts": { - "fix": "php-cs-fixer fix", - "dist": "composer install --no-dev --optimize-autoloader", - "test": "phpunit" - }, - "config": { - "optimize-autoloader": true, - "allow-plugins": { - "getkirby/composer-installer": true - } - }, - "extra": { - "kirby-cms-path": "tests/fixtures/kirby" - } -} diff --git a/site/plugins/kirby-highlighter/extensions/fieldmethods.php b/site/plugins/kirby-highlighter/extensions/fieldmethods.php deleted file mode 100644 index d8f1a20..0000000 --- a/site/plugins/kirby-highlighter/extensions/fieldmethods.php +++ /dev/null @@ -1,23 +0,0 @@ - function ($field) { - if ($field->isNotEmpty()) { - $value = trim((string)$field->value()); - - if (is_base64_string_s($value)) { - $field->value = base64_decode($value); - } - } - - return $field; - }, -]; diff --git a/site/plugins/kirby-highlighter/extensions/hooks.php b/site/plugins/kirby-highlighter/extensions/hooks.php deleted file mode 100644 index 520516a..0000000 --- a/site/plugins/kirby-highlighter/extensions/hooks.php +++ /dev/null @@ -1,86 +0,0 @@ - function (string|null $text) { - $kirby = App::instance(); - - // Parse KirbyText input as HTML document - $dom = new HTML5DOMDocument(); - $dom->loadHTML($text); - - // Retrieve all `pre` elements inside newly created HTML document - $preNodes = $dom->getElementsByTagName('pre'); - - // Bail if no `pre` elements have been found - if ($preNodes->length === 0) { - return $text; - } - - // Loop through all `pre` elements - foreach ($preNodes as $preNode) { - // Ensure nothing but the `code` element exists - if ($preNode->childNodes->length !== 1) { - continue; - } - - // Select direct `code` child element of `pre` block - $codeNode = $preNode->firstChild; - - // Get language code if present - $language = $codeNode->getAttribute('class'); - if (str_starts_with($language, 'language-')) { - $language = preg_replace('/^language-/', '', $language); - } - - // Bail highlighting if language isn't set and auto detection is disabled - if (empty($language) && !$kirby->option('johannschopplich.highlighter.autodetect', false)) { - continue; - } - - // Add `hljs` class to `pre` block - $preNode->setAttribute('class', $kirby->option('johannschopplich.highlighter.class', 'hljs')); - - // Get raw code data to highlight - $code = $codeNode->nodeValue; - - // Remove code element afterwards - $preNode->removeChild($codeNode); - - // Initiate `Highlighter` and use pre-defined language code, fall - // back to language auto detection if enabled - $highlighter = new Highlighter(); - - // Highlight code - if (!empty($language)) { - $highlightedCode = $highlighter->highlight($language, $code); - } elseif ($kirby->option('johannschopplich.highlighter.autodetect', false)) { - $languageSubset = $kirby->option('johannschopplich.highlighter.languages', []); - if (!empty($languageSubset)) { - $highlighter->setAutodetectLanguages($languageSubset); - } - - $highlightedCode = $highlighter->highlightAuto($code); - } - - // Line numbering - if ($kirby->option('johannschopplich.highlighter.line-numbering', false)) { - $lines = preg_split('/\R/', $highlightedCode->value); - $lineClass = $kirby->option('johannschopplich.highlighter.line-numbering-class', 'hljs-code-line'); - $highlightedCode->value = '' . implode("\n", $lines) . ''; - } - - // Append highlighted wrapped in `code` block to parent `pre` - $codeNode = $dom->createDocumentFragment(); - $codeNode->appendXML('' . $highlightedCode->value . ''); - $preNode->appendChild($codeNode); - } - - // Save all changes - $text = $dom->saveHTML(null, true); - return $text; - } -]; diff --git a/site/plugins/kirby-highlighter/extensions/kirbytags.php b/site/plugins/kirby-highlighter/extensions/kirbytags.php deleted file mode 100644 index c5acb01..0000000 --- a/site/plugins/kirby-highlighter/extensions/kirbytags.php +++ /dev/null @@ -1,27 +0,0 @@ - [ - 'attr' => [ - 'lang', - 'language', - ], - // TODO: Type as `\Kirby\Text\KirbyTag` for Kirby 4 - 'html' => function ($tag) { - $kirby = App::instance(); - $code = $tag->value; - $language = $tag->lang ?? $tag->language; - $block = new \Kirby\Cms\Block([ - 'type' => 'code', - 'content' => [ - 'language' => $language ?? 'plaintext', - 'code' => is_base64_string_s($code) ? base64_decode($code) : $code, - ] - ]); - - return $kirby->snippet('blocks/code', ['block' => $block], true); - } - ] -]; diff --git a/site/plugins/kirby-highlighter/index.php b/site/plugins/kirby-highlighter/index.php deleted file mode 100644 index 52c1592..0000000 --- a/site/plugins/kirby-highlighter/index.php +++ /dev/null @@ -1,12 +0,0 @@ - require __DIR__ . '/extensions/hooks.php', - 'fieldMethods' => require __DIR__ . '/extensions/fieldmethods.php', - 'tags' => require __DIR__ . '/extensions/kirbytags.php', - 'snippets' => [ - 'blocks/code' => __DIR__ . '/snippets/blocks/code.php' - ] -]); diff --git a/site/plugins/kirby-highlighter/phpunit.xml b/site/plugins/kirby-highlighter/phpunit.xml deleted file mode 100644 index a65d2f2..0000000 --- a/site/plugins/kirby-highlighter/phpunit.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - ./tests - - - diff --git a/site/plugins/kirby-highlighter/snippets/blocks/code.php b/site/plugins/kirby-highlighter/snippets/blocks/code.php deleted file mode 100644 index 87398f5..0000000 --- a/site/plugins/kirby-highlighter/snippets/blocks/code.php +++ /dev/null @@ -1,22 +0,0 @@ -language()->value(); -$code = $block->code()->fromBase64()->value(); - -if (empty($language) || !in_array($language, $highlighter->listRegisteredLanguages())) { - $language = 'plaintext'; -} - -$highlightedCode = $highlighter->highlight($language, $code)->value; - -// Handle line numbering -if (option('johannschopplich.highlighter.line-numbering', false)) { - $lines = preg_split('/\R/', $highlightedCode); - $lineClass = option('johannschopplich.highlighter.line-numbering-class', 'hljs-code-line'); - $highlightedCode = '' . implode("\n", $lines) . ''; -} - -?> -