Difference between revisions of "Module:Icon"

From Foundation - Wiki
Jump to navigation Jump to search
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
--<nowiki>
--<nowiki>
local p = {}
local p = {}
local com = require('Module:Common')


require('Module:Icon/data')


local function trim(object)
local function _requirements(resource, value, link, text, lang)
if string.sub(object, 1, 1) == ' ' then  
--[[ Language switch isn't acctually needed, but stems from early design when
object = string.sub(object, 2)
all languages would have a single wiki, leaving in as this may change in the
future ]]
if lang and string.lower(lang) == 'en' then
require('Module:Icon/en')
else
require('Module:Icon/en') -- if no language is supplied or code is invalid, fallback to english
end
end
resource = string.lower(com.trim(resource))
resource = iconList[resource]
if string.sub(object, string.len(object)) == ' ' then
if resource ~= nil then
object = string.sub(object, 1, string.len(object) - 1)
-- Init link here, will be nil if resource is nil
end
if not com.exists(link) then
link = resource['link']
return object
end
end
 
--No point trimming the value if it won't be used
local function _requirements(resource, value, link, text)
value = com.trim(value)
resource = trim(resource)
--Check after trim as may be whitespace only
if iconList[string.lower(resource)] ~= nil then
if com.exists(value) then
result = '[[file:' .. iconList[resource]['file'] .. '|16px|link='
value = ' (' .. value .. ')'
if link ~= nil and link ~= '' then
result = result .. link
else
else
result = result .. iconList[resource]['link']
value = ''
end
end
result = result .. ']]'
 
if text ~= '' and text ~= nil then
result = '[[file:'
.. resource['file']
.. '|16px|link='
.. link
.. ']]'
if com.exists(text) then
if string.lower(text) ~= 'no' then
if string.lower(text) ~= 'no' then
result = result .. '[['
result = result .. '[['
if link ~= nil and link ~= '' then
.. link
result = result .. link
.. ']]'
else
result = result .. iconList[resource]['link']
end
result = result .. ']]'
end
end
end
end
if value ~= nil and value ~= '' then
result = result .. value
result = result .. ' (' .. value .. ')'
end
else
else
result = 'Icon does not exist[[Category:Missing Icon]]'
result = 'Icon does not exist[[Category:Missing Icon]]'
Line 48: Line 53:


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


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


function p.documentation()
function p.documentation(frame)
require('Module:Icon/en')
keys = {}
keys = {}
     for z in pairs(iconList) do
     for z in pairs(iconList) do
Line 90: Line 107:
dataTable[i] = dataTable[i]  
dataTable[i] = dataTable[i]  
.. '| ' .. v
.. '| ' .. v
.. ' || ' .. _requirements(v, '', '', '')  
.. ' || ' .. _requirements(v, '', '', '', '')  
.. ' || ' ..  iconList[v]['link']
.. ' || ' ..  iconList[v]['link']
.. '\n|-\n'
.. '\n|-\n'
Line 98: Line 115:
i = i + 1
i = i + 1
end
end
mw.log(k)
end
end

Latest revision as of 17:08, 26 October 2022

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

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


local function _requirements(resource, value, link, text, lang)
	--[[ Language switch isn't acctually needed, but stems from early design when
	all languages would have a single wiki, leaving in as this may change in the
	future ]]
	if lang and string.lower(lang) == 'en' then
		require('Module:Icon/en')
	else
		require('Module:Icon/en') -- if no language is supplied or code is invalid, fallback to english
	end
	resource = string.lower(com.trim(resource))
	resource = iconList[resource]
	
	if resource ~= nil then
		-- Init link here, will be nil if resource is nil
		if not com.exists(link) 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]
	l = frame.args['lang']
	return _requirements(r, v, l, t, l)
end

function p.icon(resource, value, link, text, lang)
	return _requirements(resource, value, link, text, lang)
end

function p.iconList(frame)
	if string.match(string.lower(frame.args[1]), '%[%[file:') ~= nil then
		return frame.args[1] .. '[[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'], frame.args['lang'])
			if k ~= table.getn(list) then
				result = result .. '<br />'
			end
		end
	end

	return result
end

function p.documentation(frame)
	require('Module:Icon/en')
	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