-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f002ef0
Showing
8 changed files
with
784 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Pygments; | ||
|
||
class FormatException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Pygments; | ||
|
||
class LexerException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Pygments; | ||
|
||
class StyleException extends \Exception | ||
{ | ||
} |
Oops, something went wrong.