Module:WeaponOugi

From Granblue Fantasy Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:WeaponOugi/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 tryFindArg(needle, haystack)
	local result = findArg(needle, haystack)
	if result == 'Not Found' then
		return ''
	end
	return result
end

function p.renderOugi(frame)
	mw.log('render start: ' .. os.clock())
	local template = frame.args.template
	local weaponName = frame.args.weaponName
	local additionalArgs = frame.args.additionalArgs

	mw.log('load data: ' .. os.clock())
	local title = mw.title.new(weaponName)
	local rawPage = title:getContent()
	
	mw.log('find args:' .. os.clock())
	local nameArg = 'ougi_name'
	local descArg = 'ougi'
	local name = findArg(nameArg, rawPage)
	local desc = findArg(descArg, rawPage)
	local desc4s = tryFindArg('ougi_4s', rawPage)
	local desc5s = tryFindArg('ougi_5s', rawPage)
	
	mw.log('expandTemplate')
	local args = {}
	args['link'] = weaponName
	args['name'] = name
	args['desc'] = frame:preprocess(desc)
	args['desc4s'] = frame:preprocess(desc4s)
	args['desc5s'] = frame:preprocess(desc5s)

	-- 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

function p.testRender()
	local frame = mw.getCurrentFrame()
	frame.args.template = 'WeaponOugi/Template'
	frame.args.weaponName = 'Parazonium'
	frame.args.additionalArgs = 'large=false;noicon=false'
	return p.renderOugi(frame)
end

return p