Module:Sandbox: Difference between revisions
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 | |||
-- ---------------------------------------------------------------------------- | |||
-- 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. | function p.item_link (frame) | ||
local | -- | ||
-- 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 | else | ||
local result | -- offset 1 is needed | ||
if | 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 | ||
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 | -- 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
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/.
The above documentation is transcluded from Module:Sandbox/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-- ----------------------------------------------------------------------------
-- 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