Module:Template test case: Difference between revisions
don't require __TEMPLATENAME__ magic word for nowiki invocations if there is only one template being displayed
(fix bad variable name) |
(don't require __TEMPLATENAME__ magic word for nowiki invocations if there is only one template being displayed) |
||
Line 138: | Line 138: | ||
function Template:getInvocation(format) | function Template:getInvocation(format) | ||
local invocation = self._invocation:getInvocation | local invocation = self._invocation:getInvocation{ | ||
template = self:getName(), | |||
requireMagicWord = self.requireMagicWord, | |||
} | |||
if format == 'code' then | if format == 'code' then | ||
invocation = '<code>' .. mw.text.nowiki(invocation) .. '</code>' | invocation = '<code>' .. mw.text.nowiki(invocation) .. '</code>' | ||
Line 153: | Line 156: | ||
function Template:getOutput() | function Template:getOutput() | ||
return self._invocation:getOutput | return self._invocation:getOutput{ | ||
template = self:getName(), | |||
requireMagicWord = self.requireMagicWord, | |||
} | |||
end | end | ||
Line 264: | Line 270: | ||
return ret | return ret | ||
end)(templateOptions) | end)(templateOptions) | ||
-- Don't require the __TEMPLATENAME__ magic word for nowiki invocations if | |||
-- there is only one template being output. | |||
if #templateOptions <= 1 then | |||
templateOptions[1].requireMagicWord = false | |||
end | |||
mw.logObject(templateOptions) | |||
-- Make the template objects | -- Make the template objects | ||
obj.templates = {} | obj.templates = {} | ||
for i, | for i, options in ipairs(templateOptions) do | ||
table.insert(obj.templates, Template.new(invocationObj, | table.insert(obj.templates, Template.new(invocationObj, options)) | ||
end | end | ||
Line 500: | Line 514: | ||
end | end | ||
function NowikiInvocation:getInvocation( | function NowikiInvocation:getInvocation(options) | ||
template = template:gsub('%%', '%%%%') -- Escape "%" with "%%" | local template = options.template:gsub('%%', '%%%%') -- Escape "%" with "%%" | ||
local invocation, count = self.invocation:gsub( | local invocation, count = self.invocation:gsub( | ||
self.cfg.templateNameMagicWordPattern, | self.cfg.templateNameMagicWordPattern, | ||
template | template | ||
) | ) | ||
if count < 1 then | if options.requireMagicWord ~= false and count < 1 then | ||
error(self:message( | error(self:message( | ||
'nowiki-magic-word-error', | 'nowiki-magic-word-error', | ||
Line 515: | Line 529: | ||
end | end | ||
function NowikiInvocation:getOutput( | function NowikiInvocation:getOutput(options) | ||
local invocation = self:getInvocation( | local invocation = self:getInvocation(options) | ||
return mw.getCurrentFrame():preprocess(invocation) | return mw.getCurrentFrame():preprocess(invocation) | ||
end | end | ||
Line 536: | Line 550: | ||
end | end | ||
function TableInvocation:getInvocation( | function TableInvocation:getInvocation(options) | ||
if self.code then | if self.code then | ||
local nowikiObj = NowikiInvocation.new(self.code, self.cfg) | local nowikiObj = NowikiInvocation.new(self.code, self.cfg) | ||
return nowikiObj:getInvocation( | return nowikiObj:getInvocation(options) | ||
else | else | ||
return require('Module:Template invocation').invocation( | return require('Module:Template invocation').invocation( | ||
template, | options.template, | ||
self.invokeArgs | self.invokeArgs | ||
) | ) | ||
Line 548: | Line 562: | ||
end | end | ||
function TableInvocation:getOutput( | function TableInvocation:getOutput(options) | ||
return mw.getCurrentFrame():expandTemplate{ | return mw.getCurrentFrame():expandTemplate{ | ||
title = template, | title = options.template, | ||
args = self.invokeArgs | args = self.invokeArgs | ||
} | } |