2015-07-29 22:31:41 +02:00
|
|
|
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
|
2015-07-28 17:25:03 +02:00
|
|
|
|
2018-06-06 23:20:29 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Todaymade\Daux\Cache;
|
2015-07-28 17:25:03 +02:00
|
|
|
use Todaymade\Daux\Config;
|
2018-06-06 23:20:29 +02:00
|
|
|
use Todaymade\Daux\Daux;
|
2015-07-31 12:53:22 +02:00
|
|
|
use Todaymade\Daux\Tree\Content;
|
2015-07-28 17:25:03 +02:00
|
|
|
|
2015-07-29 22:31:41 +02:00
|
|
|
class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
|
2015-07-28 17:25:03 +02:00
|
|
|
{
|
|
|
|
/** @var Config */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/** @var CommonMarkConverter */
|
2019-11-30 21:46:58 +01:00
|
|
|
private $converter;
|
2015-07-28 17:25:03 +02:00
|
|
|
|
|
|
|
public function __construct(Config $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
2019-11-30 21:46:58 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
protected function createConverter()
|
|
|
|
{
|
2019-11-30 21:46:58 +01:00
|
|
|
return new CommonMarkConverter(['daux' => $this->config]);
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
protected function getConverter()
|
|
|
|
{
|
2019-11-30 21:46:58 +01:00
|
|
|
if (!$this->converter) {
|
|
|
|
$this->converter = $this->createConverter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->converter;
|
2015-07-28 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-29 23:46:57 +02:00
|
|
|
* @return string[]
|
2015-07-28 17:25:03 +02:00
|
|
|
*/
|
|
|
|
public function getExtensions()
|
|
|
|
{
|
|
|
|
return ['md', 'markdown'];
|
|
|
|
}
|
|
|
|
|
2018-06-06 23:20:29 +02:00
|
|
|
protected function doConversion($raw)
|
|
|
|
{
|
2020-04-22 22:24:52 +02:00
|
|
|
Daux::writeln('Running conversion', OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
|
2019-11-30 21:46:58 +01:00
|
|
|
return $this->getConverter()->convertToHtml($raw);
|
2018-06-06 23:20:29 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 12:53:22 +02:00
|
|
|
public function convert($raw, Content $node)
|
2015-07-28 17:25:03 +02:00
|
|
|
{
|
2016-07-04 20:33:44 +02:00
|
|
|
$this->config->setCurrentPage($node);
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$can_cache = $this->config->canCache();
|
2018-06-06 23:20:29 +02:00
|
|
|
|
2018-09-21 22:12:00 +02:00
|
|
|
// TODO :: add daux version to cache key
|
2018-06-06 23:20:29 +02:00
|
|
|
$cacheKey = $this->config->getCacheKey() . sha1($raw);
|
|
|
|
|
|
|
|
$payload = Cache::get($cacheKey);
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($can_cache && $payload) {
|
2020-04-22 22:24:52 +02:00
|
|
|
Daux::writeln('Using cached version', OutputInterface::VERBOSITY_VERBOSE);
|
2019-12-05 21:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$can_cache || !$payload) {
|
2020-04-22 22:24:52 +02:00
|
|
|
Daux::writeln($can_cache ? 'Not found in the cache, generating...' : 'Cache disabled, generating...', OutputInterface::VERBOSITY_VERBOSE);
|
2018-06-06 23:20:29 +02:00
|
|
|
$payload = $this->doConversion($raw);
|
2019-12-05 21:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($can_cache) {
|
2018-06-06 23:20:29 +02:00
|
|
|
Cache::put($cacheKey, $payload);
|
2020-04-22 21:55:53 +02:00
|
|
|
}
|
2018-06-06 23:20:29 +02:00
|
|
|
|
|
|
|
return $payload;
|
2015-07-28 17:25:03 +02:00
|
|
|
}
|
|
|
|
}
|