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');
     $handlers = new ImmutableHandlerContainer($handlers);
     $this->assertNull($handlers->get('missing'));
     $this->assertNotNull($handlers->get('code'));
     $this->assertNotNull($handlers->get('c'));
     $defaultHandlers = new HandlerContainer();
     $defaultHandlers->setDefault(function () {
     });
     $defaultHandlers = new ImmutableHandlerContainer($defaultHandlers);
     $this->assertNotNull($defaultHandlers->get('missing'));
 }
 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 direct(Entry $entry, ActivityLog $activityLog = null)
 {
     /* @var $resolver \Dewdrop\ActivityLog\HandlerResolver */
     $resolver = Pimple::getResource('activity-log.handler-resolver');
     $handlers = new HandlerContainer();
     $handlers->setDefault(function (ShortcodeInterface $s) use($entry, $resolver) {
         try {
             $handler = $resolver->resolve($s->getName());
             $entity = $entry->getEntity($handler, $s->getParameter('id'));
             return trim($this->view->activityLogEntity($entity));
         } catch (ActivityLog\Exception\HandlerNotFound $e) {
             return '<strong>Unknown Type</strong>';
         } catch (ActivityLog\Exception\EntityNotFound $e) {
             return '<strong>Entity Not in DB</strong>';
         }
     });
     $processor = new Processor(new RegularParser(), $handlers);
     return $processor->process($entry->getMessage());
 }
 /**
  * [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);
     });
 }
Beispiel #6
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());
 }
 /**
  * setup the markdown parser to handle shortcodes properly
  * 
  * @param  mixed $markdown the markdown parser object
  */
 public function setupMarkdown($markdown)
 {
     $markdown->addBlockType('[', 'ShortCodes', true, false);
     $markdown->blockShortCodes = function ($Line) {
         $valid_shortcodes = implode('|', $this->handlers->getNames());
         $regex = '/^\\[\\/?(?:' . $valid_shortcodes . ')[^\\]]*\\]$/';
         if (preg_match($regex, $Line['body'], $matches)) {
             $Block = array('markup' => $Line['body']);
             return $Block;
         }
     };
 }
 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 static function createFromHandlers(HandlerContainer $handlers)
 {
     return static::createFromNames($handlers->getNames());
 }
Beispiel #10
0
 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 addHandlerAlias($alias, $name)
 {
     $this->handlers->addAlias($alias, $name);
     return $this;
 }
Beispiel #12
0
 /**
  * Return true is the given content contain the given name shortcode.
  *
  * @param string $content
  * @param string $name
  *
  * @return bool
  */
 public function contains($content, $name)
 {
     $hasShortcode = false;
     $handlers = new HandlerContainer();
     $handlers->setDefault(function (ShortcodeInterface $s) use($name, &$hasShortcode) {
         if ($s->getName() === $name) {
             $hasShortcode = true;
         }
     });
     $processor = new Processor(new RegexParser(), $handlers);
     $processor->process($content);
     return $hasShortcode;
 }
Beispiel #13
0
 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]'));
 }
 /**
  * @param $defaultHandler
  * @return Processor
  */
 private function createShortcodeProcessor($defaultHandler)
 {
     $handlers = new HandlerContainer();
     $handlers->setDefault($defaultHandler);
     return new Processor(new RegularParser(), $handlers);
 }
Beispiel #15
0
use Thunder\Shortcode\Parser\RegexParser;
use Thunder\Shortcode\Parser\WordpressParser;
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" />