Module:Sandbox/Hike395/Compact list
Appearance
From Wikipedia, the free encyclopedia
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
localgetArgs=require('Module:Arguments').getArgs localclist=require('Module:Collapsible list').main localcompressSparseArray=require('Module:TableTools').compressSparseArray localp={} --[[ 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}}) --]] functionp._main(args) localpattern="^"..args[1].."_?(%d+)$"-- pattern to match localvalues={} fork,vinpairs(args)do--- loop through all arguments ifk==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 ifordthen 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 locallimit=tonumber(args._limit)or4 if#values>limitthen returnclist(values)--- if longer than limit, call Module:Collapsible list end returnmw.text.listToText(values)--- otherwise just print out pretty text list end functionp.main(frame) localargs=getArgs(frame) returnp._main(args) end returnp