Module:Collapsible list/sandbox: Difference between revisions
Appearance
Content deleted Content added
poke? Tag: Reverted |
gotta return that root Tag: Reverted |
||
Line 90: | Line 90: | ||
:done() |
:done() |
||
:wikitext(gettitlestyletracking(args.title_style or args.titlestyle)) |
:wikitext(gettitlestyletracking(args.title_style or args.titlestyle)) |
||
return tostring(root) |
|||
end |
end |
||
Revision as of 23:19, 28 December 2022
![]() | This is the module sandbox page for Module:Collapsible list (diff). |
![]() | This Lua module is used on 63,000+ 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. |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module uses TemplateStyles: |
This module implements {{collapsible list}}. Please see the template page for documentation.
local p = {}
local list_mod = require('Module:List')
local function gettitlestyletracking( ts )
if not ts then return '' end
ts = mw.ustring.gsub(mw.ustring.lower(ts), '%s', '')
local tsvals = mw.text.split(ts, ';')
table.sort(tsvals)
local skey = table.concat(tsvals,';')
skey = mw.ustring.gsub(skey, '^;', '')
skey = mw.text.encode(mw.text.encode(skey),'%c%[%]=')
if (mw.ustring.match(';' .. ts, ';background:') or mw.ustring.match(';' .. ts, ';background%-color:'))
and mw.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
-- Returns an array containing the keys of all positional arguments
-- that contain data (i.e. non-whitespace values).
local function getArgNums( args )
local nums = {}
for k, v in pairs( args ) do
if type( k ) == 'number' and
k >= 1 and
math.floor( k ) == k and
type( v ) == 'string' and
mw.ustring.match( v, '%S' ) then
table.insert( nums, k )
end
end
table.sort( nums )
return nums
end
local function buildList( args )
-- Get the list items.
local listItems = {}
local argNums = getArgNums( args )
for _, num in ipairs( argNums ) do
table.insert( listItems, not type( args[ num ] ) == 'string' and '' or args[ num ] )
end
if #listItems == 0 then
return ''
end
-- hack around mw-collapsible show/hide jumpiness by looking for text-alignment
-- by setting a margin if centered
local textAlignmentCentered = 'text%-align%s*:%s*center'
local centeredTitle = (args.title_style and args.title_style:lower():match(textAlignmentCentered)
or args.titlestyle and args.titlestyle:lower():match(textAlignmentCentered))
local centeredTitleSpacing = centeredTitle and 'margin: 0 4em'
-- pass default attributes to Module:List
listItems['class'] = 'mw-collapsible-content'
local list_style = ''
if args.list_style then list_style = list_style .. args.list_style end
if args.liststyle then list_style = list_style .. args.liststyle end
listItems['list_style'] = list_style
local list = 'unbulleted'
if args.hlist then
list = 'horizontal'
elseif args.bullets then
list = 'bulleted'
end
local root = mw.html.create()
root:tag('div')
:addClass('collapsible-list mw-collapsible')
:addClass(not args.expand and 'mw-collapsed' or '')
-- text align set to counteract the pull of .infobox-full-data
-- leaving it to be centered probably wouldn't hurt anyone though
-- so add a TODO for someone to look at/think about in the future
:cssText('text-align: left;')
:cssText(args.frame_style)
:cssText(args.framestyle)
:tag('div')
:addClass('collapsible-list-title')
:cssText('line-height: 1.6em; font-weight: bold;')
:cssText(args.title_style)
:cssText(args.titlestyle)
:tag('div')
:cssText(centeredTitleSpacing)
:wikitext(args.title or 'List')
:done()
:done()
:wikitext(list_mod[list](listItems))
:done()
:wikitext(gettitlestyletracking(args.title_style or args.titlestyle))
return tostring(root)
end
function p.main( frame )
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
local args = {}
for k, v in pairs( origArgs ) do
if type( k ) == 'number' or v ~= '' then
args[ k ] = v
end
end
return buildList( args )
end
return p