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

61 lines
1.6 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 */
protected $converter;
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
}
/**
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)
{
Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE);
return $this->converter->convertToHtml($raw);
}
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
2018-06-06 23:20:29 +02:00
if (!$this->config->canCache()) {
return $this->doConversion($raw);
}
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 ($payload) {
Daux::writeln("Using cached version", OutputInterface::VERBOSITY_VERBOSE);
} else {
Daux::writeln("Not found in the cache, generating...", OutputInterface::VERBOSITY_VERBOSE);
$payload = $this->doConversion($raw);
Cache::put($cacheKey, $payload);
}
return $payload;
}
}