thunderer / shortcode Goto Github PK
View Code? Open in Web Editor NEWAdvanced shortcode (BBCode) parser and engine for PHP
Home Page: http://kowalczyk.cc
License: MIT License
Advanced shortcode (BBCode) parser and engine for PHP
Home Page: http://kowalczyk.cc
License: MIT License
Hello,
I'm dealing with nested shortcodes and I faced the following issue. Giving this shortcode structure:
[shortcode1]
[shortcode2]
[shortcode3]
[/shortcode3]
[/shortcode2]
[/shortcode1]
calling getParent() on shortcode3 returns the shortcode1 and not the expected shortcode2.
Per #70, I'm still having this issue. Attached is a zip to help debug:
thuderer-shortcode-issue#77.zip
Just extract, run composer install
and then php shortcode.php
The output with each parser can be seen via this online diff: https://www.diffchecker.com/vejLhuQq
Notice, the shortcodes are processed at the bottom of the file for the regular parser, but as things progress upwards, it cuts out.
Hello, I have a problem. I need to show the contents of the array using foreach()
with the shortcode, like:
[customers]
Name - [cust_name]
Contact - [cust_contact]
[/customers]
How I can achieve this? Please help me. Thanks.
This is a cool idea by @wollywombat to bring another set of helper methods to Shortcode
class:
getPosition()
: match number in currently processed text,getNamePosition()
: match number from shortcodes with the same name,getMatchOffset()
: match offset in processed text,getParent()
: parent shortcode when processing recursively or null
if in root scope.These methods names are only a proposition and any other ideas are welcome.
Now your great library has been a big part of my new plugin. I've also mentioned you in a few places in the readme file.
https://github.com/jenstornell/kirby-shortcode
https://forum.getkirby.com/t/kirby-shortcode/9569
I wanted to try my own thing, so I created an alternative to your Shortcode library.
https://github.com/jenstornell/php-shortcode
I link to your library in the readme file, as an inspiration.
There are some main differences. I will just bring up the differences.
[shortcode]Content[/shortcode]
.I was inspired by your library as well as WordPress Shortcode API. If you find inspiration from my library, you are welcome to snag a few ideas.
It would be cool to be able to register events at various parts of the text processing flow, for example:
PRE_PROCESS_TEXT
- input text,PRE_EXTRACT
- match position and string,POST_EXTRACT
- ExtractorMatch
instance,PRE_PARSE
- string match,POST_PARSE
- ShortcodeInterface
instance,PRE_PROCESS
- handler and ShortcodeInterface
instance,POST_PROCESS
- result of the handler,POST_PROCESS_TEXT
- output text.Some of them do not make sense, but I wanted to dump all possible ideas. These events could be used to properly implement things present in other libraries like adding links to plain URLs and emails or handling emoticons.
Processor
would be in charge of firing those events through EventDispatcher
(optional constructor argument) which would be configured just like HandlerContainer
.
It is stated in section https://github.com/thunderer/Shortcode#handlers that NameHandler always returns shortcode's name. [xyz arg=val]content[/xyz] becomes sample,
. Should not it be xyz
instead of sample
? If so, can I create PR to fix this?
Wasn't sure if this was a deliberate decision but in parsing tags containing underscores aren't matched.
eg: [testtag]
works but [test_tag]
does not.
Both the regular parser here:
https://github.com/thunderer/Shortcode/blob/master/src/Parser/RegularParser.php#L81
and the Wordpress parser here:
https://github.com/thunderer/Shortcode/blob/master/src/Parser/WordpressParser.php#L27
use a slightly different match syntax but neither contain the _
character.
Reason I ask is because I'm trying to use this library to make a content importer from Wordpress and Wordpress does seem to parse them correctly.
While debugging an issue with the Grav Shortcode UI plugin: (getgrav/grav-plugin-shortcode-ui#29) I discovered the regular
parser will skip over an x
param with the following format: [shortcode x=0]
.
[shortcode x="0"]
works, as well as [shortcode x=1]
, but not x=0
. This only seems to affect the regular
parser. It works fine and as expected with regex
and wordpress
parsers.
I gave it a quick debug and it seems like the match()
method in the regular parser is trying to match the wrong type (6 vs 5?). I dunno, it's a bit hard for me to fully debug this as I'm not super familiar with this tokenized parser.
The result is the parent shortcode is completely skipped, causing child shortcodes to not have a parent, hence the exception to be thrown. This seems like a pretty nasty issue as any shortcode with x=0
type params will just be skipped entirely.
For example TinyMCE, wraps everything in
tags. Block elements like embedded elements, should be stripped of this tag, for HTML5 compliance.
So if the block-element is <p>[embedcode]</p>
, the resulting output should be just the embedcode, without the <p>
elements.
Hi guys,
Thanks for this package, it is very valuable for integrating Wordpress with other platforms.
I'm having an issue when I try to replace only a set of shortcodes, specifically with the non handled ones that have "special" characters.
Example:
$handlers = new HandlerContainer();
$processor = new Processor(new RegularParser(), $handlers);
$sampleValue = '[listing-link id="12345"]Holá[/listing-link]';
$processedValue = $processor->process($sampleValue);
$this->assertEquals($sampleValue, $processedValue);
The test fails and the processedValue has:
[listing-link id="12345"]Holá?[/listing-link]
Instead of returning the same string.
I've already tried with the Regular and Wordpress parsers, and the behaviours are the same.
Right now I'm solving the issue by creating a handler that returns the same shortcode, but that's far from the expected functionality.
I'm trying to find the best way to write a [raw][/raw]
shortcode that stops the Shortcode library from processing anything between these raw tags. My current implementation fakes it by taking setting the 'text' back to the original unmodified text. However, this doesn't stop Shortcode from processing that inner stuff first.
private function addRawHandler()
{
$this->handlers->add('raw', function(ShortcodeInterface $shortcode) {
$raw = trim(preg_replace('/\[raw\](.*?)\[\/raw\]/is','${1}', $shortcode->getShortcodeText()));
return $raw;
});
}
This raised it's head when I ran into the [0]
bug in some javascript example code on my page. While a fix was quickly found, a situation could come up where a fix is impossible or not practical. Turning off shortcodes processing in parts of a page is therefore an important function to have. What is the better way of doing this?
Cheers!
Using CommonSyntax
, the following text won't be picked up by the regular parser:
[url=http://example.com]link[/url]
The parser correctly recognize the opening shortcode, continue to getting the parameter value, then finds a /
which is a marker token, then look for a closing token, doesn't find it and return false.
Using parameter delimiter like this [url="http://example.com"]link[/url]
yields the expected result.
Is it possible to use either Events::FILTER_SHORTCODES
or Events::REPLACE_SHORTCODES
to modify/extend the replacement string provided by the handler?
I am looking for a way to insert additional HTML (say, a </div>...<div>
) around shortcodes. When determining this additional HTML, I'd need to know at lease the shortcode name. And I would like to keep this out of the handler, because it is context/situation specific.
Any pointers?
I'm trying to process nested shortcodes but it doesn't work:
$syntaxBuilder = (new SyntaxBuilder())
->setOpeningTag('<')
->setClosingTag('>')
->setClosingTagMarker('/')
->setParameterValueSeparator('=')
->setParameterValueDelimiter('"')
->getSyntax();
$syntaxParser = new RegexParser($syntaxBuilder);
$tagsHandler = new HandlerContainer();
$tagsProcessor = new Processor(new RegexParser($syntaxBuilder), $tagsHandler);
$tagsHandler->add('div', function (ShortcodeInterface $s) {
return $s->getContent();
});
echo $tagsProcessor->process("<div>TEST1 <div>TEST2</div></div>");
Result:
TEST1 TEST2</div>
Really need a BBCode parser and glad to have found this one that looks actively supported? But I have no way of using composer. Any easy way to still use this?
Thanks!!
Your documentations seems to be hard to understand,
Can you provide working examples for each of the base functions so that one can get better understanding?
Thanks
Hey,
I'm testing following assertion based on README example:
$handlers = new HandlerContainer();
$handlers->add('sample', function(ShortcodeInterface $s) {
return (new JsonSerializer())->serialize($s);
});
$processor = new Processor(new RegexParser(), $handlers);
$text = 'x [sample arg=val]cnt[/sample] y';
$result = 'x {"name":"sample","args":{"arg":"val"},"content":"cnt"} y';
assert($result === $processor->process($text));
it works fine unless I put some unicode characters inside shortcode.
In case of polish character ń
it returns 'x {"name":"sample","parameters":{"arg":"val"},"content":"\u0144","bbCode":null}] y'
(notice closing ]
).
In case of 4 polish characters żółć
it returns 'x {"name":"sample","parameters":{"arg":"val"},"content":"\u017c\u00f3\u0142\u0107","bbCode":null}ple] y'
(notice ple]
after shortcode replacement).
I'm using PHP Version 5.5.9-1ubuntu4.11
with Multibyte Support enabled.
Hi,
I am creating a simple forum using Laravel PHP Framework. Currently, for converting BBCode to HTML I am using the following package: https://github.com/Genert/bbcode
This package uses regex to detect and convert BBCode, for example:
// Add horizontal rule - [hr]
$bbCode->addParser(
'hr', // name of the "parser"
'/\[hr\]/', // BBCode to detect (regex)
'<hr>', // convert to HTML
''
);
// Image
$bbCode->addParser(
'image',
'/\[img\](.*?)\[\/img\]/s',
'<img class="img-fluid" src="$1">',
'$1' // content
);
// Youtube
$bbCode->addParser(
'youtube',
'/\[youtube\](.*?)\[\/youtube\]/s',
'<div class="videoWrapper"><iframe src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe></div>',
'$1'
);
// Add emoticons:
$icons = [
':)' => 'smile',
':angel:' => 'angel',
':angry:' => 'angry',
'8-)' => 'cool',
];
foreach ($icons as $icon => $name) {
$icon = preg_quote($icon);
$bbCode->addParser(
"$name",
"/$icon/s",
"<img class='' src='/test/javascripts/sceditor/emoticons/$name.png' alt='$name' title='$name'/>",
""
);
}
// . . . AND SO ON . . .
I am having some problems with this (nesting or link) and on Stackoverflow I saw that you have recommended this package, and I have a few beginner questions:
1. If I understood correctly, after installing you will have to add rules for converting BBCode to HTML (it doesn't have any rules for converting by itself)?
2. Can you give me a few examples (starting points - how to register rules) for converting BBCode to HTML, for example for:
[quote]...[/quote]
[quote=author]...[/quote]
:)
or :alien:
)Thanks!
Parameter values using double quotes get passed back with double quotes when using getParameter
. Maybe there's a way to modify this configuration to strip double quotes?
$handlers = new HandlerContainer();
$handlers->add('hello', function (ShortcodeInterface $s) {
return sprintf('Hello, %s', $s->getParameter('name'));
});
$processor = new Processor(new WordpressParser(), $handlers);
return $processor->process($input);
This example shows two of the same shortcodes: one with double quotes for the parameter value and one without.
Hi, thanks for the project! I like it very much so far. Nonetheless, I'm having a hard Time registering some handles. Trying to register [*]
as handle Name doesn't result in an Exception but neither does it seem to result in a working handle. It is not parsed from the input. For example:
$facade = new ShortcodeFacade();
$facade->addHandler('*', function(ShortcodeInterface $s) {
return '<li>' .$s->getContent() .'</li>';
});
echo $facade->process('[*]Hello World[/*]');
results in:
[*]Hello World[/*]
I tried escaping the asterisk as well. Is it me doing something wrong? I mean [*]
is a pretty standard BBCode Element, isn't it? Would be a pain to replace it in WYSIWYG editors for this reason.
Thanks, Mark.
When trying to create a ColorHandler
to replace
[color=#F00]This is red[/color]
by
<span style="color:#F00">This is red</span>
$shortCode->getBbCode()
returns only "#" and $shortCode->getBbCode()
returns :
[
"F00" => null
]
I've been trying really hard to break this thing. It was not easy but I found a way to break it.
Shortcodes like this will not work:
[characters]
[]
[/characters]
If the brackets has characters like [sada], the characters
shortcode works as expected. It's only when it's empty it does not work. In that case it just leave it unparsed.
On the upside I tried '"_åäö
and other strange characters which works fine.
Couple of loose thoughts:
Syntax
class) to define fragments and regular expressions builder (regular and strict variant), with separate namespaces and interfaces for both,Extractor
class needs to be moved into its own namespace and renamed to RegexExtractor
taking regular expression builder as a dependency (will need a factory or at least named constructor to be usable),Parser
class,HandlerContainer
needs to be extracted from Processor
with separate interface,class_alias()
but probably will be broken (these refactorings could take place when moving towards 1.0).Deprecated: mb_strrpos(): Passing the encoding as third parameter is deprecated.
Use an explicit zero offset in Processor\Processor.php on line 139
Passing the encoding as 3rd parameter to mb_strrpos() is deprecated.
Instead pass a 0 offset, and encoding as 4th parameter.
<?php
include 'vendor/autoload.php';
use Thunder\Shortcode\ShortcodeFacade;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
$facade = new ShortcodeFacade();
$facade->addHandler('hello', function(ShortcodeInterface $s) {
return sprintf('Hello, %s!' . $s->getContent(), $s->getParameter('name'));
});
$text = '
<p>Start</p>
[hello name="Thomas"]
[hello name="Peter"]
[/hello]
<p>End</p>
';
echo $facade->process($text);
Result
<p>Start</p>
Hello, Thomas!
[hello name="Peter"]
[/hello]
<p>End</p>
$text = '
[hello name="Thomas"]
[hello name="Peter"]
[/hello]
';
Result
Hello, Thomas!
Hello, Peter!
Hi,
I'm having a problem with this amazing library. I use the shortcodes for really big replaces (Tables, imgs, etc).
The code: http://pastebin.com/NGWtVa4x (I couldn't insert in github with format)
The output: http://pastebin.com/27dQxrcL
As you can see, only works fine in the first shortcode. The second only first line. And , if you append more shortcodes, ignore them.
What's going on? I'm using dev-master version in composer.
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
and Processor::withShortcodeFactory()
(think of a better name) to allow creating custom shortcode objects using ProcessorContext
that are compatible with ProcessedShortcode
. This will allow users to put their information inside while still maintaining how library works,getText
and getShortcodeText
between ParsedShortcode
and ProcessedShortcode
,ShortcodeFacade
a mutable class with all the shortcuts to ease library usage (#36),[list]- [item/],[/list]
which renders multiple "item" elements, children of list
(item
shortcodes) receive context from data passed to list
,__invoke()
to ease events usage (#33),Processor::process()
return value using array of replacementsProcessorInterface::process()
to receive array of parsed shortcodes to allow greater flexibility (eg. filtering by parent)symfony/yaml
in composer.json
for YAML serializer,[[code]value[/code]]
),Regular parser:
BBCode:
Shortcode
constructor, and separate getter,Built-in handlers:
DeclareHandler
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.As outlined in the original PR comment: #26 (comment), I was testing the Shortcode
against some documentation I have for Grav CMS where I have created a Tab shortcode. I was running into a couple of issues, but one of them is related to very slow parsing of this example page with the RegularParser
.
After a few back and discussions with @thunderer, he believes it is related to the various non-shortcode []
references contained in the document.
I have put together a simple test scenario (https://github.com/rhukster/shortcode-test) that shows this issue. There are two documents, a small one that is considerably slower than either the RegexParser
or the WordpressParser
. The RegularParser
is not able to even process the full document as it just continues to spin until the PHP process is terminated.
FYI Both Wordpress and Regex parsers are able to parse this document. (note: corruption issues are handled in a separate issue #25 (comment))
For convenience and reducing the amount of code needed to be written by end user, this project should include some handlers ready to use or with minimal configuration. Since handling of raw closures would be difficult, those handlers should be made using __invoke()
magic method. Several examples to work on:
RawHandler
always returns getTextContent()
to provide "raw" shortcode handler,WrapHandler($before, $after)
returns shortcode content with $before
and $after
added in respective places,NameHandler()
returns shortcode name,DeclareHandler()
allows using constructs like [declare age value=18]Your age is %value%.[/declare]
and then automatically handle constructs like [age]
, [age value=20]
and so on by adding handler to processor which replaces all parameters into content placeholders with honoring default values from declare
,ContentHandler()
return shortcode content,UrlHandler()
converts shortcode content to an url,EmailHandler()
converts shortcode content to a clickable email mailto:
link,CallbackHandler($callback)
passes ShortcodeInterface
to given callback and returns its result (but you can pass the callback directly, so it's somewhat useless)PlaceholderHandler()
replaces %placeholders%
using shortcode arguments inside its content,EmbedHandler($type)
embeds posts from given 3rd party website like Facebook, Twitter, YouTube and so onNullHandler()
default handler usable when you want to discard a shortcode or provide default handler to Processor
,SerializerHandler(SerializerInterface $serializer)
returns serialized version of passed shortcode.Hi there!
I have a 300+ documents having a set of shortcodes like [banner-top]
, [banner-bottom]
and so on. I do not know the full set of these shortcodes but I know the naming convention [banner-*]
where *
is fitting [a-z-_]
regex. All I need is to register them in DB and replace with another shortcode like [banner type="top"]
.
Is it possible to define one handler for whole set of these shortcodes without collecting them manually before actual processing with your lib?
Hey there,
i know the project is in 0.* stadium.
But I'm using it for quite some time and it seems stable to me.
Would you mind to tag a 1.0 release somehow or how is the plan?
My problem is: I'm working on a php5.3 project (yep, really that old) and I can't influence the maintainers. So a "not semver" piece of software is very dangerous.
I can fix the version, but I'm not sure if this is good in that case.
4ac3b1b
could break that project.
Should i lock the version or do you plan to release a mayor version somehow?
Or do you follow an other (maybe ~) version policy?
Kind regards
Patrick
Hello,
Thank you because this library.
But I have some issues with some content below:
[container]
[Tiêu đề]
[/container]
In this case I want to show <div class="container">[Tiêu đề]</div>
("Tiêu đề" is "Title" in English) but the container shortcode auto close and I get <div class="container"></div>[Tiêu đề][/container]
But this content will working if it is not contain Vietnamese characters:
[container]
[Tieu de]
[/container]
[container]
[]
[/container]
In this case I want to show <div class="container">[]</div>
but the container shortcode auto close and I get <div class="container"></div>[][/container]
. Similar to issue one.
Can you help me fix these issues?
Thank you.
In src/Processor/Processor.php
line 143:
mb_strrpos(): Passing the encoding as third parameter is deprecated. Use an explicit zero offset
Hello, we will be having a use case when we need to add similar behaviour to a couple of shortcodes, being the only difference, their name. We don't see in the documentation, so we were wondering if its possible to provide an array of possible names or pipe between them?
like so
$facade->addHandler('hello|world', function(ShortcodeInterface $s) {
....
});
or
$facade->addHandler(['hello','world'], function(ShortcodeInterface $s) {
....
});
This would considerably shrink the codebase.
Best regards
We're using thunderer/shortcode in our Grav CMS shortcode-core
plugin: https://github.com/getgrav/grav-plugin-shortcode-core
And it was reported to me that while using also using the Gantry5 Plugin while Shortcode Core was active, resulted in an infinite loop bug. I dug into this a bit and it appears that some HTML code output by the plugin causes this PHP infinite loop bug when using the RegularParser
. It works fine with the RegexParser
or WordpressParser
BTW.
I have created a little test project where I'm using JUST the thunderer/shortcode library and the sample content in question, and it's definitely repeatable even out of the context of Grav and the Shortcode Core plugin.
In this file, you can see the HTML that is causing the issue in question. I've tried 'cleaning' the HTML, but that does not change the results.
My hunch is that it's the HTML data attributes that are throwing the parser for a loop:
...
<li>
<a
data-g5-ajaxify
data-g5-ajaxify-target="[data-g5-content]"
href="/testing/grav-g5/admin/gantry/positions?nonce=85998f180935f47b4b01ad293ac1325c"
><i class="fa fa-fw fa-object-group" aria-hidden="true"></i>
Positions</a
>
</li>
<li>
<a
data-g5-ajaxify
data-g5-ajaxify-target="[data-g5-content]"
href="/testing/grav-g5/admin/gantry/menu?nonce=85998f180935f47b4b01ad293ac1325c"
><i class="fa fa-fw fa-bars" aria-hidden="true"></i>
<span>Menu</span></a
>
</li>
...
I'm inclined to disable the Grav Shotcode Core while Gantry5 plugin is processing, as it's really not being used here, it's just that Grav is processing ALL page content for potential shortcodes.
Hi,
First, thank you for making this library available. It appears very comprehensive, but I think I've found an issue that I am banging my head against for the past 3 days. In short, I believe that this code starts to omit shortcodes when there are several levels of. Please see my input text with shortcodes below:
[la-row] [la-column width="100%"] [la-text format="h1"]Welcome![/la-text] [la-text]This page allows you to send commands to LightAct. These commands are simple strings that can be read with LightAct Layer Layouts and acted upon. This page uses standard web technologies such as html, Jquery, and AJAX so, if you can use these frameworks, you can write your own page. You can also use our own page builder, which you can access at[/la-text] [/la-column] [/la-row] [la-row] [la-column width="25%"] [la-text format="h4"]Sample heading 3[/la-text] [la-text]Sample text[/la-text] [/la-column] [la-column width="25%"] [la-text format="h4"]Sample heading 3[/la-text] [la-text]Sample text[/la-text] [/la-column] [la-column width="25%"] [la-text format="h4"]Sample heading 3[/la-text] [la-text]Sample text[/la-text] [/la-column] [la-column width="25%"] [la-text format="h4"]Sample heading 3[/la-text] [la-text]Sample text[/la-text] [/la-column] [/la-row]
I've written a php code using your library which transforms the above text into this webpage. Up to here it all works fine.
But if I multiply the above shortcodes 4 times, the last couple of columns start to get omitted as shown here.
Now this is only one manifestation of this issue. From my experience, the more shortcodes there are, especially if they are nested, the sooner this problem appears.
I was wondering if there is anything you can do to help?
Hello there,
Since the code has already Interfaces and Managers, you should use a repository pattern to organize and centralize the whole code.
It'll also remove any chances of duplications and make the code less complex to use including the fact that you will only need to inject a single depency instead of 5, as exemple :
use Thunder\Shortcode\Extractor;
use Thunder\Shortcode\Parser;
use Thunder\Shortcode\Processor;
use Thunder\Shortcode\Shortcode;
use Thunder\Shortcode\Serializer\JsonSerializer;
$processor = new Processor(new Extractor(), new Parser());
$processor->addHandler('sample', function(Shortcode $s) {
return (new JsonSerializer())->serialize($s);
});
assert('x {"name":"sample","args":{"arg":"val"},"content":"cnt"} y'
=== $processor->process('x [sample arg=val]cnt[/sample] y');
Will be like :
use Thunder\Shortcode\ShortcodeManager;
$instance = new ShortcodeManager();
$instance->processor()->addHandler('sample', function(Shortcode $s) {
return $instance->serializer()->serialize($s)->toJson();
});
assert('x {"name":"sample","args":{"arg":"val"},"content":"cnt"} y'
=== $instance->processor()->process('x [sample arg=val]cnt[/sample] y');
Also, it'll allow us to make a globalized serializer that'll serialize to JSON, XML and more ;)
You can think about changing some method to make these more simple to use with a repository, all the dependencies are already injected and ready to use.
Using Laravel 5.4
public function getContentAttribute()
{
$facade = new ShortCodeFacade();
$facade->addHandler('hello', function (ShortcodeInterface $s) {
return sprintf('%s', $s->getParameter('alt_text'));
});
return $facade->process($this->post_content);
}
$this->post_content
// [img alt_text='' description='']https://noschool.xsrv.jp/wp/wp-content/uploads/2018/06/Simulator-Screen-Shot-iPhone-X-2018-06-08-at-21.40.37.png[/img]
It doesnt escape shortcodes, and cannot get parameter.
In the current implementation of Processor
, TextSerializer
is used to serialize shortcodes without registered handler. This may cause bugs when for example text contains consecutive shortcodes of the same name. Let's say that we have text with two shortcodes, one self-closed and one with content: [code /] [code]content[/code]
and it does not have a registered handler. The result of the processing will be: [code] [code]content[/code]
because said serializer does not have the information about shortcode being self-closing or not. Then if this text gets passed again into Processor
the result will be a single shortcode with content [code]content
. Possible solutions to this problem:
null
),RegexParser
to also extract information about that "slash" and ParsedShortcode
to store it as a separate field.The Regular parser is not working when having HTML content on shortcodes.
Example:
$handlers->add($emptyCustomTag, function (ShortcodeInterface $s) {
return '';
});
$processor = new Processor(new RegularParser(), $handlers);
$processor->process("[isolated-block]<a>Hola</a>[/isolated-block]");
Returns
<a>Hola</a>[/isolated-block]
Current implementation of AbstractShortcode
throws an exception when trying to get parameter of the shortcode which is not present. Method getParameter($name, $default)
detects if default value was provided by checking if $default
value is null
so that when someone explicitly passes null
exception is thrown. Two possible solutions of this problem are as follows:
$default
,null
and func_num_args() < 2
.Maybe I'm missing a setting or something but I can't get the thing below to work properly:
<?php
include 'vendor/autoload.php';
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="users">
[hello name="Thomas"]
[hello name="Peter"]
</div>
';
echo $facade->process($text);
The result will be:
<div class="users">
Hello, Thomas!
[hello name="Peter"]
</div>
So when wrapping both shortcodes around a single div it will only parse the first one.
The strange thing is that this works:
(se full example above. I replaced the $text variable)
$text = '
[hello name="Thomas"]
[hello name="Peter"]
';
The result of the last test is:
Hello, Thomas!
Hello, Peter!
Hi @thunderer ,
I was wondering if there was a specific reason for making the RegularParser
class final and most of its methods private instead of protected?
The RegularParser
is almost what I need but I would like to override the shotcode
function, and I can't do it currently without modifying your library. The other option is to duplicate all the private code and I'm not too enthusiastic about it.
What do you think?
Hi, I'm creating shortcode for my project. It useful when we can have default shortcode. Imagine when we use WordPress, we want to retrieve any post field (included post meta value which can be any string). It's awesome when we can do something like this:
$handler->add('*', function (ShortcodeInterface $s) {
$shortcode_name = $s->getName();
return get_post_meta(get_the_ID(), $shortcode_name, true);
});
So if we have a post with a field country
and value is Hong Kong
, when we run [country]
shortcode, it returns Hong Kong.
I've been implementing this library on a project in my company, which scraps a whole local Wordpress with httrack and then modifies the files in order to fit to our needs.
To make some parts dynamic, we're implementing shortcodes (which we translate to custom PHP code). I've been testing your library without any problems, but when I gave it a definitive file, it didn't triggered any handler, and the parser pushed PHP memory consumption over 800MB.
After several tests, I've decided to try the RegexParser. It parsed the files correctly, and using a riddiculous amount of time and memory compared with the standard one.
I can understand the extra time and memory consumption (uncer certain limits), but I don't get why the parser didn't saw any of my tags, I am the only one who suffered this issue?
BTW, thank you very much for your work, your library is awesome and I'm enjoying it a lot!
I'm yet to use this library but I was wondering is Custom Shortcodes being supported? (#16 "add ProcessedShortcodeInterface and Processor::withShortcodeFactory() (think of a better name) to allow creating custom shortcode objects")
Hello,
Is there any built-in method to strip all shortcodes in provided text ?
I don't want to reinvent a wheel here.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.