public function testWordPress() { $parser = new WordpressParser(); $this->testParser($parser, '[code arg="<html" oth=\'val\']', array(new ParsedShortcode(new Shortcode('code', array('arg' => '', 'oth' => 'val'), null), '[code arg="<html" oth=\'val\']', 0))); $this->testParser($parser, '[code "xxx"]', array(new ParsedShortcode(new Shortcode('code', array('xxx' => null), null, null), '[code "xxx"]', 0))); $handlers = new HandlerContainer(); $handlers->add('_', function () { }); $handlers->add('na_me', function () { }); $handlers->add('_n_', function () { }); $this->testParser(WordpressParser::createFromHandlers($handlers), '[_][na_me][_name][name_][n_am_e][_n_]', array(new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0), new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3), new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32))); $this->testParser(WordpressParser::createFromNames(array('_', 'na_me', '_n_')), '[_][na_me][_name][name_][n_am_e][_n_]', array(new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0), new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3), new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32))); }
public function testImmutableHandlerContainer() { $handlers = new HandlerContainer(); $handlers->add('code', function () { }); $handlers->addAlias('c', 'code'); $imHandlers = new ImmutableHandlerContainer($handlers); $handlers->add('not', function () { }); $this->assertNull($imHandlers->get('missing')); $this->assertNotNull($imHandlers->get('code')); $this->assertNotNull($imHandlers->get('c')); $this->assertNull($imHandlers->get('not')); $defaultHandlers = new HandlerContainer(); $defaultHandlers->setDefault(function () { }); $imDefaultHandlers = new ImmutableHandlerContainer($defaultHandlers); $this->assertNotNull($imDefaultHandlers->get('missing')); }
/** * [declare name]Your name is %value%[/declare] * [name value="Thomas" /] * * @param ShortcodeInterface $shortcode */ public function __invoke(ShortcodeInterface $shortcode) { $args = $shortcode->getParameters(); if (empty($args)) { return; } $keys = array_keys($args); $name = array_shift($keys); $content = $shortcode->getContent(); $delimiter = $this->delimiter; $this->handlers->add($name, function (ShortcodeInterface $shortcode) use($content, $delimiter) { $args = $shortcode->getParameters(); $keys = array_map(function ($key) use($delimiter) { return $delimiter . $key . $delimiter; }, array_keys($args)); $values = array_values($args); return str_replace($keys, $values, $content); }); }
public function testDefaultApplier() { $handlers = new HandlerContainer(); $handlers->add('name', function (ShortcodeInterface $s) { return $s->getName(); }); $handlers->add('content', function (ShortcodeInterface $s) { return $s->getContent(); }); $handlers->add('root', function (ProcessedShortcode $s) { return 'root[' . $s->getContent() . ']'; }); $events = new EventContainer(); $events->addListener(Events::REPLACE_SHORTCODES, function (ReplaceShortcodesEvent $event) { $event->setResult(array_reduce(array_reverse($event->getReplacements()), function ($state, ReplacedShortcode $r) { $offset = $r->getOffset(); $length = mb_strlen($r->getText()); return mb_substr($state, 0, $offset) . $r->getReplacement() . mb_substr($state, $offset + $length); }, $event->getText())); }); $processor = new Processor(new RegularParser(), $handlers); $processor = $processor->withEventContainer($events); $this->assertSame('a root[x name c name y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b')); }
public function testFacade() { $handlers = new HandlerContainer(); $handlers->add('name', function (ShortcodeInterface $s) { return $s->getName(); })->addAlias('n', 'name'); $facade = ShortcodeFacade::create($handlers, new CommonSyntax()); $facade->addHandler('content', function (ShortcodeInterface $s) { return $s->getContent(); }); $facade->addHandlerAlias('c', 'content'); $facade->setParser(new RegexParser()); $this->assertSame('n', $facade->process('[n]')); $this->assertSame('c', $facade->process('[c]c[/c]')); $shortcodes = $facade->parse('[b]'); $this->assertInstanceOf('Thunder\\Shortcode\\Shortcode\\ParsedShortcodeInterface', $shortcodes[0]); }
public function testFacade() { $handlers = new HandlerContainer(); $handlers->add('name', function (ShortcodeInterface $s) { return $s->getName(); })->add('content', function (ShortcodeInterface $s) { return $s->getContent(); })->addAlias('c', 'content')->addAlias('n', 'name'); $facade = ShortcodeFacade::create($handlers, new CommonSyntax()); $this->assertSame('n', $facade->process('[n]')); $this->assertSame('c', $facade->process('[c]c[/c]')); $shortcodes = $facade->parse('[b]'); $this->assertInstanceOf('Thunder\\Shortcode\\Shortcode\\ShortcodeInterface', $shortcodes[0]); $s = new Shortcode('c', array(), null); $this->assertSame('[c /]', $facade->serializeToText($s)); $this->assertSame('c', $facade->unserializeFromText('[c]')->getName()); $json = '{"name":"c","parameters":[],"content":null,"bbCode":null}'; $this->assertSame($json, $facade->serializeToJson($s)); $this->assertSame('c', $facade->unserializeFromJson($json)->getName()); }
public function testPreventInfiniteLoop() { $handlers = new HandlerContainer(); $handlers->add('name', function (ShortcodeInterface $s) { return $s->getName(); })->add('content', function (ShortcodeInterface $s) { return $s->getContent(); })->add('reverse', new ReverseShortcode())->addAlias('c', 'content')->addAlias('n', 'name')->add('self', function () { return '[self]'; })->add('other', function () { return '[self]'; })->add('random', function () { return '[various]'; }); $processor = new Processor(new RegexParser(), $handlers); $processor->withMaxIterations(null); $processor->process('[self]'); $processor->process('[other]'); $processor->process('[random]'); }
public function addHandler($name, $handler) { $this->handlers->add($name, $handler); return $this; }
/** * Register new shortcode. * * @param string $name * @param mixed $callback */ public function register($name, $callback) { $this->handlers->add($name, $callback); }
public function testValidProcessAfterHandlerRemoval() { $handlers = new HandlerContainer(); $handlers->add('name', function (ShortcodeInterface $s) { return $s->getName(); }); $handlers->addAlias('n', 'name'); $processor = new Processor(new RegexParser(), $handlers); $this->assertSame('n', $processor->process('[n]')); $this->assertSame('name', $processor->process('[name]')); $handlers->remove('name'); $this->assertSame('n', $processor->process('[n]')); $this->assertSame('[name]', $processor->process('[name]')); }
use Thunder\Shortcode\Parser\RegularParser; use Thunder\Shortcode\Processor\Processor; use Thunder\Shortcode\Shortcode\ShortcodeInterface; use Thunder\Shortcode\Syntax\CommonSyntax; mb_internal_encoding("UTF-8"); $profiler = new Profiler(); // Get the content to work with (example-small.md or example-full.md) $content = file_get_contents('example-small.md'); // Process the markdown first $extra = new ParsedownExtra(); $content = $extra->text($content); $profiler->start('shortcode'); // Create Handler container and add a simple handler example $handler = new HandlerContainer(); $handler->add('u', function (ShortcodeInterface $shortcode) { return '<span style="text-decoration: underline;">' . $shortcode->getContent() . '</span>'; }); $handler->add('blue', function (ShortcodeInterface $shortcode) { return '<span style="background:blue;color:white;">' . $shortcode->getContent() . '</span>'; }); // Change the Parser to see the difference in speed/corruption $processor = new Processor(new RegularParser(new CommonSyntax()), $handler); $processed_content = $processor->process($content); $profiler->end('shortcode'); ?> <!DOCTYPE HTML> <html> <head> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <style> body {margin: 100px;}