Difference between revisions of "Module:Icon"

From Foundation - Wiki
Jump to navigation Jump to search
Line 58: Line 58:


function p.iconList(frame)
function p.iconList(frame)
list = string.gsub(string.gsub(frame.args[1], '%[%[', ''), '%]%]', '')
if string.match(string.lower(frame.args[1]), '%[%[file:') ~= nil then
-- list = string.gsub(string.gsub(list, '%{%{item%|', ''), '%}%}', '')
return '[[Category:Material List with Embedded Templates]]'
-- list = string.gsub(list, '<br%s>', ';')
else
-- list = string.gsub(list, '%|', '-')
list = string.gsub(string.gsub(frame.args[1], '%[%[', ''), '%]%]', '')
mw.log(list)
mw.log(list)
list = mw.text.split(list, ';')
list = mw.text.split(list, ';')
result = ''
result = ''
for k, v in ipairs(list) do
for k, v in ipairs(list) do
vs = mw.text.split(v, '-')
vs = mw.text.split(v, '-')
result = result .. _requirements(vs[1], vs[2], nil, frame.args['text'])
result = result .. _requirements(vs[1], vs[2], nil, frame.args['text'])
if k ~= table.getn(list) then
if k ~= table.getn(list) then
result = result .. '<br />'
result = result .. '<br />'
end
end
end
end
end

Revision as of 17:07, 14 February 2022

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

--<nowiki>
local p = {}
local com = require('Module:Common')
require('Module:Icon/data')

local function _requirements(resource, value, link, text)
	resource = string.lower(com.trim(resource))
	resource = iconList[resource]
	
	if resource ~= nil then
		-- Init link here, will be nil if resource is nil
		if com.exists(link) == false then
			link = resource['link']
		end
		
		--No point trimming the value if it won't be used
		value = com.trim(value)
		--Check after trim as may be whitespace only
		if com.exists(value) then
			value = ' (' .. value .. ')'
		else
			value = ''
		end
		
		
		result = '[[file:' 
			.. resource['file'] 
			.. '|16px|link='
			.. link
			.. ']]'
		
		if com.exists(text) then
			if string.lower(text) ~= 'no' then
				result = result ..	'[['
					.. link
					.. ']]'
			end
		end
		result = result .. value
	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.icon(resource, value, link, text)
	return _requirements(resource, value, link, text)
end

function p.iconList(frame)
	if string.match(string.lower(frame.args[1]), '%[%[file:') ~= nil then
		return '[[Category:Material List with Embedded Templates]]'
	else
		list = string.gsub(string.gsub(frame.args[1], '%[%[', ''), '%]%]', '')
		mw.log(list)
		list = mw.text.split(list, ';')
		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
	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
	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