Phiki allows you to modify data at various stages in the syntax highlighting process. We refer to these as “transformers” and they can be used to modify the input code, individual tokens, the HTML AST and the final HTML output and the return values from each “hook” are passed to the next. All transformers must implement the Phiki\Contracts\TransformerInterface interface.
If you only need to modify data at a specific stage, you can extend the Phiki\Transformers\AbstractTransformer class instead and override the methods you need.

Using transformers

To use a transformer, provide an instance of one to the PendingHtmlOutput::transformer() method when highlighting code.
use Phiki\Phiki;
use Phiki\Theme\Theme;
use Phiki\Transformers\AbstractTransformer;

class MyTransformer extends AbstractTransformer
{
    // Implement the methods you need here...
}

$html = (new Phiki)
    ->codeToHtml("<?php echo ...", Grammar::Php, Theme::GithubLight)
    ->transformer(new MyTransformer)
    ->toString();
Transformers are applied in the order they are added. If you add mutliple transformers, the output from one will be passed to the next.Be careful when modifying the HTML AST, especially if you are adding or removing elements, as this can break the structure expected by later transformers.

Available hooks

MethodDescription
preprocessModify the input code before it is tokenized.
tokensModify the array of tokens after tokenization but before highlighting.
highlightedModify the array of highlighted tokens after highlighting but before structuring.
rootModify the root node of the HTML AST, usually contains a single <pre> element.
preModify the <pre> element in the HTML AST, usually contains a single <code> element.
codeModify the <code> element in the HTML AST, usually contains 1 <span> element for each line.
lineModify each line element in the HTML AST, usually a <span> element.
gutterModify the gutter element in the HTML AST, usually a <span> element.
tokenModify each token element in the HTML AST, usually a <span> element.
postprocessModify the final HTML output before it is returned as a string.