-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ideas #16
Comments
I have a few of ideas to add to the list:
FYI Some of these ideas stem from the discussions in PR #26 |
@rhukster these are all great ideas, let me explain how they could be achieved:
Please let me know what do you think. |
Thanks! |
@rhukster |
Yup, i saw you merged your facade updates. I'm going to take a look and probably update my Grav Prior to recent updates, i was caching independently of the page, so each shortcode needed a unique id site-wide, which a simple offset would not provide. However, now, things are cached per-page, so should be good to go. Thanks! |
Can you add option for method What I mean: public function getParameter($name, $default = null, $boolean = false)
{
$value = $this->hasParameter($name) ? $this->parameters[$name] : $default;
return $boolean ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : $value;
} |
@dimayakovlev can you show me some use cases for this? Isn't casting to boolean sufficient? $value = (bool)$s->getParameter('name'); |
It can be useful if parameter value is This is example from Grav Shortcode plugin: It does not matter if Simple casting to boolean in this scenario does not work (no problems if parameter value can be only |
@dimayakovlev Thanks, but in this case, I would rather prefer that you call $value = filter_var($s->getParameter('name'), FILTER_VALIDATE_BOOLEAN); |
@thunderer ok. :) |
I have an idea. The first thing I noticed was that it quite much code, even for the most simple task, as we can see here: use Thunder\Shortcode\ShortcodeFacade;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
$facade = new ShortcodeFacade();
$facade->addHandler('hello', function(ShortcodeInterface $s) {
return sprintf('Hello, %s!', $s->getParameter('name'));
});
$text = '
<div class="user">[hello name="Thomas"]</div>
<p>Your shortcodes are very good, keep it up!</p>
<div class="user">[hello name="Peter"]</div>
';
echo $facade->process($text); Maybe for this case simplify it even more. Something like this? shortcode::create('hello', function($s) {
return sprintf('Hello, %s!', $s->get('name'));
});
$text = '
<div class="user">[hello name="Thomas"]</div>
<p>Your shortcodes are very good, keep it up!</p>
<div class="user">[hello name="Peter"]</div>
';
echo shortcode::process($text); |
@jenstornell I already had that discussion back when I released first versions, you can read my answer here. The <?php
declare(strict_types=1);
namespace X;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
final class ShortcodeSingleton
{
/** @var HandlerContainer */
private static $handlers;
/** @var Processor */
private static $processor;
private static function instance(): Processor
{
if(null === static::$processor) {
static::$handlers = new HandlerContainer();
static::$processor = new Processor(new RegularParser(), static::$handlers);
}
}
public static function add(string $name, callable $handler): void
{
static::instance();
static::$handlers->add($name, $handler);
}
public static function process(string $text): string
{
static::instance();
return static::$processor->process($text);
}
} |
I did something similar to my Kirby CMS plugin: https://github.com/jenstornell/kirby-shortcode/blob/master/lib/shortcode.php echo shortcode::parse($text); But I understand your concerns with this approach. For my plugin it's probably the best but not for all other scenarios. |
Just a list of issues to remember:
getTextUntilNext()
which returns the content up to the next shortcode opening tag,[x=http://x.com]
,[x x=http://x.com]
,[[x /]]
,[[y a=b]]
,[x=]
and[x= arg=val]
,ProcessedShortcodeInterface
andProcessor::withShortcodeFactory()
(think of a better name) to allow creating custom shortcode objects usingProcessorContext
that are compatible withProcessedShortcode
. This will allow users to put their information inside while still maintaining how library works,getText
andgetShortcodeText
betweenParsedShortcode
andProcessedShortcode
,ShortcodeFacade
a mutable class with all the shortcuts to ease library usage (Improved facade #36),[list]- [item/],[/list]
which renders multiple "item" elements, children oflist
(item
shortcodes) receive context from data passed tolist
,__invoke()
to ease events usage (Builtin handlers #33),configurable handler for producing(events FTW),Processor::process()
return value using array of replacementsHUGE BC (just an idea, no worries): Change,ProcessorInterface::process()
to receive array of parsed shortcodes to allow greater flexibility (eg. filtering by parent)symfony/yaml
incomposer.json
for YAML serializer,[[code]value[/code]]
),Regular parser:
BBCode:
Shortcode
constructor, and separate getter,Built-in handlers:
(needsDeclareHandler
should typehint interface in constructor,add()
method)EmailHandler
could be a BBCode,PlaceholderHandler
should have configurable placeholder braces,UrlHandler
could be a BBCode,WrapHandler
could have several most common variants (eg. bold) created as named constructors.The text was updated successfully, but these errors were encountered: