Skip to content

Commit f002ef0

Browse files
committed
first commit
0 parents  commit f002ef0

File tree

8 files changed

+784
-0
lines changed

8 files changed

+784
-0
lines changed

.gitignore

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# IDE files
2+
/.idea/
3+
4+
# Eclipse files
5+
*.pydevproject
6+
.metadata
7+
.gradle
8+
/tmp/
9+
*.tmp
10+
*.bak
11+
*.swp
12+
*~.nib
13+
local.properties
14+
.settings/
15+
.loadpath
16+
17+
# External tool builders
18+
.externalToolBuilders/
19+
20+
# Locally stored "Eclipse launch configurations"
21+
*.launch
22+
23+
# CDT-specific
24+
.cproject
25+
26+
# PDT-specific
27+
.buildpath
28+
29+
# sbteclipse plugin
30+
.target
31+
32+
# TeXlipse plugin
33+
.texlipse
34+
35+
# Composer files
36+
composer.lock
37+
38+
# Vendor files
39+
/vendor/

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Pygments for PHP
2+
================
3+
4+
PHP Server side syntax highlighter based on [Pygments](http://pygments.org/ "") highlighter software.
5+
6+
## Installation:
7+
To use this plugin you need pygments in your server:
8+
9+
```
10+
sudo apt-get python-pygments
11+
```
12+
13+
That's all. Now you can download the plugin via Composer or as independent library and use it.
14+
15+
## Usage
16+
17+
```php
18+
19+
$code = file_get_contents("test.js");
20+
echo Pygmentize::format($code, "js");
21+
```
22+
##Options
23+
Pygments::render($code, $language, $style, $linenumbers)
24+
* `$code`: the source code to highlight
25+
* `$language`: Language type. see section "Languages and filetypes supported" in this doc.

composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "pygments/pygments",
3+
"type": "library",
4+
"description": "Syntax highlighter bridge for pygments",
5+
"homepage": "https://github.com/php-mod/pygments",
6+
"keywords": ["Syntax highlighter", "highlighter", "pygments"],
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Hassan Amouhzi",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.5.9"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "4.3.*"
19+
},
20+
"autoload": {
21+
"psr-0": {
22+
"Pygments": "src/"
23+
}
24+
}
25+
}

src/Pygments/FormatException.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Pygments;
4+
5+
class FormatException extends \Exception
6+
{
7+
}

src/Pygments/LexerException.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Pygments;
4+
5+
class LexerException extends \Exception
6+
{
7+
}

src/Pygments/Pygmentize.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Pygments;
4+
5+
/**
6+
* Class Pygmentize
7+
* @package Pygments
8+
*/
9+
class Pygmentize
10+
{
11+
12+
const EXIT_SUCCESS = 0;
13+
14+
private static $bin = '/usr/bin/pygmentize';
15+
16+
public static function version()
17+
{
18+
$return = self::exec('-V');
19+
$res = preg_match(
20+
'/Pygments version ([^,]*), \(c\) (?:.*)/i',
21+
$return,
22+
$matches
23+
);
24+
if($res) {
25+
return $matches[1];
26+
} else {
27+
return $return;
28+
}
29+
}
30+
31+
public static function lexers()
32+
{
33+
$return = self::exec('-L lexers');
34+
preg_match_all('/\* (.*):(?:\s*)([^\(\*\n]*)/', $return, $matches);
35+
$lexers = array();
36+
foreach($matches[1] as $k=>$match) {
37+
$list = explode(', ', $match);
38+
foreach($list as $e) {
39+
$lexers[$e] = $matches[2][$k];
40+
}
41+
}
42+
return $lexers;
43+
}
44+
45+
public static function styles()
46+
{
47+
$return = self::exec('-L styles');
48+
preg_match_all('/\* (.*):(?:\s*)([^\n]*)/', $return, $matches);
49+
$styles = array();
50+
foreach($matches[1] as $k=>$match) {
51+
$styles[$match] = $matches[2][$k];
52+
}
53+
return $styles;
54+
}
55+
56+
public static function getStyle($style)
57+
{
58+
if(!array_key_exists($style, self::styles())) {
59+
throw new StyleException('Style not supported: ' . $style);
60+
}
61+
return self::exec('-f html -S ' . $style);
62+
}
63+
64+
public static function format($source, $lexer)
65+
{
66+
if(!array_key_exists($lexer, self::lexers())) {
67+
throw new LexerException('Lexer not supported: ' . $lexer);
68+
}
69+
$format = self::exec('-f html -l ' . $lexer, $source);
70+
preg_match('#<div class="highlight"><pre>(.*)\s</pre></div>#s', $format, $matches);
71+
return $matches[1];
72+
}
73+
74+
/**
75+
* @param $options
76+
* @param string $pipe
77+
* @throws FormatException
78+
* @throws \Exception
79+
* @return string
80+
*/
81+
private static function exec($options, $pipe = null)
82+
{
83+
$cmd = $pipe ? 'echo ' . escapeshellarg($pipe) . ' | ' : '';
84+
exec($cmd . self::$bin . ' ' . $options . ' 2>&1', $output, $returnVar);
85+
$output = implode(PHP_EOL, $output);
86+
if ($returnVar == self::EXIT_SUCCESS) {
87+
return $output;
88+
}
89+
$error = self::parseError($output);
90+
if($error == 'format') {
91+
throw new FormatException($output);
92+
} else {
93+
throw new \Exception($output);
94+
}
95+
}
96+
97+
private static function parseError($output)
98+
{
99+
if(preg_match("/Error: No formatter found for name '(.*)'/", $output)) {
100+
return 'format';
101+
} else {
102+
return 'error';
103+
}
104+
}
105+
}
106+
107+

src/Pygments/StyleException.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Pygments;
4+
5+
class StyleException extends \Exception
6+
{
7+
}

0 commit comments

Comments
 (0)