Skip to content

Commit

Permalink
Add ability to modify url before rendering using custom renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzykhys committed Nov 12, 2013
1 parent 4447340 commit eb7b71f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/Ciconia/Extension/Core/LinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a href=\"$url\"");

$linkOptions = [
'href' => $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</a>");

return $this->getRenderer()->renderLink($linkText->getString(), $linkOptions);

// $result = new Text("<a href=\"$url\"");
// if ($this->markdown->getTitleRegistry()->exists($id)) {
// $title = new Text($this->markdown->getTitleRegistry()->get($id));
// $title->escapeHtml();
// $result->append(" title=\"$title\"");
// }
// return $result->append(">$linkText</a>");
} else {
if ($options['strict']) {
throw new SyntaxError(
Expand Down Expand Up @@ -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("<a href=\"$url\"");

$linkOptions = [
'href' => $url->getString(),
];

if ($title) {
$title->replace('/"/', '&quot;')->escapeHtml();
$result->append(" title=\"$title\"");
$linkOptions['title'] = $title->replace('/"/', '&quot;')->escapeHtml()->getString();
}

return $result->append(">$linkText</a>");
return $this->getRenderer()->renderLink($linkText, $linkOptions);

// $result = new Text("<a href=\"$url\"");
// if ($title) {
// $title->replace('/"/', '&quot;')->escapeHtml();
// $result->append(" title=\"$title\"");
// }
//
// return $result->append(">$linkText</a>");
});
}

Expand All @@ -173,7 +196,11 @@ public function processInlineLink(Text $text)
*/
public function processAutoLink(Text $text)
{
$text->replace('{<((https?|ftp):[^\'">\s]+)>}', '<a '.'href="$1">$1</a>');
//$text->replace('{<((https?|ftp):[^\'">\s]+)>}', '<a '.'href="$1">$1</a>');

$text->replace('{<((https?|ftp):[^\'">\s]+)>}', function (Text $w, Text $url) {
return $this->getRenderer()->renderLink($url, ['href' => $url->getString()]);
});

/** @noinspection PhpUnusedParameterInspection */
$text->replace('{
Expand Down Expand Up @@ -212,7 +239,8 @@ function ($char) { return $char; }
$address = $chars->join();
$text = $chars->slice(7)->join();

return "<a"." href=\"$address\">$text</a>";
return $this->getRenderer()->renderLink($text, ['href' => $address]);
//return "<a"." href=\"$address\">$text</a>";
});
}

Expand Down
26 changes: 26 additions & 0 deletions src/Ciconia/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ciconia\Renderer;

use Ciconia\Common\Tag;
use Ciconia\Common\Text;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand Down Expand Up @@ -61,6 +62,31 @@ public function renderCodeSpan($content, array $options = array())
return "<code>$content</code>";
}

/**
* @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}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Ciconia/Renderer/RendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb7b71f

Please sign in to comment.