Skip to content

Commit f2101f4

Browse files
committed
Refactor Shortcode parsing
1 parent 54ad455 commit f2101f4

File tree

172 files changed

+7763
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+7763
-79
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# v1.7.1-beta.8
2+
## XX-05-2019
3+
4+
1. [](#improved)
5+
- Initialize API earlier
6+
- Refactor Parser->interpretShortcodes()
7+
- Parse Shortcodes with Thunderer/Shortcode
8+
2. [](#new)
9+
- Option for Shortcode Parser
10+
111
# v1.7.1-beta.7
212
## 19-05-2019
313

blueprints.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ docs: https://github.com/OleVik/grav-plugin-presentation/blob/master/README.md
1414
license: MIT
1515
dependencies:
1616
- { name: grav, version: ">=1.6" }
17-
- { name: shortcode-core, version: ">=4.1" }
1817

1918
form:
2019
validation: strict
@@ -280,6 +279,14 @@ form:
280279
description: PLUGIN_PRESENTATION.ADMIN.ADVANCED.SHORTCODE_CLASSES.DESCRIPTION
281280
validate:
282281
type: string
282+
shortcode_parser:
283+
type: select
284+
label: PLUGIN_PRESENTATION.ADMIN.ADVANCED.SHORTCODE_PARSER.LABEL
285+
description: PLUGIN_PRESENTATION.ADMIN.ADVANCED.SHORTCODE_PARSER.LABEL
286+
options:
287+
RegularParser: PLUGIN_PRESENTATION.ADMIN.ADVANCED.SHORTCODE_PARSER.OPTIONS.REGULAR
288+
RegexParser: PLUGIN_PRESENTATION.ADMIN.ADVANCED.SHORTCODE_PARSER.OPTIONS.REGEX
289+
WordpressParser: PLUGIN_PRESENTATION.ADMIN.ADVANCED.SHORTCODE_PARSER.OPTIONS.WORDPRESS
283290
transition:
284291
type: toggle
285292
label: PLUGIN_PRESENTATION.ADMIN.ADVANCED.TRANSITION.LABEL

classes/Parser.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
use Grav\Common\Uri;
1818
use Grav\Common\Utils;
1919
use Grav\Plugin\PresentationPlugin\Utilities;
20+
use Thunder\Shortcode\Parser\RegularParser;
21+
use Thunder\Shortcode\Parser\RegexParser;
22+
use Thunder\Shortcode\Parser\WordpressParser;
23+
use Thunder\Shortcode\Processor\Processor;
24+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
25+
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
2026

2127
/**
2228
* Parser API
@@ -35,7 +41,6 @@ class Parser implements ParserInterface
3541
* Regular expressions
3642
*/
3743
const REGEX_FRAGMENT_SHORTCODE = '~\[fragment=*([a-zA-Z-]*)\](.*)\[\/fragment\]~im';
38-
const REGEX_SHORTCODES = '~((?:\[\s*(?<name>[a-zA-Z0-9-_]+)\s*(?:\=\s*(?<bbCode>\"(?:[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"|((?:(?!=\s*|\]|\/\])[^\s])+)))?\s*(?<parameters>(?:\s*(?:\w+(?:\s*\=\s*\"(?:[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"|\s*\=\s*((?:(?!=\s*|\]|\/\])[^\s])+)|(?=\s|\]|\/\s*\]|$))))*)\s*(?:\](?<content>.*?)\[\s*(?<markerContent>\/)\s*(\k<name>)\s*\]|\]|(?<marker>\/)\s*\])))~u';
3944
const REGEX_MEDIA_P = '/<p>\s*(<a .*>\s*<img.*\s*<\/a>|\s*<img.*|<img.*\s*|<video.*|<audio.*)\s*<\/p>/i';
4045

4146
/**
@@ -48,6 +53,7 @@ public function __construct($config, $transport)
4853
{
4954
$this->config = $config;
5055
$this->transport = $transport;
56+
$this->props = [];
5157
}
5258

5359
/**
@@ -60,32 +66,28 @@ public function __construct($config, $transport)
6066
*/
6167
public function interpretShortcodes(string $content, string $id)
6268
{
63-
$return = array();
64-
preg_match_all(
65-
self::REGEX_SHORTCODES,
66-
$content,
67-
$matches,
68-
PREG_SET_ORDER,
69-
0
70-
);
71-
if (!empty($matches)) {
72-
foreach ($matches as $match) {
73-
$name = $match['name'];
74-
$value = trim($match['bbCode'], '"');
75-
$content = str_replace($match[0], '', $content);
69+
$handlers = new HandlerContainer();
70+
$handlers->setDefault(
71+
function (ShortcodeInterface $sc) {
72+
$return = array();
73+
$name = $sc->getName();
74+
$value = $sc->getParameter($name, $sc->getBbCode());
7675
if (Utils::startsWith($name, 'class')) {
77-
$return['class'] = $value;
78-
} elseif (Utils::startsWith($name, 'hide')) {
79-
$return['hide'] = true;
76+
$this->props['class'] = $value;
8077
} elseif (Utils::startsWith($name, 'style')) {
8178
$name = str_replace('style-', '', $name);
82-
$return['styles'][$name] = $value;
79+
$this->props['styles'][$name] = $value;
8380
} elseif (Utils::startsWith($name, 'data')) {
84-
$return['styles'][$name] = $value;
81+
$this->props['styles'][$name] = $value;
82+
} elseif (Utils::startsWith($name, 'hide')) {
83+
$this->props['hide'] = [true];
8584
}
85+
return;
8686
}
87-
}
88-
return ['content' => $content, 'props' => $return];
87+
);
88+
$parser = "Thunder\Shortcode\Parser\\" . $this->config['shortcode_parser'];
89+
$processor = new Processor(new $parser(), $handlers);
90+
return ['content' => $processor->process($content), 'props' => $this->props];
8991
}
9092

9193
/**
@@ -111,10 +113,11 @@ public function processFragments(string $content)
111113
* @param array $styles List of key-value pairs
112114
* @param string $route Route to Page for relative assets
113115
* @param string $id Slide id-attribute
116+
* @param string $base Base path to prepend to file
114117
*
115118
* @return string Processed styles, in inline string
116119
*/
117-
public function processStylesData(array $styles, string $route, string $id)
120+
public function processStylesData(array $styles, string $route, string $id, string $base = "")
118121
{
119122
$inline = $data = '';
120123
foreach ($styles as $property => $value) {
@@ -128,7 +131,7 @@ public function processStylesData(array $styles, string $route, string $id)
128131
$locations = Utilities::explodeFileLocations($locations, GRAV_ROOT, '/', '/');
129132
$file = Utilities::fileFinder($value, $locations);
130133
$file = str_ireplace(GRAV_ROOT, '', $file);
131-
$value = $this->config['base_url'] . $file;
134+
$value = $base . $file;
132135
}
133136
$inline .= $property . ': url(' . $value . ');';
134137
} elseif (Utils::startsWith($property, 'data')) {

composer.json

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,14 @@
2727
},
2828
"require": {
2929
"php": ">=7.1.3",
30-
"getgrav/grav": ">=1.6",
31-
"getgrav/grav-plugin-shortcode-core": "4.1.*",
3230
"michelf/php-smartypants": "^1.8",
33-
"composer/installers": "~1.0"
34-
},
35-
"suggest": {
36-
"getgrav/grav-plugin-shortcode-core": "Enables Shortcodes"
31+
"composer/installers": "~1.0",
32+
"thunderer/shortcode": "^0.7.2"
3733
},
3834
"autoload": {
3935
"psr-4": {
4036
"Grav\\Plugin\\PresentationPlugin\\": "classes/",
4137
"Grav\\Plugin\\PresentationPlugin\\API\\": "classes/"
4238
}
43-
},
44-
"repositories": [
45-
{
46-
"type": "package",
47-
"package": {
48-
"name": "getgrav/grav-plugin-shortcode-core",
49-
"version": "dev-master",
50-
"type": "grav-plugin",
51-
"require": {
52-
"thunderer/shortcode": "~0.7",
53-
"composer/installers": "~1.0"
54-
},
55-
"source": {
56-
"url": "https://github.com/getgrav/grav-plugin-shortcode-core.git",
57-
"type": "git",
58-
"reference": "4.1.0"
59-
}
60-
}
61-
}
62-
]
39+
}
6340
}

composer.lock

Lines changed: 176 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

languages.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ en:
188188
SHORTCODE_CLASSES:
189189
LABEL: Shortcode Classes
190190
DESCRIPTION: Default Classes to use with Presentation-shortcodes, each separated by a space.
191+
SHORTCODE_PARSER:
192+
LABEL: Shortcode Parser
193+
DESCRIPTION: "Processor to use for Shortcode parsing: RegularParser, RegexParser, or WordpressParser"
194+
OPTIONS:
195+
REGULAR: RegularParser
196+
REGEX: RegexParser
197+
WORDPRESS: WordpressParser
191198
TRANSITION:
192199
LABEL: OnLoad Transition
193200
DESCRIPTION: Fade in from white when page (DOM-structure) is ready.

0 commit comments

Comments
 (0)