Simplify the plugin call

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Simplify the plugin call

Post by cmb » Sun Jul 06, 2014 10:10 pm

Hello Community,

in a German thread the idea came up to simplify the plugin call, so that the trailing semicolon can be omitted, and also empty parentheses. For instance:

Code: Select all

{{{quoteoftheday}}}
We may even consider to make the parentheses generally optional, e.g.:

Code: Select all

{{{audio 'song'}}}
That saves a few keystrokes and makes the code most likely better readable for non-programmers.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: Simplify the plugin call

Post by svasti » Mon Jul 07, 2014 10:37 am

What about one more simplification: only 2 x {{ ;)

{{calendar}}
cmb wrote:parentheses generally optional
what about numerical variables, could they go without ' ' ? E.g. {{calendar 2}} (=> display of two months in coming calendar 2.0)

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify the plugin call

Post by cmb » Mon Jul 07, 2014 11:02 am

svasti wrote:What about one more simplification: only 2 x {{ ;)
I've thought about that already, but I'm not sure, if there are existing installations which use double braces for something else. Maybe we should delay that to 1.7.
svasti wrote:what about numerical variables, could they go without ' ' ? E.g. {{calendar 2}} (=> display of two months in coming calendar 2.0)
Yes, that already works due to PHP's dynamic typing, and I don't see, why that would be a problem after the simplifications.
Christoph M. Becker – Plugins for CMSimple_XH

Tata
Posts: 3586
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Re: Simplify the plugin call

Post by Tata » Mon Jul 07, 2014 12:35 pm

I basically don't see any serious reason for changing the calls. It there is no reason, I would leave things as they are right now.
I even welcomed very much the {{{PLUGIN:...}}}. It was good visible in the code and it was prety safe to explain to any enuser that he must never touch these parts.
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify the plugin call

Post by cmb » Mon Jul 07, 2014 1:02 pm

Tata wrote:I basically don't see any serious reason for changing the calls.
I do not see a serious reason, too, but I consider it a slight convenience.

And IMO we should not change the plugin call, but rather make some parts that are not strictly necessary optional.
Tata wrote:I even welcomed very much the {{{PLUGIN:...}}}. It was good visible in the code and it was prety safe to explain to any enuser that he must never touch these parts.
Well, this notation does still work. While PLUGIN: was necessary in earlier versions, now it is optional. And actually, PLUGIN: is now simply treated as a comment, so one could write:

Code: Select all

{{{DON'T CHANGE THIS LINE:plugin('...');}}}
Or maybe, for users who do not speak English to explain the call. For instance, for a German user:

Code: Select all

{{{Zeigt das Zitat des Tages:quoteoftheday();}}}
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify the plugin call

Post by cmb » Thu Dec 18, 2014 10:59 pm

I suggest the following patch, which allows to omit the trailing semicolon and/or the parentheses and allows for arbitrary whitespace between the function name and the arguments:

Code: Select all

Index: cmsimple/functions.php
===================================================================
--- cmsimple/functions.php	(revision 1402)
+++ cmsimple/functions.php	(working copy)
@@ -225,21 +225,23 @@
 
     $message = '<span class="xh_fail">' . $tx['error']['plugincall']
         . '</span>';
-    $re = '/{{{(?:[^:]+:)?(([a-z_0-9]+)\(.*?\);)}}}/iu';
+    $re = '/{{{(?:[^:]+:)?([a-z_0-9]+)\s*\(?(.*?)\)?;?}}}/iu';
     preg_match_all($re, $text, $calls, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
     $results = array();
     foreach ($calls as $call) {
-        $expression = preg_replace(
+        $arguments = preg_replace(
             array(
                 '/&(quot|#34);/i', '/&(amp|#38);/i', '/&(apos|#39);/i',
                 '/&(lt|#60);/i', '/&(gt|#62);/i', '/&(nbsp|#160);/i'
             ),
             array('"', '&', '\'', '<', '>', ' '),
-            $call[1][0]
+            $call[2][0]
         );
-        $function = $call[2][0];
+        $function = $call[1][0];
         if (function_exists($function)) {
-            $results[] = XH_evaluateSinglePluginCall($expression);
+            $results[] = XH_evaluateSinglePluginCall(
+                $function . '(' . $arguments . ')'
+            );
         } else {
             $results[] = sprintf($message, $function);
         }
@@ -273,7 +275,7 @@
     foreach ($GLOBALS as $___var => $___value) {
         $$___var = $GLOBALS[$___var];
     }
-    return eval('return ' . $___expression);
+    return eval('return ' . $___expression . ';');
 }
 
 /**
Index: tests/unit/FunctionsTest.php
===================================================================
--- tests/unit/FunctionsTest.php	(revision 1402)
+++ tests/unit/FunctionsTest.php	(working copy)
@@ -130,7 +130,16 @@
                 'foo {{{PLUGIN:doesnotexist();}}} bar',
                 'foo <span class="xh_fail">Function doesnotexist() is not defined!</span> bar'
             ),
-            array('foo {{{PLUGIN:trim(\':\');}}} bar', 'foo : bar')
+            array('foo {{{PLUGIN:trim(\':\');}}} bar', 'foo : bar'),
+            array( // without trailing semicolon
+                'foo {{{trim(\'baz\');}}} bar', 'foo baz bar'
+            ),
+            array( // with whitespace before the opening parenthesis
+                'foo {{{trim(\'baz\');}}} bar', 'foo baz bar'
+            ),
+            array( // without parentheses
+                'foo {{{trim \'baz\';}}} bar', 'foo baz bar'
+            )
         );
     }
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Simplify the plugin call

Post by cmb » Sat Dec 27, 2014 5:03 pm

Done (1409).
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply