17
17
use Grav \Common \Uri ;
18
18
use Grav \Common \Utils ;
19
19
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 ;
20
26
21
27
/**
22
28
* Parser API
@@ -35,7 +41,6 @@ class Parser implements ParserInterface
35
41
* Regular expressions
36
42
*/
37
43
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 ' ;
39
44
const REGEX_MEDIA_P = '/<p>\s*(<a .*>\s*<img.*\s*<\/a>|\s*<img.*|<img.*\s*|<video.*|<audio.*)\s*<\/p>/i ' ;
40
45
41
46
/**
@@ -48,6 +53,7 @@ public function __construct($config, $transport)
48
53
{
49
54
$ this ->config = $ config ;
50
55
$ this ->transport = $ transport ;
56
+ $ this ->props = [];
51
57
}
52
58
53
59
/**
@@ -60,32 +66,28 @@ public function __construct($config, $transport)
60
66
*/
61
67
public function interpretShortcodes (string $ content , string $ id )
62
68
{
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 ());
76
75
if (Utils::startsWith ($ name , 'class ' )) {
77
- $ return ['class ' ] = $ value ;
78
- } elseif (Utils::startsWith ($ name , 'hide ' )) {
79
- $ return ['hide ' ] = true ;
76
+ $ this ->props ['class ' ] = $ value ;
80
77
} elseif (Utils::startsWith ($ name , 'style ' )) {
81
78
$ name = str_replace ('style- ' , '' , $ name );
82
- $ return ['styles ' ][$ name ] = $ value ;
79
+ $ this -> props ['styles ' ][$ name ] = $ value ;
83
80
} 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 ];
85
84
}
85
+ return ;
86
86
}
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 ];
89
91
}
90
92
91
93
/**
@@ -111,10 +113,11 @@ public function processFragments(string $content)
111
113
* @param array $styles List of key-value pairs
112
114
* @param string $route Route to Page for relative assets
113
115
* @param string $id Slide id-attribute
116
+ * @param string $base Base path to prepend to file
114
117
*
115
118
* @return string Processed styles, in inline string
116
119
*/
117
- public function processStylesData (array $ styles , string $ route , string $ id )
120
+ public function processStylesData (array $ styles , string $ route , string $ id, string $ base = "" )
118
121
{
119
122
$ inline = $ data = '' ;
120
123
foreach ($ styles as $ property => $ value ) {
@@ -128,7 +131,7 @@ public function processStylesData(array $styles, string $route, string $id)
128
131
$ locations = Utilities::explodeFileLocations ($ locations , GRAV_ROOT , '/ ' , '/ ' );
129
132
$ file = Utilities::fileFinder ($ value , $ locations );
130
133
$ file = str_ireplace (GRAV_ROOT , '' , $ file );
131
- $ value = $ this -> config [ ' base_url ' ] . $ file ;
134
+ $ value = $ base . $ file ;
132
135
}
133
136
$ inline .= $ property . ': url( ' . $ value . '); ' ;
134
137
} elseif (Utils::startsWith ($ property , 'data ' )) {
0 commit comments