„Modul:LuaWiki“ – Versionsunterschied
Erscheinungsbild
[ungesichtete Version] | [gesichtete Version] |
Inhalt gelöscht Inhalt hinzugefügt
update |
update |
||
Zeile 1: | Zeile 1: | ||
--[=[ 2013-05- |
--[=[ 2013-05-07 |
||
LuaWiki - Support Lua programming in Wiki environment |
LuaWiki - Support Lua programming in Wiki environment |
||
* .getArg() |
* .getArg() |
||
Zeile 14: | Zeile 14: | ||
local wikiVariables |
local wikiVariables |
||
local LuaWiki = {} |
local LuaWiki = {} |
||
LuaWiki.error = function ( about ) |
|||
-- Enclose errorMsg with <span> |
|||
-- Precondition: |
|||
-- about -- string |
|||
local r = about |
|||
if type( about ) == "string" then |
|||
if #about == 0 then |
|||
r = "Error in Lua" |
|||
end |
|||
else |
|||
r = tostring( about ) |
|||
end |
|||
return "<span class='error'>" .. error( r, 3 ) .. "</span>" |
|||
end -- LuaWiki.error() |
|||
Zeile 54: | Zeile 71: | ||
if g[ 1 ] == seek then |
if g[ 1 ] == seek then |
||
r = g[ 2 ] |
r = g[ 2 ] |
||
break; |
|||
end |
end |
||
end |
end |
||
Zeile 139: | Zeile 156: | ||
-- Uses: |
-- Uses: |
||
-- mw.getCurrentFrame() |
-- mw.getCurrentFrame() |
||
-- error() |
|||
local r = { s, "1" } |
local r = { s, "1" } |
||
local frame = mw.getCurrentFrame() |
local frame = mw.getCurrentFrame() |
||
Zeile 148: | Zeile 166: | ||
end |
end |
||
else |
else |
||
r = " |
r = error( "No transclude page '" .. s .. "'" ) |
||
end |
end |
||
return r |
return r |
Aktuelle Version vom 8. Mai 2013, 21:25 Uhr
Vorlagen- programmierung |
Diskussionen | Lua | Unterseiten | ||||||
Modul | Deutsch | English
|
Esperanto | Dolnoserbski | Hornjoserbsce | Modul: | WP:Lua |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
--[=[ 2013-05-07
LuaWiki - Support Lua programming in Wiki environment
* .getArg()
* .initVariables()
* .getVariable()
* .isExisting()
* .setFrame()
* .transclude()
]=]
-- Module globals
local wikiVariables
local LuaWiki = {}
LuaWiki.error = function ( about )
-- Enclose errorMsg with <span>
-- Precondition:
-- about -- string
local r = about
if type( about ) == "string" then
if #about == 0 then
r = "Error in Lua"
end
else
r = tostring( about )
end
return "<span class='error'>" .. error( r, 3 ) .. "</span>"
end -- LuaWiki.error()
LuaWiki.getArg = function ( arg, assign )
-- Retrieve template argument
-- Precondition:
-- arg -- string or number; argument identifier
-- assign -- any, optional; default value
-- Uses:
-- mw.getCurrentFrame()
local r = mw.getCurrentFrame().args[ arg ]
if type( r ) ~= "string" then
if type( assign ) == nil then
r = "{{{<" .. arg .. ">}}}"
else
r = assign
end
end
return r
end -- LuaWiki.getArg()
LuaWiki.getVariable = function ( seek, numeric )
-- Retrieve item from wikiVariables; populate if not yet present
-- Precondition:
-- seek -- string; name of variable
-- numeric -- true: seek is numeric (else string)
-- Uses:
-- >< wikiVariables
-- mw.getCurrentFrame()
local g, i, n
local r = false
if type( wikiVariables ) == "table" then
n = #wikiVariables
for i = 1, n do
g = wikiVariables[ i ]
if g then
if g[ 1 ] == seek then
r = g[ 2 ]
break;
end
end
end -- for i
else
wikiVariables = { }
n = 0
end
if not r then
g = mw.getCurrentFrame():preprocess( "{{" .. seek .. "}}" )
r = mw.ustring.match( g, "^(.*)$" )
if numeric then
r = tonumber( g )
end
table.insert( wikiVariables, n + 1, { seek, r } )
end
return r
end -- LuaWiki.getVariable()
LuaWiki.initVariables = function ( request )
-- Initialize wikiVariables
-- Precondition:
-- request -- table; every element either
-- * string; name of variable
-- * table; name string and true, if numeric
-- Uses:
-- < wikiVariables
-- mw.getCurrentFrame()
local g, i, n, s
local src = "|"
wikiVariables = { }
for i = 1, #request do
s = request[ i ]
if type( s ) == "table" then
s = s[ 1 ]
end
src = src .. s .. "={{" .. s .. "}}|"
end -- for i
src = mw.getCurrentFrame():preprocess( src )
for i = 1, #request do
s = request[ i ]
n = ( type( s ) == "table" )
if n then
n = s[ 2 ]
end
if n then
g = "-?%d+"
s = s[ 1 ]
else
g = "[^|]*"
end
g = mw.ustring.match( src, "|" .. s .. "=(" .. g .. ")|" )
if n then
g = tonumber( g )
end
table.insert( wikiVariables, i, { s, g } )
end -- for i
end -- LuaWiki.initVariables()
LuaWiki.isExisting = function ( seek )
-- Return true if page exists, else false
-- Precondition:
-- seek -- string; full page name
-- Uses:
-- mw.getCurrentFrame()
local g = mw.getCurrentFrame():callParserFunction( "#ifexist",
{ seek,
"1",
"0" } )
return ( g == "1" )
end -- LuaWiki.isExisting()
LuaWiki.transclude = function ( s, args )
-- Save transclusion of a page, or error message
-- Precondition:
-- s -- string; page name
-- args -- table or nil; arguments
-- Uses:
-- mw.getCurrentFrame()
-- error()
local r = { s, "1" }
local frame = mw.getCurrentFrame()
if frame:callParserFunction( "#ifexist", r ) == "1" then
if args then
r = frame:expandTemplate{ title = s, args = args }
else
r = frame:expandTemplate{ title = s }
end
else
r = error( "No transclude page '" .. s .. "'" )
end
return r
end -- LuaWiki.transclude()
return LuaWiki