diff --git a/composer.json b/composer.json index 6f4c0fb889..aa957a39c4 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,6 @@ "phpdocumentor/type-resolver": "^1.0", "psr/cache": "^1.0", "psr/log": "^1.1", - "scrivo/highlight.php": "^9.17", "symfony/cache": "^5.0", "symfony/config": "^5.0", "symfony/console": "^5.0", diff --git a/composer.lock b/composer.lock index 500ab78605..3369867cd5 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": "acc838630ea3769a4a9a90193a1665d1", + "content-hash": "bfa643ca88ba1d5b73777b6482fdbd05", "packages": [ { "name": "doctrine/event-manager", @@ -1554,78 +1554,6 @@ ], "time": "2020-03-23T09:12:05+00:00" }, - { - "name": "scrivo/highlight.php", - "version": "v9.18.1.6", - "source": { - "type": "git", - "url": "https://github.com/scrivo/highlight.php.git", - "reference": "44a3d4136edb5ad8551590bf90f437db80b2d466" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/scrivo/highlight.php/zipball/44a3d4136edb5ad8551590bf90f437db80b2d466", - "reference": "44a3d4136edb5ad8551590bf90f437db80b2d466", - "shasum": "" - }, - "require": { - "ext-json": "*", - "ext-mbstring": "*", - "php": ">=5.4" - }, - "require-dev": { - "phpunit/phpunit": "^4.8|^5.7", - "sabberworm/php-css-parser": "^8.3", - "symfony/finder": "^2.8|^3.4", - "symfony/var-dumper": "^2.8|^3.4" - }, - "type": "library", - "autoload": { - "psr-0": { - "Highlight\\": "", - "HighlightUtilities\\": "" - }, - "files": [ - "HighlightUtilities/functions.php" - ] - }, - "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" - ], - "funding": [ - { - "url": "https://github.com/allejo", - "type": "github" - } - ], - "time": "2020-12-22T19:20:29+00:00" - }, { "name": "symfony/cache", "version": "v5.2.2", diff --git a/data/templates/default/css/normalize.css.twig b/data/templates/default/css/normalize.css.twig index 46f646a5c0..579152b07f 100644 --- a/data/templates/default/css/normalize.css.twig +++ b/data/templates/default/css/normalize.css.twig @@ -230,7 +230,7 @@ code, kbd, pre, samp { - font-family: monospace, monospace; + font-family: var(--font-monospace); font-size: 1em; } diff --git a/data/templates/default/css/variables.css.twig b/data/templates/default/css/variables.css.twig index 3670137465..cfe2e63c85 100644 --- a/data/templates/default/css/variables.css.twig +++ b/data/templates/default/css/variables.css.twig @@ -2,6 +2,7 @@ /* Typography */ --font-primary: 'Source Sans Pro', Helvetica, Arial, sans-serif; --font-secondary: 'Source Sans Pro', Helvetica, Arial, sans-serif; + --font-monospace: 'Source Code Pro', monospace; --line-height--primary: 1.6; --letter-spacing--primary: .05rem; --text-base-size: 1em; @@ -22,12 +23,13 @@ --primary-color: hsl(96, 57%, 60%); --primary-color-darken: hsl(96, 57%, 40%); --primary-color-darker: hsl(96, 57%, 20%); + --primary-color-darkest: hsl(96, 57%, 10%); --primary-color-lighten: hsl(96, 57%, 80%); --primary-color-lighter: hsl(96, 57%, 99%); --dark-gray: #d1d1d1; --light-gray: #f0f0f0; - --text-color: #4b3b40; + --text-color: var(--primary-color-darkest); --header-height: var(--spacing-xxxxl); --header-bg-color: var(--primary-color); diff --git a/data/templates/default/guides/code.html.twig b/data/templates/default/guides/code.html.twig index fcbf185d29..0a8a2d2dd1 100644 --- a/data/templates/default/guides/code.html.twig +++ b/data/templates/default/guides/code.html.twig @@ -1 +1 @@ -
{{ codeNode.value }}
+
 'twig',
-        'html+twig' => 'twig',
-        'jinja' => 'twig',
-        'html+php' => 'html',
-        'xml+php' => 'xml',
-        'php-annotations' => 'php',
-        'terminal' => 'bash',
-        'rst' => 'markdown',
-        'php-standalone' => 'php',
-        'php-symfony' => 'php',
-        'varnish4' => 'c',
-        'varnish3' => 'c',
-        'vcl' => 'c',
-    ];
-
     /** @var CodeNode */
     private $codeNode;
 
@@ -63,81 +33,19 @@ public function __construct(CodeNode $codeNode)
 
     public function render() : string
     {
-        $this->configureHighlighter();
-
         $value = $this->codeNode->getValue();
 
         if ($this->codeNode->isRaw()) {
             return $value;
         }
 
-        $lines = $this->getLines($value);
-        $code = implode("\n", $lines);
-
-        $lineNumbers = '';
-        for ($i = 1, $nbLines = count($lines); $i <= $nbLines; ++$i) {
-            $lineNumbers .= str_pad((string) $i, 2, ' ', STR_PAD_LEFT) . "\n";
-        }
-
-        $language = $this->codeNode->getLanguage() ?? 'php';
-
-        if ($language !== 'text' && $language !== '') {
-            $highLighter = new Highlighter();
-            $code = $highLighter->highlight(self::LANGUAGES_MAPPING[$language] ?? $language, $code)->value;
-        }
-
         return $this->renderer->render(
             'code.html.twig',
             [
-                'codeNode' => $this->codeNode,
-                'language' => $language,
-                'languageMapping' => self::LANGUAGES_MAPPING[$language] ?? $language,
-                'code' => $code,
-                'lineNumbers' => rtrim($lineNumbers),
+                'code' => $this->codeNode->getValue(),
+                'language' => $this->codeNode->getLanguage(),
+                'startingLineNumber' => $this->codeNode->getStartingLineNumber(),
             ]
         );
     }
-
-    public static function isLanguageSupported(string $lang) : bool
-    {
-        $highlighter = new Highlighter();
-        $supportedLanguages = array_merge(
-            array_keys(self::LANGUAGES_MAPPING),
-            $highlighter->listLanguages(true),
-            // not highlighted, but valid
-            ['text']
-        );
-
-        return in_array($lang, $supportedLanguages, true);
-    }
-
-    /**
-     * @return array
-     */
-    private function getLines(string $code) : array
-    {
-        $lines = preg_split('/\r\n|\r|\n/', $code);
-        $reversedLines = array_reverse($lines);
-
-        // trim empty lines at the end of the code
-        foreach ($reversedLines as $key => $line) {
-            if (trim($line) !== '') {
-                break;
-            }
-
-            unset($reversedLines[$key]);
-        }
-
-        return array_reverse($reversedLines);
-    }
-
-    private function configureHighlighter() : void
-    {
-//        if (!self::$isHighlighterConfigured) {
-//            Highlighter::registerLanguage('php', 'guides/highlight.php/php.json', true);
-//            Highlighter::registerLanguage('twig', 'guides/highlight.php/twig.json', true);
-//        }
-//
-//        self::$isHighlighterConfigured = true;
-    }
 }
diff --git a/src/Guides/RestructuredText/Directives/Code.php b/src/Guides/RestructuredText/Directives/Code.php
new file mode 100644
index 0000000000..c7d6ba450e
--- /dev/null
+++ b/src/Guides/RestructuredText/Directives/Code.php
@@ -0,0 +1,24 @@
+setLanguage(trim($data));
+            $this->setStartingLineNumberBasedOnOptions($options, $node);
         }
 
         if ($variable !== '') {
@@ -56,4 +59,23 @@ public function wantCode() : bool
     {
         return true;
     }
+
+    /**
+     * @param string[] $options
+     */
+    private function setStartingLineNumberBasedOnOptions(array $options, CodeNode $node) : void
+    {
+        $startingLineNumber = null;
+        if (isset($options['linenos'])) {
+            $startingLineNumber = 1;
+        }
+
+        $startingLineNumber = $options['number-lines'] ?? $options['lineno-start'] ?? $startingLineNumber;
+
+        if ($startingLineNumber === null) {
+            return;
+        }
+
+        $node->setStartingLineNumber((int) $startingLineNumber);
+    }
 }
diff --git a/src/Guides/RestructuredText/HTML/Directives/CodeBlockDirective.php b/src/Guides/RestructuredText/HTML/Directives/CodeBlockDirective.php
deleted file mode 100644
index 73e58c748b..0000000000
--- a/src/Guides/RestructuredText/HTML/Directives/CodeBlockDirective.php
+++ /dev/null
@@ -1,56 +0,0 @@
-setLanguage($data);
-
-        if ($variable !== '') {
-            $environment = $parser->getEnvironment();
-            $environment->setVariable($variable, $node);
-        } else {
-            $document = $parser->getDocument();
-            $document->addNode($node);
-        }
-    }
-
-    public function wantCode() : bool
-    {
-        return true;
-    }
-}
diff --git a/src/Guides/RestructuredText/Parser.php b/src/Guides/RestructuredText/Parser.php
index cf0de4a028..03df112a15 100644
--- a/src/Guides/RestructuredText/Parser.php
+++ b/src/Guides/RestructuredText/Parser.php
@@ -100,6 +100,7 @@ public function initDirectives(array $directives) : void
         $directives = array_merge(
             [
                 new Directives\Dummy(),
+                new Directives\Code(),
                 new Directives\CodeBlock(),
                 new Directives\Raw(),
                 new Directives\Replace(),
diff --git a/symfony.lock b/symfony.lock
index dbe62dd0cb..32edad2a4a 100644
--- a/symfony.lock
+++ b/symfony.lock
@@ -101,9 +101,6 @@
     "psr/log": {
         "version": "1.0.2"
     },
-    "scrivo/highlight.php": {
-        "version": "v9.17.1.0"
-    },
     "symfony/cache": {
         "version": "v4.0.3"
     },