daux.io/libs/ContentTypes/Markdown/ContentType.php

78 lines
1.9 KiB
PHP
Raw Normal View History

2015-07-29 22:31:41 +02:00
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
2018-06-06 23:20:29 +02:00
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Cache;
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-29 22:31:41 +02:00
class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
{
/** @var Config */
protected $config;
/** @var CommonMarkConverter */
private $converter;
public function __construct(Config $config)
{
$this->config = $config;
}
2020-04-22 21:55:53 +02:00
protected function createConverter()
{
return new CommonMarkConverter(['daux' => $this->config]);
}
2020-04-22 21:55:53 +02:00
protected function getConverter()
{
if (!$this->converter) {
$this->converter = $this->createConverter();
}
return $this->converter;
}
/**
2016-07-29 23:46:57 +02:00
* @return string[]
*/
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);
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)
{
$this->config->setCurrentPage($node);
2016-07-27 21:32:51 +02: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);
if ($can_cache && $payload) {
2020-04-22 22:24:52 +02:00
Daux::writeln('Using cached version', OutputInterface::VERBOSITY_VERBOSE);
}
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);
}
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;
}
}