Skip to content

Commit 6be4af9

Browse files
committed
Merge branch 'release/4.1.0'
2 parents e903a9e + 564a0d9 commit 6be4af9

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v4.1.0
2+
## 04/14/2019
3+
4+
1. [](#new)
5+
* Support for a `ShortCodeManager::getRawHandlers()` to support shortcodes that need to process **before** Markdown (like upcoming `Prism-Highlighter`)
6+
17
# v4.0.1
28
## 03/21/2019
39

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.0.1
2+
version: 4.1.0
33
description: "This plugin provides the core functionality for shortcode plugins"
44
icon: code
55
author:

classes/ShortcodeManager.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class ShortcodeManager
2323
/** @var HandlerContainer $handlers */
2424
protected $handlers;
2525

26+
/** @var HandlerContainer $raw_handlers */
27+
protected $raw_handlers;
28+
2629
/** @var EventContainer $events */
2730
protected $events;
2831

@@ -41,6 +44,7 @@ public function __construct()
4144
$this->grav = Grav::instance();
4245
$this->config = $this->grav['config'];
4346
$this->handlers = new HandlerContainer();
47+
$this->raw_handlers = new HandlerContainer();
4448
$this->events = new EventContainer();
4549
$this->states = [];
4650
$this->assets = [];
@@ -138,6 +142,16 @@ public function getHandlers()
138142
return $this->handlers;
139143
}
140144

145+
/**
146+
* returns the current raw handler container object
147+
*
148+
* @return HandlerContainer
149+
*/
150+
public function getRawHandlers()
151+
{
152+
return $this->raw_handlers;
153+
}
154+
141155
/**
142156
* returns the current event container object
143157
*
@@ -211,25 +225,36 @@ public function setupMarkdown($markdown)
211225
/**
212226
* process the content by running over all the known shortcodes with the
213227
* chosen parser
214-
*
215-
* @param Page $page the page to work on
216-
* @param Data $config configuration merged with the page config
228+
*
229+
* @param Page $page the page to work on
230+
* @param Data $config configuration merged with the page config
231+
* @param null $handlers
232+
* @return string
217233
*/
218-
public function processContent(Page $page, Data $config)
234+
public function processContent(Page $page, Data $config, $handlers = null)
219235
{
220236
$parser = $this->getParser($config->get('parser'));
221237

238+
if (!$handlers) {
239+
$handlers = $this->handlers;
240+
}
241+
222242
if ($page && $config->get('enabled')) {
223243
$this->page = $page;
224244
$content = $page->getRawContent();
225-
$processor = new Processor(new $parser(new CommonSyntax()), $this->handlers);
245+
$processor = new Processor(new $parser(new CommonSyntax()), $handlers);
226246
$processor = $processor->withEventContainer($this->events);
227247
$processed_content = $processor->process($content);
228248

229249
return $processed_content;
230250
}
231251
}
232252

253+
public function processRawContent(Page $page, Data $config)
254+
{
255+
return $this->processContent($page, $config, $this->raw_handlers);
256+
}
257+
233258
/**
234259
* Allow the processing of shortcodes directly on a string
235260
* For example when used by Twig directly

shortcode-core.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function onPluginsInitialized()
4545
'onThemeInitialized' => ['onThemeInitialized', 0],
4646
'onMarkdownInitialized' => ['onMarkdownInitialized', 0],
4747
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
48+
'onPageContentRaw' => ['onPageContentRaw', 0],
4849
'onPageContentProcessed' => ['onPageContentProcessed', 0],
4950
'onPageContent' => ['onPageContent', 0],
5051
'onTwigInitialized' => ['onTwigInitialized', 0],
@@ -73,39 +74,45 @@ public function onMarkdownInitialized(Event $event)
7374
$this->shortcodes->setupMarkdown($event['markdown']);
7475
}
7576

77+
/**
78+
* Process shortcodes before Grav's processing
79+
*
80+
* @param Event $e
81+
*/
82+
public function onPageContentRaw(Event $e)
83+
{
84+
$this->processShortcodes($e['page'], 'processRawContent');
85+
}
86+
7687
/**
7788
* Process shortcodes after Grav's processing, but before caching
7889
*
7990
* @param Event $e
8091
*/
8192
public function onPageContentProcessed(Event $e)
8293
{
83-
/** @var Page $page */
84-
$page = $e['page'];
85-
$config = $this->mergeConfig($page);
94+
$this->processShortcodes($e['page'], 'processContent');
95+
}
96+
97+
protected function processShortcodes($page, $type = 'processContent') {
8698
$meta = [];
99+
$config = $this->mergeConfig($page);
87100

88101
// Don't run in admin pages other than content
89102
$admin_pages_only = isset($config['admin_pages_only']) ? $config['admin_pages_only'] : true;
90-
if ($admin_pages_only &&
91-
$this->isAdmin() &&
92-
!Utils::startsWith($page->filePath(), $this->grav['locator']->findResource('page://'))) {
103+
if ($admin_pages_only && $this->isAdmin() && !Utils::startsWith($page->filePath(), $this->grav['locator']->findResource('page://'))) {
93104
return;
105+
} else {
106+
$this->active = $config->get('active', true);
94107
}
95108

96-
$this->active = $config->get('active', true);
97-
98109
// if the plugin is not active (either global or on page) exit
99110
if (!$this->active) {
100111
return;
101112
}
102113

103-
// reset objects and assets for the page
104-
$this->shortcodes->resetObjects();
105-
$this->shortcodes->resetAssets();
106-
107114
// process the content for shortcodes
108-
$page->setRawContent($this->shortcodes->processContent($page, $config));
115+
$page->setRawContent($this->shortcodes->$type($page, $config));
109116

110117
// if objects found set them as page content meta
111118
$shortcode_objects = $this->shortcodes->getObjects();
@@ -125,6 +132,24 @@ public function onPageContentProcessed(Event $e)
125132
}
126133
}
127134

135+
protected function getConfig($page)
136+
{
137+
$config = $this->mergeConfig($page);
138+
$this->active = false;
139+
140+
// Don't run in admin pages other than content
141+
$admin_pages_only = isset($config['admin_pages_only']) ? $config['admin_pages_only'] : true;
142+
if ($admin_pages_only &&
143+
$this->isAdmin() &&
144+
!Utils::startsWith($page->filePath(), $this->grav['locator']->findResource('page://'))) {
145+
146+
} else {
147+
$this->active = $config->get('active', true);
148+
}
149+
150+
return $config;
151+
}
152+
128153
/**
129154
* Handle the assets that might be associated with this page
130155
*/

0 commit comments

Comments
 (0)