2017 © Pedro Peláez
 

library psx

PHP CLI Syntax Highlighter

image

php-school/psx

PHP CLI Syntax Highlighter

  • Thursday, March 31, 2016
  • by AydinHassan
  • Repository
  • 3 Watchers
  • 13 Stars
  • 4,671 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

PHP CLI Syntax Highlighter

Build Status Windows Build Status Coverage Status Scrutinizer Code Quality, (*1)

This is a tool to syntax highlight PHP code for display on a terminal. It takes in an AST produced by nikic/php-parser and prints the code. It only decorates a subset of the language. It is literally a copy-paste of PrettyPrinter/Standard with some decorating added around the printing., (*2)

Usage

use PhpSchool\PSX\Factory;

$highlighterFactory = new Factory;
$highlighter = $highlighterFactory->__invoke();

echo $highlighter->highlight($phpCode);

The colouring by default uses kevinlebrun/colors.php. You can use any library you want by building an adapter class which implements \PhpSchool\PSX\ColourAdapterInterface, you will need to map the colours in \PhpSchool\PSX\Colours to your library., (*3)

Customising Colours

use PhpParser\ParserFactory;
use Colors\Color;
use PhpSchool\PSX\SyntaxHighlighter;
use PhpSchool\PSX\SyntaxHighlightPrinter;
use PhpSchool\PSX\SyntaxHighlighterConfig;
use PhpSchool\PSX\ColorsAdapter;

$parserFactory  = new ParserFactory;
$color          = new Color;
$color->setForceStyle(true);
$highlighter = new SyntaxHighlighter(
    $parserFactory->create(ParserFactory::PREFER_PHP7),
    new SyntaxHighlightPrinter(
        new SyntaxHighlighterConfig([
            SyntaxHighlighterConfig::TYPE_KEYWORD       => Colours::GREEN,
            SyntaxHighlighterConfig::TYPE_RETURN_NEW    => Colours::BLACK,
        ]),
        new ColorsAdapter($color)
    )
);

This will set any keywords as green and return & new statements as black., (*4)

Types

Types are defined in \PhpSchool\PSX\SyntaxHighlighterConfig Each type can have a it's own colour. Every keyword inside each type will be coloured by that colour., (*5)

TYPE_KEYWORD

  • if
  • elseif
  • else
  • for
  • foreach
  • while
  • do
  • switch
  • finally
  • try
  • catch
  • break
  • continue
  • throw
  • goto
  • function

TYPE_BRACE

  • {
  • }

TYPE_STRING

  • 'some-string'

TYPE_CONSTRUCT

  • echo

TYPE_RETURN_NEW

  • return
  • new

TYPE_VAR_DEREF

  • ->

TYPE_CALL_PARENTHESIS

  • (
  • )

TYPE_CLASS

  • SyntaxHighlighterConfig

TYPE_OPEN_TAG

  • <?php

The Versions

31/03 2016

dev-master

9999999-dev

PHP CLI Syntax Highlighter

  Sources   Download

MIT

The Requires

 

The Development Requires

31/03 2016

1.1.2

1.1.2.0

PHP CLI Syntax Highlighter

  Sources   Download

MIT

The Requires

 

The Development Requires

04/12 2015

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

PHP CLI Syntax Highlighter

  Sources   Download

MIT

The Requires

 

The Development Requires

04/12 2015

1.1.1

1.1.1.0

PHP CLI Syntax Highlighter

  Sources   Download

MIT

The Requires

 

The Development Requires