Extract ContentType from format

This commit is contained in:
Stéphane Goetz
2015-07-29 22:31:41 +02:00
parent 386f323dd0
commit e543a107b1
13 changed files with 14 additions and 18 deletions

View File

@ -0,0 +1,17 @@
<?php namespace Todaymade\Daux\ContentTypes;
use Todaymade\Daux\Config;
interface ContentType
{
public function __construct(Config $config);
/**
* Get the file extensions supported by this Content Type
*
* @return string[]
*/
public function getExtensions();
public function convert($html);
}

View File

@ -0,0 +1,53 @@
<?php namespace Todaymade\Daux\ContentTypes;
use Todaymade\Daux\Tree\Content;
class ContentTypeHandler
{
/**
* @var ContentType[] $types
*/
protected $types;
/**
* @param ContentType[] $types
*/
public function __construct($types)
{
$this->types = array_reverse($types);
}
/**
* Get all valid content file extensions
*
* @return string[]
*/
public function getContentExtensions()
{
$extensions = [];
foreach ($this->types as $type) {
$extensions = array_merge($extensions, $type->getExtensions());
}
return array_unique($extensions);
}
/**
* Get the ContentType able to handle this node
*
* @param Content $node
* @return ContentType
*/
public function getType(Content $node)
{
$extension = pathinfo($node->getPath(), PATHINFO_EXTENSION);
foreach ($this->types as $type) {
if (in_array($extension, $type->getExtensions())) {
return $type;
}
}
throw new \RuntimeException("no contentType found for {$node->getPath()}");
}
}

View File

@ -0,0 +1,38 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;
class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
{
/**
* Create a new commonmark converter instance.
*
* @param array $config
*/
public function __construct(array $config = array())
{
$environment = Environment::createCommonMarkEnvironment();
$environment->mergeConfig($config);
$this->extendEnvironment($environment);
if (array_key_exists('processor_instance', $config['daux'])) {
$config['daux']['processor_instance']->extendCommonMarkEnvironment($environment);
}
$this->docParser = new DocParser($environment);
$this->htmlRenderer = new HtmlRenderer($environment);
}
protected function getLinkRenderer(Environment $environment)
{
return new LinkRenderer($environment->getConfig('daux'));
}
protected function extendEnvironment(Environment $environment)
{
$environment->addInlineRenderer('Link', $this->getLinkRenderer($environment));
}
}

View File

@ -0,0 +1,31 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
use Todaymade\Daux\Config;
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]);
}
/**
* @return array
*/
public function getExtensions()
{
return ['md', 'markdown'];
}
public function convert($html)
{
return $this->converter->convertToHtml($html);
}
}

View File

@ -0,0 +1,74 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
use League\CommonMark\HtmlElement;
use League\CommonMark\HtmlRendererInterface;
use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Element\Link;
use Todaymade\Daux\Config;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception;
use Todaymade\Daux\Tree\Entry;
class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
{
/**
* @var Config
*/
protected $daux;
public function __construct($daux)
{
$this->daux = $daux;
}
/**
* @param string $url
* @return Entry
* @throws Exception
*/
protected function resolveInternalFile($url)
{
$file = DauxHelper::getFile($this->daux['tree'], $url);
if ($file) {
return $file;
}
$file = DauxHelper::getFile($this->daux['tree'], $url . '.html');
if ($file) {
return $file;
}
throw new Exception("Could not locate file '$url'");
}
/**
* @param Link $inline
* @param HtmlRendererInterface $htmlRenderer
*
* @return HtmlElement
*/
public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
{
// This can't be in the method type as
// the method is an abstract and should
// have the same interface
if (!$inline instanceof Link) {
throw new \RuntimeException(
"Wrong type passed to " . __CLASS__ . "::" . __METHOD__ .
" the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" .
get_class($inline) . "' was provided"
);
}
$element = parent::render($inline, $htmlRenderer);
$url = $inline->getUrl();
if (!empty($url) && $url[0] == '!') {
$file = $this->resolveInternalFile(ltrim($url, "!"));
$element->setAttribute('href', $this->daux['base_url'] . $file->getUrl());
}
return $element;
}
}