This commit is contained in:
Simon Vieille 2015-03-02 21:51:04 +01:00
commit 4ac14cd3c9
3 changed files with 178 additions and 0 deletions

BIN
DEBIAN/.control.swp Normal file

Binary file not shown.

8
DEBIAN/control Normal file
View file

@ -0,0 +1,8 @@
Package: walldeblan
Version: 0.1
Section: base
Priority: optional
Architecture: all
Depends: php5 (>= 5.3.3-7+squeeze15), php5-curl
Maintainer: Simon Vieille <simon@deblan.fr>
Description: Client en ligne de commande pour envoyer des scripts à colorer sur le service en ligne wall.deblan.org.

170
usr/bin/walldeblan Executable file
View file

@ -0,0 +1,170 @@
#!/usr/bin/php
<?php
class WallException extends Exception {}
class Wall
{
private $curl = null;
private $language = 'html';
private $code = null;
private $title = null;
private $showLines = true;
private static $instance = null;
public static function getInstance()
{
return (self::$instance === null) ? self::$instance = new Wall() : self::$instance;
}
public function __construct()
{
$this->curl = curl_init();
}
public function setCode($code)
{
$this->code = $code;
return $this;
}
public function setCodeFromFile($file)
{
$this->code = (file_exists($file) && is_readable($file)) ? file_get_contents($file) : '';
return $this;
}
public function getCode()
{
return $this->code;
}
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
public function getLanguage()
{
return $this->language;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setShowLines($showLines)
{
$this->showLines = (bool) $showLines;
return $this;
}
public function getShowLines()
{
return $this->showLines;
}
public function paste()
{
$options = array(
CURLOPT_URL => 'https://wall.deblan.org',
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(array(
'code' => $this->getCode(),
'language' => $this->getLanguage(),
'title' => $this->getTitle(),
'line' => $this->getShowLines()
))
);
curl_setopt_array($this->curl, $options);
$header = curl_exec($this->curl);
if (curl_errno($this->curl)) {
throw new WallException('Curl error: '.curl_error($this->curl));
} else {
$http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if (in_array($http_code, array(301, 302, 200))) {
preg_match('`location: ([^\s]+)`i', $header, $matches);
return 'https://wall.deblan.org'.trim(array_pop($matches));
} else {
throw new WallException(sprintf('HTTP header error: unexpected response code (%s)', $http_code));
}
}
}
}
function getHelp()
{
global $argv;
return <<<EOH
$argv[0] <file> [language]
Using "-" as filename for stdin.
Available languages:
"html" for HTML
"xml" for XML
"css" for CSS
"javascript" for JAVASCRIPT
"php" for PHP
"sql" for SQL
"yaml" for YAML
"perl" for PERL
"c" for C/C++
"asp" for ASP
"python" for PYTHON
"bash" for BASH
"actionscript" for ACTION SCRIPT
"texte" for TEXTE
EOH;
}
try {
if (!isset($argv[1])) {
throw new InvalidArgumentException('I miss the first parameter...');
}
Wall::getInstance()->setLanguage(isset($argv[2]) ? $argv[2] : 'texte');
if ($argv[1] == '-') {
$handle = fopen('php://stdin', 'r');
$code = "";
while (!feof($handle)) {
$code.= rtrim(fgets($handle)).PHP_EOL;
}
Wall::getInstance()->setCode($code);
} else {
Wall::getInstance()->setCodeFromFile($argv[1]);
}
echo Wall::getInstance()->paste(), PHP_EOL;
} catch (WallException $e) {
echo 'WALL ERROR: '.$e->getMessage();
echo getHelp();
} catch (Exception $e) {
echo 'SCRIPT ERROR: ', $e->getMessage(), PHP_EOL;
echo getHelp();
}