From eb7b71ffdfe245b229e8345f9c918e88126b54e6 Mon Sep 17 00:00:00 2001 From: Kazuyuki Hayashi Date: Tue, 12 Nov 2013 15:09:00 +0900 Subject: [PATCH] Add ability to modify url before rendering using custom renderer. --- src/Ciconia/Extension/Core/LinkExtension.php | 48 ++++++++++++++++---- src/Ciconia/Renderer/HtmlRenderer.php | 26 +++++++++++ src/Ciconia/Renderer/RendererInterface.php | 8 ++++ 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/Ciconia/Extension/Core/LinkExtension.php b/src/Ciconia/Extension/Core/LinkExtension.php index 97ef2ac..9234e12 100644 --- a/src/Ciconia/Extension/Core/LinkExtension.php +++ b/src/Ciconia/Extension/Core/LinkExtension.php @@ -110,13 +110,25 @@ public function processReferencedLink(Text $text, array $options = array()) if ($this->markdown->getUrlRegistry()->exists($id)) { $url = new Text($this->markdown->getUrlRegistry()->get($id)); $url->escapeHtml(); - $result = new Text(" $url->getString(), + ]; + if ($this->markdown->getTitleRegistry()->exists($id)) { $title = new Text($this->markdown->getTitleRegistry()->get($id)); - $title->escapeHtml(); - $result->append(" title=\"$title\""); + $linkOptions['title'] = $title->escapeHtml()->getString(); } - return $result->append(">$linkText"); + + return $this->getRenderer()->renderLink($linkText->getString(), $linkOptions); + +// $result = new Text("markdown->getTitleRegistry()->exists($id)) { +// $title = new Text($this->markdown->getTitleRegistry()->get($id)); +// $title->escapeHtml(); +// $result->append(" title=\"$title\""); +// } +// return $result->append(">$linkText"); } else { if ($options['strict']) { throw new SyntaxError( @@ -156,13 +168,24 @@ public function processInlineLink(Text $text) ) }xs', function (Text $w, Text $whole, Text $linkText, Text $url, Text $a = null, Text $q = null, Text $title = null) { $url->escapeHtml(); - $result = new Text(" $url->getString(), + ]; + if ($title) { - $title->replace('/"/', '"')->escapeHtml(); - $result->append(" title=\"$title\""); + $linkOptions['title'] = $title->replace('/"/', '"')->escapeHtml()->getString(); } - return $result->append(">$linkText"); + return $this->getRenderer()->renderLink($linkText, $linkOptions); + +// $result = new Text("replace('/"/', '"')->escapeHtml(); +// $result->append(" title=\"$title\""); +// } +// +// return $result->append(">$linkText"); }); } @@ -173,7 +196,11 @@ public function processInlineLink(Text $text) */ public function processAutoLink(Text $text) { - $text->replace('{<((https?|ftp):[^\'">\s]+)>}', '$1'); + //$text->replace('{<((https?|ftp):[^\'">\s]+)>}', '$1'); + + $text->replace('{<((https?|ftp):[^\'">\s]+)>}', function (Text $w, Text $url) { + return $this->getRenderer()->renderLink($url, ['href' => $url->getString()]); + }); /** @noinspection PhpUnusedParameterInspection */ $text->replace('{ @@ -212,7 +239,8 @@ function ($char) { return $char; } $address = $chars->join(); $text = $chars->slice(7)->join(); - return "$text"; + return $this->getRenderer()->renderLink($text, ['href' => $address]); + //return "$text"; }); } diff --git a/src/Ciconia/Renderer/HtmlRenderer.php b/src/Ciconia/Renderer/HtmlRenderer.php index ff654c4..5136c35 100644 --- a/src/Ciconia/Renderer/HtmlRenderer.php +++ b/src/Ciconia/Renderer/HtmlRenderer.php @@ -3,6 +3,7 @@ namespace Ciconia\Renderer; use Ciconia\Common\Tag; +use Ciconia\Common\Text; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -61,6 +62,31 @@ public function renderCodeSpan($content, array $options = array()) return "$content"; } + /** + * @param string|Text $content + * @param array $options + * + * @return string + */ + public function renderLink($content, array $options = array()) + { + $options = $this->createResolver() + ->setRequired(array('href')) + ->setDefaults(array('href' => '#', 'title' => '')) + ->setAllowedTypes(array('href' => 'string', 'title' => 'string')) + ->resolve($options); + + $tag = new Tag('a'); + $tag->setText($content); + $tag->setAttribute('href', $options['href']); + + if ($options['title']) { + $tag->setAttribute('title', $options['title']); + } + + return $tag->render(); + } + /** * {@inheritdoc} */ diff --git a/src/Ciconia/Renderer/RendererInterface.php b/src/Ciconia/Renderer/RendererInterface.php index 8f466de..651ba91 100644 --- a/src/Ciconia/Renderer/RendererInterface.php +++ b/src/Ciconia/Renderer/RendererInterface.php @@ -45,6 +45,14 @@ public function renderCodeBlock($content, array $options = array()); */ public function renderCodeSpan($content, array $options = array()); + /** + * @param string|Text $content + * @param array $options + * + * @return string + */ + public function renderLink($content, array $options = array()); + /** * @param string|Text $content * @param array $options