|
| 1 | +# ALTO \ Code Highlight |
| 2 | + |
| 3 | +**Syntax highlighting for PHP projects** |
| 4 | + |
| 5 | +Server-side highlighting for the full PHP stack: [27 languages](#languages) |
| 6 | +covering PHP, HTML, Twig, JavaScript, CSS, YAML, and |
| 7 | +more. [Zero dependencies](#install), [semantic PHP syntax](#features) |
| 8 | +understanding definitions vs. calls, [embedded language support](#embeddings), |
| 9 | +and [490+ compatible themes](#compatibility). Works |
| 10 | +in [Twig templates](#integrations), Laravel Blade, Symfony controllers—anywhere |
| 11 | +PHP runs. |
| 12 | + |
| 13 | +[](https://github.com/phpalto/code-highlight) |
| 14 | +[](https://github.com/phpalto/code-highlight) |
| 15 | +[](https://www.php.net) |
| 16 | +[](LICENSE) |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- **Built for PHP ecosystems:** Highlight the full stack—PHP, HTML, Twig, |
| 21 | + JavaScript, CSS, SQL, YAML, and 20+ more languages in a single library. |
| 22 | +- **Zero dependencies:** No Node.js, Python, or external processes. Just |
| 23 | + Composer. Works in any PHP 8.4+ environment. |
| 24 | +- **Full PHP syntax:** Semantic parser that understands context—distinguishes |
| 25 | + `class User` (definition) from `new User()` (usage), `function greet()` ( |
| 26 | + definition) from `greet()` (call). |
| 27 | +- **Embedded languages:** Automatically switches parsers for `<style>` and |
| 28 | + `<script>` tags in HTML, fenced code blocks in Markdown, and `{% block css %}` |
| 29 | + in Twig. |
| 30 | +- **Dark mode support:** Multiple dark themes included out of the box ( |
| 31 | + CupertinoDark, Dracula, Noctis) plus compatibility with 490+ Highlight.js and |
| 32 | + Prism themes. |
| 33 | + |
| 34 | +## Install |
| 35 | + |
| 36 | +```bash |
| 37 | +composer require alto/code-highlight |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +```php |
| 43 | +use Alto\Code\Highlight\Highlighter; |
| 44 | +use Alto\Code\Highlight\Theme\GitHubTheme; |
| 45 | + |
| 46 | +// 1. Initialize with a theme |
| 47 | +$highlighter = new Highlighter(new GitHubTheme()); |
| 48 | + |
| 49 | +// 2. Output the theme's CSS (typically in your <head>) |
| 50 | +echo "<style>" . $highlighter->getTheme()->getStylesheet() . "</style>"; |
| 51 | +
|
| 52 | +// 3. Highlight your code |
| 53 | +echo $highlighter->highlight($code, 'php'); |
| 54 | +``` |
| 55 | +
|
| 56 | +## Languages |
| 57 | +
|
| 58 | +### PHP |
| 59 | +
|
| 60 | +Alto uses a semantic parser for PHP that goes beyond pattern matching to |
| 61 | +understand code context. It correctly distinguishes between: |
| 62 | +
|
| 63 | +- **Definitions vs. usage:** `class User` vs. `new User()`, `function greet()` |
| 64 | + vs. `greet()` |
| 65 | +- **Context-aware scoping:** Variables, function calls, class instantiation, |
| 66 | + method calls |
| 67 | +
|
| 68 | +### Embeddings |
| 69 | +
|
| 70 | +Four languages support automatic embedded language detection: |
| 71 | +
|
| 72 | +- **HTML** — CSS in `<style>` tags, JavaScript in `<script>` tags |
| 73 | +- **SVG** — CSS in `<style>` tags, JavaScript in `<script>` tags |
| 74 | +- **Markdown** — Any language in fenced code blocks (` ```language `) |
| 75 | +- **Twig** — Languages via block names (`{% block css %}`, |
| 76 | + `{% block javascript %}`) |
| 77 | +
|
| 78 | +### Full list |
| 79 | +
|
| 80 | +| Category | Languages | |
| 81 | +|-----------------|----------------------------------------------------------------------------| |
| 82 | +| **Programming** | Bash, C#, Go, Java, JavaScript, PHP, Python, Ruby, Rust, Swift, TypeScript | |
| 83 | +| **Markup** | HTML, SVG, XML | |
| 84 | +| **Data** | Diff, DotEnv, HTTP, JSON, YAML | |
| 85 | +| **Prose** | Markdown | |
| 86 | +| **Query** | SQL | |
| 87 | +| **Stylesheet** | CSS, SCSS | |
| 88 | +| **Template** | Twig | |
| 89 | +| **Config** | Dockerfile, INI, Makefile | |
| 90 | +
|
| 91 | +## Themes |
| 92 | +
|
| 93 | +### Built-in themes |
| 94 | +
|
| 95 | +Alto includes 7 built-in themes ready to use: |
| 96 | +
|
| 97 | +- **Light themes:** `Alto`, `GitHub`, `Polar`, `Solar` |
| 98 | +- **Dark themes:** `CupertinoDark`, `Dracula`, `Noctis` |
| 99 | +
|
| 100 | +```php |
| 101 | +use Alto\Code\Highlight\Theme\DraculaTheme; |
| 102 | +
|
| 103 | +$highlighter = new Highlighter(new DraculaTheme()); |
| 104 | +``` |
| 105 | +
|
| 106 | +### Dark mode |
| 107 | +
|
| 108 | +Three dark themes are included out of the box: |
| 109 | +
|
| 110 | +- **CupertinoDark** — macOS-inspired dark theme |
| 111 | +- **Dracula** — Popular dark theme with vibrant colors |
| 112 | +- **Noctis** — Low-contrast dark theme for extended coding sessions |
| 113 | +
|
| 114 | +### Compatibility |
| 115 | +
|
| 116 | +Use existing CSS from the **Highlight.js** (240+ themes), **Prism** (250+ |
| 117 | +themes), or **TextMate** (.tmTheme) ecosystems: |
| 118 | +
|
| 119 | +```php |
| 120 | +use Alto\Code\Highlight\Adapter\HighlightJsThemeAdapter; |
| 121 | +
|
| 122 | +$theme = HighlightJsThemeAdapter::fromFile('/path/to/github-dark.css'); |
| 123 | +$highlighter = new Highlighter($theme); |
| 124 | +``` |
| 125 | +
|
| 126 | +```php |
| 127 | +use Alto\Code\Highlight\Adapter\TextMateThemeAdapter; |
| 128 | +
|
| 129 | +$theme = TextMateThemeAdapter::fromFile('/path/to/monokai.tmTheme', isDark: true); |
| 130 | +$highlighter = new Highlighter($theme); |
| 131 | +``` |
| 132 | +
|
| 133 | +## Integrations |
| 134 | +
|
| 135 | +### Twig Extension |
| 136 | +
|
| 137 | +**[Twig Extension](https://github.com/phpalto/twig-code-highlight)** — Highlight |
| 138 | +code directly in Twig templates using blocks or filters. |
| 139 | +
|
| 140 | +## Contributing |
| 141 | +
|
| 142 | +Contributions are welcome! Please feel free |
| 143 | +to [submit issues](https://github.com/phpalto/code-highlight/issues) |
| 144 | +or [pull requests](https://github.com/phpalto/code-highlight/pulls). |
| 145 | +
|
| 146 | +## License |
| 147 | +
|
| 148 | +Released by the [Alto project](https://github.com/phpalto) under the MIT |
| 149 | +License. |
| 150 | +See the [LICENSE](LICENSE) file for details. |
0 commit comments