/** * Process shortcodes after Grav's processing, but before caching * * @param Event $e */ public function onPageContentProcessed(Event $e) { $page = $e['page']; $config = $this->mergeConfig($page); $this->active = $config->get('active', true); // if the plugin is not active (either global or on page) exit if (!$this->active) { return; } switch ($config->get('parser')) { case 'regular': $parser = 'Thunder\\Shortcode\\Parser\\RegularParser'; break; case 'wordpress': $parser = 'Thunder\\Shortcode\\Parser\\WordpressParser'; break; default: $parser = 'Thunder\\Shortcode\\Parser\\RegexParser'; break; } if ($page && $config->get('enabled')) { $content = $e['page']->getRawContent(); $processor = new Processor(new $parser(new CommonSyntax()), $this->handlers); $processed_content = $processor->process($content); $e['page']->setRawContent($processed_content); } }
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()); }
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]'); }
/** * Parse content and replace parts of it using registered handlers * * @param $content * * @return string */ public function parse($content) { $processor = new Processor(new RegexParser(), $this->handlers); return $processor->process($content); }
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]')); }
$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;} </style> </head> <body> <?php echo $processed_content; echo $profiler->render(); ?>