Jump to content

Module:Collapsible list

From Simple English Wikipedia, the free encyclopedia

Documentation for this module may be created atModule:Collapsible list/doc

-- This module implements {{collapsible list}}.

localp={}

localfunctiongettitlestyletracking(ts)
ifnottsthenreturn''end
ts=mw.ustring.gsub(mw.ustring.lower(ts),'%s','')
localtsvals=mw.text.split(ts,';')
table.sort(tsvals)
localskey=table.concat(tsvals,';')
skey=mw.ustring.gsub(skey,'^;','')
skey=mw.text.encode(mw.text.encode(skey),'%c%[%]=')
if(mw.ustring.match(';'..ts,';background:')ormw.ustring.match(';'..ts,';background%-color:'))
andmw.ustring.match(';'..ts,';text%-align:')then
return'[[Category:Pages using collapsible list with both background and text-align in titlestyle|'..skey..' ]]'
end
return'[[Category:Pages using collapsible list without both background and text-align in titlestyle|'..skey..' ]]'
end

localfunctiongetListItem(data)
ifnottype(data)=='string'then
return''
end
returnmw.ustring.format('<li style= "line-height: inherit; margin: 0" >%s</li>',data)
end

-- Returns an array containing the keys of all positional arguments
-- that contain data (i.e. non-whitespace values).
localfunctiongetArgNums(args)
localnums={}
fork,vinpairs(args)do
iftype(k)=='number'and
k>=1and
math.floor(k)==kand
type(v)=='string'and
mw.ustring.match(v,'%S')then
table.insert(nums,k)
end
end
table.sort(nums)
returnnums
end

-- Formats a list of classes, styles or other attributes.
localfunctionformatAttributes(attrType,...)
localattributes={...}
localnums=getArgNums(attributes)
localt={}
fori,numinipairs(nums)do
table.insert(t,attributes[num])
end
if#t==0then
return''-- Return the blank string so concatenation will work.
end
returnmw.ustring.format(' %s= "%s" ',attrType,table.concat(t,' '))
end

localfunctionbuildList(args)
-- Get the list items.
locallistItems={}
localargNums=getArgNums(args)
fori,numinipairs(argNums)do
table.insert(listItems,getListItem(args[num]))
end
if#listItems==0then
return''
end
listItems=table.concat(listItems)

-- hack around mw-collapsible show/hide jumpiness by looking for text-alignment
-- by setting a margin if centered
localtextAlignmentCentered='text%-align%s*:%s*center'
localcenteredTitle=(args.title_styleandargs.title_style:lower():match(textAlignmentCentered)
orargs.titlestyleandargs.titlestyle:lower():match(textAlignmentCentered))
localcenteredTitleSpacing
ifcenteredTitlethen
centeredTitleSpacing='margin: 0 4em'
else
centeredTitleSpacing=''
end

-- Get class, style and title data.
localcollapsibleContainerClass=formatAttributes(
'class',
'mw-collapsible',notargs.expandand'mw-collapsed'
)
localcollapsibleContainerStyle=formatAttributes(
'style',
'text-align: center; font-size: 95%;',
args.frame_style,
args.framestyle
)
localcollapsibleTitleStyle=formatAttributes(
'style',
'line-height: 1.6em; font-weight: bold; font-size: 100%; text-align: left;',
args.title_style,
args.titlestyle
)
localjumpyTitleStyle=formatAttributes(
'style',
centeredTitleSpacing
)
localtitle=args.titleor'List'
localulclass=formatAttributes('class','mw-collapsible-content',args.hlistand'hlist')
localulstyle=formatAttributes(
'style',
'font-size: 105%; margin-top: 0; margin-bottom: 0; line-height: inherit; text-align: left;',
notargs.bulletsand'list-style: none none; margin-left: 0;',
args.list_style,
args.liststyle
)

-- Build the list.
returnmw.ustring.format(
'<div%s%s>\n<div%s><div%s>%s</div></div>\n<ul%s%s>%s</ul>\n</div>',
collapsibleContainerClass,collapsibleContainerStyle,
collapsibleTitleStyle,jumpyTitleStyle,title,ulclass,ulstyle,listItems
)..gettitlestyletracking(args.title_styleorargs.titlestyle)
end

functionp.main(frame)
localorigArgs
ifframe==mw.getCurrentFrame()then
origArgs=frame:getParent().args
fork,vinpairs(frame.args)do
origArgs=frame.args
break
end
else
origArgs=frame
end

localargs={}
fork,vinpairs(origArgs)do
iftype(k)=='number'orv~=''then
args[k]=v
end
end
returnbuildList(args)
end

returnp