Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switched code highlighting from js to php implementation #153

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./output_prod
cname: thephp.foundation
cname: thephp.foundation
2 changes: 2 additions & 0 deletions app/SculpinKernel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Bundles\AtomFeedGeneratorBundle\SculpinAtomFeedGeneratorBundle;
use App\Bundles\HighlightBundle\SculpinHighlightBundle;
use App\Bundles\MermaidBundle\SculpinMermaidBundle;
use App\Bundles\SharingImageGeneratorBundle\SculpinSharingImageGeneratorBundle;
use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel;
Expand All @@ -13,6 +14,7 @@ protected function getAdditionalSculpinBundles(): array
SculpinAtomFeedGeneratorBundle::class,
SculpinSharingImageGeneratorBundle::class,
SculpinMermaidBundle::class,
SculpinHighlightBundle::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Bundles\HighlightBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Extension\Extension;

class SculpinHighlightExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
22 changes: 22 additions & 0 deletions app/src/Bundles/HighlightBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="inline_theme" class="Tempest\Highlight\Themes\InlineTheme">
<argument>%kernel.project_dir%/vendor/tempest/highlight/src/Themes/Css/highlight-light-lite.css</argument>
</service>

<service id="highlighter" class="Tempest\Highlight\Highlighter">
<argument type="service" id="inline_theme"/>
</service>

<service id="sculpin_highlight_extension" class="App\Bundles\HighlightBundle\TwigHighlightExtension">
<argument type="service" id="highlighter"/>
<tag name="twig.extension"/>
</service>
</services>
</container>
9 changes: 9 additions & 0 deletions app/src/Bundles/HighlightBundle/SculpinHighlightBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Bundles\HighlightBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SculpinHighlightBundle extends Bundle
{
}
46 changes: 46 additions & 0 deletions app/src/Bundles/HighlightBundle/TwigHighlightExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Bundles\HighlightBundle;

use Tempest\Highlight\Highlighter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class TwigHighlightExtension extends AbstractExtension
{
# Regular expression pattern to extract content between <code> tags, with optional class attribute.
private const string CODE_CONTENT_PATTERN = '/<code(?: class="([^"]*)")?>(.*?)<\/code>/s';

public function __construct(
private readonly Highlighter $highlighter
) {
}

public function getFilters(): array
{
return [
new TwigFilter('highlight', [$this, 'highlight'], ['is_safe' => ['html']]),
];
}

public function highlight(?string $content, string $defaultLanguage = 'php'): string
{
if ($content === null) {
return '';
}

return preg_replace_callback(
self::CODE_CONTENT_PATTERN,
function ($matches) use ($defaultLanguage) {
[, $code_language, $code] = $matches;
$language = $code_language ?: $defaultLanguage;
$code = htmlspecialchars_decode($code);

return sprintf('<code>%s</code>', $this->highlighter->parse($code, $language));
},
$content
);
}
}
18 changes: 16 additions & 2 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,28 @@ footer > ul, footer > p {
padding: 0;
}

pre {
max-width: 100%;
overflow-x: auto;
width: 100%;
padding: 0.2em 0.4em;
border-radius: 0.375rem;
font-size: 85%;
background: rgba(135, 131, 120, 0.15);
}

pre code {
padding: 0;
background: transparent;
}

code {
font-family: Consolas, "PT Mono", "Liberation Mono", Courier, monospace;
line-height: normal;
background: rgba(135, 131, 120, 0.15);
color: #EB5757;
border-radius: 3px;
font-size: 85%;
padding: 0.2em 0.4em;
background: rgba(135, 131, 120, 0.15);
}

.alert {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"ext-gd": "*",
"ext-dom": "*",
"sculpin/sculpin": "3.3.0-alpha3",
"symfony/process": "^6.4|^7.0"
"symfony/process": "^6.4|^7.0",
"tempest/highlight": "^2.7"
},
"require-dev": {
"roave/security-advisories": "dev-latest"
Expand Down
Loading
Loading