Module:SummonCall

From Granblue Fantasy Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:SummonCall/doc

local p = {}

function findArg(needle, haystack)
	local pattern = '|'..needle..'='
	
	local startIndex = haystack:find(pattern, 0, true)
	if startIndex == nil then
		return 'Not Found'
	end
	startIndex = startIndex + pattern:len()
	
	local endIndex = haystack:find('\n|', startIndex, true)
	endIndex = endIndex - 1
	
	local result = haystack:sub(startIndex,endIndex)
	return mw.text.trim(result)
end

function p.renderCall(frame)
	mw.log('render start: ' .. os.clock())
	local template = frame.args.template
	local summonName= frame.args.summonName
	local callIndex= frame.args.callIndex
	local additionalArgs = frame.args.additionalArgs

	mw.log('load data: ' .. os.clock())
	local title = mw.title.new(summonName)
	local rawPage = title:getContent()
	
	mw.log('find args:' .. os.clock())
	local nameArg = 'call_name'
	local descArg = 'call_'..callIndex
	local name = findArg(nameArg, rawPage)
	local desc = findArg(descArg, rawPage)

	mw.log('expandTemplate')
	local args = {}
	args['link'] = summonName
	args['name'] = name
	args['desc'] = frame:preprocess(desc)

	-- Pass `frame.args.additionalArgs` to `args`
	if additionalArgs ~= nil then
		for arg in (additionalArgs):gmatch('[^;]+') do
			local i = arg:find('=')
			args[arg:sub(0, i-1)] = arg:sub(i+1)
		end
	end

	mw.log('done: ' .. os.clock())
	return frame:expandTemplate{ title = template, args = args }
end

return p