2015-07-29 22:31:41 +02:00
|
|
|
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
|
2015-06-29 16:16:39 +02:00
|
|
|
|
2015-10-21 23:26:42 +02:00
|
|
|
use League\CommonMark\ElementRendererInterface;
|
2016-07-29 23:46:57 +02:00
|
|
|
use League\CommonMark\HtmlElement;
|
2015-06-29 16:16:39 +02:00
|
|
|
use League\CommonMark\Inline\Element\AbstractInline;
|
|
|
|
use League\CommonMark\Inline\Element\Link;
|
|
|
|
use Todaymade\Daux\Config;
|
|
|
|
use Todaymade\Daux\DauxHelper;
|
2016-08-16 23:25:06 +02:00
|
|
|
use Todaymade\Daux\Exception\LinkNotFoundException;
|
2015-06-29 16:16:39 +02:00
|
|
|
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
|
2016-08-16 23:25:06 +02:00
|
|
|
* @throws LinkNotFoundException
|
2015-06-29 16:16:39 +02:00
|
|
|
*/
|
2015-07-17 23:38:06 +02:00
|
|
|
protected function resolveInternalFile($url)
|
|
|
|
{
|
2016-07-04 20:33:44 +02:00
|
|
|
$triedAbsolute = false;
|
|
|
|
|
|
|
|
// Legacy absolute paths could start with
|
|
|
|
// "!" In this case we will try to find
|
|
|
|
// the file starting at the root
|
|
|
|
if ($url[0] == '!' || $url[0] == '/') {
|
2016-07-27 21:32:51 +02:00
|
|
|
$url = ltrim($url, '!/');
|
2016-07-04 20:33:44 +02:00
|
|
|
|
|
|
|
if ($file = DauxHelper::getFile($this->daux['tree'], $url)) {
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
$triedAbsolute = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems it's not an absolute path or not found,
|
|
|
|
// so we'll continue with the current folder
|
|
|
|
if ($file = DauxHelper::getFile($this->daux->getCurrentPage()->getParent(), $url)) {
|
2015-06-29 16:16:39 +02:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
// If we didn't already try it, we'll
|
|
|
|
// do a pass starting at the root
|
|
|
|
if (!$triedAbsolute && $file = DauxHelper::getFile($this->daux['tree'], $url)) {
|
2015-06-29 16:16:39 +02:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
throw new LinkNotFoundException("Could not locate file '$url'");
|
2015-06-29 16:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-04 20:33:44 +02:00
|
|
|
* @param AbstractInline|Link $inline
|
2015-10-21 23:26:42 +02:00
|
|
|
* @param ElementRendererInterface $htmlRenderer
|
2015-06-29 16:16:39 +02:00
|
|
|
* @return HtmlElement
|
2016-08-16 23:25:06 +02:00
|
|
|
* @throws LinkNotFoundException
|
2015-06-29 16:16:39 +02:00
|
|
|
*/
|
2015-10-21 23:26:42 +02:00
|
|
|
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
|
2015-06-29 16:16:39 +02:00
|
|
|
{
|
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(
|
2016-07-27 21:32:51 +02:00
|
|
|
'Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ .
|
2015-07-20 15:59:52 +02:00
|
|
|
" 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();
|
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
// empty urls and anchors should
|
|
|
|
// not go through the url resolver
|
|
|
|
if (empty($url) || $url[0] == '#') {
|
2016-07-04 20:33:44 +02:00
|
|
|
return $element;
|
2015-06-29 16:16:39 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
// Absolute urls, shouldn't either
|
|
|
|
if (preg_match('|^(?:[a-z]+:)?//|', $url)) {
|
|
|
|
$element->setAttribute('class', 'external');
|
|
|
|
return $element;
|
|
|
|
}
|
2016-07-04 20:33:44 +02:00
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
try {
|
|
|
|
$file = $this->resolveInternalFile($url);
|
|
|
|
$url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
|
|
|
|
} catch(LinkNotFoundException $e) {
|
|
|
|
if ($this->daux->isStatic()) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$element->setAttribute('class', 'broken');
|
|
|
|
}
|
2016-07-04 20:33:44 +02:00
|
|
|
|
|
|
|
$element->setAttribute('href', $url);
|
|
|
|
|
2015-06-29 16:16:39 +02:00
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
}
|