Skip to content

Languages

phil/highlight

Every language is asked for by its name or one of its aliases, in any case: Highlighter::default()->toHtml($code, 'TS') reads TypeScript.

LanguageNameAliasesTokenizer
Bash and other shellsbashsh, shell, zsh, kshCode with Definitions::bash()
CchCode with Definitions::c()
C++cppc++, cc, cxx, hpp, hh, hxxCode with Definitions::cpp()
C#csharpcs, c#, dotnetCode with Definitions::csharp()
CSScss—StyleSheet
Diffs and patchesdiffpatch, udiffDiff
Dockerfiledockerfiledocker, containerfileDockerfile, arguments read as shell
GogogolangCode with Definitions::go()
HTMLhtmlhtm, xhtml, vue, svelteMarkup, scripts as JavaScript and styles as CSS
INI, .env, propertiesinidosini, cfg, conf, properties, env, dotenv, editorconfig, gitconfigIni
JavajavajspCode with Definitions::java()
JavaScriptjavascriptjs, mjs, cjs, jsx, nodeCode with Definitions::javascript()
JSONjsonjsonc, json5, geojson, webmanifestCode with Definitions::json()
Markdownmarkdownmd, mkd, mdx, commonmark, gfmMarkdown
PHPphpphp8, phtmlPhp: code with Definitions::php(), markup as HTML
Pythonpythonpy, py3, python3, gypCode with Definitions::python()
Rubyrubyrb, gemspec, podspec, rakeCode with Definitions::ruby()
RustrustrsCode with Definitions::rust()
SCSSscssless, sassStyleSheet(scss: true)
SQLsqlmysql, postgresql, postgres, pgsql, sqlite, plsql, tsql, mariadbCode with Definitions::sql()
Twigtwightml.twig, jinja, jinja2, nunjucks, njk, liquidTwig: expressions by Definitions::twigExpression(), text as HTML
TypeScripttypescriptts, tsx, mts, ctsCode with Definitions::typescript()
XMLxmlsvg, xsl, xslt, xsd, rss, atom, plist, wsdl, xaml, mathmlMarkup
YAMLyamlymlYaml

Programming languages

Code reads a source once and, at each position, tries in order: spaces, meta marks, a preprocessor line, comments, quoted texts, variables, annotations, symbols, numbers, words, regular expressions, operators and punctuation.

WhatTypeLanguages
Reserved wordskeywordAll; SQL in any case
Type names, and names that start with a capitaltypeAll with a list; capitals in PHP, JavaScript, TypeScript, Python, Ruby, Rust, Java, C#
true, null, None, nil…constantAll
Functions the language provides that are often called without parenthesesbuiltinShell (echo, cd…), Python, Ruby, Go, SQL, JavaScript (console)
A name followed by (, a Rust macro (vec!), a Twig filter (`upper`)function
$var, ${var}, $?, @var, @@varvariablePHP, shell, Ruby, SQL
Quoted texts, with their prefixes (f"…", b"…", u8"…", $"…", @"…") and character literalsstringAll; a Rust lifetime ('a) is not a character
JSON keyspropertyJSON
Numbers: 0x1F, 1_000u32, .5, 1e-3numberAll
Comments: line, blockcommentAll
Regular expression literals where a value is expectedregexJavaScript, TypeScript, Ruby
Decorators and annotations (@Override), PHP and Rust attributes (#[…]), PHP tags, preprocessor lines, shebangsmetaPython, JavaScript, TypeScript, Java; PHP, Rust; C, C++, C#; shell, Python, Ruby, JavaScript
Ruby symbols (:name)constantRuby

A Ruby method name may end with ? or !, as empty?. A quote that may span lines does: JavaScript template literals, Python triple quotes, Java and C# text blocks, Go raw strings, shell and SQL strings. A doubled quote in SQL ('it''s') reads as one string.

Markup

Markup reads tags, attributes and their values, comments, character references (&amp;, &#38;), CDATA sections, doctypes and processing instructions. In HTML the content of <script> is read as JavaScript and that of <style> as CSS, up to the end tag in any case.

Style sheets

StyleSheet reads each statement as an at-rule (@media, a keyword), a selector or a declaration: at the top level a statement is a selector; inside a block it is a nested selector when a { comes before the next ; or }, and a declaration otherwise. A property is a property, a custom property (--x) a variable; in a value, numbers with their units and colours (#fff) are number, functions function, !important a keyword, and the address in url(), quoted or not, a string. SCSS adds $variables, #{…} interpolations and line comments.

Line languages

LanguageWhat it colours
YAMLComments, --- and ..., sequence dashes, keys plain or quoted, quoted and plain scalars — a constant (true, yes, on, null, ~…), a number or a string —, anchors and aliases, tags, flow collections, and the lines of block scalars (`
INIComments opened by ; or #, [sections], keys, = and :, values — quoted, numbers, constants or strings —, and a comment after a value and a space
DockerfileComments, the instruction that opens a line in any case, and its arguments, continued by a backslash, read as shell
DiffFile headers (diff, index, ---, +++), hunk headers (@@), lines added (+, >) and removed (-, <), and \ No newline notes
MarkdownFenced code blocks, headings, thematic breaks, quote and list marks, task boxes; code spans, strong emphasis, emphasis, links, images and autolinks closed on their line

Mixed languages

Php reads a source with no opening tag as code; otherwise markup up to each <?php or <?=, and code up to the next ?>. Twig reads comments, the delimiters of prints and tags with their whitespace controls, the expressions between them, and the text around them as HTML. Each stretch of markup is read on its own: a tag cut by a Twig expression — <a href="{{ url }}"> — loses some colours after the cut.

What is left alone

The tokenizers colour what a reader needs; they do not parse. Heredocs and nowdocs, nested block comments, Rust raw strings with # delimiters, C++ raw strings, and expressions interpolated inside strings take the colour of what holds them. Markdown inline marks must close on their line. A source of one language read as another never fails: it comes back with fewer colours.