본문으로 이동

모듈:Portals

위키배움터

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

local p = {}

-- Lede returns the lede (text header) of a given page title.
-- page: The title of a page.
-- maxlength: (optional) The maximum size to return in characters.
function p.lede(frame)
	local page = frame.args[1]
	local maxlength = frame.args[2]
	
	if page == nil or page == '' then
		return 'Page.lede: First parameter must be an existing page title.'
	end
	
	page = page:gsub("%|.*", "")

	local title = mw.title.new(page)
	
	if title == nil then
		return 'Page.lede: Could not load page ' .. page
	end
	
	if title.id == 0 then
		return 'Page.lede: Could not load page ' .. page
	end

	if maxlength == nil then
		maxlength = 500
	end
	
	local text = title:getContent()
	
	text = text:gsub("{{TOCright}}", "")
	text = text:gsub("{{RightTOC}}", "")
	text = text:gsub("{{:{{PAGENAME}}/Sidebar}}", "")

	text = "{{center|'''[[" .. page .. "]]'''}}\n" .. text
	text = frame:preprocess(text)
	
	index = text:find('==')
	if index ~= nil then
		text = text:sub(1, index - 1)
	end
	
	text = text:match('^%s*(.*%S)') or ''

	text = text .. '\n[[' .. page .. '|(more...)]]'

	--if text:len() > length then
	--	index = text:find(' ', length, true)
	--	text = text:sub(1, index)
	--end
	
	return text
end

-- Randomlink returns a random link based on a given page title.
-- page: The title of a page in which to find a random link.
-- seed: (optional) A seed for the random number generator.
--       If seed is not provided, it will randomize based on current time.
--       Supplying a seed value based on the current day would result in the
--       same link being returned all day.
function p.randomlink(frame)
	local page = frame.args[1]
	local seed = frame.args[2]
	
	if page == nil or page == '' then
		return 'Portal:Randomlink: First parameter must be an existing page title.'
	end
	
	local title = mw.title.new(page)
	if title.id == 0 then
		return 'Portal:Randomlink: First parameter must be an existing page title.'
	end

	if seed == nil then
		math.randomseed(os.time() + math.floor(os.clock() * 1000000000))
	else
		math.randomseed(seed) 
	end

	local text = title:getContent()
	local links = {}
	local count = 0

	for link in text:gmatch("%[%[([^%]]*)%]%]") do
		table.insert(links, link)
		count = count + 1
	end
	
	if count == 0 then
		return 'Portal:Randomlink: Page has no links.'
	else
		local index = math.random(count)
		return links[index]
	end
end

return p