|
|
| Line 1: |
Line 1: |
| -- Adapted from: https://genshin-impact.fandom.com/wiki/Module:Item
| | function p.show(frame) |
| | local args = frame:getParent().args |
| | mw.logObject(args) |
|
| |
|
| local p = {} | | local bucket_name = mw.text.trim(args[1] or '') |
| local lib = require('Module:Feature')
| | mw.log("bucket_name = [" .. bucket_name .. "]") |
|
| |
|
| function p.main(frame)
| | local select_field = mw.text.trim(args[2] or '') |
| local args = require('Module:Arguments').getArgs(frame, { | | mw.log("select_field = [" .. select_field .. "]") |
| parentFirst = true,
| |
| removeBlanks = false
| |
| })
| |
| return p._main(args)
| |
| end
| |
| | |
| function p._main(args)
| |
| local item = lib.nilIfEmpty(args[1]) or lib.nilIfEmpty(args.name) or 'Unknown'
| |
| local size = lib.nilIfEmpty(args[2]) or lib.nilIfEmpty(args.size) or 30
| |
| local count = lib.nilIfEmpty(args.count) or lib.nilIfEmpty(args.x) or nil
| |
| local note = lib.nilIfEmpty(args.note) or nil
| |
| local ref = lib.nilIfEmpty(args.ref) or nil
| |
| local typeInput = lib.nilIfEmpty(args['type']) or 'Item'
| |
| local text = lib.nilIfEmpty(args.text) or item
| |
| local link = args.link or lib.ternary(item == 'Unknown', '', item)
| |
| local invert = lib.nilIfEmpty(args.invert)
| |
| local newline = tostring(args.newline) == '1' | |
| local noText = tostring(args.no_text or args.notext) == '1'
| |
| local white = tostring(args.white) == '1'
| |
| | |
| local prefix = ''
| |
| local suffix = ' Icon'
| |
|
| |
| if (typeInput ~= 'Neighbor' and item == 'Unknown') then
| |
| prefix = ''
| |
| suffix = ' Icon'
| |
| end
| |
|
| |
| local invertIcon = require('Module:Item/Invert')
| |
| if not invert then
| |
| invert = invertIcon[item]
| |
| end
| |
| | |
| local invertClass = nil
| |
| if invert then
| |
| invert = invert:lower()
| |
| if invert == 'day' or invert == 'light' then
| |
| invertClass = 'day-invert-image'
| |
| elseif invert == 'night' or invert == 'dark' then
| |
| invertClass = 'night-invert-image'
| |
| end
| |
| end
| |
|
| |
| if (white) then suffix = suffix .. ' White' end
| |
|
| |
| local fileName = 'File:' .. prefix .. item .. suffix .. '.png'
| |
| local class = invertClass and ('|class=' .. invertClass) or ''
| |
| local file = '[[' .. fileName .. '|' .. size .. 'x' .. size .. 'px|alt=' .. item .. '|link=' .. link .. class .. ']]'
| |
| | |
| local icon = nil
| |
| local corner_size = math.floor(tonumber(size) / 2.5)
| |
| local offset = 0
| |
| if (corner_size > 21) then
| |
| offset = 0
| |
| elseif (corner_size > 8) then
| |
| offset = math.floor((corner_size - 22) / 2)
| |
| else
| |
| offset = -6
| |
| end
| |
|
| |
|
| if (lib.isEmpty(item)) then | | local b = mw.bucket(bucket_name) |
| return ''
| | mw.log("mw.bucket() succeeded") |
| end
| |
|
| |
|
| local outerClass = lib.ternary(typeInput:lower() == 'item', 'item', 'item ' .. typeInput:lower()) | | b = b.select(select_field) |
| local result = mw.html.create():tag('span'):addClass(outerClass) | | mw.log("select() succeeded") |
|
| |
|
| local item_image = result:tag('span') | | b = b.limit(1) |
| :addClass('item-image')
| | mw.log("limit() succeeded") |
| :tag('span')
| |
| :css({
| |
| width = size .. 'px',
| |
| height = size .. 'px',
| |
| })
| |
| :attr('data-size', size)
| |
| :wikitext(file)
| |
| :done()
| |
|
| |
|
| if (icon ~= nil) then | | for k, v in pairs(args) do |
| item_image:tag('span'):addClass('item-icon')
| | if k ~= 1 and k ~= 2 then |
| :css({
| | mw.log("where: " .. tostring(k) .. "=" .. tostring(v)) |
| top = offset .. 'px',
| | b = b.where(k, v) |
| width = corner_size .. 'px',
| |
| height = corner_size .. 'px'
| |
| })
| |
| :attr('data-offset', offset)
| |
| :attr('data-corner-size', corner_size)
| |
| :wikitext(icon) | |
| end
| |
|
| |
| if (newline) then
| |
| result:addClass('newline')
| |
| result:tag('br'):addClass('hidden')
| |
| end
| |
|
| |
| function parseAmE(a, b)
| |
| local a = a or ''
| |
| local b = b or ''
| |
|
| |
| function addPunct(var)
| |
| if args.comma or args.comma_spc then var = var .. ','
| |
| elseif args.dot or args.dot_spc then var = var .. '.'
| |
| end
| |
| var = var
| |
| :gsub('%.%.%."%s-[%.%,]$', '..."') -- no punctuation if quote ends in elipses
| |
| :gsub('([%?%!])"%s-[%.%,]$', '%1"') -- no punctuation if quote ends in question or exclamation
| |
| :gsub('"%s-([%.%,])$', '%1"') -- move punctuation inside quote for anything else bc AmE
| |
| return var
| |
| end | | end |
|
| |
| -- needs to append to the last one with text
| |
| if lib.isEmpty(b) then a = addPunct(a)
| |
| else b = addPunct(b)
| |
| end
| |
|
| |
| if args.comma_spc or args.dot_spc then b = b .. ' ' end -- space always on the very end
| |
|
| |
| return {a, b}
| |
| end | | end |
|
| |
|
| local item_text = result:tag('span'):addClass('item-text') | | local data = b:run() |
| if (not noText) then | | mw.logObject(data) |
| local before = newline and '' or ' '
| |
| local ame = ((count or note) and {text, ''} or parseAmE(text, ''))
| |
| local label = ame[1]
| |
| local after = ame[2]
| |
| if (ref and not(count or note)) then after:gsub('%s*$', '') end
| |
|
| |
| if (lib.isEmpty(link)) then
| |
| item_text:wikitext(before, label, after)
| |
| else
| |
| item_text:wikitext(before, '[[', link, '|', label, ']]', after)
| |
| end
| |
| end
| |
|
| |
|
| if (count) then
| | return "done" |
| count = (count:gsub(',', ''))
| |
| if tonumber(count)~=nil then count = lib.thousandsSeparator(count) end
| |
| count = table.concat(note and {count, ''} or parseAmE(count, ''), '')
| |
| if (ref and not note) then count:gsub('%s*$', '') end
| |
| item_text:wikitext((noText and '' or ' ') .. '×' .. count)
| |
| end
| |
|
| |
| if (note) then
| |
| local after = table.concat(parseAmE('', ''), '')
| |
| if (ref) then after:gsub('%s*$', '') end
| |
| item_text:tag('small'):wikitext(((noText and not count) and '' or ' '),'(', note, ')', after)
| |
| end
| |
|
| |
| if (ref) then
| |
| local after = (args.comma_spc or args.dot_spc) and ' ' or '' -- avoid space between ref and punctuation
| |
| item_text:wikitext(ref, after)
| |
| end
| |
|
| |
| return tostring(result); | |
| end | | end |
|
| |
| return p
| |