Module:Utils: Difference between revisions

From Eco - English Wiki
[checked revision][checked revision]
No edit summary
No edit summary
 
(26 intermediate revisions by 4 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Trims and parses the args into a table, then returns the table
--- Trims and parses the args into a table, then returns the table
function p.normaliseArgs(frame)
function p.normalise(args)
    local origArgs = frame:getParent().args
    local args = {}


    for k, v in pairs(origArgs) do
for k, v in pairs(args) do
        v = mw.text.trim(tostring(v))
v = mw.text.trim(tostring(v))
        if v ~= '' then
if v ~= '' then
            args[k] = v
args[k] = v
        end
end
    end
end


    return args
return args
end
end


function p.checkImage(name, too_expensive)
--- Trims and parses the args into a table, then returns the table
    local icon = name:gsub('%s+', '') .. '_Icon.png'
-- @author User:Avaren
    if too_expensive then
function p.normaliseArgs(frame)
        return icon
local origArgs = frame:getParent().args
    end
local args = {}


    if mw.title.makeTitle('File', icon).file.exists then
for k, v in pairs(origArgs) do
        return icon
v = mw.text.trim(tostring(v))
    else
if v ~= '' then
        return 'NoImage.png'
args[k] = v
    end
end
end
end


local function in_array(item, array)
return args
    -- Should only use on short arrays
    local set = {}
    for _, l in ipairs(array) do
        set[l] = true
    end
    return set[item] ~= nil
end
end


---@param name string
--- Get path to icon file.
---@param size string|nil
-- @author User:Avaren
---@param bg string|nil
function p.checkImage(name, too_expensive)
---@param border string|nil
local icon = name:gsub('%s+', '') .. '_Icon.png'
---@param too_expensive boolean|nil
if too_expensive then
function p.build_icon(name, size, bg, border, too_expensive)
return icon
    -- Size options are iconNormal or iconRecipe - 64px or 44px - defaults to iconNormal
end


    local L = require('Module:Localization') -- local import
if mw.title.makeTitle('File', icon).file.exists then
return icon
else
return 'NoIcon.png'
end
end


    if not size then
function p.itemId(name)
        size = 'iconNormal'
return name:gsub('%s+', '') .. 'Item'
    end
    local icon_bg
    local icon_border
 
    local item_data = mw.loadData('Module:ItemData')
    local item = item_data.items[name]
    local image
    if item then
        if item['group'] == L.t('Skill Books') then
            image = 'SkillBook.png'
            icon_bg = 'iconGold'
        elseif item['group'] == L.t('Skill Scrolls') then
            image = 'Skill Scroll'
            icon_bg = 'iconGold'
        -- Attempt to generate skill page
        elseif in_array(L.t('Basic  Research'), item['tagGroups']) then
            image = string.sub(item['untranslated'], 1, -7):gsub('%s+', '') .. '_Icon.png'
            icon_bg = 'paperBasic'
        elseif in_array(L.t('Advanced  Research'), item['tagGroups']) then
            image = string.sub(item['untranslated'], 1, -11):gsub('%s+', '') .. '_Icon.png'
            icon_bg = 'paperAdvanced'
        elseif in_array(L.t('Modern  Research'), item['tagGroups']) then
            image = string.sub(item['untranslated'], 1, -9):gsub('%s+', '') .. '_Icon.png'
            icon_bg = 'paperModern'
        else
            image = p.checkImage(item['untranslated'], too_expensive)
        end
        if not bg and not icon_bg then
            if item['group'] == L.t('Food') then
                icon_bg = 'iconGreen'
            elseif item['carried'] == L.t('Hands') then
                icon_bg = 'iconBrown'
            end
        end
    else
        image = p.checkImage(name, too_expensive)
    end
 
    if border then
        icon_border = border
    end
 
    if not icon_bg then
        icon_bg = 'iconBlue'
    end
 
    if not icon_border then
        icon_border = 'borderBlue'
    end
 
    local file = '[[File:' .. image .. '|frameless|class=' .. size .. ' ' .. icon_bg .. ']]'
    return '<div class="iconContainer"><div class="iconStack">' .. file .. '</div><div class="iconBorder ' .. icon_border .. '" style="position:absolute;"></div></div>'
end
end


function p.Icon(frame)
--- Check if <code>item</code> is in given <code>array</code>.
    args = p.normaliseArgs(frame)
-- @param item Item to look for
    return p.build_icon(args.name, args.size, args.bg, args.border, args.too_expensive)
-- @param #table array Table to check
-- @return #bool <code>true</code> if <code>item</code> is in <code>array</code>
-- @author User:Avaren
local function in_array(item, array)
-- Should only use on short arrays
local set = {}
for _, l in ipairs(array) do
set[l] = true
end
return set[item] ~= nil
end
end


-- mw.LoadData prevents #table from working correctly
--- Calculate the length of a table by iterating over every item in it.
--
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly.
-- @param #table tbl Table to calculate the length of
-- @return #number Length of the table.
-- @author User:Avaren
function p.tableLen(tbl)
function p.tableLen(tbl)
    local count = 0
local count = 0
    for _, v in ipairs(tbl) do
for _, v in ipairs(tbl) do
        if v == nil then
if v == nil then
            return count
return count
        end
end
        count = count + 1
count = count + 1
    end
end
    return count
return count
end
end


return p
return p

Latest revision as of 05:09, 24 November 2024

This module provides utility functions used from other modules.

Usage[edit source]

Add the following line of code at the top of your file.

local Utils = require("Module:Utils")

-- You may then call functions from this module in your script. For example:
local tableLength = Utils.tableLen(myTable)

local p = {}

--- Trims and parses the args into a table, then returns the table
function p.normalise(args)

	for k, v in pairs(args) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end

	return args
end

--- Trims and parses the args into a table, then returns the table
-- @author User:Avaren
function p.normaliseArgs(frame)
	local origArgs = frame:getParent().args
	local args = {}

	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end

	return args
end

--- Get path to icon file.
-- @author User:Avaren
function p.checkImage(name, too_expensive)
	local icon = name:gsub('%s+', '') .. '_Icon.png'
	if too_expensive then
		return icon
	end

	if mw.title.makeTitle('File', icon).file.exists then
		return icon
	else
		return 'NoIcon.png'
	end
end

function p.itemId(name)
	return name:gsub('%s+', '') .. 'Item'
end

--- Check if <code>item</code> is in given <code>array</code>.
-- @param item Item to look for
-- @param #table array Table to check
-- @return #bool <code>true</code> if <code>item</code> is in <code>array</code>
-- @author User:Avaren
local function in_array(item, array)
	-- Should only use on short arrays
	local set = {}
	for _, l in ipairs(array) do
		set[l] = true
	end
	return set[item] ~= nil
end

--- Calculate the length of a table by iterating over every item in it.
--
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly.
-- @param #table tbl Table to calculate the length of
-- @return #number Length of the table.
-- @author User:Avaren
function p.tableLen(tbl)
	local count = 0
	for _, v in ipairs(tbl) do
		if v == nil then
			return count
		end
		count = count + 1
	end
	return count
end

return p