ID

Phiki

Phiki is a PHP package for syntax highlighting code. It is designed as a replacement for other syntax highlighters like Shiki and Highlight.php. A key feature of Phiki is that it has zero third-party dependencies, which makes it easy to integrate into any PHP project.

Overview

Phiki’s core functionality involves highlighting code using TextMate grammar files and Visual Studio Code themes. This approach aims to achieve a high degree of highlighting accuracy, making code appear as if highlighted in a preferred code editor.

How it works

Phiki processes code through a five-step internal pipeline:

  1. Tokenization
    Phiki begins by tokenizing your code using a TextMate grammar file. This process breaks the code into smaller pieces of text, each with detailed “scope” information. A scope is a dot-separated string that describes the type of text, for example, variable.other.php for a PHP variable.

  2. Highlighting
    After tokenization, Phiki highlights the tokens using a chosen Visual Studio Code theme. The scopes from each token are matched against a ruleset in the theme file. This ruleset defines the background color, foreground color, and font style for the token. The tokens are then transformed into a HighlightedToken object, which contains both the raw token and its styling information.

  3. Structuring
    Following the highlighting, Phiki then creates an AST-like structure (Abstract Syntax Tree-like) to represent the desired HTML output. This intermediate structure allows for easier manipulation of the final output without complex string operations.

  4. Transformation
    If necessary, Phiki can transform this intermediate structure. This stage is typically where extensions can hook into the process to modify the AST and add features such as line highlighting or custom class names.

  5. Rendering
    Finally, Phiki “stringifies” the completed AST into HTML. This HTML can then be directly outputted to a browser, included in a template file, or saved to disk.

Why it exists

Phiki was developed to address shortcomings found in existing syntax highlighting solutions.

Phiki aims to solve these issues by offering a pure PHP implementation of both a TextMate tokenizer and a Visual Studio Code highlighter. This provides high-quality highlighting without the need for any third-party dependencies or services. The creator also mentions a personal interest in building and pushing the limits of what is possible with PHP.

Key concepts and features

Integrations

Phiki supports integrations with various platforms and tools, including:

Checkout Phiki —>