模組:Collapsible list:修訂間差異

出自眾神廳
跳至導覽 跳至搜尋
國家模板>Cwek
args.expand 为 true,会注入boolean的false,参数错误,简单修复
 
已匯入 1 筆修訂
 
(無差異)

2026年9月17日 (四) 09:22的最新修訂

可在模組:Collapsible list/doc建立此模組的說明文件

local p = {}
local yesno = require('Module:Yesno')

local function getListItem( data )
    if not type( data ) == 'string' then
        return ''
    end
    local emptyList = mw.html.create('li')
	    :cssText('line-height: inherit')
	    :cssText('margin: 0')
	    :wikitext(data)
    return tostring(emptyList)
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

-- TODO: use Module:List. Since the update for this comment is routine,
-- Modification: uses module:yesno and mw.html.create 
local function buildList( args )

    local argNums = getArgNums( args )
    if #argNums == 0 then
        return ''
    end
    
    -- ordered list option added
    local isOrdered = yesno(args.ordered, false)
    local isBullets = yesno(args.bullets, false)
    -- hack:collapsible button jumps with title
    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' or nil

    local hlist_templatestyles = ''
    if args.hlist then
        hlist_templatestyles = mw.getCurrentFrame():extensionTag{
            name = 'templatestyles',
            args = { src = 'Hlist/styles.css' }
        }
    end

	local collapsedClazz = ''
	if not args.expand then collapsedClazz='mw-collapsed' end
    -- collapsible frame
    local root = mw.html.create('div')
        :addClass('collapsible-list')
        :addClass('mw-collapsible')
        :addClass(collapsedClazz)
        :cssText('text-align: left;')
        :cssText(args.frame_style or args.framestyle)

    -- title
    local titleWrapper = root:tag('div')
        :css('line-height', '1.6em')
        :css('font-weight', 'bold')
        :cssText(args.title_style or args.titlestyle)

    titleWrapper:tag('div')
        :cssText(centeredTitleSpacing)
        :wikitext(args.title or '列表')

    -- list(ul / ol)
    local list = root:tag(isOrdered and 'ol' or 'ul')
        :addClass('mw-collapsible-content')
        :addClass(args.hlist and 'hlist')
        :css('margin-top', 0)
        :css('margin-bottom', 0)
        :css('line-height', 'inherit')
        :cssText(args.list_style or args.liststyle)

    if not isOrdered and not isBullets then
        list
            :css('list-style', 'none')
            :css('margin-left', 0)
    end

    -- list items
    for _, num in ipairs( argNums ) do
        list:tag('li')
            :css('line-height', 'inherit')
            :css('margin', 0)
            :wikitext(args[num])
    end

    return hlist_templatestyles .. 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