Questions answered by this recipe
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.
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.
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.
(: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
- 1.0.1, 2014-02-07 Fix: Initialization script not working while in safe mode.
- 1.0.0, 2014-02-03 Initial release.