Languages
The default registry contains 27 languages. Pass the identifier in the
Identifier column to Highlighter::highlight().
Identifiers are case-insensitive after trimming, but there are no short
aliases. Use javascript, not js; typescript, not ts; bash, not sh;
yaml, not yml; and csharp, not cs.
Default registry
| Language | Identifier | Category | Typical extension | Parsing focus | Example |
|---|---|---|---|---|---|
| Bash | bash |
Shell | .sh |
Shell keywords, built-ins, variables, strings, and here-docs | Source |
| C# | csharp |
Programming | .cs |
Two-pass definitions, calls, and type references | Source |
| CSS | css |
Stylesheet | .css |
Selectors, declarations, values, comments, and at-rules | Source |
| Diff | diff |
Change | .diff |
File headers, hunks, added lines, and removed lines | Source |
| Dockerfile | dockerfile |
Build | Dockerfile |
Instructions, modifiers, variables, strings, and comments | Source |
| dotenv | dotenv |
Configuration | .env |
Assignments, export, references, values, and comments |
Source |
| Go | go |
Programming | .go |
Two-pass functions, methods, receivers, and type references | Source |
| HTML | html |
Markup | .html |
Tags, attributes, text, and embedded CSS or JavaScript | Source |
| HTTP | http |
Protocol | .http |
Request or response lines, headers, and body text | Source |
| INI | ini |
Configuration | .ini |
Sections, assignments, typed values, references, and comments | Source |
| Java | java |
Programming | .java |
Two-pass definitions, calls, types, and brace context | Source |
| JavaScript | javascript |
Programming | .js |
Declarations, calls, literals, templates, and regular expressions | Source |
| JSON | json |
Data | .json |
Object keys, strings, numbers, booleans, and null | Source |
| Makefile | makefile |
Build | .mk |
Targets, dependencies, recipes, variables, and directives | Source |
| Markdown | markdown |
Markup | .md |
Blocks, inline markup, and registered fenced languages | Source |
| PHP | php |
Programming | .php |
Two-pass semantic definitions, calls, types, and variables | Source |
| Python | python |
Programming | .py |
Two-pass function and class definitions, calls, and references | Source |
| Ruby | ruby |
Programming | .rb |
Two-pass definitions, calls, constants, and variables | Source |
| Rust | rust |
Programming | .rs |
Two-pass definitions, calls, types, lifetimes, and macros | Source |
| SCSS | scss |
Stylesheet | .scss |
CSS plus variables, nesting, mixins, and interpolation | Source |
| SQL | sql |
Data | .sql |
Keywords, functions, identifiers, literals, and comments | Source |
| SVG | svg |
Markup | .svg |
XML-style markup plus embedded CSS or JavaScript | Source |
| Swift | swift |
Programming | .swift |
Two-pass definitions, calls, types, attributes, and variables | Source |
| Twig | twig |
Templating | .twig |
Twig expressions and tags with embedded HTML by default | Source |
| TypeScript | typescript |
Programming | .ts |
JavaScript plus types, interfaces, enums, and decorators | Source |
| XML | xml |
Markup | .xml |
Tags, attributes, processing instructions, and CDATA | Source |
| YAML | yaml |
Data | .yaml |
Mappings, sequences, anchors, aliases, values, and comments | Source |
The source files above are the canonical compact documentation examples. See Examples for generated previews.
PHP snippets without an opening tag
php-snippet is a convenience identifier handled by Highlighter; it is not
a 28th registered language. It lets you highlight PHP fragments that omit the
opening tag:
<?php
$html = $highlighter->highlight(
'$total = array_sum($prices);',
'php-snippet',
);The highlighter temporarily prepends <?php , parses the fragment as php,
and omits the synthetic opening tag from the result. The output uses the
language-php class.
Register a custom language
Custom parsers implement LanguageInterface and return a ParsedStream:
<?php
use Alto\Code\Highlight\Language\LanguageInterface;
use Alto\Code\Highlight\Parser\ParsedStream;
use Alto\Code\Highlight\Parser\ParsedToken;
use Alto\Code\Highlight\Scope;
final class MyLanguage implements LanguageInterface
{
public function getIdentifier(): string
{
return 'my-language';
}
public function parse(string $code): ParsedStream
{
return new ParsedStream([
new ParsedToken($code, Scope::MarkupText),
]);
}
}
$highlighter->registerLanguage(new MyLanguage());Registering an existing identifier replaces that parser on the highlighter instance. Theme authors style the generic semantic scopes emitted by parsers; see Creating a theme.