Skip to content

yiipress/highligher

Repository files navigation

YiiPress Highlighter

YiiPress Highlighter PHP Extension


Latest Stable Version Total Downloads Tests Windows

The package provides a native PHP extension for fast server-side syntax highlighting powered by syntect.

Requirements

  • PHP 8.1 - 8.5.
  • PIE to install the extension.
  • On Linux and macOS: cargo, phpize, php-config, C compiler, and make.

Prebuilt Windows DLLs are attached to GitHub releases.

Installation

Install the extension with PIE:

pie install yiipress/highlighter

PIE installs the native extension into the target PHP installation. If the extension is not enabled automatically, add it to your php.ini:

extension=highlighter

In an application, use Composer to declare that the extension must be available:

composer require ext-highlighter:*

Composer does not build or enable native PHP extensions, so composer require yiipress/highlighter is not an installation substitute for PIE.

General usage

Highlight an HTML fragment containing code blocks in the form <pre><code class="language-php">...</code></pre>:

<?php

declare(strict_types=1);

use YiiPress\Highlighter;

$highlighter = new Highlighter();

$html = $highlighter->highlightHtml(
    '<pre><code class="language-php">&lt;?php echo "Hello";</code></pre>',
);

Highlight a raw code string by passing the language name explicitly:

<?php

declare(strict_types=1);

use YiiPress\Highlighter;

$highlighter = new Highlighter();

$html = $highlighter->highlight('echo "Hello";', 'php');

Set a different default theme for a highlighter instance:

<?php

declare(strict_types=1);

use YiiPress\Highlighter;

$highlighter = new Highlighter('Solarized (dark)');

Pass a theme for a single call:

<?php

declare(strict_types=1);

use YiiPress\Highlighter;

$highlighter = new Highlighter();

$html = $highlighter->highlight($code, 'php', 'base16-mocha.dark');

When no theme is specified, InspiredGitHub is used.

Themes available by default are:

  • base16-ocean.dark
  • base16-eighties.dark
  • base16-mocha.dark
  • base16-ocean.light
  • InspiredGitHub
  • Solarized (dark)
  • Solarized (light)

You can also pass a .tmTheme file path as a theme name:

<?php

declare(strict_types=1);

use YiiPress\Highlighter;

$highlighter = new Highlighter('/path/to/theme.tmTheme');

Use class_exists(YiiPress\Highlighter::class) to check whether the extension is loaded in the current PHP process.

License

YiiPress Highlighter PHP Extension is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.