Module:Template parameter value

Documentation for this module may be created at Module:Template parameter value/doc

Documentation for this module may be created at Module:Template parameter value/doc

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


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.