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;
});
$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['git'] = function ($app) {
$wrapper = new GitWrapper('/usr/bin/git');
return $wrapper->init($app['gist_path']);
$app['git_wrapper'] = function ($app) {
return new GitWrapper('/usr/bin/git');
};
$app['git_working_copy'] = function ($app) {
return $app['git_wrapper']->init($app['gist_path']);
};
$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:
path: /
defaults: { _controller: Gist\Controller\EditController::createAction, _locale: en }
defaults: {_controller: Gist\Controller\EditController::createAction, _locale: en}
view:
path: /{gist}
defaults: { _controller: Gist\Controller\ViewController::viewAction, _locale: en }
path: /{gist}/{commit}
defaults: {_controller: Gist\Controller\ViewController::viewAction, _locale: en, commit: 0}

View File

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

View File

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

View File

@ -5,6 +5,7 @@ namespace Gist\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery;
use Gist\Model\Gist;
/**
* Class HomeController
@ -12,7 +13,7 @@ use Gist\Model\GistQuery;
*/
class ViewController
{
public function viewAction(Request $request, Application $app, $gist)
public function viewAction(Request $request, Application $app, $gist, $commit)
{
$gist = GistQuery::create()->findOneByFile($gist);
@ -20,10 +21,21 @@ class ViewController
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(
'View/view.html.twig',
array(
'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');
}
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(
'xml' => '',
'css' => '',
'js' => '',
'javascript' => '',
'php' => '',
'sql' => '',
'yaml'=> '',
@ -82,7 +82,7 @@ class CreateGistForm extends AbstractForm
'asp' => '',
'python' => '',
'bash' => '',
'as' => '',
'actionscript3' => '',
'text' => '',
);

View File

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

View File

@ -4,6 +4,9 @@ namespace Gist\Service;
use Gist\Model\Gist;
use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use GitWrapper\GitCommand;
use GeSHi;
/**
* Class GistService
@ -13,31 +16,72 @@ class GistService
{
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->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)
{
$gist->hydrateWith($data);
$gist->generateFilename();
$dir = getcwd();
chdir($this->gistPath);
file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
$this->git
$this->gitWorkingCopy
->add($gist->getFile())
->commit('Init');
chdir($dir);
$gist->save();
return $gist;
}
public function highlight($type, $content)
{
$this->geshi->set_source($content);
$this->geshi->set_language($type);
return $this->geshi->parse_code();
}
}