Created
November 12, 2013 06:13
-
-
Save kzykhys/7426285 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Ciconia\Renderer\HtmlRenderer; | |
/** | |
* Example for kzykhys/Ciconia#12 | |
* https://github.com/kzykhys/Ciconia/issues/12 | |
* | |
* Requires Ciconia 0.1.5+ | |
*/ | |
require __DIR__ . '/../vendor/autoload.php'; | |
class CustomLinkRendererExample extends HtmlRenderer | |
{ | |
/** | |
* @return string | |
*/ | |
public function getBaseUrl() { | |
return '/mysite/'; | |
} | |
/** | |
* @param $relativeUrl | |
* @return string | |
*/ | |
public function resolvePath($relativeUrl) | |
{ | |
// You should care about "../" | |
return $this->getBaseUrl() . $relativeUrl; | |
} | |
public function renderLink($content, array $options = array()) | |
{ | |
// Do something like this | |
if (!preg_match('{^((https?://|/).*|.*?(\:).*)}', $options['href'])) { | |
$options['href'] = $this->resolvePath($options['href']) . '.md'; | |
} | |
return parent::renderLink($content, $options); | |
} | |
} | |
// Replace the renderer to yours | |
$ciconia = new \Ciconia\Ciconia(new CustomLinkRendererExample()); | |
echo $ciconia->render(<<<EOF | |
[link1](relative/url) | |
[link2](/absolute/url) | |
[link3](http://www.example.com/example) | |
<http://www.example.com/> | |
<mailto:[email protected]> | |
EOF | |
); | |
// will output | |
?> | |
<p><a href="/mysite/relative/url.md">link1</a></p> | |
<p><a href="/absolute/url">link2</a></p> | |
<p><a href="http://www.example.com/example">link3</a></p> | |
<p><a href="http://www.example.com/">http://www.example.com/</a></p> | |
<p><a href="mailto:example@example.com">example@example.com</a></p> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment