Module:CraftingJSON: Difference between revisions
Jump to navigation
Jump to search
Created page with "local p = {} local slot = require('Module:Inventory slot') local function formatItemName(rawName) if not rawName then return "" end local name = rawName:gsub("^[^:]+:", "") name = name:gsub("_", " ") name = name:gsub("(%a)([%w_']*)", function(first, rest) return first:upper() .. rest:lower() end) return name end function p.render(frame) local args = frame:getParent().args local jsonStr = args[1] or "{}" local data = mw.text..." |
No edit summary |
||
| Line 14: | Line 14: | ||
function p.render(frame) | function p.render(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local jsonStr = args[1] | local jsonStr = args[1] | ||
if not data or not data.pattern then | if not jsonStr or mw.text.trim(jsonStr) == "" then | ||
return "Error: | return "Error: No JSON provided." | ||
end | |||
local success, data = pcall(mw.text.jsonDecode, mw.text.trim(jsonStr)) | |||
if not success then | |||
return "Error: Invalid JSON syntax. Please check for trailing commas or missing quotes." | |||
end | |||
if not data.pattern then | |||
return "Error: Missing JSON pattern." | |||
end | end | ||
Revision as of 11:40, 27 April 2026
Documentation for this module may be created at Module:CraftingJSON/doc
local p = {}
local slot = require('Module:Inventory slot')
local function formatItemName(rawName)
if not rawName then return "" end
local name = rawName:gsub("^[^:]+:", "")
name = name:gsub("_", " ")
name = name:gsub("(%a)([%w_']*)", function(first, rest)
return first:upper() .. rest:lower()
end)
return name
end
function p.render(frame)
local args = frame:getParent().args
local jsonStr = args[1]
if not jsonStr or mw.text.trim(jsonStr) == "" then
return "Error: No JSON provided."
end
local success, data = pcall(mw.text.jsonDecode, mw.text.trim(jsonStr))
if not success then
return "Error: Invalid JSON syntax. Please check for trailing commas or missing quotes."
end
if not data.pattern then
return "Error: Missing JSON pattern."
end
local container = mw.html.create('div'):addClass('mc-crafting-wrapper')
local grid = container:tag('div'):addClass('mc-grid')
for r = 1, 3 do
local rowStr = data.pattern[r] or " "
rowStr = rowStr .. string.rep(" ", 3 - #rowStr)
for c = 1, 3 do
local char = rowStr:sub(c, c)
local cell = grid:tag('div'):addClass('mc-cell')
if char ~= " " and data.key and data.key[char] then
local itemName = formatItemName(data.key[char].item)
cell:wikitext(slot.slot{itemName})
end
end
end
container:tag('div'):addClass('mc-arrow'):wikitext('→')
local resultCell = container:tag('div'):addClass('mc-result')
if data.result and data.result.item then
local resultName = formatItemName(data.result.item)
local resultAmount = data.result.count or 1
if resultAmount > 1 then
resultCell:wikitext(slot.slot{resultName, resultAmount})
else
resultCell:wikitext(slot.slot{resultName})
end
end
return tostring(container)
end
return p