Skip to content

Instantly share code, notes, and snippets.

@JoshyPHP
Last active August 29, 2015 14:25
Show Gist options
  • Save JoshyPHP/918d393796f7f66cc10f to your computer and use it in GitHub Desktop.
Save JoshyPHP/918d393796f7f66cc10f to your computer and use it in GitHub Desktop.
Flarum mentions
<?php
include '/s9e/TextFormatter/src/autoloader.php';
$configurator = new s9e\TextFormatter\Configurator;
$tagName = 'MENTION';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->filterChain->prepend('mentionator::addId')->addParameterByName('mentionator');
$tag->template = '<a href="{$PROFILE_URL}{@id}"><xsl:value-of select="@username"/></a>';
$configurator->Preg->match('/@(?<username>\\w+)/', $tagName);
class mentionator
{
protected $cache = ['Bob' => 123];
public static function addId($tag, self $mentionator)
{
$tag->setAttribute('id', $mentionator->getId($tag->getAttribute('username')));
return true;
}
protected function getId($username)
{
return (isset($this->cache[$username])) ? $this->cache[$username] : false;
}
}
extract($configurator->finalize());
$mentionator = new mentionator;
$parser->registeredVars['mentionator'] = $mentionator;
$renderer->setParameter('PROFILE_URL', 'http://example.org/profile/');
$text = '@Bob @NotBob';
$xml = $parser->parse($text);
$html = $renderer->render($xml);
echo $xml, "\n", $html, "\n";
<a href="http://example.org/profile/123">Bob</a> @NotBob
<r><MENTION id="123" username="Bob">@Bob</MENTION> @NotBob</r>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment