Jump to content

Module:Collapsible list/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
''
Tag: Reverted
poke
Tag: Reverted
Line 87: Line 87:
:done()
:done()
:done()
:done()
:wikitext(list_mod.makeList(list, list_args))
:wikitext(list_mod.makeList(list, listItems))
:done()
:done()
:wikitext(gettitlestyletracking(args.title_style or args.titlestyle))
:wikitext(gettitlestyletracking(args.title_style or args.titlestyle))

Revision as of 23:17, 28 December 2022

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.makeList(list, listItems))
    	:done()
    :wikitext(gettitlestyletracking(args.title_style or args.titlestyle))
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