„Modul:LuaWiki“ – Versionsunterschied
Erscheinungsbild
[ungesichtete Version] | [ungesichtete Version] |
Inhalt gelöscht Inhalt hinzugefügt
update |
update |
||
Zeile 1: | Zeile 1: | ||
--[=[ 2013- |
--[=[ 2013-05-02 |
||
LuaWiki - Support Lua programming in Wiki environment |
LuaWiki - Support Lua programming in Wiki environment |
||
* .getArg() |
* .getArg() |
||
Zeile 13: | Zeile 13: | ||
-- Module globals |
-- Module globals |
||
local wikiVariables |
local wikiVariables |
||
local templateFrame |
|||
local LuaWiki = {} |
local LuaWiki = {} |
||
Zeile 24: | Zeile 23: | ||
-- assign -- any, optional; default value |
-- assign -- any, optional; default value |
||
-- Uses: |
-- Uses: |
||
-- |
-- mw.getCurrentFrame() |
||
local r = |
local r = mw.getCurrentFrame().args[ arg ] |
||
if type( r ) ~= "string" then |
if type( r ) ~= "string" then |
||
if type( assign ) == nil then |
if type( assign ) == nil then |
||
Zeile 44: | Zeile 43: | ||
-- numeric -- true: seek is numeric (else string) |
-- numeric -- true: seek is numeric (else string) |
||
-- Uses: |
-- Uses: |
||
-- > templateFrame |
|||
-- >< wikiVariables |
-- >< wikiVariables |
||
-- mw.getCurrentFrame() |
|||
local g, i, n |
local g, i, n |
||
local r = false |
local r = false |
||
Zeile 64: | Zeile 63: | ||
end |
end |
||
if not r then |
if not r then |
||
g = |
g = mw.getCurrentFrame():preprocess( "{{" .. seek .. "}}" ) |
||
r = mw.ustring.match( g, "^(.*)$" ) |
r = mw.ustring.match( g, "^(.*)$" ) |
||
if numeric then |
if numeric then |
||
Zeile 83: | Zeile 82: | ||
-- * table; name string and true, if numeric |
-- * table; name string and true, if numeric |
||
-- Uses: |
-- Uses: |
||
-- > templateFrame |
|||
-- < wikiVariables |
-- < wikiVariables |
||
-- mw.getCurrentFrame() |
|||
local g, i, n, s |
local g, i, n, s |
||
local src = "|" |
local src = "|" |
||
Zeile 95: | Zeile 94: | ||
src = src .. s .. "={{" .. s .. "}}|" |
src = src .. s .. "={{" .. s .. "}}|" |
||
end -- for i |
end -- for i |
||
src = |
src = mw.getCurrentFrame():preprocess( src ) |
||
for i = 1, #request do |
for i = 1, #request do |
||
s = request[ i ] |
s = request[ i ] |
||
Zeile 123: | Zeile 122: | ||
-- seek -- string; full page name |
-- seek -- string; full page name |
||
-- Uses: |
-- Uses: |
||
-- |
-- mw.getCurrentFrame() |
||
local g = |
local g = mw.getCurrentFrame():callParserFunction( "#ifexist", |
||
{ seek, |
|||
"1", |
|||
"0" } ) |
|||
return ( g == "1" ) |
return ( g == "1" ) |
||
end -- isExisting() |
end -- isExisting() |
||
⚫ | |||
-- Initialize frame for this utility |
|||
-- Precondition: |
|||
-- frame -- templateFrame |
|||
⚫ | |||
-- < templateFrame |
|||
templateFrame = frame |
|||
⚫ | |||
Zeile 148: | Zeile 138: | ||
-- args -- table or nil; arguments |
-- args -- table or nil; arguments |
||
-- Uses: |
-- Uses: |
||
-- |
-- mw.getCurrentFrame() |
||
local r = { s, "1" } |
local r = { s, "1" } |
||
local frame = mw.getCurrentFrame() |
|||
if |
if frame:callParserFunction( "#ifexist", r ) == "1" then |
||
if args then |
if args then |
||
r = |
r = frame:expandTemplate{ title = s, args = args } |
||
else |
else |
||
r = |
r = frame:expandTemplate{ title = s } |
||
end |
end |
||
else |
else |
||
Zeile 161: | Zeile 152: | ||
return r |
return r |
||
end -- transclude() |
end -- transclude() |
||
⚫ | |||
⚫ | |||
⚫ | |||
Version vom 4. Mai 2013, 10:57 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-02
LuaWiki - Support Lua programming in Wiki environment
* .getArg()
* .initVariables()
* .getVariable()
* .isExisting()
* .setFrame()
* .transclude()
]=]
-- Module globals
local wikiVariables
local LuaWiki = {}
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 -- 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 ]
i = n
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 -- 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 -- 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 -- 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()
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 = "<span class='error'>No transclude page '" .. s .. "'</span>"
end
return r
end -- transclude()
LuaWiki.setFrame = function ()
-- OBSOLETED
end -- setFrame()
return LuaWiki