Module:Icon

From Foundation - Wiki
Revision as of 15:54, 23 October 2021 by Sakaratte (talk | contribs)
Jump to navigation Jump to search

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

--<nowiki>
local p = {}

require('Module:Icon/data')

local function _requirements(resource, value, link, text)
	if iconList[string.lower(resource)] ~= nil then
		result = '[[file:' .. iconList[resource]['file'] .. '|16px|link='
		if link ~= nil and link ~= '' then
			result = result .. link
		else
			result = result .. iconList[resource]['link']
		end
		result = result .. ']]'
		if text ~= '' and text ~= nil then
			if string.lower(text) ~= 'no' then
				result = result ..	'[['
				if link ~= nil and link ~= '' then
					result = result .. link
				else
					result = result .. iconList[resource]['link']
				end
				result = result .. ']]'
			end
		end
		if value ~= nil and value ~= '' then
			result = result .. ' (' .. value .. ')'
		end
	else
		result = 'Icon does not exist[[Category:Missing Icon]]'
	end
	
	return result
end

function p.requirements(frame)
	r = string.lower(frame.args['resource'])
	v = frame.args['value']
	l = frame.args['link']
	t = frame.args[1]
	return _requirements(r, v, l, t)
end

function p.iconList(frame)
	list = mw.text.split(frame.args[1], ';')
	result = ''
	for k, v in ipairs(list) do
		vs = mw.text.split(v, '-')
		result = result .. _requirements(vs[1], vs[2], nil, frame.args['text'])
		if k ~= table.getn(list) then
			result = result .. '<br />'
		end
	end
	
	return result
end

function p.documentation()
	keys = {}
    for z in pairs(iconList) do
        table.insert(keys, z)
    end
 
    table.sort(keys)
	dataTable = {[1] = '', [2] = '', [3] = ''}
	header = '\n{| class="wikitable"\n'
		.. '! Code !! Icon !! Link\n'
		.. '\n|-\n'
	footer = '|}'
	i = 1 
	while i < 4 do
		dataTable[i] = header
		i = i + 1
	end
	i = 1
	for k, v in ipairs(keys) do 
		dataTable[i] = dataTable[i] 
		.. '| ' .. v
		.. ' || ' .. _requirements(v, '', '', '') 
		.. ' || ' ..  iconList[v]['link']
		.. '\n|-\n'
		if i == 3 then 
			i = 1
		else
			i = i + 1
		end
		mw.log(k)
	end
	
	i = 1
	while i < 4 do
		dataTable[i] = dataTable[i] .. footer
		i = i + 1
	end
	
	result = mw.html.create('div')
	result:css('display', 'grid')
		:css('grid-template-columns', '1fr 1fr 1fr')
		:wikitext(dataTable[1] .. dataTable[2] .. dataTable[3] .. '\n')
	return result
end

return p