Download the PHP package mensbeam/lit without Composer
On this page you can find all versions of the php package mensbeam/lit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package 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's Highlights which is used in the Atom text editor to syntax highlight code. Atom's Highlights is in turn based upon TextMate'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
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
MensBeam\Lit\Grammar::__construct
Creates a new MensBeam\Lit\Grammar
object.
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
Imports an Atom JSON grammar into the MensBeam\Lit\Grammar
object.
Parameters
filename - The JSON file to be imported
MensBeam\Lit\GrammarRegistry::clear
Clears all grammars from the registry
MensBeam\Lit\GrammarRegistry::get
Retrieves a grammar from the registry
Parameters
scopeName - The scope name (eg: text.html.php) of the grammar that is being requested
Return Values
Returns a MensBeam\Lit\Grammar
object on success and false
on failure.
MensBeam\Lit\GrammarRegistry::set
Retrieves a grammar from the registry
Parameters
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
Returns true
on success and false
on failure.
MensBeam\Lit\Highlight::toElement
Highlights incoming string data and outputs a PHP DOMElement
.
Parameters
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
Returns a pre
DOMElement
.
MensBeam\Lit\Highlight::toString
Highlights incoming string data and outputs a string containing serialized HTML.
Parameters
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
Returns a string.
Examples
Here's an example of highlighting PHP code:
This will produce:
An already existing DOMDocument
may be used as the owner document of the returned pre
element:
Other DOM libraries which inherit from and/or encapsulate PHP's DOM such as MensBeam\HTML-DOM
may also be used:
Of course Lit can simply output a string, too:
Lit has quite a long list of out-of-the-bag supported languages, but sometimes other languages need to be highlighted:
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