LuaExecute
Questions answered by this recipe
Description
This recipe lets you execute Lua scripts in PmWiki pages. It is as simple as writing your Lua code inside the (:lua:) ... (:luaend:) markups. The recipe executes the Lua code and displays its output in place of the (:lua:) ... (:luaend:) wiki text.
The Lua script is executed client-side, by the visitor's web browser, after the page load. This makes use of a Lua 5.2 virtual machine written in JavaScript[1]. By default, no HTML is allowed in the output - the output data is escaped before being displayed.
See a live demo at http://www.206labs.com/f/?n=PmWikiRecipes.LuaExecute
What is Lua
Lua is a powerful, fast, lightweight, embeddable scripting language. The language combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
More information is available at the official website at http://www.lua.org.
Installation
Download LuaExecute.zipΔ, unpack the files to the associated directories, and add to config.php:
include_once("cookbook/luaexecute.php");
Configuration
Add configuration settings before include of the script file!
You can add an initialization Lua script with custom functions:
-
$LuaConfig['initscript'] = file_get_contents('cookbook/luaexecute.lua');
- The default is empty.
By default, the Lua output data is escaped before being displayed, and the ability to call JS code from within Lua is disabled, but you can turn off this safe mode:
-
$LuaConfig['safemode'] = false;
- The default is true. Keep in mind that disabling the safe mode is dangerous: Lua code inserted by a malicious visitor could be quite harmful. Only disable it on a wiki if you can trust all your editors.
Usage Example
(:lua:) print('Hello from '.._VERSION..'!') bye = string.reverse('!eyB') print('Pi is: '..math.pi) print(bye) (:luaend:)
This will output the following:
Hello from Lua 5.2!
Pi is: 3.1415926535898
Bye!
See also
Contributors
Releases Notes
- 1.0.2, 2017-12-31 Updated for PHP 5.5 and PHP 7.2 compatibility (Said Achmiz)
- 1.0.1, 2014-02-07 Fix: Initialization script not working while in safe mode.
- 1.0.0, 2014-02-03 Initial release.
Comments
See discussion at LuaExecute-Talk
User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.