This commit is contained in:
Simon Vieille 2015-05-06 22:24:42 +02:00
parent bf04b74d02
commit 809aa35989
9 changed files with 146 additions and 48 deletions

View file

@ -11,3 +11,11 @@ $app->extend('twig', function ($twig, $app) {
return $twig; return $twig;
}); });
$app['geshi'] = function ($app) {
$geshi = new GeSHi();
$geshi->enable_classes();
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
return $geshi;
};

View file

@ -5,11 +5,14 @@ use Gist\Service\GistService;
$app['gist_path'] = $app['root_path'].'/data/git'; $app['gist_path'] = $app['root_path'].'/data/git';
$app['git'] = function ($app) { $app['git_wrapper'] = function ($app) {
$wrapper = new GitWrapper('/usr/bin/git'); return new GitWrapper('/usr/bin/git');
return $wrapper->init($app['gist_path']); };
$app['git_working_copy'] = function ($app) {
return $app['git_wrapper']->init($app['gist_path']);
}; };
$app['gist'] = function ($app) { $app['gist'] = function ($app) {
return new GistService($app['gist_path'], $app['git']); return new GistService($app['gist_path'], $app['git_wrapper'], $app['git_working_copy'], $app['geshi']);
}; };

View file

@ -1,7 +1,7 @@
home: home:
path: / path: /
defaults: { _controller: Gist\Controller\EditController::createAction, _locale: en } defaults: {_controller: Gist\Controller\EditController::createAction, _locale: en}
view: view:
path: /{gist} path: /{gist}/{commit}
defaults: { _controller: Gist\Controller\ViewController::viewAction, _locale: en } defaults: {_controller: Gist\Controller\ViewController::viewAction, _locale: en, commit: 0}

View file

@ -14,6 +14,9 @@ gist:
title: 'Oops!' title: 'Oops!'
message: 'The gist was not found...' message: 'The gist was not found...'
date:
format: 'Y-m-d h:i:s'
form: form:
error: error:
not_blank: 'This value should not be blank bro!' not_blank: 'This value should not be blank bro!'
@ -30,7 +33,7 @@ form:
choice: choice:
xml: 'HTML/XML' xml: 'HTML/XML'
css: 'CSS' css: 'CSS'
js: 'JAVASCRIPT' javascript: 'JAVASCRIPT'
php: 'PHP' php: 'PHP'
sql: 'SQL' sql: 'SQL'
yaml: 'YAML' yaml: 'YAML'
@ -39,5 +42,5 @@ form:
asp: 'ASP' asp: 'ASP'
python: 'PYTHON' python: 'PYTHON'
bash: 'BASH' bash: 'BASH'
as: 'ACTION SCRIPT' actionscript3: 'ACTION SCRIPT'
text: 'TEXT' text: 'TEXT'

View file

@ -14,6 +14,9 @@ gist:
title: 'Arf...' title: 'Arf...'
message: "Ce gist n'existe pas." message: "Ce gist n'existe pas."
date:
format: 'd/m/Y H\hi s\s'
form: form:
error: error:
not_blank: 'Vous devez saisir cette donnée.' not_blank: 'Vous devez saisir cette donnée.'
@ -30,7 +33,7 @@ form:
choice: choice:
xml: 'HTML/XML' xml: 'HTML/XML'
css: 'CSS' css: 'CSS'
js: 'JAVASCRIPT' javascript: 'JAVASCRIPT'
php: 'PHP' php: 'PHP'
sql: 'SQL' sql: 'SQL'
yaml: 'YAML' yaml: 'YAML'
@ -39,5 +42,5 @@ form:
asp: 'ASP' asp: 'ASP'
python: 'PYTHON' python: 'PYTHON'
bash: 'BASH' bash: 'BASH'
as: 'ACTION SCRIPT' actionscript3: 'ACTION SCRIPT'
text: 'TEXTE' text: 'TEXTE'

View file

@ -5,6 +5,7 @@ namespace Gist\Controller;
use Silex\Application; use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery; use Gist\Model\GistQuery;
use Gist\Model\Gist;
/** /**
* Class HomeController * Class HomeController
@ -12,7 +13,7 @@ use Gist\Model\GistQuery;
*/ */
class ViewController class ViewController
{ {
public function viewAction(Request $request, Application $app, $gist) public function viewAction(Request $request, Application $app, $gist, $commit)
{ {
$gist = GistQuery::create()->findOneByFile($gist); $gist = GistQuery::create()->findOneByFile($gist);
@ -20,10 +21,21 @@ class ViewController
return $this->notFoundResponse($app); return $this->notFoundResponse($app);
} }
$history = $app['gist']->getHistory($gist);
if (empty($history)) {
return $this->notFoundResponse($app);
}
$content = $this->getContentByCommit($app, $gist, $commit, $history);
return $app['twig']->render( return $app['twig']->render(
'View/view.html.twig', 'View/view.html.twig',
array( array(
'gist' => $gist, 'gist' => $gist,
'type' => $gist->getType(),
'history' => $history,
'content' => $app['gist']->highlight($gist->getType(), $content),
) )
); );
} }
@ -32,4 +44,25 @@ class ViewController
{ {
return $app['twig']->render('View/notFound.html.twig'); return $app['twig']->render('View/notFound.html.twig');
} }
protected function getContentByCommit(Application $app, Gist $gist, $commit, $history)
{
if ($commit === 0) {
$commit = $history[0]['commit'];
} else {
$commitExists = false;
foreach ($history as $ci) {
if ($commit === $ci['commit']) {
$commitExists = true;
}
}
if (!$commitExists) {
return null;
}
}
return $app['gist']->getContent($gist, $commit);
}
} }

View file

@ -73,7 +73,7 @@ class CreateGistForm extends AbstractForm
$types = array( $types = array(
'xml' => '', 'xml' => '',
'css' => '', 'css' => '',
'js' => '', 'javascript' => '',
'php' => '', 'php' => '',
'sql' => '', 'sql' => '',
'yaml'=> '', 'yaml'=> '',
@ -82,7 +82,7 @@ class CreateGistForm extends AbstractForm
'asp' => '', 'asp' => '',
'python' => '', 'python' => '',
'bash' => '', 'bash' => '',
'as' => '', 'actionscript3' => '',
'text' => '', 'text' => '',
); );

View file

@ -1,5 +1,11 @@
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block css %}
{{ parent() }}
<link rel="stylesheet" href="{{ web_path }}app/css/geshi/twilight.css" />
{% endblock %}
{% block body %} {% block body %}
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
@ -11,7 +17,7 @@
<li> <li>
<a href="#revisions" data-toggle="tab"> <a href="#revisions" data-toggle="tab">
Révisions Révisions
<span class="badge">4</span> <span class="badge">{{ history|length }}</span>
</a> </a>
</li> </li>
</ul> </ul>
@ -53,31 +59,35 @@
<div id="viewer"> <div id="viewer">
{% if gist.cipher %} {% if gist.cipher %}
<pre> <pre>{{ content }}</pre>
{{ 'foo' }} {% else %}
</pre> {{ content|raw }}
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% if not gist.cipher %} {% if not gist.cipher %}
<div id="revisions" class="tab-pane out"> <div id="revisions" class="tab-pane out">
<div class="commit"> {% for commit in history %}
<p> <div class="commit">
Commit <strong class="text-warning">82110bbb53fbc5bf010ea4a50d999ab7cb22ae4f</strong> <p>
<br/> Commit <strong class="text-warning">{{ commit.commit }}</strong>
Date: Mon May 4 23:09:44 2015 +0200 <br/>
</p> Date: {{ commit.date|date('date.format'|trans) }}
</p>
<p>
<a href="{{ path('view', {gist: gist.file, commit: commit.commit}) }}" class="btn btn-primary">
Afficher
</a>
<a class="btn btn-default">
DIFF
</a>
</p>
</div>
<p> {% if not loop.last %}
<a class="btn btn-primary"> <hr />
Afficher {% endif %}
</a> {% endfor %}
<a class="btn btn-default">
DIFF
</a>
</p>
</div>
<hr>
</div> </div>
{% endif %} {% endif %}
</div> </div>
@ -89,10 +99,4 @@
{% block js %} {% block js %}
{{ parent() }} {{ parent() }}
{% if gist is not null %}
<script type="text/javascript">
document.location.href = '{{ path("view", {gist: gist.file}) }}'{% if gist.cipher %} + '#' + (document.location.href).split('#')[1]{% endif %};
</script>
{% endif %}
{% endblock %} {% endblock %}

View file

@ -4,6 +4,9 @@ namespace Gist\Service;
use Gist\Model\Gist; use Gist\Model\Gist;
use GitWrapper\GitWorkingCopy; use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use GitWrapper\GitCommand;
use GeSHi;
/** /**
* Class GistService * Class GistService
@ -13,31 +16,72 @@ class GistService
{ {
protected $gistPath; protected $gistPath;
protected $git; protected $gitWrapper;
public function __construct($gistPath, GitWorkingCopy $git) protected $gitWorkingCopy;
protected $geshi;
public function __construct($gistPath, GitWrapper $gitWrapper, GitWorkingCopy $gitWorkingCopy, GeSHi $geshi)
{ {
$this->gistPath = $gistPath; $this->gistPath = $gistPath;
$this->git = $git; $this->gitWrapper = $gitWrapper;
$this->gitWorkingCopy = $gitWorkingCopy;
$this->geshi = $geshi;
}
public function getHistory(Gist $gist)
{
$command = GitCommand::getInstance('log', '--format=medium', $gist->getFile());
$command->setDirectory($this->gistPath);
$command->bypass(false);
$output = $this->gitWrapper->run($command);
preg_match_all('/commit ([^\n]+)\n/isU', $output, $commits, PREG_SET_ORDER);
preg_match_all('/Date:\s+([^\n]+)\n/isU', $output, $dates, PREG_SET_ORDER);
$history = [];
for ($i = count($commits) - 1; $i >= 0; $i--) {
$history[] = array(
'commit' => trim($commits[$i][1]),
'date' => new \DateTime(trim($dates[$i][1])),
);
}
return $history;
}
public function getContent(Gist $gist, $commit)
{
$command = GitCommand::getInstance('cat-file', '--textconv', $commit.':'.$gist->getFile());
$command->setDirectory($this->gistPath);
$command->bypass(false);
return $this->gitWrapper->run($command);
} }
public function create(Gist $gist, array $data) public function create(Gist $gist, array $data)
{ {
$gist->hydrateWith($data); $gist->hydrateWith($data);
$gist->generateFilename(); $gist->generateFilename();
$dir = getcwd();
chdir($this->gistPath);
file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']); file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
$this->git $this->gitWorkingCopy
->add($gist->getFile()) ->add($gist->getFile())
->commit('Init'); ->commit('Init');
chdir($dir);
$gist->save(); $gist->save();
return $gist; return $gist;
} }
public function highlight($type, $content)
{
$this->geshi->set_source($content);
$this->geshi->set_language($type);
return $this->geshi->parse_code();
}
} }