Module:Template test case: Difference between revisions

    (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(self:getName())
    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(self:getName())
    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, t in ipairs(templateOptions) do
    for i, options in ipairs(templateOptions) do
    table.insert(obj.templates, Template.new(invocationObj, t))
    table.insert(obj.templates, Template.new(invocationObj, options))
    end
    end


    Line 500: Line 514:
    end
    end


    function NowikiInvocation:getInvocation(template)
    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(template)
    function NowikiInvocation:getOutput(options)
    local invocation = self:getInvocation(template)
    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(template)
    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(template)
    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(template)
    function TableInvocation:getOutput(options)
    return mw.getCurrentFrame():expandTemplate{
    return mw.getCurrentFrame():expandTemplate{
    title = template,
    title = options.template,
    args = self.invokeArgs
    args = self.invokeArgs
    }
    }