모듈:접이식 목록

설명문서 [편집] [역사] [새로고침]

틀:접이식 목록에서 사용하는 모듈입니다.

이 모듈 문서는 영어 위키백과en:Module:Collapsible list 문서 907814207판에서 분기하였습니다.
연습장이나 사용자 문서에서 틀의 사용이나 수정을 연습할 수 있습니다.
분류는 /설명문서에 넣어주세요.

-- {{접이식 목록}}을 위한 모듈입니다.

local ustring = mw.ustring

local p = {}

local function gettitlestyletracking( ts )
	if not ts then return '' end
	ts = ustring.gsub(ustring.lower(ts), '%s', '')
	local tsvals = mw.text.split(ts, ';')
	table.sort(tsvals)
	local skey = table.concat(tsvals,';')
	skey = ustring.gsub(skey, '^;', '')
	skey = mw.text.encode(mw.text.encode(skey),'%c%[%]=')
	if (ustring.match(';' .. ts, ';background:') or ustring.match(';' .. ts, ';background%-color:')) 
		and 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

local function getListItem( data )
    if not type( data ) == 'string' then
        return ''
    end
    return 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).
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
            ustring.match( v, '%S' ) then
                table.insert( nums, k )
        end
    end
    table.sort( nums )
    return nums
end

-- Formats a list of classes, styles or other attributes.
local function formatAttributes( attrType, ... )
    local attributes = { ... }
    local nums = getArgNums( attributes )
    local t = {}
    for i, num in ipairs( nums ) do
        table.insert( t, attributes[ num ] )
    end
    if #t == 0 then
        return '' -- Return the blank string so concatenation will work.
    end
    return ustring.format( ' %s="%s"', attrType, table.concat( t, ' ' ) )
end

local function buildList( args )
    -- Get the list items.
    local listItems = {}
    local argNums = getArgNums( args )
    for i, num in ipairs( argNums ) do
        table.insert( listItems, getListItem( args[ num ] ) )
    end
    if #listItems == 0 then
        return ''
    end
    listItems = table.concat( listItems )
    
    -- 속성(class), 모양(style)과 제목(title)을 얻어옴
    local div1class = formatAttributes( 'class', 'NavFrame', not args['펼치기'] and 'collapsed' )
    local div1style = formatAttributes(
        'style',
        args['테두리모양'],
        not ( args['테두리모양'] ) and 'border: none; padding: 0;'
    )
    local div2class = formatAttributes( 'class', 'NavHead' )
    local div2style = formatAttributes(
        'style',
        'font-size: 105%;',
        args['제목모양'],
        not ( args['제목모양'] ) and 'background: transparent; text-align: left;'
    )
    local title = args['제목'] or '목록'
    local ulclass = formatAttributes( 'class', 'NavContent', args['가로목록'] and 'hlist' )
    local ulstyle = formatAttributes( 
        'style',
        not args['머리글 기호'] and 'list-style: none none; margin-left: 0;',
        args['목록모양'],
        not ( args['목록모양'] ) and 'text-align: left;',
        'font-size: 105%; margin-top: 0; margin-bottom: 0; line-height: inherit;'
    )
    
    -- Build the list.
    return ustring.format( 
        '<div%s%s>\n<div%s%s>%s</div>\n<ul%s%s>%s</ul>\n</div>',
        div1class, div1style, div2class, div2style, title, ulclass, ulstyle, listItems
    )-- .. gettitlestyletracking(args['제목모양'])
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