모듈:대문 슬라이더

이 모듈에 대한 설명문서는 모듈:대문 슬라이더/설명문서에서 만들 수 있습니다

--[==[ 대문 슬라이더들을 목록 문서에서 읽어 그린다.

목록 문서 꼴은 [[MediaWiki:Liberty-Navbar]]와 같은 결이다.

	* 칩 이름
	** 제목 | 문서 | 사진 | 부제

	* 칩은 몇 개든 늘릴 수 있다(열두 개까지).
	* 문서를 비우면 제목과 같은 이름의 문서로 간다.
	* 사진을 비우면 그 문서에 쓰인 첫 그림을 찾아 쓴다. 가운데를 건너뛸 때는 작대기만 남긴다 — ** 모로니아 || 그림.jpg
	* 부제는 제목 아래 작은 회색 줄.

부르는 법
	{{#invoke:대문 슬라이더|겹침|목록=위키스:대문 슬라이더/프로젝트|간격=7000}}
	{{#invoke:대문 슬라이더|격자|목록=위키스:대문 슬라이더/추천 문서|칸수=5}}
	{{#invoke:대문 슬라이더|격자|목록=위키스:대문 슬라이더/책과 논문|칸수=6|꼴=세로}}
]==]

local p = {}

local IMG_EXT = { png = true, jpg = true, jpeg = true, gif = true, svg = true, webp = true }
local FILE_NS = { ['파일'] = true, ['그림'] = true, ['file'] = true, ['image'] = true }
local MAX_CHIP = 12

local function trim( s )
	return ( tostring( s or '' ):gsub( '^%s+', '' ):gsub( '%s+$', '' ) )
end

local function content( title )
	if title == '' then
		return nil
	end
	local t = mw.title.new( title )
	if not t then
		return nil
	end
	local ok, text = pcall( function () return t:getContent() end )
	if ok then
		return text
	end
	return nil
end

-- 문서 본문에서 첫 그림 이름을 찾는다
local function firstImage( title )
	local text = content( title )
	if not text then
		return nil
	end
	for link in text:gmatch( '%[%[([^%[%]|]+)' ) do
		local ns, name = link:match( '^%s*([^:]+)%s*:%s*(.+)$' )
		if ns and FILE_NS[ mw.ustring.lower( trim( ns ) ) ] then
			return trim( name )
		end
	end
	for name, ext in text:gmatch( '[=|]%s*([^|=%[%]{}<>\n]-)%.(%a+)%s*[|}\n]' ) do
		name = trim( name )
		if IMG_EXT[ mw.ustring.lower( ext ) ] and name ~= '' and not name:find( '/', 1, true ) then
			return name .. '.' .. ext
		end
	end
	return nil
end

-- 목록 문서를 읽어 { 칩 = {이름...}, 줄 = { {제목, 문서, 사진, 부제}... } } 로 돌려준다
local function parse( src )
	local text = content( src )
	if not text then
		return nil
	end
	text = text:gsub( '<!%-%-.-%-%->', '' )  -- 숨긴 글(주석)은 읽지 않는다

	local chips, sets, dropped = {}, {}, 0
	for line in text:gmatch( '[^\r\n]+' ) do
		local l = trim( line )
		if l:sub( 1, 2 ) == '**' then
			local f = {}
			for part in ( trim( l:sub( 3 ) ) .. '|' ):gmatch( '([^|]*)|' ) do
				f[ #f + 1 ] = trim( part )
			end
			local name = f[1] or ''
			if name ~= '' and #sets > 0 then
				local link = ( f[2] and f[2] ~= '' ) and f[2] or name
				local img = f[3] or ''
				if img == '' then
					img = firstImage( link ) or ''
				end
				table.insert( sets[ #sets ], { name = name, link = link, img = img, sub = f[4] or '' } )
			end
		elseif l:sub( 1, 1 ) == '*' then
			local nm = trim( l:sub( 2 ) )
			if nm ~= '' then
				if #chips < MAX_CHIP then
					chips[ #chips + 1 ] = nm
					sets[ #sets + 1 ] = {}
				else
					dropped = dropped + 1
				end
			end
		end
	end
	return chips, sets, dropped
end

local function build( frame, src, outer, inner, extra, mkargs )
	local chips, sets, dropped = parse( src )
	if not chips then
		return '<strong class="error">[[' .. src .. ']] 문서를 읽을 수 없습니다.</strong>'
	end
	if #chips == 0 then
		return '<strong class="error">[[' .. src .. ']]에 칩이 하나도 없습니다.</strong>'
	end

	local targs = { ['고치기'] = src }
	for k, v in pairs( extra ) do
		targs[k] = v
	end
	for i = 1, #chips do
		local buf = {}
		for _, row in ipairs( sets[i] ) do
			buf[ #buf + 1 ] = frame:expandTemplate{ title = inner, args = mkargs( row ) }
		end
		targs[ '칩' .. i ] = chips[i]
		targs[ '칸' .. i ] = table.concat( buf )
	end
	local out = frame:expandTemplate{ title = outer, args = targs }
	if dropped > 0 then
		out = out .. '<strong class="error">칩은 ' .. MAX_CHIP .. '개까지만 나옵니다(' .. dropped .. '개 넘침).</strong>'
	end
	return out
end

-- 겹침(coverflow) 슬라이더
function p.cover( frame )
	local args = frame.args
	local src = trim( args['목록'] or args[1] or '' )
	return build( frame, src,
		'대문 겹침 슬라이더', '대문 겹침 슬라이더/장',
		{ ['간격'] = trim( args['간격'] or '' ) },
		function ( row )
			return { ['그림'] = row.img, ['제목'] = row.name, ['문서'] = row.link, ['부제'] = row.sub }
		end )
end

-- 촘촘한 격자
function p.grid( frame )
	local args = frame.args
	local src = trim( args['목록'] or args[1] or '' )
	local cols = trim( args['칸수'] or '5' )
	local width = trim( args['폭'] or '' )
	if width == '' then
		width = ( cols == '6' or cols == '7' ) and '320' or '420'
	end
	return build( frame, src,
		'대문 격자 슬라이더', '대문 격자 슬라이더/칸',
		{ ['칸수'] = cols, ['꼴'] = trim( args['꼴'] or '' ) },
		function ( row )
			return { ['그림'] = row.img, ['제목'] = row.name, ['문서'] = row.link, ['부제'] = row.sub, ['폭'] = width }
		end )
end

-- 한글 이름으로도 부를 수 있게 (루아 5.1 은 함수 이름에 한글을 못 쓴다)
p['겹침'] = p.cover
p['격자'] = p.grid
p['보이기'] = p.cover

return p