Module:Compact list

local getArgs = require('Module:Arguments').getArgs
local clist = require('Module:Collapsible list').main
local hlist = require('Module:List').horizontal
local compressSparseArray = require('Module:TableTools').compressSparseArray
local p = {}

--[[
Combine named-and-numbered arguments into a pretty list.
"Named-and-numbered" means foo, foo0, foo_1, foo234: anything that matches foo_?%d+

Arguments:
  args[1] = name to search arguments
  rest of args = arguments to search
Returns:
  Pretty list, in order of argument number.
  "foo" comes first, then "foo0", "foo1", ... "fooN"
  The argument numbering does not have to be sequential
  
  If number of args that match <= args[_limit] (4 default),
     returns text list of the form "A, B, C and D"
  otherwise returns collapsible list ({{clist}})
--]]
function p._main(args)
	local pattern = "^"..args[1].."_?(%d+)$"  -- pattern to match
	local values = {}
	for k, v in pairs(args) do  --- loop through all arguments
		if k == args[1] then    --- if argument is just "foo", put it first
			values[1] = v
		else
			ord = tonumber(mw.ustring.match(k,pattern)) --- if "foo_?%d+", extract number
			if ord then
				values[ord+2] = v  --- put value into list at number+2 (to keep "foo" first, even for foo0)
			end
		end
	end
	values = compressSparseArray(values)  --- squeeze out gaps/nils in values, keep ordering
	local limit = tonumber(args._limit) or 4
	if #values == 0 then
		return ''
	end
	if #values == 1 then
		return values[1]
	end
	if #values > limit then
		return clist(values)   --- if longer than limit, call Module:Collapsible list
	end
	return hlist(values) --- otherwise create horizontal list
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
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.