2015-06-29 16:16:39 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Base\CommonMark;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $url
|
2015-06-29 16:16:39 +02:00
|
|
|
* @return Entry
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-07-17 23:38:06 +02:00
|
|
|
protected function resolveInternalFile($url)
|
|
|
|
{
|
2015-06-29 16:16:39 +02:00
|
|
|
$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)
|
|
|
|
{
|
2015-07-20 15:59:52 +02:00
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-29 16:16:39 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|