Module:Template parameter value explained

local p = local PrepareText = require("Module:Wikitext Parsing").PrepareText

local function getTitle(title) local success, titleObj = pcall(mw.title.new, title) if success then return titleObj else return nil endend

--string.gmatch will check the largest block it can without re-scanning whats inside, but we need whats insidelocal function matchAllTemplates(str) local matches = for template in string.gmatch(str, "") do table.insert(matches, template) local innerContent = string.sub(template, 3, -3) for _,subtemplate in next,matchAllTemplates(innerContent) do table.insert(matches, subtemplate) end end return matchesend

--Forked version of getParameters from with extra features removedlocal function escapeString(str) return string.gsub(str, '[%^%$%(%)%.%[%]%*%+%-%?%%]', '%%%0')endlocal function getParameters(template) local parameters, parameterOrder =, local params = string.match(template, '}') if params then local count = 0 -- Temporarily replace pipes in subtemplates and wikilinks to avoid chaos for subtemplate in string.gmatch(params, '') do params = string.gsub(params, escapeString(subtemplate), string.gsub(subtemplate, ".",)) end for wikilink in string.gmatch(params, '%[%b[]%]') do params = string.gsub(params, escapeString(wikilink), string.gsub(wikilink, ".",)) end for parameter in mw.text.gsplit(params, '|') do local parts = mw.text.split(parameter, '=') local key = mw.text.trim(parts[1]) local value if #parts

1 then value = key count = count + 1 key = tostring(count) else value = mw.text.trim(table.concat(parts, '=', 2)) end value = string.gsub(string.gsub(value, '@@:@@', '|'), '@@_@@', '=') key = string.gsub(string.gsub(key, '@@:@@', '|'), '@@_@@', '=') table.insert(parameterOrder, key) parameters[key] = value end end return parameters, parameterOrderend

-- Returns a table containing parameters and a table with the order in which each of their values were found.-- Since this considers all subtemplates, a single parameter is expected to have multiple values.-- E.g. -- Would return local function getAllParameters(template, ignore_blank, only_subtemplates) local parameterTree = setmetatable local params, paramOrder = getParameters(template) for _,key in ipairs(paramOrder) do local value = params[key] if not ignore_blank or value ~= "" then if not only_subtemplates then table.insert(parameterTree[key], value) --Insert the initial value into the tree end for subtemplate in string.gmatch(value, "") do --And now check for subvalues local subparams = getAllParameters(subtemplate, ignore_blank) for subkey,subset in next,subparams do for _,subvalue in ipairs(subset) do table.insert(parameterTree[subkey], subvalue) --And add any we find to our tree end end end end end return parameterTreeend

--Module entry point. Returns a success boolean and either the target template or why it failedfunction p.getTemplate(page, templates, options) if not templates then --Required parameters return false, "Missing required parameter 'templates'" end options = options or local template_index = tonumber(options.template_index) or 1 local treat_as_regex = options.treat_as_regex or false if type(templates)

"string" then -- TODO: Find a good way to allow specifying multiple templates via template invocation -- (Modules can just provide a table so no concerns there) -- Comma splitting is a bad idea (lots of templates have a comma in their name) templates = end local title = getTitle(page) if title

nil then return false, "Requested title doesn't exist" end local content = PrepareText(title:getContent or "") local foundTemplates = 0 for _,template in next,matchAllTemplates(content) do for _,wantedTemplate in pairs(templates) do if not treat_as_regex then wantedTemplate = escapeString(wantedTemplate) end local firstLetter = string.sub(wantedTemplate, 1, 1) local firstUpper, firstLower = firstLetter:upper, firstLetter:lower if firstUpper ~= firstLower then wantedTemplate = "[" .. firstUpper .. firstLower .. "]" .. string.sub(wantedTemplate, 2) end if string.match(template, "^