Rework version updating so it only downloads legit versions

This commit is contained in:
Kacper Donat 2020-04-13 20:44:29 +02:00
parent 98ded1c9ac
commit 51f448aad6
2 changed files with 37 additions and 3 deletions

View File

@ -2,8 +2,10 @@
namespace App\CommonMark;
use Kadet\Highlighter\Formatter\HtmlFormatter;
use Kadet\Highlighter\KeyLighter;
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Element\FencedCode;
use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\ElementRendererInterface;
use League\CommonMark\HtmlElement;
@ -16,7 +18,7 @@ class KeylighterCodeRenderer implements BlockRendererInterface
public function __construct(KeyLighter $keylighter)
{
$this->keylighter = $keylighter;
$this->formatter = $keylighter->getFormatter('html');
$this->formatter = new HtmlFormatter();
}
/**
@ -24,7 +26,7 @@ class KeylighterCodeRenderer implements BlockRendererInterface
*/
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
{
$language = $this->keylighter->getLanguage($block->getInfo());
$language = $this->keylighter->getLanguage($block instanceof FencedCode ? $block->getInfo() : 'plaintext');
$text = $block->getStringContent();
return new HtmlElement('pre', ['class' => 'keylighter'], @$this->keylighter->highlight($text, $language, $this->formatter));

View File

@ -7,6 +7,7 @@ use Kadet\Highlighter\KeyLighter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Messenger\MessageBusInterface;
use Tightenco\Collect\Support\Collection;
class KeyLighterVersioner
{
@ -47,7 +48,7 @@ class KeyLighterVersioner
$info = $this->getPackageInfo();
$installed = $this->getInstalledInfo();
foreach ($info as $version => $revision) {
foreach ($this->reduceVersions($info) as $version => $revision) {
if (array_key_exists($version, $installed) && $installed[$version] == $revision['dist']['reference']) {
continue;
}
@ -56,6 +57,37 @@ class KeyLighterVersioner
}
}
private function reduceVersions($info)
{
return collect($info)
->filter(function ($metadata, $version) {
// only dev-master and proper versions
return $version === "dev-master" || ($version[0] === 'v' && version_compare(substr($version, 1), '0.7.0', '>'));
})
->groupBy(function ($metadata, $version) {
if ($version[0] !== 'v') {
return $version;
}
return preg_replace("/v(\d+\.\d+).*$/", '$1', $version);
})
->map(function (Collection $metadata, $version) {
return $metadata->reduce(function ($a, $b) {
if ($a === null) {
return $b;
}
return strtotime($a['time']) > strtotime($b['time']) ? $a : $b;
});
})
->map(function ($revision, $version) {
$revision['version'] = $version;
return $revision;
})
;
}
private function getPackageInfo()
{
return json_decode(file_get_contents(self::URL), true)['packages']['kadet/keylighter'];