From 918872fde10f1b5ec4f15e5fe4e9585c185f6d2e Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 6 Aug 2023 12:50:37 +0300 Subject: [PATCH] code highlighting added --- composer.json | 1 + composer.lock | 80 +++++++++++++++++++++++++++++- public/css/ocean.css | 74 +++++++++++++++++++++++++++ src/Controller/PasteController.php | 6 ++- src/Entity/CodeHighlighter.php | 26 ++++++++++ templates/base.html.twig | 9 ++-- templates/paste.html.twig | 7 ++- templates/show_paste.html.twig | 24 +++++++++ 8 files changed, 218 insertions(+), 9 deletions(-) create mode 100644 public/css/ocean.css create mode 100644 src/Entity/CodeHighlighter.php create mode 100644 templates/show_paste.html.twig diff --git a/composer.json b/composer.json index 288abba..13fa1dd 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ "doctrine/doctrine-bundle": "^2.10", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.15", + "scrivo/highlight.php": "v9.18.1.10", "symfony/console": "6.3.*", "symfony/dotenv": "6.3.*", "symfony/flex": "^2", diff --git a/composer.lock b/composer.lock index e63a245..7efc02b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f716d81393aa0f266916ea52e0fa0b53", + "content-hash": "de2f472bb7e2d9dbd3c6363258ac286e", "packages": [ { "name": "doctrine/cache", @@ -1521,6 +1521,84 @@ }, "time": "2021-07-14T16:46:02+00:00" }, + { + "name": "scrivo/highlight.php", + "version": "v9.18.1.10", + "source": { + "type": "git", + "url": "https://github.com/scrivo/highlight.php.git", + "reference": "850f4b44697a2552e892ffe71490ba2733c2fc6e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/scrivo/highlight.php/zipball/850f4b44697a2552e892ffe71490ba2733c2fc6e", + "reference": "850f4b44697a2552e892ffe71490ba2733c2fc6e", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=5.4" + }, + "require-dev": { + "phpunit/phpunit": "^4.8|^5.7", + "sabberworm/php-css-parser": "^8.3", + "symfony/finder": "^2.8|^3.4|^5.4", + "symfony/var-dumper": "^2.8|^3.4|^5.4" + }, + "suggest": { + "ext-mbstring": "Allows highlighting code with unicode characters and supports language with unicode keywords" + }, + "type": "library", + "autoload": { + "files": [ + "HighlightUtilities/functions.php" + ], + "psr-0": { + "Highlight\\": "", + "HighlightUtilities\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Geert Bergman", + "homepage": "http://www.scrivo.org/", + "role": "Project Author" + }, + { + "name": "Vladimir Jimenez", + "homepage": "https://allejo.io", + "role": "Maintainer" + }, + { + "name": "Martin Folkers", + "homepage": "https://twobrain.io", + "role": "Contributor" + } + ], + "description": "Server side syntax highlighter that supports 185 languages. It's a PHP port of highlight.js", + "keywords": [ + "code", + "highlight", + "highlight.js", + "highlight.php", + "syntax" + ], + "support": { + "issues": "https://github.com/scrivo/highlight.php/issues", + "source": "https://github.com/scrivo/highlight.php" + }, + "funding": [ + { + "url": "https://github.com/allejo", + "type": "github" + } + ], + "time": "2022-12-17T21:53:22+00:00" + }, { "name": "symfony/cache", "version": "v6.3.1", diff --git a/public/css/ocean.css b/public/css/ocean.css new file mode 100644 index 0000000..8f7d4ee --- /dev/null +++ b/public/css/ocean.css @@ -0,0 +1,74 @@ +/* Ocean Dark Theme */ +/* https://github.com/gavsiu */ +/* Original theme - https://github.com/chriskempson/base16 */ + +/* Ocean Comment */ +.hljs-comment, +.hljs-quote { + color: #65737e; +} + +/* Ocean Red */ +.hljs-variable, +.hljs-template-variable, +.hljs-tag, +.hljs-name, +.hljs-selector-id, +.hljs-selector-class, +.hljs-regexp, +.hljs-deletion { + color: #bf616a; +} + +/* Ocean Orange */ +.hljs-number, +.hljs-built_in, +.hljs-builtin-name, +.hljs-literal, +.hljs-type, +.hljs-params, +.hljs-meta, +.hljs-link { + color: #d08770; +} + +/* Ocean Yellow */ +.hljs-attribute { + color: #ebcb8b; +} + +/* Ocean Green */ +.hljs-string, +.hljs-symbol, +.hljs-bullet, +.hljs-addition { + color: #a3be8c; +} + +/* Ocean Blue */ +.hljs-title, +.hljs-section { + color: #8fa1b3; +} + +/* Ocean Purple */ +.hljs-keyword, +.hljs-selector-tag { + color: #b48ead; +} + +.hljs { + display: block; + overflow-x: auto; + background: #2b303b; + color: #c0c5ce; + padding: 0.5em; +} + +.hljs-emphasis { + font-style: italic; +} + +.hljs-strong { + font-weight: bold; +} \ No newline at end of file diff --git a/src/Controller/PasteController.php b/src/Controller/PasteController.php index 057f0d2..eec1a2e 100644 --- a/src/Controller/PasteController.php +++ b/src/Controller/PasteController.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace App\Controller; use App\DTO\PasteFormData; +use App\Entity\CodeHighlighter; use App\Entity\Paste; use App\Form\Type\PasteForm; use App\Repository\PasteRepository; @@ -43,8 +44,9 @@ class PasteController extends AbstractController $pasteData = new PasteFormData($paste); $form = $this->createForm(PasteForm::class, $pasteData); - return $this->render('paste.html.twig', [ + return $this->render('show_paste.html.twig', [ 'form' => $form, - ]); + 'highlighted_text' => CodeHighlighter::highlight($pasteData->language, $pasteData->text) + ]); } } diff --git a/src/Entity/CodeHighlighter.php b/src/Entity/CodeHighlighter.php new file mode 100644 index 0000000..50e0486 --- /dev/null +++ b/src/Entity/CodeHighlighter.php @@ -0,0 +1,26 @@ +highlight($language, $code); + $highlighted_text = "
language}\">".$highlighted->value.'
'; + } + catch (\DomainException $e) { + // This is thrown if the specified language does not exist + $highlighted_text = '
'.htmlentities($code).'
'; + } + + return $highlighted_text; + } +} \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 67598ac..4553093 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -2,15 +2,14 @@ - {% block title %}Welcome!{% endblock %} + {% block title %}Copypaste{% endblock %} + {% block stylesheets %} {% endblock %} - - {% block javascripts %} - {% endblock %} - {% block body %}{% endblock %} + {% block content %}{% endblock %} + diff --git a/templates/paste.html.twig b/templates/paste.html.twig index 0c1a4ce..f8bddfe 100644 --- a/templates/paste.html.twig +++ b/templates/paste.html.twig @@ -1 +1,6 @@ -{{ form(form) }} +{% extends "base.html.twig" %} + +{% block content %} + {{ form(form) }} +{% endblock %} + diff --git a/templates/show_paste.html.twig b/templates/show_paste.html.twig new file mode 100644 index 0000000..f14fcfb --- /dev/null +++ b/templates/show_paste.html.twig @@ -0,0 +1,24 @@ +{% extends "base.html.twig" %} + +{% block stylesheets %} + +{% endblock %} + +{% block content %} + +
+
+ {{ highlighted_text|raw }} +
+
+ {% include 'paste.html.twig' %} +
+
+{% endblock %} \ No newline at end of file