PHPackages                             mensbeam/lit - 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. mensbeam/lit

ActiveLibrary

mensbeam/lit
============

TextMate-like syntax highlighting in PHP

0.0.3(4y ago)013MITPHPPHP ^8.0

Since Jan 11Pushed 4y ago2 watchersCompare

[ Source](https://github.com/mensbeam/Lit)[ Packagist](https://packagist.org/packages/mensbeam/lit)[ RSS](/packages/mensbeam-lit/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Lit
===

[](#lit)

Lit is a multilanguage syntax highlighter written in PHP. It takes code as input and returns an HTML pre element containing the code highlighted using span elements with classes based upon tokens in the code. It is loosely based upon [Atom](https://atom.io)'s [Highlights](https://github.com/atom/highlights) which is used in the Atom text editor to syntax highlight code. Atom's Highlights is in turn based upon [TextMate](https://macromates.com)'s syntax highlighting using its concepts of scope selectors and common keywords for components of programming languages. Lit is not a port of Atom's Highlights but instead an independent implementation of what I can understand of TextMate's grammar syntax, parsing, and tokenization by analyzing other implementations. It aims to at least have feature parity or better with Atom's Highlights.

Warning Before Using
--------------------

[](#warning-before-using)

This library is experimental. The code is not tested at all, and writing tests for it will be an incredible undertaking because there's no specification whatsoever to test against. It would require creating a specification as well which is beyond the scope of a project that exists just to scratch an itch. Atom's Highlights is also barely tested itself for the same reason. There's numerous PHP libraries out there without a test suite; not having one is not up to our usual standards, though. So, that's why this warning exists.

Documentation
-------------

[](#documentation)

### MensBeam\\Lit\\Grammar::\_\_construct

[](#mensbeamlitgrammar__construct)

Creates a new `MensBeam\Lit\Grammar` object.

```
public function MensBeam\Lit\Grammar::__construct(?string $scopeName = null, ?array $patterns = null, ?string $name = null, ?array $injections = null, ?array $repository = null)
```

#### Parameters

[](#parameters)

In normal usage of the library the parameters won't be used (see `MensBeam\Lit\Grammar::loadJSON` and examples below for more information), but they are listed below for completeness' sake.

***scopeName*** - The scope name of the grammar
***patterns*** - The list of patterns in the grammar
***name*** - A human-readable name for the grammar
***injections*** - The list of injections in the grammar
***repository*** - The list of repository items in the grammar

### MensBeam\\Lit\\Grammar::loadJSON

[](#mensbeamlitgrammarloadjson)

Imports an Atom JSON grammar into the `MensBeam\Lit\Grammar` object.

```
public function MensBeam\Lit\Grammar::loadJSON(string $filename)
```

#### Parameters

[](#parameters-1)

***filename*** - The JSON file to be imported

### MensBeam\\Lit\\GrammarRegistry::clear

[](#mensbeamlitgrammarregistryclear)

Clears all grammars from the registry

```
public static function MensBeam\Lit\GrammarRegistry::clear()
```

### MensBeam\\Lit\\GrammarRegistry::get

[](#mensbeamlitgrammarregistryget)

Retrieves a grammar from the registry

```
public static function MensBeam\Lit\GrammarRegistry::get(string $scopeName): Grammar|false
```

#### Parameters

[](#parameters-2)

***scopeName*** - The scope name (eg: text.html.php) of the grammar that is being requested

#### Return Values

[](#return-values)

Returns a `MensBeam\Lit\Grammar` object on success and `false` on failure.

### MensBeam\\Lit\\GrammarRegistry::set

[](#mensbeamlitgrammarregistryset)

Retrieves a grammar from the registry

```
public static function MensBeam\Lit\GrammarRegistry::set(string $scopeName, MensBeam\Lit\Grammar $grammar): bool
```

#### Parameters

[](#parameters-3)

***scopeName*** - The scope name (eg: text.html.php) of the grammar that is being set
***grammar*** - The grammar to be put into the registry

#### Return Values

[](#return-values-1)

Returns `true` on success and `false` on failure.

### MensBeam\\Lit\\Highlight::toElement

[](#mensbeamlithighlighttoelement)

Highlights incoming string data and outputs a PHP `DOMElement`.

```
public static MensBeam\Lit\Highlight::toElement(string $data, string $scopeName, ?\DOMDocument $document = null, string $encoding = 'windows-1252'): \DOMElement
```

#### Parameters

[](#parameters-4)

***data*** - The input data string
***scopeName*** - The scope name (eg: text.html.php) of the grammar that's needed to highlight the input data
***document*** - An existing `DOMDocument` to use as the owner document of the returned `DOMElement`; if omitted one will be created instead
***encoding*** - The encoding of the input data string; only used if a document wasn't provided in the previous parameter, otherwise it uses the encoding of the existing `DOMDocument`; defaults to HTML standard default windows-1252

#### Return Values

[](#return-values-2)

Returns a `pre` `DOMElement`.

### MensBeam\\Lit\\Highlight::toString

[](#mensbeamlithighlighttostring)

Highlights incoming string data and outputs a string containing serialized HTML.

```
public static MensBeam\Lit\Highlight::toString(string $data, string $scopeName, string $encoding = 'windows-1252'): string
```

#### Parameters

[](#parameters-5)

***data*** - The input data string
***scopeName*** - The scope name (eg: text.html.php) of the grammar that's needed to highlight the input data
***encoding*** - The encoding of the input data string; defaults to HTML standard default windows-1252

#### Return Values

[](#return-values-3)

Returns a string.

Examples
--------

[](#examples)

Here's an example of highlighting PHP code:

```
$code =
CODE;

// Use UTF-8 as the encoding to preserve the emojis.
$element = MensBeam\Lit\Highlight::toElement($code, 'text.html.php', null, 'UTF-8');
$element->setAttribute('class', 'highlighted');

// Use PHP DOM's DOMDocument::saveHTML method to print the highlighted markup
// when finished with manipulating it.
echo $element->ownerDocument->saveHTML($element);
```

This will produce:

```
&lt;?php
echo "🐵 OOK! 🐵";
?&gt;
```

An already existing `DOMDocument` may be used as the owner document of the returned `pre` element:

```
...
$document = new DOMDocument();
// $element will be owned by $document.
$element = MensBeam\Lit\Highlight::toElement($code, 'text.html.php', $document);
```

Other DOM libraries which inherit from and/or encapsulate PHP's DOM such as [`MensBeam\HTML-DOM`](https://code.mensbeam.com/MensBeam/HTML) may also be used:

```
...
$document = new MensBeam\HTML\DOM\Document();
// $element will be owned by $document.
$element = MensBeam\Lit\Highlight::toElement($code, 'text.html.php', $document->innerNode);
$element = $element->ownerDocument->getWrapperNode($element);
// MensBeam\HTML\DOM\Element can simply be cast to a string to serialize.
$string = (string)$element;
```

Of course Lit can simply output a string, too:

```
...
$string = MensBeam\Lit\Highlight::toString($code, 'text.html.php');
```

Lit has quite a long list of out-of-the-bag supported languages, but sometimes other languages need to be highlighted:

```
...
// Import a hypothetical Ook Atom JSON language grammar into a Grammar object
// and add it to the registry.
$grammar = new MensBeam\Lit\Grammar;
$grammar->loadJSON('/path/to/source.ook.json');
MensBeam\Lit\GrammarRegistry::set($grammar->scopeName, $grammar);

// Now the grammar can be used to highlight code
$element = MensBeam\Lit\Highlight::toElement($code, $grammar->scopeName);
```

Supported Languages &amp; Formats
---------------------------------

[](#supported-languages--formats)

- AppleScript
- C
- C#
- C# Cake file
- C# Script file
- C++
- CoffeeScript
- CSS
- Diff
- Github Flavored Markdown
- Git config
- Go
- Go modules
- Go templates
- HTML
- Java
- Java expression language
- Java properties
- JavaScript
- JavaScript Regular Expressions
- JSDoc
- JSON
- Less
- Lua
- Makefile
- Markdown (CommonMark)
- Objective C
- Perl
- Perl 6
- PHP
- Plist
- Plist (XML, old-style)
- Python
- Python console
- Python Regular Expressions
- Python traceback
- Ruby
- Ruby gemfile
- Ruby on Rails (RJS)
- Rust
- Sass
- SassDoc
- SCSS
- Shell (Bash)
- Shell session (Bash)
- SQL
- SQL (Mustache)
- Textile
- Todo
- XML
- XSL

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

3

Last Release

1585d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/fa45644fa8847647e3d12802571072bbd4f3da8d27ed03f6f8df3c3859db6f6e?d=identicon)[dustinwilson](/maintainers/dustinwilson)

---

Top Contributors

[![dustinwilson](https://avatars.githubusercontent.com/u/1885928?v=4)](https://github.com/dustinwilson "dustinwilson (116 commits)")

### Embed Badge

![Health badge](/badges/mensbeam-lit/health.svg)

```
[![Health](https://phpackages.com/badges/mensbeam-lit/health.svg)](https://phpackages.com/packages/mensbeam-lit)
```

PHPackages © 2026

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