본문으로 이동

모듈:Outline

리버티게임, 모두가 만들어가는 자유로운 게임


모듈 설명문서[보기] [편집] [역사] [새로 고침]

다른 문서의 문단 개요를 생성합니다.

getSectionOutline[원본 편집]

특정 문서의 최상위 문단으로 생성된 개요를 테이블 형식으로 얻습니다. 반환되는 테이블에는 각 문단 제목이 문자열로 들어있습니다.

local Outline = require('모듈:Outline')
return Outline.getSectionOutline('모듈:Outline/설명문서')

----

{
    "getSectionOutline",
    "bulletOutline"
}

bulletOutline[원본 편집]

특정 문서의 최상위 문단으로 생성된 개요를 목록으로 출력합니다. 또한 목록의 각 항목은 해당 문단으로 이동하는 링크가 걸립니다.

code_blocks 코드
{{#invoke:Outline|bulletOutline|모듈:Outline/설명문서}}
code
낙서장에서 확인

quarterForumOutline[원본 편집]

분기로 나뉘는 포럼 문서의 주제(문단)들의 제목을 목록으로 출력합니다. 또한 각 주제로 이동하는 링크를 추가합니다.

code_blocks 코드
{{#invoke:Outline|quarterForumOutline|사용자:Hsl0/리버티게임:업데이트|8|새 게임이 없습니다. [[리버티게임:게임 만들기|한번 만들어보시겠어요?]]|minQuarters=4}}
code
낙서장에서 확인


local p = {}
local contentLang = mw.language.getContentLanguage()
local sp = require('모듈:SectionParser')

function p.getSectionOutline(title)
	local titleObj = mw.title.new(title)
	
	if not titleObj.exists then
		error(title .. ' is not existing page')
	end
	
	local content = titleObj:getContent()
	local sectionTbl = sp.tree(content)
	local sectionOutline = {}
	
	for n, section in ipairs(sectionTbl) do
		table.insert(sectionOutline, section.name)
	end
	
	return sectionOutline
end

function imap(tbl, fun)
	local newtbl = {}
	
	for k, v in ipairs(tbl) do
		table.insert(newtbl, fun(k, v))
	end
	
	return newtbl
end

function join(target, source)
	for index, value in ipairs(source) do
		table.insert(target, value)
	end
	
	return target
end

function unwrapLink(source)
	return source:gsub("%[%[%f[^%[](.-)%]%]", function(exp)
		local href, display = unpack(mw.text.split(exp, '|'))
		display = display or href
		
		return display
	end)
end

function createLink(title, section, display)
	return '[[' .. title .. '#' .. mw.uri.anchorEncode(section) .. '|' .. unwrapLink(display) .. ']]'
end

function bulletList(tbl)
	return '* ' .. table.concat(tbl, '\n* ') .. '\n'
end

function p.bulletOutline(frame)
	local title = frame.args[1]
	local outline = p.getSectionOutline(title)
	local outlineLinks = imap(outline, function(n, section)
		return createLink(title, section, section)
	end)
	
	return bulletList(outlineLinks)
end

function getQuarter(now)
	return math.ceil(tonumber(contentLang:formatDate('n', now)) / 3)
end

function prevQuarter(year, quarter)
	if quarter > 1 then
		return year, quarter - 1
	else
		return year - 1, 4
	end
end

function p.quarterForumOutline(frame)
	local prefix = frame.args[1] -- 포럼 문서 접두어
	local length = tonumber(frame.args[2]) -- 최대 개요 개수
	local fallback = frame.args[3] -- 결과가 없을 때 대신 표시할 내용
	local date = frame.args.date -- 지정된 기준 날짜
	local minQuarters = tonumber(frame.args.minQuarters) or 1 -- 필수 포함 분기 수
	local maxQuarters = tonumber(frame.args.maxQuarters) -- 표시 가능 분기 수
	
	-- 현재 분기 문서
	local year = tonumber(contentLang:formatDate('Y', date)) -- 연도
	local quarter = getQuarter(date) -- 분기

	local outline = {}
	
	-- 최대 개요 개수에 미달시, 이전 분기에서 개요 가져옴
	-- minQuarters <= (final)index <= maxQuarters
	-- (newest)date - index = oldestDate
	local index = 0
	while #outline < length do
		index = index + 1
		local page = prefix .. '/' .. year .. '년 ' .. quarter .. '분기'
		
		-- 최대 분기 수 초과 시 종료
		if maxQuarters ~= nil and index > maxQuarters then
			mw.log('Max quarters exceeded (' .. maxQuarters .. ')')
			break
		end
		
		-- 이전 분기 문서가 없으면 종료
		if not mw.title.new(page).exists then
			mw.log('[[' .. page .. ']] page is not exist')
			
			if index > minQuarters then
				break
			end
		else
			local thisOutline = p.getSectionOutline(page)
			thisOutline = imap(thisOutline, function(n, section)
				return createLink(page, section, section)
				-- return section .. ' (' .. createLink(page, section, '자세히') .. ')'
			end)
			
			outline = join(thisOutline, outline)
		end
		
		year, quarter = prevQuarter(year, quarter)
	end
	
	-- 최대 개요 개수 초과시, 앞(오래된) 문단부터 지우기
	while #outline > length do
		table.remove(outline, 1)
	end
	
	-- 개요가 없으면 fallback 출력
	if #outline <= 0 then
		return fallback or ""
	end
	
	-- 문단 개요를 목록으로 출력
	return bulletList(outline)
end

return p