ramsey/pygments
ramsey/pygments is a PHP wrapper for Pygments, the Python syntax highlighter, forked from the Pygments.php project.
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Requirements
- PHP 5.5.9 or greater (including PHP 7)
- Python
- Pygments (
pip install Pygments)
Python and Pygments versions supported:
| Pygments: | 1.6 | 2.0 | 2.1 | 2.2 |
|---|---|---|---|---|
| Python 2.6 | - | |||
| Python 2.7 | ||||
| Python 3.2 | - | - | - | |
| Python 3.3 | ||||
| Python 3.4 | ||||
| Python 3.5 | ||||
| Python 3.6 |
Installation
The preferred method of installation is via Packagist and Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:
composer require ramsey/pygments
Usage
Highlight source code
use Ramsey\Pygments\Pygments;
$pygments = new Pygments();
$html = $pygments->highlight(file_get_contents('index.php'), 'php', 'html');
$text = $pygments->highlight('package main', 'go', 'ansi');Generate CSS
use Ramsey\Pygments\Pygments;
$pygments = new Pygments();
$css = $pygments->getCss('monokai');
$prefixedCss = $pygments->getCss('default', '.syntax');Guesses lexer name
use Ramsey\Pygments\Pygments;
$pygments = new Pygments();
$pygments->guessLexer('foo.rb'); // rubyGet a list of lexers/formatters/styles
use Ramsey\Pygments\Pygments;
$pygments = new Pygments();
$pygments->getLexers()
$pygments->getFormatters();
$pygments->getStyles();Set a custom pygmentize path
use Ramsey\Pygments\Pygments;
$pygments = new Pygments('/path/to/pygmentize');Copyright and license
Copyrights for portions of ramsey/pygments are held by Kazuyuki Hayashi as part of Pygments.php. All other copyrights for ramsey/pygments are held by Ben Ramsey. Please see LICENSE for more information.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
