Module:Sandbox: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
>OmegaK2
No edit summary
>OmegaK2
No edit summary
Line 1: Line 1:
-- ----------------------------------------------------------------------------
-- Imports
-- ----------------------------------------------------------------------------
local xtable = require('Module:Table')
local m_util = require('Module:Util')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}
local c = {}
c.image_size = 39
c.image_size_full = c.image_size * 2


p.cases = {
-- ----------------------------------------------------------------------------
    -- Basic test list
-- Strings
    {
-- ----------------------------------------------------------------------------
        parser = {
-- This section contains strings used by this module.
            explicit = {
-- Add new strings here instead of in-code directly, this will help other
                _type = 'list',
-- people to correct spelling mistakes easier and help with translation to
            },
-- other PoE wikis.
        },
--
 
-- TODO: Maybe move this out to a separate sub-page module
        input = {
local i18n = {
            explicit1 = 'Test1',
    categories = {
            explicit2 = 'Test2',
         -- maintenance cats
        },
         broken_item_links = '[[Category:Pages with broken item links]]',
          
         expected = {
            explicit = {'Test1', 'Test2'},
        },
     },
     },
     -- Basic test table
     errors = {
    {
         item_link_invalid_args = 'Item link: page, item_name or item_name_exact must be specified',
        parser = {
         item_link_no_results = 'Item link: No results found for search parameter "%s".',
            stat = {
        item_link_too_many_results = 'Item link: Too many results for search parameter "%s". Consider using page parameter instead.',
                _type = 'table',
         item_link_alt_art_undefined = 'Item link: Image parameter was specified, but there is no alternate art defined on page "%s"',
                id = {},
        item_link_alt_art_invalid_index = 'Item Link: Alternate art with index/name "%s" not found on page "%s"',
                value = {},
            },
        },
         input = {
            stat1_id = 'test1',
            stat1_value = '1',
            stat2_id = 'test2',
            stat2_value = '2',
        },
        expected = {
            stat = {
                [1] = {
                    id = 'test1',
                    value = '1',
                },
                [2] = {
                    id = 'test2',
                    value = '2',
                },
            },
        },
    },
    -- Test nested table
    {
         parser = {
            stat_column = {
                _type = 'table',
                header = {
                    _func = nil,
                    _type = nil,
                },
                format = {
                    _func = nil,
                    _type = nil,
                },
                stat_format = {
                    _func = nil,
                    _type = nil,
                },
                stat = {
                    _type = 'list',
                    --id = {
                    --    _func = nil,
                    --    _type = nil,
                    --},
                },
            },
        },
        input = {
            stat_column1_header = 'Local Attack<br>Chaos Damage',
            stat_column1_format = '%s-%s',
            stat_column1_stat_format = 'separate',
            stat_column1_stat1_id = 'local_minimum_added_chaos_damage',
            stat_column1_stat2_id = 'local_maximum_added_chaos_damage',
            stat_column2_header = 'Global Attack<br>Chaos Damage',
            stat_column2_format = '%s-%s',
            stat_column2_stat_format = 'separate',
            stat_column2_stat1_id = 'attack_minimum_added_chaos_damage',
            stat_column2_stat2_id = 'attack_minimum_added_chaos_damage',
        },
         expected = {
            stat_column = {
                [1] = {
                    header = 'Local Attack<br>Chaos Damage',
                    format = '%s-%s',
                    stat_format = 'separate',
                    stat = {
                        [1] = {
                            id = 'local_minimum_added_chaos_damage',
                        },
                        [2] = {
                            id = 'local_maximum_added_chaos_damage',
                        },
                    },
                },
                [2] = {
                    header = 'Global Attack<br>Chaos Damage',
                    format = '%s-%s',
                    stat_format = 'separate',
                    stat = {
                        [1] = {
                            id = 'attack_minimum_added_chaos_damage',
                        },
                        [2] = {
                            id = 'attack_minimum_added_chaos_damage',
                        },
                    },
                },
            },
        },
     },
     },
}
}


local core = {}
core.item_link_params = {
    name = 'Has name',
    inventory_icon = 'Has inventory icon',
    html = 'Has infobox HTML',
}
--
-- Template:Item link & Template:Sl
--


function p.validator(args, parser, prefix)
function p.item_link (frame)
     local rtr = {}
    --
     prefix = prefix or ''
    -- Args/Frame
     for key, data in pairs(parser) do
    --
        local pkey = prefix .. key
   
        if data._type == 'table' then
     local tpl_args = getArgs(frame, {
            local i = 1
        parentFirst = true,
            local stop = false
        removeBlanks = false,
            local tbl = {}
    })
            repeat
     frame = m_util.misc.get_frame(frame)
                local result = p.validator(args, data, string.format('%s%s_', pkey, i))
   
               
     -- Backwards compability
                -- As long there was one entry, it is valid
    tpl_args.item_name = tpl_args.item_name or tpl_args[1]
                local count = 0
    if tpl_args.item_name ~= nil then
                for _ in pairs(result) do
        tpl_args.item_name = mw.ustring.lower(tpl_args.item_name)
                    count = count + 1
    end
                    break
    tpl_args.name = tpl_args.name or tpl_args[2]
                end
   
               
    if m_util.table.has_all_value(tpl_args, {'page', 'item_name', 'item_name_exact'}) then
                -- Stop if we don't get any results anymore
        error(i18n.errors.item_link_invalid_args)
                if count == 0 then
    end
                    stop = true
   
                 else
    tpl_args.large = m_util.cast.boolean(tpl_args.large)
                    tbl[#tbl+1] = result
   
                    i = i + 1
    local img
                end
    local result
               
   
            until stop == true
    if m_util.table.has_one_value(tpl_args, core.item_link_params, nil) or tpl_args.item_name ~= nil then
        local query = {}
       
        if tpl_args.page ~= nil then
            -- TODO returns the result even if the + format is specified.
            query[#query+1] = string.format('[[%s]]', tpl_args.page)
        else
            if tpl_args.item_name ~= nil then
                 query[#query+1] = string.format('[[Has lowercase names::%s]]', tpl_args.item_name)
            elseif tpl_args.item_name_exact ~= nil then
                query[#query+1] = string.format('[[Has name::%s]]', tpl_args.item_name_exact)
            end
              
              
             rtr[key] = tbl
             query[#query] = query[#query] .. ' [[Has inventory icon::+]] [[Has infobox HTML::+]]'
        elseif data._type == 'list' then
            local i = 1
            local stop = false
            local tbl = {}
            repeat
                local result = args[pkey .. i]
                if data._func ~= nil then
                    result = data._func(result)
                end
               
                if result == nil then
                    stop = true
                else
                    tbl[#tbl+1] = result
                    i = i + 1
                end
            until stop == true
              
              
             rtr[key] = tbl
             if tpl_args.link_type == 'skill' then
                query[#query] = query[#query] .. ' [[Concept:Skill gems]]'
            end
        end
       
        query[#query+1] = '?Has name'
        query[#query+1] = '?Has inventory icon'
        query[#query+1] = '?Has infobox HTML'
        query[#query+1] = '?Has alternate inventory icons'
        query[#query+1] = '?Has inventory width'
        query[#query+1] = '?Has inventory height'
       
        -- attributes
        result = m_util.smw.query(query, frame)
       
        local err
        if #result == 0 then
            err = m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_no_results,
                tpl_args.page or tpl_args.item_name or tpl_args.item_name_exact
            )}
        elseif #result > 1 then
            err = m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_too_many_results,
                tpl_args.page or tpl_args.item_name or tpl_args.item_name_exact
            )}
        end
       
        if err ~= nil then
            return err .. i18n.categories.broken_item_links
        end
       
        result = result[1]
    else
        result = {tpl_args.page or tpl_args.item_name_exact}
    end
   
    for k, prop in pairs(core.item_link_params) do
        if tpl_args[k] ~= nil then
            result[prop] = tpl_args[k]
        end
    end
   
    if tpl_args.image ~= nil then
        if result['Has alternate inventory icons'] == '' then
            return m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_alt_art_undefined,
                result[1]
            ) .. i18n.categories.broken_item_links}
        end
       
        result['Has alternate inventory icons'] = m_util.string.split(result['Has alternate inventory icons'], '<MANY>')
       
        local index = tonumber(tpl_args.image)
        if index ~= nil then
            img = result['Has alternate inventory icons'][index]
         else
         else
             local result = args[pkey]
            -- offset 1 is needed
             if data._func ~= nil then
             local suffix = string.len(' inventory icon.png') + 1
                 result = data._func(result)
            -- add an extra offset by 1 to account for the space
            local prefix = string.len(string.sub(result['Has inventory icon'], 1, -suffix)) + 2
           
             for _, filename in ipairs(result['Has alternate inventory icons']) do
                if string.sub(filename, prefix, -suffix) == tpl_args.image then
                    img = filename
                    break
                 end
             end
             end
            rtr[key] = result
         end
         end
       
        if img == nil then
            return m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_alt_art_invalid_index,
                tpl_args.image, result[1]
            ) .. i18n.categories.broken_item_links}
        end
    elseif result['Has inventory icon'] ~= '' then
        img = result['Has inventory icon']
     end
     end
      
      
     return rtr
    -- output
   
    local container = mw.html.create('span')
    container:addClass('c-item-hoverbox')
   
    local activator = mw.html.create('span')
    activator:addClass('c-item-hoverbox__activator')
 
    if not tpl_args.large then
        activator:wikitext(string.format('[[%s|16x16px|link=|alt=]]', img))
    end
     
    activator:wikitext(string.format('[[%s|%s]]', result[1], result['Has name'] or result[1]))
   
    local display = mw.html.create('span')
    display:attr('class', 'c-item-hoverbox__display')
 
    if result['Has infobox HTML'] ~= '' then
        display
            :wikitext(result['Has infobox HTML'])
            :wikitext(string.format('[[%s|link=|alt=]]', img))
            :done()
    end
 
    if tpl_args.large then
        local width = tonumber(result['Has inventory width']) or tonumber(tpl_args.width)
        local height = tonumber(result['Has inventory height']) or tonumber(tpl_args.height)
        if width and height then
            img = string.format('<br>[[%s|%sx%spx|link=%s|alt=]]', img, width*c.image_size, height*c.image_size, result[1])
        elseif width then
            img = string.format('<br>[[%s|%spx|link=%s|alt=]]', img, width*c.image_size, result[1])
        elseif height then
            img = string.format('<br>[[%s|x%spx|link=%s|alt=]]', img, height*c.image_size, result[1])
        else
            img = string.format('<br>[[%s|link=%s|alt=]]', img, result[1])
        end
        activator:wikitext(img)
    end
 
    container
        :node(activator)
        :node(display)
        :done()
       
     return tostring(container)
end
end


return p
return p

Revision as of 23:44, 4 September 2017

Module documentation[view] [edit] [history] [purge]


This page is not an actual Scribunto module. It exists to provide editors a place to create experimental modules.

Naming your modules

To keep things tidy, please use the following format to name your experimental modules:

Module:Sandbox/Your username/Module name

Cleaning up unused modules

Experimental modules may be deleted by admins upon request or after a long period of inactivity.

List of modules in this area

For a list of the experimental modules under Module:Sandbox, see Special:PrefixIndex/Module:Sandbox/.

-- ----------------------------------------------------------------------------
-- Imports
-- ----------------------------------------------------------------------------
local xtable = require('Module:Table')
local m_util = require('Module:Util')
local getArgs = require('Module:Arguments').getArgs

local p = {}
local c = {}
c.image_size = 39
c.image_size_full = c.image_size * 2

-- ----------------------------------------------------------------------------
-- Strings
-- ----------------------------------------------------------------------------
-- This section contains strings used by this module.
-- Add new strings here instead of in-code directly, this will help other
-- people to correct spelling mistakes easier and help with translation to
-- other PoE wikis.
--
-- TODO: Maybe move this out to a separate sub-page module
local i18n = {
    categories = {
        -- maintenance cats
        broken_item_links = '[[Category:Pages with broken item links]]',
    },
    errors = {
        item_link_invalid_args = 'Item link: page, item_name or item_name_exact must be specified',
        item_link_no_results = 'Item link: No results found for search parameter "%s".',
        item_link_too_many_results = 'Item link: Too many results for search parameter "%s". Consider using page parameter instead.',
        item_link_alt_art_undefined = 'Item link: Image parameter was specified, but there is no alternate art defined on page "%s"',
        item_link_alt_art_invalid_index = 'Item Link: Alternate art with index/name "%s" not found on page "%s"',
    },
}

local core = {}
core.item_link_params = {
    name = 'Has name',
    inventory_icon = 'Has inventory icon',
    html = 'Has infobox HTML',
}

--
-- Template:Item link & Template:Sl
--

function p.item_link (frame)
    --
    -- Args/Frame
    --
    
    local tpl_args = getArgs(frame, {
        parentFirst = true,
        removeBlanks = false,
    })
    frame = m_util.misc.get_frame(frame)
    
    -- Backwards compability
    tpl_args.item_name = tpl_args.item_name or tpl_args[1]
    if tpl_args.item_name ~= nil then
        tpl_args.item_name = mw.ustring.lower(tpl_args.item_name)
    end
    tpl_args.name = tpl_args.name or tpl_args[2]
    
    if m_util.table.has_all_value(tpl_args, {'page', 'item_name', 'item_name_exact'}) then
        error(i18n.errors.item_link_invalid_args)
    end
    
    tpl_args.large = m_util.cast.boolean(tpl_args.large)
    
    local img
    local result
    
    if m_util.table.has_one_value(tpl_args, core.item_link_params, nil) or tpl_args.item_name ~= nil then
        local query = {}
        
        if tpl_args.page ~= nil then
            -- TODO returns the result even if the + format is specified. 
            query[#query+1] = string.format('[[%s]]', tpl_args.page)
        else
            if tpl_args.item_name ~= nil then
                query[#query+1] = string.format('[[Has lowercase names::%s]]', tpl_args.item_name)
            elseif tpl_args.item_name_exact ~= nil then
                query[#query+1] = string.format('[[Has name::%s]]', tpl_args.item_name_exact)
            end
            
            query[#query] = query[#query] .. ' [[Has inventory icon::+]] [[Has infobox HTML::+]]'
            
            if tpl_args.link_type == 'skill' then
                query[#query] = query[#query] .. ' [[Concept:Skill gems]]'
            end
        end
        
        query[#query+1] = '?Has name'
        query[#query+1] = '?Has inventory icon'
        query[#query+1] = '?Has infobox HTML'
        query[#query+1] = '?Has alternate inventory icons'
        query[#query+1] = '?Has inventory width'
        query[#query+1] = '?Has inventory height'
        
        -- attributes
        result = m_util.smw.query(query, frame)
        
        local err
        if #result == 0 then
            err = m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_no_results, 
                tpl_args.page or tpl_args.item_name or tpl_args.item_name_exact 
            )}
        elseif #result > 1 then
            err = m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_too_many_results,
                tpl_args.page or tpl_args.item_name or tpl_args.item_name_exact
            )}
        end
        
        if err ~= nil then
            return err .. i18n.categories.broken_item_links
        end
        
        result = result[1]
    else
        result = {tpl_args.page or tpl_args.item_name_exact}
    end
    
    for k, prop in pairs(core.item_link_params) do
        if tpl_args[k] ~= nil then
            result[prop] = tpl_args[k]
        end
    end
    
    if tpl_args.image ~= nil then
        if result['Has alternate inventory icons'] == '' then
            return m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                i18n.errors.item_link_alt_art_undefined,
                result[1]
            ) .. i18n.categories.broken_item_links}
        end
        
        result['Has alternate inventory icons'] = m_util.string.split(result['Has alternate inventory icons'], '<MANY>')
        
        local index = tonumber(tpl_args.image)
        if index ~= nil then
            img = result['Has alternate inventory icons'][index]
        else
            -- offset 1 is needed
            local suffix = string.len(' inventory icon.png') + 1 
            -- add an extra offset by 1 to account for the space 
            local prefix = string.len(string.sub(result['Has inventory icon'], 1, -suffix)) + 2
            
            for _, filename in ipairs(result['Has alternate inventory icons']) do
                if string.sub(filename, prefix, -suffix) == tpl_args.image then
                    img = filename
                    break
                end
            end
        end
        
        if img == nil then
            return m_util.misc.raise_error_or_return{raise_required=true, args=tpl_args, msg=string.format(
                 i18n.errors.item_link_alt_art_invalid_index,
                tpl_args.image, result[1]
            ) .. i18n.categories.broken_item_links}
        end
    elseif result['Has inventory icon'] ~= '' then
        img = result['Has inventory icon']
    end
    
    -- output
    
    local container = mw.html.create('span')
    container:addClass('c-item-hoverbox')
    
    local activator = mw.html.create('span')
    activator:addClass('c-item-hoverbox__activator')

    if not tpl_args.large then
        activator:wikitext(string.format('[[%s|16x16px|link=|alt=]]', img))
    end
       
    activator:wikitext(string.format('[[%s|%s]]', result[1], result['Has name'] or result[1]))
    
    local display = mw.html.create('span')
    display:attr('class', 'c-item-hoverbox__display')

    if result['Has infobox HTML'] ~= '' then
        display
            :wikitext(result['Has infobox HTML'])
            :wikitext(string.format('[[%s|link=|alt=]]', img))
            :done()
    end

    if tpl_args.large then
        local width = tonumber(result['Has inventory width']) or tonumber(tpl_args.width)
        local height = tonumber(result['Has inventory height']) or tonumber(tpl_args.height)
        if width and height then
            img = string.format('<br>[[%s|%sx%spx|link=%s|alt=]]', img, width*c.image_size, height*c.image_size, result[1])
        elseif width then
            img = string.format('<br>[[%s|%spx|link=%s|alt=]]', img, width*c.image_size, result[1])
        elseif height then
            img = string.format('<br>[[%s|x%spx|link=%s|alt=]]', img, height*c.image_size, result[1])
        else
            img = string.format('<br>[[%s|link=%s|alt=]]', img, result[1])
        end
        activator:wikitext(img)
    end

    container
        :node(activator)
        :node(display)
        :done()
        
    return tostring(container)
end

return p