Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
amouhzi committed Oct 10, 2014
0 parents commit f002ef0
Show file tree
Hide file tree
Showing 8 changed files with 784 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# IDE files
/.idea/

# Eclipse files
*.pydevproject
.metadata
.gradle
/tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse

# Composer files
composer.lock

# Vendor files
/vendor/
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Pygments for PHP
================

PHP Server side syntax highlighter based on [Pygments](http://pygments.org/ "") highlighter software.

## Installation:
To use this plugin you need pygments in your server:

```
sudo apt-get python-pygments
```

That's all. Now you can download the plugin via Composer or as independent library and use it.

## Usage

```php

$code = file_get_contents("test.js");
echo Pygmentize::format($code, "js");
```
##Options
Pygments::render($code, $language, $style, $linenumbers)
* `$code`: the source code to highlight
* `$language`: Language type. see section "Languages and filetypes supported" in this doc.
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "pygments/pygments",
"type": "library",
"description": "Syntax highlighter bridge for pygments",
"homepage": "https://github.com/php-mod/pygments",
"keywords": ["Syntax highlighter", "highlighter", "pygments"],
"license": "MIT",
"authors": [
{
"name": "Hassan Amouhzi",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.5.9"
},
"require-dev": {
"phpunit/phpunit": "4.3.*"
},
"autoload": {
"psr-0": {
"Pygments": "src/"
}
}
}
7 changes: 7 additions & 0 deletions src/Pygments/FormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Pygments;

class FormatException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Pygments/LexerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Pygments;

class LexerException extends \Exception
{
}
107 changes: 107 additions & 0 deletions src/Pygments/Pygmentize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Pygments;

/**
* Class Pygmentize
* @package Pygments
*/
class Pygmentize
{

const EXIT_SUCCESS = 0;

private static $bin = '/usr/bin/pygmentize';

public static function version()
{
$return = self::exec('-V');
$res = preg_match(
'/Pygments version ([^,]*), \(c\) (?:.*)/i',
$return,
$matches
);
if($res) {
return $matches[1];
} else {
return $return;
}
}

public static function lexers()
{
$return = self::exec('-L lexers');
preg_match_all('/\* (.*):(?:\s*)([^\(\*\n]*)/', $return, $matches);
$lexers = array();
foreach($matches[1] as $k=>$match) {
$list = explode(', ', $match);
foreach($list as $e) {
$lexers[$e] = $matches[2][$k];
}
}
return $lexers;
}

public static function styles()
{
$return = self::exec('-L styles');
preg_match_all('/\* (.*):(?:\s*)([^\n]*)/', $return, $matches);
$styles = array();
foreach($matches[1] as $k=>$match) {
$styles[$match] = $matches[2][$k];
}
return $styles;
}

public static function getStyle($style)
{
if(!array_key_exists($style, self::styles())) {
throw new StyleException('Style not supported: ' . $style);
}
return self::exec('-f html -S ' . $style);
}

public static function format($source, $lexer)
{
if(!array_key_exists($lexer, self::lexers())) {
throw new LexerException('Lexer not supported: ' . $lexer);
}
$format = self::exec('-f html -l ' . $lexer, $source);
preg_match('#<div class="highlight"><pre>(.*)\s</pre></div>#s', $format, $matches);
return $matches[1];
}

/**
* @param $options
* @param string $pipe
* @throws FormatException
* @throws \Exception
* @return string
*/
private static function exec($options, $pipe = null)
{
$cmd = $pipe ? 'echo ' . escapeshellarg($pipe) . ' | ' : '';
exec($cmd . self::$bin . ' ' . $options . ' 2>&1', $output, $returnVar);
$output = implode(PHP_EOL, $output);
if ($returnVar == self::EXIT_SUCCESS) {
return $output;
}
$error = self::parseError($output);
if($error == 'format') {
throw new FormatException($output);
} else {
throw new \Exception($output);
}
}

private static function parseError($output)
{
if(preg_match("/Error: No formatter found for name '(.*)'/", $output)) {
return 'format';
} else {
return 'error';
}
}
}


7 changes: 7 additions & 0 deletions src/Pygments/StyleException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Pygments;

class StyleException extends \Exception
{
}
Loading

0 comments on commit f002ef0

Please sign in to comment.