मॉड्यूल:Template parameter value
| This Lua module is used on many pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
मॉड्यूल:Lua_banner में पंक्ति 113 पर लुआ त्रुटि: attempt to index field 'edit' (a nil value)। Implements {{Template parameter value}} and {{HasTemplate}}, and can be used from other modules.
Module functions
[संपादित करें]getParameter
[संपादित करें]getParameter takes 4 arguments: The page name (string), the template/s (string or table of strings), the parameter (string), and an optional options table. It will return either true and the contents of the requested parameter or false and a reason for failure.
The following options are available:
- template_index: Which occurrence of the template to look for the parameter in. Set to -1 for last occurrence. (default: 1)
- parameter_index: Which occurrence of the parameter to look for (default: 1; only applies when
ignore_subtemplatesis false) - ignore_subtemplates: If parameters should only be searched for in the top-level template, ignoring the parameters in subtemplates (default: false)
- only_subtemplates: If parameters should only be searched for in subtemplates of the top-level template (default: false)
- ignore_blank: Whether or not blank values should count towards
parameter_index(default: false) - treat_as_regex: Whether or not the template string(s) should be treated as a lua regex (default: false)
getTemplate
[संपादित करें]getTemplate takes 3 arguments: The page name (string), the template/s (string or table of strings), and an optional options table. It will return either true and the text of the requested template or false and a reason for failure.
getTemplate supports the options template_index and treat_as_regex from getParameter.
Helper functions
[संपादित करें]The module exposes some of the helper functions used (matchAllTemplates, getParameters, and getAllParameters) for convenience. Each function has some comments above it in the code explaining its rough purpose.
Template functions
[संपादित करें]main implements {{Template parameter value}} and acts as a template wrapper for getParameter.
hasTemplate implements {{HasTemplate}} and somewhat acts as a wrapper for getTemplate (it only provides if the template was found, not the template text itself).
Testcases
[संपादित करें]Testcases are available at Module talk:Template parameter value/testcases
local p = {}
local escape = require("Module:String")._escapePattern
function trimspaces(s)
return string.gsub(s, "^%s*(.-)%s*$", "%1")
end
local function getTitle(title)
local success, titleObj = pcall(mw.title.new, title)
if success then return titleObj
else return nil end
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Template parameter value'
})
local template = escape(args[2])
local parameter = escape(args[4])
local numberedParameter = (tonumber(parameter) ~= nil)
local templateCount = 0
local parameterCount = 0
local pipemarker = '~_~TPVPIPEMARKER~_~'
local templateMatch = tonumber(args[3] or 1)
local parameterMatch = tonumber(args[5] or 1)*(numberedParameter and parameter or 1)
local targettitle = getTitle(args[1])
if targettitle == nil then return "" end
local content = string.gsub(targettitle:getContent() or "", "[\r\n]", "")
-- Escape some pipes
content = string.gsub(content, '(%[%[[^%[{}]*)|([^%[%]{}]*%]%])', '%1' .. pipemarker ..'%2')
while templateCount ~= templateMatch do
if content == nil then return "" end
content = string.match(content, '{{' .. template .. "(.+)")
templateCount = templateCount + 1
end
while parameterCount ~= parameterMatch do
if content == nil then return "" end
content = string.match(content, '|%s*' .. (numberedParameter and "" or parameter .. '%s*=%s*') .. '([^|].*)')
parameterCount = parameterCount + 1
end
if content == nil then return "" end
content = string.gsub(content, "</?%a*include%a*>", "")
content = string.match(content, '^([^|}]*{{[^}]+}}[^|}]*)|') or string.match(content, '([^|}]+)')
content = string.gsub(content, pipemarker, '|')
content = frame:preprocess{text = content}
content = trimspaces(content)
return content
end
return p