Difference between revisions of "Module:Icon"

From Foundation - Wiki
Jump to navigation Jump to search
Line 27: Line 27:
resource = string.lower(trim(resource))
resource = string.lower(trim(resource))
resource = iconList[resource]
resource = iconList[resource]
value = trim(value)
if resource ~= nil then
if resource ~= nil then
Line 35: Line 34:
end
end
result = '[[file:' .. resource['file'] .. '|16px|link='
--No point trimming the value if it won't be used
result = result .. link
value = trim(value)
result = result .. ']]'
--Check after trim as may be whitespace only
if exists(value) == true then
value = ' (' .. value .. ')'
end
result = '[[file:'  
.. resource['file']  
.. '|16px|link='
.. link
.. ']]'
if exists(text) == true then
if exists(text) == true then
if string.lower(text) ~= 'no' then
if string.lower(text) ~= 'no' then
result = result .. '[['
result = result .. '[['
result = result .. link
.. link
result = result .. resource['link']
.. ']]'
result = result .. ']]'
end
end
end
end
if exists(value) == true then
result = result .. ' (' .. value .. ')'
end
else
else
result = 'Icon does not exist[[Category:Missing Icon]]'
result = 'Icon does not exist[[Category:Missing Icon]]'

Revision as of 19:50, 23 October 2021

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

--<nowiki>
local p = {}

require('Module:Icon/data')

local function trim(object)
	if string.sub(object, 1, 1) == ' ' then 
		object = string.sub(object, 2)
	end
	
	if string.sub(object, string.len(object)) == ' ' then
		object = string.sub(object, 1, string.len(object) - 1)
	end
	
	return object
end

function exists(object)
	if object == nil or object == '' then
		return false
	else
		return true
	end
end

local function _requirements(resource, value, link, text)
	resource = string.lower(trim(resource))
	resource = iconList[resource]
	
	if resource ~= nil then
		-- Init link here, will be nil if resource is nil
		if exists(link) == false then
			link = resource['link']
		end
		
		--No point trimming the value if it won't be used
		value = trim(value)
		
		--Check after trim as may be whitespace only
		if exists(value) == true then
			value = ' (' .. value .. ')'
		end
		
		result = '[[file:' 
			.. resource['file'] 
			.. '|16px|link='
			.. link
			.. ']]'
		
		if exists(text) == true then
			if string.lower(text) ~= 'no' then
				result = result ..	'[['
					.. link
					.. ']]'
			end
		end
		
	else
		result = 'Icon does not exist[[Category:Missing Icon]]'
	end
	
	return result
end

function p.requirements(frame)
	r = 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)
	mw.log('Begin List')
	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
	mw.log('End List')
	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