PHPackages                             php-school/keylighter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. php-school/keylighter

ActiveLibrary

php-school/keylighter
=====================

Yet another syntax highlighter for PHP

0.8.4(5y ago)05.8k↓50%3MITPHPPHP ^7.1.3 | ^8.0

Since Mar 27Pushed 5y agoCompare

[ Source](https://github.com/php-school/KeyLighter)[ Packagist](https://packagist.org/packages/php-school/keylighter)[ RSS](/packages/php-school-keylighter/feed)WikiDiscussions 0.8 Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (10)Used By (3)

TEMPORARY FORK OF  FOR PHP8 SUPPORT
============================================================================

[](#temporary-fork-of-httpsgithubcomkadet1090keylighter-for-php8-support)

[![Logo](https://camo.githubusercontent.com/57edf427c3261cef687bea8a6a8e15096e8beae4f5a734a3a34c28c5da12cf7f/687474703a2f2f6b616465742e6e65742f6b65796c6967687465722f6c6f676f2e706e67)](https://camo.githubusercontent.com/57edf427c3261cef687bea8a6a8e15096e8beae4f5a734a3a34c28c5da12cf7f/687474703a2f2f6b616465742e6e65742f6b65796c6967687465722f6c6f676f2e706e67)

[![Packagist](https://camo.githubusercontent.com/22dac123f322e0dbf82636c15ce6c5a801c7ff158104cc013278a4c7724fb6b9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b616465742f6b65796c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kadet/keylighter)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/2807265577345e670f33d9fec3433403134424fcdb9fcf4f049768726b6d205e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6b61646574313039302f6b65796c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kadet1090/KeyLighter/?branch=master)[![Travis build](https://camo.githubusercontent.com/ce2f7da16c66c57ee535ff37668b3c4abb08450f61a9bf54d20cb0228fbee96d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6b61646574313039302f4b65794c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/kadet1090/KeyLighter)[![Code Coverage](https://camo.githubusercontent.com/99fe64264984290176f3c77245ceca8ee2a8011b9dd89c43b596962a724b81fe/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b61646574313039302f6b65796c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/99fe64264984290176f3c77245ceca8ee2a8011b9dd89c43b596962a724b81fe/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b61646574313039302f6b65796c6967687465722e7376673f7374796c653d666c61742d737175617265)[![Try it](https://camo.githubusercontent.com/97e4abfd820de9e70ba1061b978f2f191854575a237f50cdaa696edc2d4a7e6c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7777772d74727925323069742d4646393730302e7376673f7374796c653d666c61742d737175617265)](http://keylighter.kadet.net/try)

[![stability: stable](https://camo.githubusercontent.com/0325dee88e824a796d878119659dc6645417188d1a6a25c604f5968c9f0f240c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5075626c69632532304150492d737461626c652d677265656e2e7376673f7374796c653d666c61742d737175617265)](Docs/usage.md)[![stability: unstable](https://camo.githubusercontent.com/692fe570281e95c9b0841ac1deaa81f61f2dee694d8f13fdc595c4a082d05fdd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f496e7465726e616c2532304150492d756e737461626c652d79656c6c6f772e7376673f7374796c653d666c61742d737175617265)](Docs/extending.md)

Yet another Syntax Highlighter in PHP meant to be as extensible and easy to use as it only can, but with performance in mind.

You can try it live with most recent version on .

Name
----

[](#name)

Name "KeyLighter" is inspired by Key Light concept in photography and cinematography.

> The key light is the first and usually most important light that a photographer, cinematographer, lighting cameraman, or other scene composer will use in a lighting setup. The purpose of the key light is to highlight the form and dimension of the subject.

KeyLighter is supposed to do the same thing - for code.

Installation
============

[](#installation)

```
$ composer require kadet/keylighter
```

To use **KeyLighter** you just need PHP 5.5 or later, no special extensions required.

Global installation
-------------------

[](#global-installation)

It's possible to install **KeyLighter** as a global composer library

```
$ composer global require kadet/keylighter
```

Then you can use builtin simple cli highlighting app:

```
$ keylighter [command = highlight] [-l|--language [LANGUAGE]] [-f|--format [FORMAT]] [-d|--debug [DEBUG]] [--]  ...
```

If you want pipe into **KeyLighter** just specify `php://stdin` as path. You can use `list` command to see all available commands, and `--help` argument for detailed help. You don't have to specify `highlight`command explicitly.

### PowerShell

[](#powershell)

You're using PowerShell on Windows? Cool! **KeyLighter** comes with integrated PowerShell module that makes CLI usage even better. Just import module (For example in profile), and you're ready to go.

```
PS> Import-Module "${env:APPDATA}\Composer\vendor\kadet\keylighter\bin\KeyLighter.psd1"
```

To use autocompletion features you will need PowerShell v5 (Comes with windows 10) or install [TabExpansion++](https://github.com/lzybkr/TabExpansionPlusPlus) Module.

[![Powershell Support](https://camo.githubusercontent.com/d4fe4f03cf9c8203149e13ddf8e9d3f34fa67d9abb7066ba71ff5461422f9136/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f36303032303130322f5368617265582f323031362d30332f323031362d30332d31395f32312d34342d35342d61322e706e67)](https://camo.githubusercontent.com/d4fe4f03cf9c8203149e13ddf8e9d3f34fa67d9abb7066ba71ff5461422f9136/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f36303032303130322f5368617265582f323031362d30332f323031362d30332d31395f32312d34342d35342d61322e706e67)

Why KeyLighter?
---------------

[](#why-keylighter)

### Simple to use

[](#simple-to-use)

```
use Kadet\Highlighter\Language;

echo \Kadet\Highlighter\highlight($source, new Language\Php(), $formatter);
// or
echo \Kadet\Highlighter\KeyLighter::get()->highlight($source, new Language\Php(), $formatter);
// or
$keylighter = new \Kadet\Highlighter\KeyLighter([options]);
echo $keylighter->highlight($source, new Language\Php(), $formatter);
```

You can find all available languages [here](https://github.com/kadet1090/KeyLighter/tree/master/Language) and formatters [here](https://github.com/kadet1090/KeyLighter/tree/master/Formatter).

### It works on CLI! And more!

[](#it-works-on-cli-and-more)

**KeyLighter** was originally designed as CLI highlighter for my own usage, but then I decided that it should be able to generate any possible output, currently supported:

#### Cli `\Kadet\Highlighter\Formatter\CliFormatter`

[](#cli-kadethighlighterformattercliformatter)

[![CLI](https://camo.githubusercontent.com/0abeeb5db04891de6338e819aa2509d0a51881df492323b199f45eab2e318835/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f36303032303130322f5368617265582f323031362d30332f323031362d30332d32375f31392d31392d32352d61662e706e67)](https://camo.githubusercontent.com/0abeeb5db04891de6338e819aa2509d0a51881df492323b199f45eab2e318835/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f36303032303130322f5368617265582f323031362d30332f323031362d30332d32375f31392d31392d32352d61662e706e67)

It can even be styled, default styles are stored in `Styles\Cli\Default.php`, but you can just pass additional argument into constructor:

```
new \Kadet\Highlighter\Formatter\CliFormatter([
    'string'      => ['color' => 'green'],
    'keyword'     => ['color' => 'yellow'],
    ...
])
```

#### HTML `\Kadet\Highlighter\Formatter\HtmlFormatter`

[](#html-kadethighlighterformatterhtmlformatter)

[![HTML](https://camo.githubusercontent.com/3c140ec36526e1ed5b6ba047ce187c74cf50c2b8ee9f2040113f58d865ccc282/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f36303032303130322f5368617265582f323031362d30332f323031362d30332d32375f31392d32372d31372d62312e706e67)](https://camo.githubusercontent.com/3c140ec36526e1ed5b6ba047ce187c74cf50c2b8ee9f2040113f58d865ccc282/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f36303032303130322f5368617265582f323031362d30332f323031362d30332d32375f31392d32372d31372d62312e706e67)

Every token is placed inside it's own `span` so it can be easily styled with css.

```
$maxOption
```

```
pre > span.variable { color: #F7750D; }
```

#### Your own?

[](#your-own)

It's easy to write your own formatters. Documentation coming soon.

### Context sensitive

[](#context-sensitive)

Some of tokens are valid in some contexts, some are not. This library is context sensitive and you can define when they are valid.

In this case, context mean just "inside of other token", for example lets assume that `string` token is defined as everything from " to the next " and `keyword` is defined as 'sit' substring.

```
↓ string:start     ↓ keyword:start
"Lorem ipsum dolor sit amtet"
         keyword:end ↑      ↑ string:end

Token tree:

Token.name           Token.pos
------------------------------
string:start         0
    keyword:start    21
    keyword:end      23
string:end           30
```

So as you can see `keyword` is inside of `string`, and therefore is not valid and should be deleted. You can define tokens valid only in some context, or invalid in other.

Oh, and token names cascade, it means that `string.single` is `string`, but `string` is necessarily not `string.single`.

### Write your own language definitions easily

[](#write-your-own-language-definitions-easily)

It's possible to easily extend `KeyLighter` with new languages, more detailed documentation coming soon.

For example XML definition looks like this:

```
class Xml extends GreedyLanguage
{
    const IDENTIFIER = '(?P[\w\.-]+:)?(?P[\w\.-]+)';

    /**
     * Tokenization rules
     */
    public function setupRules()
    {
        $this->rules->addMany([
            'tag.open'  => [
                new OpenRule(new RegexMatcher('/(:\s]/')),
                new CloseRule(new SubStringMatcher('>'), ['context' => ['!string', '!comment']])
            ],
            'tag.close' => new Rule(new RegexMatcher('/()/')),

            'symbol.tag' => new Rule(new RegexMatcher('/ '"'], ['context' => ['tag']]),
        ]);
    }

    /** {@inheritdoc} */
    public function getIdentifier()
    {
        return 'xml';
    }
}
```

I will try to write as many definitions as I only can, but any PRs are welcome.

### Embedding languages

[](#embedding-languages)

Many languages can be used simultaneously, *css* or *js* inside *html*, *sql* in *php* and so on. **KeyLighter** can handle and highlight embedded languages without any problem.

[![Embedded languages](https://camo.githubusercontent.com/5b14d535cedc1942f3f0a7bf99961426ff6f4c08fee265710e192b69067c80be/687474703a2f2f6b616465742e6e65742f6b65796c6967687465722f6c616e67756167652d656d62656464696e672e706e67)](https://camo.githubusercontent.com/5b14d535cedc1942f3f0a7bf99961426ff6f4c08fee265710e192b69067c80be/687474703a2f2f6b616465742e6e65742f6b65796c6967687465722f6c616e67756167652d656d62656464696e672e706e67)

### Fast [![Optimized for php7.0](https://camo.githubusercontent.com/fe5358cac95f7f2b0f277829f55a757cb575e93a0490083da0416860389fe1cd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f7074696d697a6564253230666f722d504850253230372d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/fe5358cac95f7f2b0f277829f55a757cb575e93a0490083da0416860389fe1cd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f7074696d697a6564253230666f722d504850253230372d3838393242462e7376673f7374796c653d666c61742d737175617265)

[](#fast-)

Even though it wasn't supposed to be fastest code highlighter in PHP it is still quite fast, up to few times faster than [GeSHi](http://geshi.org/). It performs best on PHP 7 (more than 2x faster than GeSHi in every case).

Testing [![Code Coverage](https://camo.githubusercontent.com/99fe64264984290176f3c77245ceca8ee2a8011b9dd89c43b596962a724b81fe/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b61646574313039302f6b65796c6967687465722e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/99fe64264984290176f3c77245ceca8ee2a8011b9dd89c43b596962a724b81fe/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b61646574313039302f6b65796c6967687465722e7376673f7374796c653d666c61742d737175617265)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#testing-)

**KeyLighter** uses `phpunit` for testing:

```
$ phpunit
```

Roadmap
-------

[](#roadmap)

There are still few things to do, you can find all on [trello](https://trello.com/b/9I4CO0Te/highlighter).

Contributing
------------

[](#contributing)

See [CONTRIBUTING.md](./CONTRIBUTING.md) for details.

Thanks
------

[](#thanks)

For [Maciej](https://github.com/ksiazkowicz), [Maciej](https://github.com/sobak) and Monika for all support, moral too.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 98.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~280 days

Recently: every ~394 days

Total

7

Last Release

2024d ago

PHP version history (2 changes)v0.7.0PHP &gt;=5.5.0

0.8.x-devPHP ^7.1.3 | ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/421c0adaa533effb16310b34396c544663167970aa9eb6940edb6ed1917c963d?d=identicon)[AydinHassan](/maintainers/AydinHassan)

---

Top Contributors

[![kadet1090](https://avatars.githubusercontent.com/u/747404?v=4)](https://github.com/kadet1090 "kadet1090 (273 commits)")[![AydinHassan](https://avatars.githubusercontent.com/u/2817002?v=4)](https://github.com/AydinHassan "AydinHassan (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/php-school-keylighter/health.svg)

```
[![Health](https://phpackages.com/badges/php-school-keylighter/health.svg)](https://phpackages.com/packages/php-school-keylighter)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
