Pygments |
|
---|---|
|
|
This file is part of the Pinocchio library. (c) José Nahuel Cuesta Luengo nahuelcuestaluengo@gmail.com For the full copyright and license information, please view the LICENSE file that was distributed with this source code. |
namespace Pinocchio\Highlighter;
use \Clinner\Command\Command;
use \Guzzle\Http\Client;
|
Pygments highlighter. @author José Nahuel Cuesta Luengo nahuelcuestaluengo@gmail.com |
class Pygments implements HighlighterInterface
{
const PYGMENTS_BINARY_NAME = 'pygmentize';
const PYGMENTS_SERVICE_URL = 'http://pygments.appspot.com/';
|
Whether the web service should be used or not. @var |
private $useService;
|
Constructor. Initializes the state of the instance. |
public function __construct()
{
$this->useService = !$this->binaryExists();
}
|
Checks the existence of the pygments binary on the target system. @return |
public function binaryExists()
{
$binaryExists = Command::create(self::PYGMENTS_BINARY_NAME)
->run()
->getExitCode() === 0;
return $binaryExists;
}
|
Highlight @param @param @return |
public function highlight($language, $code)
{
return $this->useService
? $this->runRemotely($language, $code)
: $this->runLocally($language, $code);
}
|
Get an Http Client. @return |
protected function getHttpClient()
{
return new Client();
}
|
Pygmentize @param @param @return |
protected function runRemotely($language, $code)
{
$postBody = array(
'lang' => $language,
'code' => $code,
);
$response = $this->getHttpClient()
->post(self::PYGMENTS_SERVICE_URL, null, $postBody)
->send();
return $response->getBody(true);
}
|
Pygmentize @param @param @return |
protected function runLocally($language, $code)
{
$args = array(
'-l' => $language,
'-f' => 'html',
);
$opts = array('delimiter' => ' ');
$response = Command::create(self::PYGMENTS_BINARY_NAME, $args, $opts)
->run($code)
->getOutput();
return $response;
}
}
|