Extract ContentType from format
This commit is contained in:
17
libs/ContentTypes/ContentType.php
Normal file
17
libs/ContentTypes/ContentType.php
Normal 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);
|
||||
}
|
53
libs/ContentTypes/ContentTypeHandler.php
Normal file
53
libs/ContentTypes/ContentTypeHandler.php
Normal 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()}");
|
||||
}
|
||||
}
|
38
libs/ContentTypes/Markdown/CommonMarkConverter.php
Normal file
38
libs/ContentTypes/Markdown/CommonMarkConverter.php
Normal 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));
|
||||
}
|
||||
}
|
31
libs/ContentTypes/Markdown/ContentType.php
Normal file
31
libs/ContentTypes/Markdown/ContentType.php
Normal 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);
|
||||
}
|
||||
}
|
74
libs/ContentTypes/Markdown/LinkRenderer.php
Normal file
74
libs/ContentTypes/Markdown/LinkRenderer.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user