Skip to content

Commit 8a5536d

Browse files
committed
Merge branch 'release/4.1.3'
2 parents 84342c7 + 2e9359c commit 8a5536d

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# v4.1.3
2+
## 08/09/2019
3+
4+
1. [](#improved)
5+
* Fix for shortcode objects not being available. For example `[section][/section]` not working previously without `process: twig: true`
6+
* `README.md` improvements
7+
18
# v4.1.2
29
## 06/22/2019
310

README.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,104 @@ You can now use shortcodes in Twig templates and process them with the `|shortco
404404
405405
## Developing Shortcode Plugins
406406
407-
The **Shortcode Core** plugin is developed on the back of the [Thunderer Advanced Shortcode Engine](https://github.com/thunderer/Shortcode) and as such loads the libraries and classes required to build third party shortcode plugins. Also we introduce a new event called `onShortcodeHandlers()` that allows a 3rd party plugin to create and add their own custom handlers. These are then all processed by the core plugin in one shot.
407+
The **Shortcode Core** plugin is developed on the back of the [Thunderer Advanced Shortcode Engine](https://github.com/thunderer/Shortcode) and as such loads the libraries and classes required to build third party shortcode plugins.
408+
409+
The simplest way to add your own custom shortcodes, it to simply create a new shortcode in a directory (e.g. `user/custom/shortcodes`) such as this simple one to allow for strike-through text:
410+
411+
```php
412+
<?php
413+
namespace Grav\Plugin\Shortcodes;
414+
415+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
416+
417+
class StrikeShortcode extends Shortcode
418+
{
419+
public function init()
420+
{
421+
$this->shortcode->getHandlers()->add('strike', function(ShortcodeInterface $sc) {
422+
return '<del>'.$sc->getContent().'</del>';
423+
});
424+
}
425+
}
426+
```
427+
428+
Then simply set the plugin to look in this directory for custom shortcodes by editing the `user/config/plugins/shortcode-core.yaml` file (create it if missing):
429+
430+
```yaml
431+
custom_shortcodes: '/user/custom/shortcodes'
432+
```
433+
434+
The more flexible approach is to create a custom plugin to do provide a tidy package for your shotdcodes.
435+
436+
We introduced a new event called `onShortcodeHandlers()` that allows a 3rd party plugin to create and add their own custom handlers. These are then all processed by the core plugin in one shot.
437+
438+
```php
439+
public static function getSubscribedEvents()
440+
{
441+
return [
442+
'onShortcodeHandlers' => ['onShortcodeHandlers', 0]
443+
];
444+
}
445+
```
446+
447+
Then you just need to listen to the event:
448+
449+
```php
450+
public function onShortcodeHandlers()
451+
{
452+
$this->grav['shortcode']->registerAllShortcodes(__DIR__.'/shortcodes');
453+
}
454+
```
455+
456+
Lastly create your shortcode in the `user/plugins/my-plugin/shortcodes/` folder, in this example we created a simple `[red][/red]` shortcode as `RedShortcode.php`:
457+
458+
```php
459+
<?php
460+
namespace Grav\Plugin\Shortcodes;
461+
462+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
463+
464+
class RedShortcode extends Shortcode
465+
{
466+
public function init()
467+
{
468+
$this->shortcode->getHandlers()->add('red', function(ShortcodeInterface $sc) {
469+
return '<span style="color:red;">'.$sc->getContent().'</span>';
470+
});
471+
}
472+
}
473+
```
408474

409475
> If you have not already done so, I suggest reading the [Grav Plugin Tutorial](http://learn.getgrav.org/plugins/plugin-tutorial) first to gain a full understanding of what you need to develop a Grav plugin.
410476

411-
The best way to see how to create a new shortcode-based plugin is to look at the **Shortcode UI** plugin that extends the **Shortcode Core** by adding more shortcodes. It also makes use of Twig to handle processing and has some more advanced shortcode techniques.
477+
The best way to see how to create a new shortcode-based plugins is to look at the **Shortcode UI** plugin that extends the **Shortcode Core** by adding more shortcodes. It also makes use of Twig to handle processing and has some more advanced shortcode techniques.
412478

413479
* Core Plugin: https://github.com/getgrav/grav-plugin-shortcode-ui/blob/develop/shortcode-ui.php
414480
* Tabs Shortcode Example: https://github.com/getgrav/grav-plugin-shortcode-ui/blob/develop/shortcodes/TabsShortcode.php
415481
* Color Shortcode Example: https://github.com/getgrav/grav-plugin-shortcode-core/blob/develop/shortcodes/ColorShortcode.php
416482
* Section Shortcode Example: https://github.com/getgrav/grav-plugin-shortcode-core/blob/develop/shortcodes/SectionShortcode.php
483+
* Section Prism Highlight Example: https://github.com/trilbymedia/grav-plugin-prism-highlight/blob/develop/shortcodes/PrismShortcode.php
484+
485+
## Processing Shortcodes Before or After Markdown processing
486+
487+
There are basically two ways of processing a shortcode:
488+
489+
1. After markdown is processed
490+
2. Before markdown is processed
491+
492+
These two approaches are important because, for the most part, shortcodes make more sense when they can 'wrap' markdown, so they process **after** markdown.
493+
494+
For example a `[div][/div]` shortcode would be useless if it ran before markdown is processed because it would add the relevant HTML `<div></div>` tags, and then the markdown parser would promptly **skip** all markdown processing between those divs because it won't process markdown **inside** HTML. So this shortcode and most others run after markdown processing has already occurred using this approach:
495+
496+
```php
497+
$this->shortcode->getHandlers()->add('div', function(ShortcodeInterface $sc) { ... }
498+
```
499+
Notice the `getHandlers()` call is the standard way to add a handler.
500+
501+
However, there are situations when you need to process the shortcode **before** the markdown processing to ensure markdown **is skipped**, like in the example of a code block. This is why in the [Prism Highlighter](https://github.com/trilbymedia/grav-plugin-prism-highlight) plugin, we use this approach to defining the shortcode:
502+
503+
```php
504+
$this->shortcode->getRawHandlers()->add('prism', function(ProcessedShortcode $sc) { ... }
505+
```
506+
507+
The difference here is it uses `getRawHandlers()` to ensure the handler is processed to the content in the _raw_ state.

blueprints.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Shortcode Core
2-
version: 4.1.2
2+
version: 4.1.3
33
description: "This plugin provides the core functionality for shortcode plugins"
44
icon: code
55
author:

classes/Shortcode.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function getParser()
4848
return $this->config->get('plugins.shortcode-core.parser');
4949
}
5050

51-
public function getBbCode($sc)
51+
public function getBbCode($sc, $default = null)
5252
{
53-
$code = null;
53+
$code = $default;
5454

5555
if ($this->getParser() === 'wordpress') {
5656
$params = $sc->getParameters();
@@ -61,6 +61,7 @@ public function getBbCode($sc)
6161
} else {
6262
$code = $sc->getBbCode();
6363
}
64+
6465
return $code;
6566
}
6667

shortcode-core.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ public function onTwigPageVariables(Event $e)
221221
{
222222
// check current event's page content meta for objects, and if found as them as twig variables
223223
$meta = $e['page']->getContentMeta('shortcodeMeta');
224-
225224
$this->mergeTwigVars($meta);
226225
}
227226

@@ -230,9 +229,11 @@ public function onTwigPageVariables(Event $e)
230229
*/
231230
public function onTwigSiteVariables()
232231
{
232+
// force page processing now as we need shortcodes available
233+
$this->grav['page']->content();
234+
233235
// check current page content meta for objects, and if found as them as twig variables
234236
$meta = $this->grav['page']->getContentMeta('shortcodeMeta');
235-
236237
$this->mergeTwigVars($meta);
237238
}
238239

0 commit comments

Comments
 (0)