Module:Xpsoc: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius get an arrow facing forwards for whatever content language we are using |
meta>Mr. Stradivarius use frame:preprocess so that this works with things like parser functions too |
||
Line 3: | Line 3: | ||
local p = {} | local p = {} | ||
local function makeInvocation(name, args) | local function makeInvocation(name, args, useNoWiki) | ||
-- This function makes a template invocation from the name and the arguments | -- This function makes a template invocation from the name and the arguments | ||
-- given. | -- given. | ||
Line 13: | Line 13: | ||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
invArgs[k] = v | invArgs[k] = v | ||
end | |||
-- Get the separators to use. | |||
local seps = { | |||
openb = '{{', | |||
closeb = '}}', | |||
pipe = '|' | |||
} | |||
if useNoWiki then | |||
for k, v in pairs(seps) do | |||
seps[k] = mw.text.nowiki(v) | |||
end | |||
end | end | ||
-- Build the invocation body with numbered args first, then named. | -- Build the invocation body with numbered args first, then named. | ||
local ret = {} | local ret = {} | ||
ret[#ret + 1] = | ret[#ret + 1] = seps.openb | ||
ret[#ret + 1] = name | ret[#ret + 1] = name | ||
for k, v in ipairs(invArgs) do | for k, v in ipairs(invArgs) do | ||
Line 25: | Line 36: | ||
break | break | ||
end | end | ||
ret[#ret + 1] = pipe | ret[#ret + 1] = seps.pipe | ||
ret[#ret + 1] = v | ret[#ret + 1] = v | ||
invArgs[k] = nil -- Erase the key so that we don't add the value twice | invArgs[k] = nil -- Erase the key so that we don't add the value twice | ||
end | end | ||
for k, v in pairs(invArgs) do | for k, v in pairs(invArgs) do | ||
ret[#ret + 1] = pipe | ret[#ret + 1] = seps.pipe | ||
ret[#ret + 1] = k | ret[#ret + 1] = k | ||
ret[#ret + 1] = '=' | ret[#ret + 1] = '=' | ||
ret[#ret + 1] = v | ret[#ret + 1] = v | ||
end | end | ||
ret[#ret + 1] = | ret[#ret + 1] = seps.closeb | ||
return table.concat(ret) | return table.concat(ret) | ||
Line 62: | Line 73: | ||
end | end | ||
local invocation = makeInvocation(name, invArgs) | local invocation = makeInvocation(name, invArgs, true) | ||
local gives = args.gives or mw.language.getContentLanguage():getArrow('forwards') | local gives = args.gives or mw.language.getContentLanguage():getArrow('forwards') | ||
local result = frame: | local result = frame:preprocess(makeInvocation(name, invArgs)) | ||
return string.format('<code>%s</code> %s %s', invocation, gives, result) | return string.format('<code>%s</code> %s %s', invocation, gives, result) |
Revision as of 05:34, 29 October 2014
Documentation for this module may be created at Module:Xpsoc/doc
-- This module implements {{xpsoc}}.
local p = {}
local function makeInvocation(name, args, useNoWiki)
-- This function makes a template invocation from the name and the arguments
-- given.
-- Copy the invocation args and convert magic words.
-- We need to make a copy of the table rather than just using the original,
-- as some of the values may be erased when building the invocation.
local invArgs = {}
for k, v in pairs(args) do
invArgs[k] = v
end
-- Get the separators to use.
local seps = {
openb = '{{',
closeb = '}}',
pipe = '|'
}
if useNoWiki then
for k, v in pairs(seps) do
seps[k] = mw.text.nowiki(v)
end
end
-- Build the invocation body with numbered args first, then named.
local ret = {}
ret[#ret + 1] = seps.openb
ret[#ret + 1] = name
for k, v in ipairs(invArgs) do
if v:find('=', 1, true) then
-- Likely something like 1=foo=bar, we need to do it as a named arg
break
end
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = v
invArgs[k] = nil -- Erase the key so that we don't add the value twice
end
for k, v in pairs(invArgs) do
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = k
ret[#ret + 1] = '='
ret[#ret + 1] = v
end
ret[#ret + 1] = seps.closeb
return table.concat(ret)
end
function p._main(args, frame)
frame = frame or mw.getCurrentFrame()
-- Get the invocation arguments.
local name = args[1]
if not name then
error('no template name passed to xpsoc', 2)
end
local invArgs = {}
for k, v in pairs(args) do
if k ~= 1 then
if type(k) == 'number' then
invArgs[k - 1] = v
else
local num = k:match('^n([1-9][0-9]*)$')
if num then
invArgs[args[k]] = args['v' .. num]
end
end
end
end
local invocation = makeInvocation(name, invArgs, true)
local gives = args.gives or mw.language.getContentLanguage():getArrow('forwards')
local result = frame:preprocess(makeInvocation(name, invArgs))
return string.format('<code>%s</code> %s %s', invocation, gives, result)
end
function p.main(frame)
return p._main(frame:getParent().args, frame)
end
return p